From 8eac8fc6f31e7420f0f5a277ebdd17953c7602be Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Wed, 6 Dec 2023 16:36:00 +0900 Subject: [PATCH] add tests --- python_tests/test_csv_download.py | 52 +++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/python_tests/test_csv_download.py b/python_tests/test_csv_download.py index de144fd0..b8491502 100644 --- a/python_tests/test_csv_download.py +++ b/python_tests/test_csv_download.py @@ -1,12 +1,60 @@ from typing import Any import optuna +from optuna.trial import TrialState from optuna_dashboard._app import create_app import pytest from .wsgi_client import send_request +def test_download_csv_no_trial() -> None: + def objective(trial: optuna.Trial) -> float: + x = trial.suggest_float("x", -100, 100) + y = trial.suggest_categorical("y", [-1, 0, 1]) + return x**2 + y + + storage = optuna.storages.InMemoryStorage() + study = optuna.create_study(storage=storage) + study.optimize(objective, n_trials=0) + app = create_app(storage) + status, _, body = send_request( + app, + "/csv/0", + "GET", + content_type="application/json", + ) + assert status == 200 + + +def test_download_csv_all_waiting() -> None: + storage = optuna.storages.InMemoryStorage() + study = optuna.create_study(storage=storage) + study.add_trial(optuna.trial.create_trial(state=TrialState.WAITING)) + app = create_app(storage) + status, _, body = send_request( + app, + "/csv/0", + "GET", + content_type="application/json", + ) + assert status == 200 + + +def test_download_csv_all_running() -> None: + storage = optuna.storages.InMemoryStorage() + study = optuna.create_study(storage=storage) + study.add_trial(optuna.trial.create_trial(state=TrialState.RUNNING)) + app = create_app(storage) + status, _, body = send_request( + app, + "/csv/0", + "GET", + content_type="application/json", + ) + assert status == 200 + + @pytest.mark.parametrize("id", [0, 1]) def test_download_csv_fail(id: int) -> None: def objective(trial: optuna.Trial) -> float: @@ -47,7 +95,7 @@ def test_download_csv_multi_obj(is_multi_obj: bool) -> None: app = create_app(storage) status, _, body = send_request( app, - f"/csv/{0}", + "/csv/0", "GET", content_type="application/json", ) @@ -68,7 +116,7 @@ def test_download_csv_user_attr() -> None: app = create_app(storage) status, _, body = send_request( app, - f"/csv/{0}", + "/csv/0", "GET", content_type="application/json", )