Skip to content

Commit

Permalink
Include router prefix in shared resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Declow committed Jan 16, 2025
1 parent cc001e7 commit 44d7cc7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
60 changes: 60 additions & 0 deletions tests/test_page_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 8 additions & 6 deletions uiwiz/page_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 44d7cc7

Please sign in to comment.