|
4 | 4 | import pytest |
5 | 5 | from django.contrib.auth.models import AnonymousUser, User |
6 | 6 | from ninja import Schema |
| 7 | +from ninja.constants import NOT_SET |
7 | 8 |
|
8 | 9 | from ninja_extra import api_controller, permissions, route |
9 | 10 | from ninja_extra.controllers import ( |
10 | 11 | AsyncRouteFunction, |
11 | 12 | Detail, |
12 | 13 | Id, |
13 | 14 | Ok, |
14 | | - Route, |
15 | 15 | RouteFunction, |
16 | 16 | RouteInvalidParameterException, |
17 | 17 | ) |
|
20 | 20 | from ninja_extra.exceptions import PermissionDenied |
21 | 21 | from ninja_extra.permissions import AllowAny |
22 | 22 |
|
| 23 | +from .schemas import UserSchema |
23 | 24 | from .utils import FakeAuth |
24 | 25 |
|
25 | 26 | anonymous_request = Mock() |
@@ -71,6 +72,10 @@ def example_list_create(self, ex_id: str): |
71 | 72 | def example_post_operation_id(self): |
72 | 73 | pass |
73 | 74 |
|
| 75 | + @route.get("/example/return-response-as-schema") |
| 76 | + def function_return_as_response_schema(self) -> UserSchema: |
| 77 | + pass |
| 78 | + |
74 | 79 |
|
75 | 80 | class TestControllerRoute: |
76 | 81 | @pytest.mark.parametrize( |
@@ -114,6 +119,14 @@ def test_controller_route_should_right_view_func_type(self): |
114 | 119 | == SomeTestController.example |
115 | 120 | ) |
116 | 121 |
|
| 122 | + def test_controller_route_should_use_userschema_as_response(self): |
| 123 | + route_function: RouteFunction = SomeTestController.example |
| 124 | + assert route_function.route.route_params.response == NOT_SET |
| 125 | + route_function: RouteFunction = ( |
| 126 | + SomeTestController.function_return_as_response_schema |
| 127 | + ) |
| 128 | + assert route_function.route.route_params.response == UserSchema |
| 129 | + |
117 | 130 | def test_route_generic_invalid_parameters(self): |
118 | 131 | with pytest.raises(RouteInvalidParameterException) as ex: |
119 | 132 |
|
|
0 commit comments