Skip to content

Commit 14e92fc

Browse files
committed
Added test for operation id route parameter
1 parent e194752 commit 14e92fc

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

ninja_extra/controllers/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ def get_route_functions(cls: Type) -> Iterable[RouteFunction]:
6060
yield method
6161

6262

63+
def get_all_controller_route_function(
64+
controller: Union[Type["ControllerBase"], Type]
65+
) -> List[RouteFunction]:
66+
route_functions: List[RouteFunction] = []
67+
for item in dir(controller):
68+
attr = getattr(controller, item)
69+
if isinstance(attr, RouteFunction):
70+
route_functions.append(attr)
71+
return route_functions
72+
73+
6374
def compute_api_route_function(
6475
base_cls: Type, api_controller_instance: "APIController"
6576
) -> None:

tests/test_route.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
RouteFunction,
2626
RouteInvalidParameterException,
2727
)
28-
from ninja_extra.controllers.base import get_route_functions
28+
from ninja_extra.controllers.base import get_all_controller_route_function
2929
from ninja_extra.controllers.route.context import RouteContext
3030
from ninja_extra.exceptions import PermissionDenied
3131
from ninja_extra.permissions import AllowAny
@@ -77,6 +77,10 @@ def example_delete(self, ex_id: str):
7777
def example_list_create(self, ex_id: str):
7878
pass
7979

80+
@http_post("/example/operation-id", operation_id="example_post_operation_id")
81+
def example_post_operation_id(self):
82+
pass
83+
8084

8185
class TestControllerRoute:
8286
@pytest.mark.parametrize(
@@ -95,8 +99,10 @@ def test_api_controller_builds_accurate_operations_list(
9599
assert len(path_view.operations) == operation_count
96100

97101
def test_controller_route_should_have_an_operation(self):
98-
for route_func in get_route_functions(SomeTestController):
99-
path_view = SomeTestController.get_path_operations().get(str(route_func))
102+
for route_func in get_all_controller_route_function(SomeTestController):
103+
path_view = SomeTestController.get_api_controller().path_operations.get(
104+
str(route_func)
105+
)
100106
operations = list(
101107
filter(
102108
lambda n: n.operation_id
@@ -105,6 +111,8 @@ def test_controller_route_should_have_an_operation(self):
105111
)
106112
)
107113
assert len(operations) == 1
114+
if str(route_func) == "/example/operation-id":
115+
assert operations[0].operation_id == "example_post_operation_id"
108116
assert route_func.route.route_params.methods == operations[0].methods
109117

110118
def test_controller_route_should_right_view_func_type(self):

0 commit comments

Comments
 (0)