Skip to content

Commit 905e8be

Browse files
committed
test refactor
1 parent 20aa33d commit 905e8be

File tree

3 files changed

+81
-34
lines changed

3 files changed

+81
-34
lines changed

tests/test_controller.py

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1+
import uuid
12
from unittest.mock import Mock, patch
23

34
import django
45
import pytest
56
from 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
1419
from ninja_extra.controllers.response import Detail, Id, Ok
1520
from ninja_extra.permissions.common import AllowAny
1621

1722
from .utils import AsyncFakeAuth, FakeAuth
1823

1924

25+
class UserSchema(BaseModel):
26+
name: str
27+
age: int
28+
29+
2030
@api_controller
2131
class 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"])
5678
class 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")

tests/test_operation.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33

44
from ninja_extra import api_controller, route
55
from ninja_extra.controllers import AsyncRouteFunction, RouteFunction
6-
from ninja_extra.operation import (
7-
AsyncControllerOperation,
8-
AsyncOperation,
9-
ControllerOperation,
10-
Operation,
11-
)
6+
from ninja_extra.operation import AsyncOperation, Operation
127
from ninja_extra.testing import TestAsyncClient, TestClient
138

149
from .utils import AsyncFakeAuth, FakeAuth, mock_log_call, mock_signal_call

tests/test_route.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@
55
from django.contrib.auth.models import AnonymousUser, User
66
from ninja import Schema
77

8-
from ninja_extra import (
9-
api_controller,
10-
http_delete,
11-
http_generic,
12-
http_get,
13-
http_patch,
14-
http_post,
15-
http_put,
16-
permissions,
17-
route,
18-
)
8+
from ninja_extra import api_controller, permissions, route
199
from ninja_extra.controllers import (
2010
AsyncRouteFunction,
2111
Detail,
@@ -42,42 +32,42 @@
4232
permissions=[permissions.IsAuthenticated & permissions.IsAdminUser],
4333
)
4434
class PermissionController:
45-
@http_post("/example_post", auth=None)
35+
@route.post("/example_post", auth=None)
4636
def example(self):
4737
return {"message": "OK"}
4838

49-
@http_get("/example_get", auth=None, permissions=[permissions.AllowAny])
39+
@route.get("/example_get", auth=None, permissions=[permissions.AllowAny])
5040
def example_allow_any(self):
5141
return {"message": "OK"}
5242

5343

5444
@api_controller
5545
class SomeTestController:
56-
@http_get("/example")
46+
@route.get("/example")
5747
def example(self):
5848
pass
5949

60-
@http_post("/example")
50+
@route.post("/example")
6151
def example_post(self):
6252
pass
6353

64-
@http_patch("/example/{ex_id}")
54+
@route.patch("/example/{ex_id}")
6555
def example_patch(self, ex_id: str):
6656
pass
6757

68-
@http_patch("/example/{ex_id}")
58+
@route.patch("/example/{ex_id}")
6959
def example_put(self, ex_id: str):
7060
pass
7161

72-
@http_delete("/example/{ex_id}")
62+
@route.delete("/example/{ex_id}")
7363
def example_delete(self, ex_id: str):
7464
pass
7565

76-
@http_generic("/example/list", methods=["POST", "GET"])
66+
@route.generic("/example/list", methods=["POST", "GET"])
7767
def example_list_create(self, ex_id: str):
7868
pass
7969

80-
@http_post("/example/operation-id", operation_id="example_post_operation_id")
70+
@route.post("/example/operation-id", operation_id="example_post_operation_id")
8171
def example_post_operation_id(self):
8272
pass
8373

0 commit comments

Comments
 (0)