mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-09 11:28:14 +08:00
Add unittests for json api
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import json
|
||||
from typing import Dict, Optional, Tuple, List, Any
|
||||
from unittest import TestCase
|
||||
|
||||
import optuna
|
||||
from .wsgi_utils import create_wsgi_env
|
||||
from optuna_dashboard.app import create_app
|
||||
|
||||
|
||||
def create_json_api_wsgi_env(
|
||||
path: str,
|
||||
method: str,
|
||||
json_body: Optional[Dict[str, Any]] = None,
|
||||
content_type: str = "application/json",
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
):
|
||||
body = json.dumps(json_body) if json_body else ""
|
||||
return create_wsgi_env(
|
||||
path, method, body, content_type=content_type, headers=headers
|
||||
)
|
||||
|
||||
|
||||
class APITestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.status: Optional[str] = None
|
||||
self.headers: Optional[List[Tuple[str, str]]] = None
|
||||
|
||||
def _start_response(self, status, headers):
|
||||
self.status = status
|
||||
self.headers = headers
|
||||
|
||||
def test_create_study(self):
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 0)
|
||||
|
||||
app = create_app(storage)
|
||||
env = create_json_api_wsgi_env(
|
||||
"/api/studies",
|
||||
"POST",
|
||||
json_body={
|
||||
"study_name": "foo",
|
||||
"direction": "minimize",
|
||||
},
|
||||
)
|
||||
_ = app(env, self._start_response)
|
||||
self.assertEqual(self.status, "201 Created")
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 1)
|
||||
|
||||
def test_create_study_duplicated(self):
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
storage.create_new_study("foo")
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 1)
|
||||
|
||||
app = create_app(storage)
|
||||
env = create_json_api_wsgi_env(
|
||||
"/api/studies",
|
||||
"POST",
|
||||
json_body={
|
||||
"study_name": "foo",
|
||||
"direction": "minimize",
|
||||
},
|
||||
)
|
||||
_ = app(env, self._start_response)
|
||||
self.assertEqual(self.status, "400 Bad Request")
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 1)
|
||||
|
||||
def test_delete_study(self):
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
storage.create_new_study("foo1")
|
||||
storage.create_new_study("foo2")
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 2)
|
||||
|
||||
app = create_app(storage)
|
||||
env = create_json_api_wsgi_env(
|
||||
"/api/studies/1",
|
||||
"DELETE",
|
||||
)
|
||||
_ = app(env, self._start_response)
|
||||
self.assertEqual(self.status, "204 No Content")
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 1)
|
||||
|
||||
def test_delete_study_not_found(self):
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
app = create_app(storage)
|
||||
env = create_json_api_wsgi_env(
|
||||
"/api/studies/1",
|
||||
"DELETE",
|
||||
)
|
||||
_ = app(env, self._start_response)
|
||||
self.assertEqual(self.status, "404 Not Found")
|
||||
@@ -0,0 +1,33 @@
|
||||
import io
|
||||
from typing import Dict, Optional, Union
|
||||
|
||||
|
||||
def create_wsgi_env(
|
||||
path: str,
|
||||
method: str,
|
||||
body: Union[str, bytes] = b"",
|
||||
queries: Optional[Dict[str, str]] = None,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
content_type: str = "text/plain; charset=utf-8",
|
||||
):
|
||||
request_method = method.upper()
|
||||
bytes_body = body if isinstance(body, bytes) else body.encode("utf-8")
|
||||
wsgi_input = io.BytesIO(bytes_body)
|
||||
content_length = len(body)
|
||||
|
||||
# 'key1=value1&key2=value2'
|
||||
query_string = "&".join([f"{k}={v}" for k, v in queries.items()]) if queries else ""
|
||||
env = {
|
||||
"HTTP_X_FORWARDED_PROTO": "http",
|
||||
"HTTP_X_FORWARDED_HOST": "localhost",
|
||||
"PATH_INFO": path,
|
||||
"REQUEST_METHOD": request_method,
|
||||
"QUERY_STRING": query_string,
|
||||
"wsgi.input": wsgi_input,
|
||||
"CONTENT_TYPE": content_type,
|
||||
"CONTENT_LENGTH": content_length,
|
||||
}
|
||||
headers = headers or {}
|
||||
for k, v in headers.items():
|
||||
env[f"HTTP_{k.upper()}"] = v
|
||||
return env
|
||||
Reference in New Issue
Block a user