Fix unittest logic & change parameter name

This commit is contained in:
Cheng Huzi
2021-07-01 04:33:48 -04:00
parent d811244429
commit 3b18fe4ec8
4 changed files with 42 additions and 26 deletions
+5 -5
View File
@@ -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)
+6 -7
View File
@@ -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) => {
+1 -1
View File
@@ -53,7 +53,7 @@ export const getStudyDetailAPI = (
return axiosInstance
.get<StudyDetailResponse>(`/api/studies/${studyId}`, {
params: {
before: nLocalTrials,
after: nLocalTrials,
},
})
.then((res) => {
+30 -13
View File
@@ -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)