|
| 1 | +import uuid |
| 2 | + |
| 3 | +import django |
| 4 | +import pytest |
| 5 | +from ninja import Schema |
| 6 | + |
| 7 | +from ninja_extra import api_controller, route |
| 8 | +from ninja_extra.testing import TestAsyncClient, TestClient |
| 9 | + |
| 10 | + |
| 11 | +@api_controller("", tags=["Users"]) |
| 12 | +class UserController: |
| 13 | + @route.get("/users") |
| 14 | + def list_users(self): |
| 15 | + return [ |
| 16 | + { |
| 17 | + "first_name": "Ninja Extra", |
| 18 | + "username": "django_ninja", |
| 19 | + |
| 20 | + } |
| 21 | + ] |
| 22 | + |
| 23 | + |
| 24 | +class TestClientWithController: |
| 25 | + def test_get_users(self): |
| 26 | + client = TestClient(UserController) |
| 27 | + response = client.get("/users") |
| 28 | + assert response.status_code == 200 |
| 29 | + body = response.json() |
| 30 | + assert isinstance(body, list) and len(body) == 1 |
| 31 | + assert body[0] == { |
| 32 | + "first_name": "Ninja Extra", |
| 33 | + "username": "django_ninja", |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +@api_controller("", tags=["Math"]) |
| 39 | +class MyMathController: |
| 40 | + @route.get("/add") |
| 41 | + async def add(self, a: int, b: int): |
| 42 | + """Return a - b to mirror the docs example.""" |
| 43 | + return {"result": a - b} |
| 44 | + |
| 45 | + |
| 46 | +class TestAsyncClientWithController: |
| 47 | + @pytest.mark.skipif(django.VERSION < (3, 1), reason="requires django 3.1 or higher") |
| 48 | + @pytest.mark.asyncio |
| 49 | + async def test_add_async(self): |
| 50 | + client = TestAsyncClient(MyMathController) |
| 51 | + response = await client.get("/add", query={"a": 3, "b": 5}) |
| 52 | + assert response.status_code == 200 |
| 53 | + assert response.json() == {"result": -2} |
| 54 | + |
| 55 | + |
| 56 | +@api_controller("/api", tags=["Users"]) |
| 57 | +class PrefixedUserController: |
| 58 | + @route.get("/users") |
| 59 | + def list_users(self): |
| 60 | + return [ |
| 61 | + { |
| 62 | + "first_name": "Ninja Extra", |
| 63 | + "username": "django_ninja", |
| 64 | + |
| 65 | + } |
| 66 | + ] |
| 67 | + |
| 68 | + |
| 69 | +class TestClientWithPrefixedController: |
| 70 | + def test_get_users_under_prefix(self): |
| 71 | + client = TestClient(PrefixedUserController) |
| 72 | + response = client.get("/users") |
| 73 | + assert response.status_code == 200 |
| 74 | + body = response.json() |
| 75 | + assert isinstance(body, list) and len(body) == 1 |
| 76 | + assert body[0]["username"] == "django_ninja" |
| 77 | + |
| 78 | + |
| 79 | +@api_controller("/math", tags=["Math"]) |
| 80 | +class PrefixedMathController: |
| 81 | + @route.get("/add") |
| 82 | + async def add(self, a: int, b: int): |
| 83 | + return {"result": a + b} |
| 84 | + |
| 85 | + |
| 86 | +class TestAsyncClientWithPrefixedController: |
| 87 | + @pytest.mark.skipif(django.VERSION < (3, 1), reason="requires django 3.1 or higher") |
| 88 | + @pytest.mark.asyncio |
| 89 | + async def test_add_async_under_prefix(self): |
| 90 | + client = TestAsyncClient(PrefixedMathController) |
| 91 | + response = await client.get("/add", query={"a": 3, "b": 5}) |
| 92 | + assert response.status_code == 200 |
| 93 | + assert response.json() == {"result": 8} |
| 94 | + |
| 95 | + |
| 96 | +class UserIn(Schema): |
| 97 | + username: str |
| 98 | + email: str |
| 99 | + |
| 100 | + |
| 101 | +@api_controller("/users/{int:org_id}/", tags=["Users"]) |
| 102 | +class OrgUsersController: |
| 103 | + @route.post("") |
| 104 | + def create_user(self, org_id: int, user: UserIn): |
| 105 | + return {"id": str(uuid.uuid4()), "org_id": org_id, "username": user.username} |
| 106 | + |
| 107 | + |
| 108 | +class TestClientWithParamPrefixedController: |
| 109 | + def test_create_user_under_param_prefix(self): |
| 110 | + client = TestClient(OrgUsersController) |
| 111 | + response = client.post( |
| 112 | + "/users/123/", json={ "username": "jane", "email": "[email protected]"} |
| 113 | + ) |
| 114 | + assert response.status_code == 200 |
| 115 | + body = response.json() |
| 116 | + assert body["org_id"] == 123 |
| 117 | + assert body["username"] == "jane" |
| 118 | + assert "id" in body |
0 commit comments