-
-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #369 from kaschnit/allow-router-path-params
Allow path parameters to be specified at router level
- Loading branch information
Showing
5 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import pytest | ||
|
||
from ninja import NinjaAPI, Path, Router | ||
from ninja.testing import TestClient | ||
|
||
api = NinjaAPI() | ||
router_with_path_type = Router() | ||
router_without_path_type = Router() | ||
router_with_multiple = Router() | ||
|
||
|
||
@router_with_path_type.get("/metadata") | ||
def get_item_metadata(request, item_id: int = Path(None)) -> int: | ||
return item_id | ||
|
||
|
||
@router_without_path_type.get("/") | ||
def get_item_metadata_2(request, item_id: str = Path(None)) -> str: | ||
return item_id | ||
|
||
|
||
@router_without_path_type.get("/metadata") | ||
def get_item_metadata_3(request, item_id: str = Path(None)) -> str: | ||
return item_id | ||
|
||
|
||
@router_without_path_type.get("/") | ||
def get_item_metadata_4(request, item_id: str = Path(None)) -> str: | ||
return item_id | ||
|
||
|
||
@router_with_multiple.get("/metadata/{kind}") | ||
def get_item_metadata_5( | ||
request, item_id: int = Path(None), name: str = Path(None), kind: str = Path(None) | ||
) -> str: | ||
return f"{item_id} {name} {kind}" | ||
|
||
|
||
api.add_router("/with_type/{int:item_id}", router_with_path_type) | ||
api.add_router("/without_type/{item_id}", router_without_path_type) | ||
api.add_router("/with_multiple/{int:item_id}/name/{name}", router_with_multiple) | ||
|
||
client = TestClient(api) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"path,expected_status,expected_response", | ||
[ | ||
("/with_type/1/metadata", 200, 1), | ||
("/without_type/1/metadata", 200, "1"), | ||
("/without_type/abc/metadata", 200, "abc"), | ||
("/with_multiple/99/name/foo/metadata/timestamp", 200, "99 foo timestamp"), | ||
], | ||
) | ||
def test_router_with_path_params(path, expected_status, expected_response): | ||
response = client.get(path) | ||
assert response.status_code == expected_status | ||
assert response.json() == expected_response | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"path,expected_exception,expect_exception_contains", | ||
[ | ||
("/with_type/abc/metadata", Exception, "Cannot resolve"), | ||
("/with_type//metadata", Exception, "Cannot resolve"), | ||
("/with_type/null/metadata", Exception, "Cannot resolve"), | ||
("/with_type", Exception, "Cannot resolve"), | ||
("/with_type/", Exception, "Cannot resolve"), | ||
("/with_type//", Exception, "Cannot resolve"), | ||
("/with_type/null", Exception, "Cannot resolve"), | ||
("/with_type/null/", Exception, "Cannot resolve"), | ||
("/without_type", Exception, "Cannot resolve"), | ||
("/without_type/", Exception, "Cannot resolve"), | ||
("/without_type//", Exception, "Cannot resolve"), | ||
("/with_multiple/abc/name/foo/metadata/timestamp", Exception, "Cannot resolve"), | ||
("/with_multiple/99", Exception, "Cannot resolve"), | ||
], | ||
) | ||
def test_router_with_path_params_nomatch( | ||
path, expected_exception, expect_exception_contains | ||
): | ||
with pytest.raises(expected_exception, match=expect_exception_contains): | ||
client.get(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
|
||
from ninja.utils import replace_path_param_notation | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input,expected_output", | ||
[ | ||
("abc/{def}", "abc/<def>"), | ||
("abc/<def>", "abc/<def>"), | ||
("abc", "abc"), | ||
("<abc>", "<abc>"), | ||
("{abc}", "<abc>"), | ||
("{abc}/{def}", "<abc>/<def>"), | ||
], | ||
) | ||
def test_replace_path_param_notation(input, expected_output): | ||
assert replace_path_param_notation(input) == expected_output |