1414 testing ,
1515)
1616from ninja_extra .controllers import ControllerBase , RouteContext , RouteFunction
17- from ninja_extra .controllers .base import APIController , get_route_functions
17+ from ninja_extra .controllers .base import (
18+ APIController ,
19+ MissingAPIControllerDecoratorException ,
20+ get_route_functions ,
21+ )
1822from ninja_extra .controllers .response import Detail , Id , Ok
23+ from ninja_extra .helper import get_route_function
1924from ninja_extra .permissions .common import AllowAny
2025
2126from .schemas import UserSchema
@@ -76,23 +81,32 @@ class DisableAutoImportController:
7681
7782class TestAPIController :
7883 def test_api_controller_as_decorator (self ):
79- api_controller_instance = api_controller (
80- "prefix " , tags = "new_tag" , auth = FakeAuth ( )
84+ controller_type = api_controller ( "prefix" , tags = "new_tag" , auth = FakeAuth ()) (
85+ type ( "Any " , (), {} )
8186 )
87+ api_controller_instance = controller_type .get_api_controller ()
8288
8389 assert not api_controller_instance .has_auth_async
8490 assert not api_controller_instance ._prefix_has_route_param
8591 assert api_controller_instance .prefix == "prefix"
8692 assert api_controller_instance .tags == ["new_tag" ]
8793 assert api_controller_instance .permission_classes == [AllowAny ]
8894
89- api_controller_instance = api_controller ()
95+ controller_type = api_controller ()(controller_type )
96+ api_controller_instance = controller_type .get_api_controller ()
9097 assert api_controller_instance .prefix == ""
91- assert api_controller_instance .tags is None
98+ assert api_controller_instance .tags == [ "any" ]
9299 assert "abc" in SomeController .__module__
93100 assert "tests.test_controller" in Some2Controller .__module__
94101 assert Some2Controller .get_api_controller ()
95102
103+ def test_controller_get_api_controller_raise_exception (self ):
104+ class BController (ControllerBase ):
105+ pass
106+
107+ with pytest .raises (MissingAPIControllerDecoratorException ):
108+ BController .get_api_controller ()
109+
96110 def test_api_controller_prefix_with_parameter (self ):
97111 @api_controller ("/{int:organisation_id}" )
98112 class UsersController :
@@ -130,7 +144,9 @@ def test_controller_should_have_path_operation_list(self):
130144 _api_controller = SomeControllerWithRoute .get_api_controller ()
131145 assert len (_api_controller ._path_operations ) == 7
132146
133- route_function : RouteFunction = SomeControllerWithRoute .example
147+ route_function : RouteFunction = get_route_function (
148+ SomeControllerWithRoute ().example
149+ )
134150 path_view = _api_controller ._path_operations .get (str (route_function ))
135151 assert path_view , "route doesn't exist in controller"
136152 assert len (path_view .operations ) == 1
@@ -144,18 +160,16 @@ def test_get_route_function_should_return_instance_route_definitions(self):
144160 assert isinstance (route_definition , RouteFunction )
145161
146162 def test_compute_api_route_function_works (self ):
147- _api_controller = api_controller ()
148-
163+ @api_controller ()
149164 class AnyClassTypeWithRoute :
150165 @http_get ("/example" )
151166 def example (self ):
152167 pass
153168
154- _api_controller (AnyClassTypeWithRoute )
155- assert len (_api_controller .path_operations ) == 1
156- path_view = _api_controller .path_operations .get (
157- str (AnyClassTypeWithRoute .example )
158- )
169+ api_controller_instance = AnyClassTypeWithRoute .get_api_controller ()
170+ assert len (api_controller_instance .path_operations ) == 1
171+ route_function = get_route_function (AnyClassTypeWithRoute ().example )
172+ path_view = api_controller_instance .path_operations .get (str (route_function ))
159173 assert path_view
160174
161175 @pytest .mark .django_db
@@ -207,29 +221,32 @@ def test_controller_base_get_object_or_none_works(self):
207221
208222@pytest .mark .skipif (django .VERSION < (3 , 1 ), reason = "requires django 3.1 or higher" )
209223def test_async_controller ():
210- api_controller_instance = api_controller (
224+ api_controller_decorator = api_controller (
211225 "prefix" , tags = "any_Tag" , auth = AsyncFakeAuth ()
212226 )
213- assert api_controller_instance .has_auth_async
214227
215228 with pytest .raises (Exception ) as ex :
216229
217- @api_controller_instance
230+ @api_controller_decorator
218231 class NonAsyncRouteInControllerWithAsyncAuth :
219232 @http_get ("/example" )
220233 def example (self ):
221234 pass
222235
223236 assert "NonAsyncRouteInControllerWithAsyncAuth" in str (ex ) and "example" in str (ex )
224237
225- @api_controller_instance
238+ @api_controller_decorator
226239 class AsyncRouteInControllerWithAsyncAuth :
227240 @http_get ("/example" )
228241 async def example (self ):
229242 pass
230243
244+ example_route_function = get_route_function (
245+ AsyncRouteInControllerWithAsyncAuth ().example
246+ )
247+ assert AsyncRouteInControllerWithAsyncAuth .get_api_controller ().has_auth_async
231248 assert isinstance (
232- AsyncRouteInControllerWithAsyncAuth . example .operation .auth_callbacks [0 ],
249+ example_route_function .operation .auth_callbacks [0 ],
233250 AsyncFakeAuth ,
234251 )
235252
@@ -291,9 +308,10 @@ def test_generic_controller_response_in_route_functions_works(self):
291308 )
292309
293310 ok_response = Ok [UserSchema ](dict (name = "John" , age = 56 ))
294- result = SomeControllerWithRoute . example_with_ok_schema_response (
295- request = Mock (), user = UserSchema ( name = "John" , age = 56 )
311+ route_function = get_route_function (
312+ SomeControllerWithRoute (). example_with_ok_schema_response
296313 )
314+ result = route_function (request = Mock (), user = UserSchema (name = "John" , age = 56 ))
297315 assert isinstance (result , tuple )
298316 assert result [1 ] == ok_response .convert_to_schema ()
299317 assert result [0 ] == ok_response .status_code
@@ -307,17 +325,19 @@ def test_controller_response_in_route_functions_works(self):
307325 assert detail .convert_to_schema ().dict () == response .json ()
308326
309327 ok_response = Ok ("5242" )
310- result = SomeControllerWithRoute . example_with_ok_response (
311- request = Mock (), ex_id = "5242"
328+ route_function = get_route_function (
329+ SomeControllerWithRoute (). example_with_ok_response
312330 )
331+ result = route_function (request = Mock (), ex_id = "5242" )
313332 assert isinstance (result , tuple )
314333 assert result [1 ] == ok_response .convert_to_schema ()
315334 assert result [0 ] == ok_response .status_code
316335
317336 id_response = Id ("5242" )
318- result = SomeControllerWithRoute . example_with_id_response (
319- request = Mock (), ex_id = "5242"
337+ route_function = get_route_function (
338+ SomeControllerWithRoute (). example_with_id_response
320339 )
340+ result = route_function (request = Mock (), ex_id = "5242" )
321341 assert isinstance (result , tuple )
322342 assert result [1 ] == id_response .convert_to_schema ()
323343 assert result [0 ] == id_response .status_code
0 commit comments