From 44d7cc7a03953d6a3c20d4625abbf3fe2ed52914 Mon Sep 17 00:00:00 2001 From: Declow Date: Thu, 16 Jan 2025 21:17:16 +0100 Subject: [PATCH] Include router prefix in shared resources --- tests/test_page_router.py | 60 +++++++++++++++++++++++++++++++++++++++ uiwiz/page_route.py | 14 +++++---- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/tests/test_page_router.py b/tests/test_page_router.py index b71b043..a8699c9 100644 --- a/tests/test_page_router.py +++ b/tests/test_page_router.py @@ -57,3 +57,63 @@ def func(): assert route == apis.get(route).path assert "func" == apis.get(route).name assert {"POST"} == apis.get(route).methods + + +def test_router_ui_prefix(): + app = UiwizApp() + + pr = PageRouter(prefix="/api") + route = "/path" + + @pr.ui(route) + def func(): + ... # pragma: no cover + + app.include_router(pr) + apis = {item.path: item for item in app.routes} + full_route = pr.prefix + route + + assert full_route == fetch_route(func) + assert full_route == apis.get(full_route).path + assert "func" == apis.get(full_route).name + assert {"POST"} == apis.get(full_route).methods + + +def test_router_page_prefix(): + app = UiwizApp() + + pr = PageRouter(prefix="/page") + route = "/path" + + @pr.page(route) + def func(): + ... # pragma: no cover + + app.include_router(pr) + apis = {item.path: item for item in app.routes} + full_route = pr.prefix + route + + assert full_route == fetch_route(func) + assert full_route == apis.get(full_route).path + assert "func" == apis.get(full_route).name + assert {"GET"} == apis.get(full_route).methods + + +def test_router_page_without_prefix(): + app = UiwizApp() + + pr = PageRouter() + route = "/path" + + @pr.page(route) + def func(): + ... # pragma: no cover + + app.include_router(pr) + apis = {item.path: item for item in app.routes} + full_route = pr.prefix + route + + assert full_route == fetch_route(func) + assert full_route == apis.get(full_route).path + assert "func" == apis.get(full_route).name + assert {"GET"} == apis.get(full_route).methods diff --git a/uiwiz/page_route.py b/uiwiz/page_route.py index 3c2bb5b..8e9f8cb 100644 --- a/uiwiz/page_route.py +++ b/uiwiz/page_route.py @@ -48,10 +48,11 @@ async def decorated(*dec_args, **dec_kwargs) -> Response: self.__ensure_request_response_signature__(decorated) - if not route_exists(path): - register_path(path, decorated) - _router = router or self + full_path = _router.prefix + path + + if not route_exists(full_path): + register_path(full_path, decorated) return _router.get(path, *args, include_in_schema=False, **kwargs)(decorated) @@ -100,10 +101,11 @@ async def decorated(*dec_args, **dec_kwargs) -> Response: self.__ensure_request_response_signature__(decorated) - if not route_exists(path): - register_path(path, decorated) - _router = router or self + full_path = _router.prefix + path + + if not route_exists(full_path): + register_path(full_path, decorated) return _router.post(path, include_in_schema=False, **kwargs)(decorated)