From ad149f073c2bdb628b91bb43c5c9bc0f563a3800 Mon Sep 17 00:00:00 2001 From: moririn2528 Date: Mon, 28 Aug 2023 14:47:07 +0900 Subject: [PATCH] add test for skip api --- optuna_dashboard/_app.py | 3 ++- optuna_dashboard/preferential/_study.py | 8 +++----- python_tests/test_api.py | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index 0ec82fe8..41673b61 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -40,7 +40,8 @@ from .artifact._backend import register_artifact_route from .artifact._backend_to_store import to_artifact_store from .preferential._study import _SYSTEM_ATTR_PREFERENTIAL_STUDY from .preferential._study import get_best_trials as get_best_preferential_trials -from .preferential._system_attrs import report_preferences, report_skip +from .preferential._system_attrs import report_preferences +from .preferential._system_attrs import report_skip if typing.TYPE_CHECKING: diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index d74fbf14..d28b7f26 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -12,11 +12,9 @@ from optuna.samplers import BaseSampler from optuna.samplers import RandomSampler from optuna.trial import FrozenTrial from optuna.trial import TrialState -from optuna_dashboard.preferential._system_attrs import ( - get_preferences, - report_preferences, - _SYSTEM_ATTR_SKIP_TRIAL, -) +from optuna_dashboard.preferential._system_attrs import _SYSTEM_ATTR_SKIP_TRIAL +from optuna_dashboard.preferential._system_attrs import get_preferences +from optuna_dashboard.preferential._system_attrs import report_preferences _logger = logging.get_logger(__name__) diff --git a/python_tests/test_api.py b/python_tests/test_api.py index c995c0e2..ae50e29a 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -151,6 +151,30 @@ class APITestCase(TestCase): assert better.number == 2 assert worse.number == 1 + def test_skip_trial(self) -> None: + storage = optuna.storages.InMemoryStorage() + study = create_study(storage=storage) + trials: list[optuna.Trial] = [] + for _ in range(3): + trial = study.ask() + study.mark_comparison_ready(trial) + trials.append(trial) + + app = create_app(storage) + study_id = study._study._study_id + status, _, _ = send_request( + app, + f"/api/studies/{study_id}/{trials[1]._trial_id}/skip", + "POST", + content_type="application/json", + ) + self.assertEqual(status, 204) + + best_trials = study.best_trials + assert len(best_trials) == 2 + assert best_trials[0].number == 0 + assert best_trials[1].number == 2 + def test_create_study(self) -> None: for name, directions, expected_status in [ ("single-objective success", ["minimize"], 201),