1+ import uuid
12from unittest .mock import Mock , patch
23
34import django
45import pytest
56from django .contrib .auth .models import Group
6-
7- from ninja_extra import NinjaExtraAPI , api_controller , exceptions , http_get , testing
8- from ninja_extra .controllers import ControllerBase , RouteContext , RouteFunction
9- from ninja_extra .controllers .base import (
10- APIController ,
11- compute_api_route_function ,
12- get_route_functions ,
7+ from pydantic import UUID4 , BaseModel
8+
9+ from ninja_extra import (
10+ NinjaExtraAPI ,
11+ api_controller ,
12+ exceptions ,
13+ http_get ,
14+ http_post ,
15+ testing ,
1316)
17+ from ninja_extra .controllers import ControllerBase , RouteContext , RouteFunction
18+ from ninja_extra .controllers .base import APIController , get_route_functions
1419from ninja_extra .controllers .response import Detail , Id , Ok
1520from ninja_extra .permissions .common import AllowAny
1621
1722from .utils import AsyncFakeAuth , FakeAuth
1823
1924
25+ class UserSchema (BaseModel ):
26+ name : str
27+ age : int
28+
29+
2030@api_controller
2131class SomeController :
2232 pass
@@ -51,6 +61,18 @@ def example_with_ok_response(self, ex_id: str):
5161 def example_with_id_response (self , ex_id : str ):
5262 return self .Id (ex_id )
5363
64+ @http_get ("/example/{uuid:ex_id}/generic" , response = Id [uuid .UUID ])
65+ def example_with_id_uuid_response (self , ex_id : str ):
66+ return self .Id [uuid .UUID ](ex_id )
67+
68+ @http_post ("/example/ok" , response = Ok [UserSchema ])
69+ def example_with_ok_schema_response (self , user : UserSchema ):
70+ return self .Ok [UserSchema ](user .dict ())
71+
72+ @http_post ("/example/details" , response = Detail [UserSchema ])
73+ def example_with_detail_schema_response (self , user : UserSchema ):
74+ return self .Detail [UserSchema ](user .dict ())
75+
5476
5577@api_controller ("" , tags = ["new tag" ])
5678class DisableAutoImportController :
@@ -224,6 +246,30 @@ class TestAPIControllerResponse:
224246 id_response = Id ("ID" )
225247 detail_response = Detail (dict (errors = [dict (test = "passed" )]), status_code = 302 )
226248
249+ ok_response_generic = Ok [UserSchema ](dict (name = "TestName" , age = 23 ))
250+ id_response_generic = Id [UserSchema ](UserSchema (name = "John" , age = 56 ))
251+ detail_response_generic = Detail [UserSchema ](
252+ UserSchema (name = "John" , age = 56 ), 400
253+ ) # not a practice example but you get the point. LOL
254+
255+ def test_generic_controller_response (self ):
256+ # OK Response
257+ assert self .ok_response_generic .get_schema () == Ok [UserSchema ].get_schema ()
258+ assert self .ok_response_generic .convert_to_schema () == Ok [
259+ UserSchema
260+ ].get_schema ()(detail = dict (name = "TestName" , age = 23 ))
261+ assert self .ok_response .status_code == Ok .status_code
262+ # ID Response
263+ assert self .id_response .get_schema () == Id .get_schema ()
264+ assert self .id_response .convert_to_schema () == Id .get_schema ()(id = "ID" )
265+ assert self .id_response .status_code == Id .status_code
266+ # Detail Response
267+ assert self .detail_response .get_schema () == Detail .get_schema ()
268+ assert self .detail_response .convert_to_schema () == Detail .get_schema ()(
269+ detail = dict (errors = [dict (test = "passed" )])
270+ )
271+ assert self .id_response .status_code != Detail .status_code
272+
227273 def test_controller_response (self ):
228274 # OK Response
229275 assert self .ok_response .get_schema () == Ok .get_schema ()
@@ -240,7 +286,23 @@ def test_controller_response(self):
240286 )
241287 assert self .id_response .status_code != Detail .status_code
242288
243- def test_controller_response_works (self ):
289+ def test_generic_controller_response_in_route_functions_works (self ):
290+ _uuid_value = str (uuid .uuid4 ())
291+ client = testing .TestClient (SomeControllerWithRoute )
292+ response = client .get (f"/example/{ _uuid_value } /generic" )
293+
294+ assert response .status_code == 201
295+ assert Id [uuid .UUID ](_uuid_value ).convert_to_schema ().dict () == response .json ()
296+
297+ ok_response = Ok [UserSchema ](dict (name = "John" , age = 56 ))
298+ result = SomeControllerWithRoute .example_with_ok_schema_response (
299+ request = Mock (), user = UserSchema (name = "John" , age = 56 )
300+ )
301+ assert isinstance (result , tuple )
302+ assert result [1 ] == ok_response .convert_to_schema ()
303+ assert result [0 ] == ok_response .status_code
304+
305+ def test_controller_response_in_route_functions_works (self ):
244306 detail = Detail ("5242" , status_code = 302 )
245307 client = testing .TestClient (SomeControllerWithRoute )
246308 response = client .get ("/example/5242" )
0 commit comments