Add some tests

This commit is contained in:
nabenabe0928
2024-01-19 09:01:15 +01:00
parent 967f6ceb55
commit d04101fb76
+28 -45
View File
@@ -1,12 +1,14 @@
from __future__ import annotations
import json
import pytest
import sys
from unittest import TestCase
import optuna
from optuna import get_all_study_summaries
from optuna.study import StudyDirection
from optuna.trial import FrozenTrial
from optuna_dashboard._app import create_app
from optuna_dashboard._app import create_new_study
from optuna_dashboard._note import note_str_key_prefix
@@ -44,70 +46,51 @@ class APITestCase(TestCase):
study_summaries = json.loads(body)["study_summaries"]
self.assertEqual(len(study_summaries), 2)
def test_get_study_details_without_after_param(self) -> None:
def run_get_study_details(
self, queries: dict[str, str] | None = None, expected_status: int = 200,
) -> tuple[int, list[FrozenTrial]]:
study = optuna.create_study()
study_id = study._study_id
study.optimize(objective, n_trials=2)
study.optimize(objective, n_trials=10)
app = create_app(study._storage)
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
queries=queries,
content_type="application/json",
)
self.assertEqual(status, 200)
all_trials = json.loads(body)["trials"]
self.assertEqual(len(all_trials), 2)
self.assertEqual(status, expected_status)
if expected_status == 400:
return []
else:
return json.loads(body)["trials"]
def test_get_study_details_without_after_param(self) -> None:
all_trials = self.run_get_study_details()
self.assertEqual(len(all_trials), 10)
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)
all_trials = self.run_get_study_details({"after": "5"})
self.assertEqual(len(all_trials), 5)
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
queries={"after": "1"},
content_type="application/json",
)
self.assertEqual(status, 200)
all_trials = json.loads(body)["trials"]
self.assertEqual(len(all_trials), 1)
def test_get_study_details_with_params(self) -> None:
for after in [0, 5, 9, 10]:
for limit in [1, 2, 5, 10]:
trials = self.run_get_study_details({"after": str(after), "limit": str(limit)})
ans = list(range(after, min(10, after + limit)))
self.assertEqual([t["number"] for t in trials], ans)
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={"after": "2"},
content_type="application/json",
)
self.assertEqual(status, 200)
all_trials = json.loads(body)["trials"]
all_trials = self.run_get_study_details({"after": "10"})
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)
self.run_get_study_details({"after": "-1"}, expected_status=400)
status, _, body = send_request(
app,
f"/api/studies/{study_id}",
"GET",
queries={"after": "-1"},
content_type="application/json",
)
self.assertEqual(status, 400)
def test_get_study_details_with_limit_param_illegal(self) -> None:
self.run_get_study_details({"limit": "-1"}, expected_status=400)
@pytest.mark.skipif(sys.version_info < (3, 8), reason="BoTorch dropped Python3.7 support")
def test_get_best_trials_of_preferential_study(self) -> None: