mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-11 12:30:25 +08:00
Fix trailing slashes hook
This commit is contained in:
@@ -7,7 +7,7 @@ import threading
|
||||
import traceback
|
||||
from typing import Union, Dict, List, Optional, TypeVar, Callable, Any, cast
|
||||
|
||||
from bottle import Bottle, Response, redirect, request, response, static_file, hook
|
||||
from bottle import Bottle, BaseResponse, redirect, request, response, static_file
|
||||
from optuna.exceptions import DuplicatedStudyError
|
||||
from optuna.storages import BaseStorage, get_storage
|
||||
from optuna.trial import FrozenTrial
|
||||
@@ -15,7 +15,7 @@ from optuna.study import StudyDirection, StudySummary
|
||||
|
||||
from . import serializer
|
||||
|
||||
BottleViewReturn = Union[str, bytes, Dict[str, Any], Response]
|
||||
BottleViewReturn = Union[str, bytes, Dict[str, Any], BaseResponse]
|
||||
BottleView = TypeVar("BottleView", bound=Callable[..., BottleViewReturn])
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -98,7 +98,7 @@ def create_app(storage_or_url: Union[str, BaseStorage]) -> Bottle:
|
||||
app = Bottle()
|
||||
storage = get_storage(storage_or_url)
|
||||
|
||||
@hook("before_request")
|
||||
@app.hook("before_request")
|
||||
def remove_trailing_slashes_hook() -> None:
|
||||
request.environ["PATH_INFO"] = request.environ["PATH_INFO"].rstrip("/")
|
||||
|
||||
|
||||
@@ -21,6 +21,21 @@ def create_json_api_wsgi_env(
|
||||
|
||||
|
||||
class APITestCase(TestCase):
|
||||
def test_get_study_summaries(self) -> None:
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
storage.create_new_study("foo1")
|
||||
storage.create_new_study("foo2")
|
||||
|
||||
app = create_app(storage)
|
||||
env = create_json_api_wsgi_env(
|
||||
"/api/studies/",
|
||||
"GET",
|
||||
)
|
||||
status, _, body = send_request(app, env)
|
||||
self.assertEqual(status, "200 OK")
|
||||
study_summaries = json.loads(body)["study_summaries"]
|
||||
self.assertEqual(len(study_summaries), 2)
|
||||
|
||||
def test_create_study(self) -> None:
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
self.assertEqual(len(storage.get_all_study_summaries()), 0)
|
||||
|
||||
Reference in New Issue
Block a user