Add unittests for study_details & change query parameter name

This commit is contained in:
Cheng Huzi
2021-07-01 01:03:21 -04:00
parent 0cdab19474
commit 2b425b5cb7
3 changed files with 63 additions and 3 deletions
+9 -2
View File
@@ -204,12 +204,19 @@ def create_app(storage: BaseStorage) -> Bottle:
@handle_json_api_exception
def get_study_detail(study_id: int) -> BottleViewReturn:
response.content_type = "application/json"
ntrials_client = int(request.params["ntrials_client"])
try:
before = int(request.params["before"])
assert before >= 0
except AssertionError:
response.status = 400 # Bad parameter
return {"reason": "`before` should be larger or equal 0."}
except KeyError:
before = 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)[ntrials_client:]
trials = get_trials(storage, study_id)[before:]
intersection, union = get_search_space(study_id, trials)
return serializer.serialize_study_detail(summary, trials, intersection, union)
+1 -1
View File
@@ -53,7 +53,7 @@ export const getStudyDetailAPI = (
return axiosInstance
.get<StudyDetailResponse>(`/api/studies/${studyId}`, {
params: {
ntrials_client: nLocalTrials,
before: nLocalTrials,
},
})
.then((res) => {
+53
View File
@@ -25,6 +25,59 @@ 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):
x = trial.suggest_float("x", -1, 1)
return x
study = optuna.create_study()
study_id = study._study_id
study.optimize(objective, n_trials=10)
app = create_app(study._storage)
# query without before parameter
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
content_type="application/json",
)
self.assertEqual(status, 200)
all_trials = json.loads(body)["trials"]
self.assertEqual(len(all_trials), 10)
# query with before parameter
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
queries={"before": 5},
content_type="application/json",
)
self.assertEqual(status, 200)
all_trials = json.loads(body)["trials"]
self.assertEqual(len(all_trials), 5)
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
queries={"before": 10},
content_type="application/json",
)
self.assertEqual(status, 200)
all_trials = json.loads(body)["trials"]
self.assertEqual(len(all_trials), 0)
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
queries={"before": -1},
content_type="application/json",
)
self.assertEqual(status, 400)
def test_create_study(self) -> None:
for name, directions, expected_status in [
("single-objective success", ["minimize"], 201),