From 3b18fe4ec805dcd72453338bb5bd3f616132f459 Mon Sep 17 00:00:00 2001 From: Cheng Huzi Date: Thu, 1 Jul 2021 04:33:48 -0400 Subject: [PATCH] Fix unittest logic & change parameter name --- optuna_dashboard/app.py | 10 +++---- optuna_dashboard/static/action.ts | 13 ++++----- optuna_dashboard/static/apiClient.ts | 2 +- tests/test_api.py | 43 +++++++++++++++++++--------- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/optuna_dashboard/app.py b/optuna_dashboard/app.py index 44f2dea1..c0b2a8af 100644 --- a/optuna_dashboard/app.py +++ b/optuna_dashboard/app.py @@ -205,18 +205,18 @@ def create_app(storage: BaseStorage) -> Bottle: def get_study_detail(study_id: int) -> BottleViewReturn: response.content_type = "application/json" try: - before = int(request.params["before"]) - assert before >= 0 + after = int(request.params["after"]) + assert after >= 0 except AssertionError: response.status = 400 # Bad parameter - return {"reason": "`before` should be larger or equal 0."} + return {"reason": "`after` should be larger or equal 0."} except KeyError: - before = 0 + after = 0 summary = get_study_summary(storage, study_id) if summary is None: response.status = 404 # Not found return {"reason": f"study_id={study_id} is not found"} - trials = get_trials(storage, study_id)[before:] + trials = get_trials(storage, study_id)[after:] intersection, union = get_search_space(study_id, trials) return serializer.serialize_study_detail(summary, trials, intersection, union) diff --git a/optuna_dashboard/static/action.ts b/optuna_dashboard/static/action.ts index 47bb4490..ded68cb3 100644 --- a/optuna_dashboard/static/action.ts +++ b/optuna_dashboard/static/action.ts @@ -35,13 +35,12 @@ export const actionCreator = () => { const updateStudyDetail = (studyId: number) => { let nLocalFixedTrials = 0 if (studyId in studyDetails) { - for (const trial of studyDetails[studyId].trials) { - if (!["Running", "Waiting"].includes(trial.state)) { - nLocalFixedTrials += 1 - } else { - break - } - } + const currentTrials = studyDetails[studyId].trials + const firstUpdatable = currentTrials.findIndex((trial) => + ["Running", "Waiting"].includes(trial.state) + ) + nLocalFixedTrials = + firstUpdatable === -1 ? currentTrials.length : firstUpdatable } getStudyDetailAPI(studyId, nLocalFixedTrials) .then((study) => { diff --git a/optuna_dashboard/static/apiClient.ts b/optuna_dashboard/static/apiClient.ts index 096420c3..add7a531 100644 --- a/optuna_dashboard/static/apiClient.ts +++ b/optuna_dashboard/static/apiClient.ts @@ -53,7 +53,7 @@ export const getStudyDetailAPI = ( return axiosInstance .get(`/api/studies/${studyId}`, { params: { - before: nLocalTrials, + after: nLocalTrials, }, }) .then((res) => { diff --git a/tests/test_api.py b/tests/test_api.py index bfb27398..0ee3c0ea 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -8,6 +8,11 @@ from optuna_dashboard.app import create_app from .wsgi_client import send_request +def objective(trial: optuna.trial.Trial) -> float: + x = trial.suggest_float("x", -1, 1) + return x + + class APITestCase(TestCase): def test_get_study_summaries(self) -> None: storage = optuna.storages.InMemoryStorage() @@ -25,17 +30,12 @@ class APITestCase(TestCase): study_summaries = json.loads(body)["study_summaries"] self.assertEqual(len(study_summaries), 2) - def test_get_study_details(self) -> None: - def objective(trial: optuna.trial.Trial) -> float: - x = trial.suggest_float("x", -1, 1) - return x - + def test_get_study_details_without_after_param(self) -> None: study = optuna.create_study() study_id = study._study_id - study.optimize(objective, n_trials=10) + study.optimize(objective, n_trials=2) app = create_app(study._storage) - # query without before parameter status, _, body = send_request( app, f"/api/studies/{study_id}", @@ -44,36 +44,53 @@ class APITestCase(TestCase): ) self.assertEqual(status, 200) all_trials = json.loads(body)["trials"] - self.assertEqual(len(all_trials), 10) + self.assertEqual(len(all_trials), 2) + + def test_get_study_details_with_after_param_partial(self) -> None: + study = optuna.create_study() + study_id = study._study_id + study.optimize(objective, n_trials=2) + app = create_app(study._storage) - # query with before parameter status, _, body = send_request( app, f"/api/studies/{study_id}", "GET", - queries={"before": "5"}, + queries={"after": "1"}, content_type="application/json", ) self.assertEqual(status, 200) all_trials = json.loads(body)["trials"] - self.assertEqual(len(all_trials), 5) + self.assertEqual(len(all_trials), 1) + + def test_get_study_details_with_after_param_full(self) -> None: + study = optuna.create_study() + study_id = study._study_id + study.optimize(objective, n_trials=2) + app = create_app(study._storage) status, _, body = send_request( app, f"/api/studies/{study_id}", "GET", - queries={"before": "10"}, + queries={"after": "2"}, content_type="application/json", ) self.assertEqual(status, 200) all_trials = json.loads(body)["trials"] self.assertEqual(len(all_trials), 0) + def test_get_study_details_with_after_param_illegal(self) -> None: + study = optuna.create_study() + study_id = study._study_id + study.optimize(objective, n_trials=2) + app = create_app(study._storage) + status, _, body = send_request( app, f"/api/studies/{study_id}", "GET", - queries={"before": "-1"}, + queries={"after": "-1"}, content_type="application/json", ) self.assertEqual(status, 400)