mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-09 11:28:14 +08:00
Use page.wait_for_selector to fix e2e tests
This commit is contained in:
@@ -46,20 +46,6 @@ def run_single_trial_objective_study(storage: optuna.storages.InMemoryStorage) -
|
||||
return study
|
||||
|
||||
|
||||
def run_single_1param_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
study = optuna.create_study(
|
||||
study_name="single-1-param", storage=storage, direction="maximize", sampler=sampler
|
||||
)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x1 = trial.suggest_float("x1", 0, 10)
|
||||
return -((x1 - 2) ** 2)
|
||||
|
||||
study.optimize(objective, n_trials=50)
|
||||
return study
|
||||
|
||||
|
||||
def run_single_dynamic_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
|
||||
# Single-objective study with dynamic search space
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
@@ -78,24 +64,6 @@ def run_single_dynamic_objective_study(storage: optuna.storages.InMemoryStorage)
|
||||
return study
|
||||
|
||||
|
||||
def run_single_inf_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
|
||||
# Single objective study with 'inf', '-inf', or 'nan' value
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
study = optuna.create_study(study_name="single-inf", storage=storage, sampler=sampler)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x = trial.suggest_float("x", -10, 10)
|
||||
if trial.number % 3 == 0:
|
||||
return float("inf")
|
||||
elif trial.number % 3 == 1:
|
||||
return float("-inf")
|
||||
else:
|
||||
return x**2
|
||||
|
||||
study.optimize(objective, n_trials=50)
|
||||
return study
|
||||
|
||||
|
||||
def run_multi_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
|
||||
# Multi-objective study
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
@@ -167,39 +135,6 @@ def run_single_pruned_without_report_objective_study(
|
||||
return study
|
||||
|
||||
|
||||
def run_single_inf_report_objective_study(
|
||||
storage: optuna.storages.InMemoryStorage,
|
||||
) -> optuna.Study:
|
||||
# Single objective pruned after reported 'inf', '-inf', or 'nan'
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
study = optuna.create_study(study_name="single-inf-report", storage=storage, sampler=sampler)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x = trial.suggest_float("x", -10, 10)
|
||||
if trial.number % 3 == 0:
|
||||
trial.report(float("inf"), 1)
|
||||
elif trial.number % 3 == 1:
|
||||
trial.report(float("-inf"), 1)
|
||||
else:
|
||||
trial.report(float("nan"), 1)
|
||||
|
||||
if x > 0:
|
||||
raise optuna.TrialPruned()
|
||||
else:
|
||||
return x**2
|
||||
|
||||
study.optimize(objective, n_trials=50)
|
||||
return study
|
||||
|
||||
|
||||
def run_single_no_trials_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
|
||||
# No trials single-objective study
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
study = optuna.create_study(study_name="single-no-trials", storage=storage, sampler=sampler)
|
||||
|
||||
return study
|
||||
|
||||
|
||||
def run_multi_no_trials_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
|
||||
# No trials multi-objective study
|
||||
sampler = optuna.samplers.RandomSampler(seed=0)
|
||||
@@ -217,14 +152,10 @@ parameterize_studies = pytest.mark.parametrize(
|
||||
[
|
||||
run_single_objective_study,
|
||||
run_single_trial_objective_study,
|
||||
run_single_1param_objective_study,
|
||||
run_single_dynamic_objective_study,
|
||||
run_single_inf_objective_study,
|
||||
run_multi_objective_study,
|
||||
run_multi_dynamic_objective_study,
|
||||
run_single_pruned_without_report_objective_study,
|
||||
run_single_inf_report_objective_study,
|
||||
run_single_no_trials_objective_study,
|
||||
run_multi_no_trials_objective_study,
|
||||
],
|
||||
)
|
||||
@@ -245,6 +176,7 @@ def test_study_list(
|
||||
page.goto(server_url)
|
||||
page.click(f"a[href='/dashboard/studies/{study_id}']")
|
||||
|
||||
page.wait_for_selector(".MuiTypography-body1")
|
||||
element = page.query_selector(".MuiTypography-body1")
|
||||
assert element is not None
|
||||
|
||||
@@ -269,6 +201,7 @@ def test_study_analytics(
|
||||
page.goto(url)
|
||||
page.click(f"a[href='/dashboard/studies/{study_id}/analytics']")
|
||||
|
||||
page.wait_for_selector(".MuiTypography-body1")
|
||||
element = page.query_selector(".MuiTypography-body1")
|
||||
assert element is not None
|
||||
|
||||
@@ -293,6 +226,7 @@ def test_trial_list(
|
||||
page.goto(url)
|
||||
page.click(f"a[href='/dashboard/studies/{study_id}/trials']")
|
||||
|
||||
page.wait_for_selector(".MuiTypography-body1")
|
||||
element = page.query_selector(".MuiTypography-body1")
|
||||
assert element is not None
|
||||
|
||||
@@ -317,6 +251,7 @@ def test_trial_table(
|
||||
page.goto(url)
|
||||
page.click(f"a[href='/dashboard/studies/{study_id}/trialTable']")
|
||||
|
||||
page.wait_for_selector(".MuiTypography-body1")
|
||||
element = page.query_selector(".MuiTypography-body1")
|
||||
assert element is not None
|
||||
|
||||
@@ -339,6 +274,7 @@ def test_trial_note(
|
||||
url = f"{server_url}/studies/{study_id}"
|
||||
|
||||
page.goto(url)
|
||||
page.wait_for_selector(".MuiTypography-body1")
|
||||
page.click(f"a[href='/dashboard/studies/{study_id}/note']")
|
||||
|
||||
element = page.query_selector(".MuiTypography-body1")
|
||||
|
||||
@@ -449,7 +449,11 @@ def create_optuna_storage(
|
||||
|
||||
# optuna-dashboard issue 410
|
||||
# https://github.com/optuna/optuna-dashboard/issues/410
|
||||
study = optuna.create_study(study_name="optuna-dashboard-issue-410", storage=storage, sampler=optuna.samplers.RandomSampler())
|
||||
study = optuna.create_study(
|
||||
study_name="optuna-dashboard-issue-410",
|
||||
storage=storage,
|
||||
sampler=optuna.samplers.RandomSampler(),
|
||||
)
|
||||
|
||||
def objective_issue_410(trial: optuna.Trial) -> float:
|
||||
trial.suggest_categorical("resample_rate", ["50ms"])
|
||||
|
||||
Reference in New Issue
Block a user