| 1 |
from typing import List, Union |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from ninja import Field, NinjaAPI, Schema, Status |
| 7 |
from ninja.operation import ResponseObject |
| 8 |
from ninja.pagination import LimitOffsetPagination, paginate |
| 9 |
from ninja.responses import codes_2xx, codes_3xx |
| 10 |
from ninja.testing import TestAsyncClient, TestClient |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class UserOut(Schema): |
| 16 |
id: int |
| 17 |
name: str |
| 18 |
|
| 19 |
|
| 20 |
class UserOutSub(UserOut): |
| 21 |
extra: str = "default" |
| 22 |
|
| 23 |
|
| 24 |
class ErrorOut(Schema): |
| 25 |
detail: str |
| 26 |
|
| 27 |
|
| 28 |
class AliasOut(Schema): |
| 29 |
user_name: str = Field(serialization_alias="userName") |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
api = NinjaAPI() |
| 35 |
|
| 36 |
|
| 37 |
@api.get("/status_dict", response={200: UserOut, 400: ErrorOut}) |
| 38 |
def status_dict(request): |
| 39 |
return Status(200, {"id": 1, "name": "John"}) |
| 40 |
|
| 41 |
|
| 42 |
@api.get("/status_error", response={200: UserOut, 400: ErrorOut}) |
| 43 |
def status_error(request): |
| 44 |
return Status(400, {"detail": "bad request"}) |
| 45 |
|
| 46 |
|
| 47 |
@api.get("/status_none", response={204: None}) |
| 48 |
def status_none(request): |
| 49 |
return Status(204, None) |
| 50 |
|
| 51 |
|
| 52 |
@api.get("/status_ellipsis", response={200: UserOut, ...: ErrorOut}) |
| 53 |
def status_ellipsis(request, code: int): |
| 54 |
if code == 200: |
| 55 |
return Status(200, {"id": 1, "name": "John"}) |
| 56 |
return Status(code, {"detail": "fallback"}) |
| 57 |
|
| 58 |
|
| 59 |
@api.get("/status_code_groups", response={codes_2xx: UserOut, codes_3xx: ErrorOut}) |
| 60 |
def status_code_groups(request, code: int): |
| 61 |
if code < 300: |
| 62 |
return Status(code, {"id": 1, "name": "John"}) |
| 63 |
return Status(code, {"detail": "redirect"}) |
| 64 |
|
| 65 |
|
| 66 |
@api.get("/status_model_instance", response={200: UserOut}) |
| 67 |
def status_model_instance(request): |
| 68 |
return Status(200, UserOut(id=1, name="John")) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
@api.get("/tuple_return", response={200: UserOut, 400: ErrorOut}) |
| 75 |
def tuple_return(request): |
| 76 |
return 200, {"id": 1, "name": "John"} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@api.get("/model_instance", response=UserOut) |
| 83 |
def model_instance(request): |
| 84 |
return UserOut(id=1, name="John") |
| 85 |
|
| 86 |
|
| 87 |
@api.get("/model_subclass", response=UserOut) |
| 88 |
def model_subclass(request): |
| 89 |
return UserOutSub(id=1, name="John", extra="bonus") |
| 90 |
|
| 91 |
|
| 92 |
@api.get("/dict_result", response=UserOut) |
| 93 |
def dict_result(request): |
| 94 |
return {"id": 1, "name": "John"} |
| 95 |
|
| 96 |
|
| 97 |
@api.get("/union_response", response={200: Union[int, UserOut], 400: ErrorOut}) |
| 98 |
def union_response(request, q: int): |
| 99 |
if q == 0: |
| 100 |
return Status(200, 1) |
| 101 |
return Status(200, UserOut(id=1, name="John")) |
| 102 |
|
| 103 |
|
| 104 |
@api.get("/list_response", response={200: List[UserOut]}) |
| 105 |
def list_response(request): |
| 106 |
return Status(200, [{"id": 1, "name": "John"}]) |
| 107 |
|
| 108 |
|
| 109 |
@api.get("/by_alias_response", response=AliasOut, by_alias=True) |
| 110 |
def by_alias_response(request): |
| 111 |
return AliasOut(user_name="Alice") |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@api.get("/paginated_status", response={201: List[UserOut]}) |
| 118 |
@paginate(LimitOffsetPagination) |
| 119 |
def paginated_status(request): |
| 120 |
return Status( |
| 121 |
201, |
| 122 |
[{"id": 1, "name": "A"}, {"id": 2, "name": "B"}, {"id": 3, "name": "C"}], |
| 123 |
) |
| 124 |
|
| 125 |
|
| 126 |
@api.get("/paginated_normal", response=List[UserOut]) |
| 127 |
@paginate(LimitOffsetPagination) |
| 128 |
def paginated_normal(request): |
| 129 |
return [{"id": 1, "name": "A"}, {"id": 2, "name": "B"}] |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
async_api = NinjaAPI() |
| 134 |
|
| 135 |
|
| 136 |
@async_api.get("/async_paginated_status", response={201: List[UserOut]}) |
| 137 |
@paginate(LimitOffsetPagination) |
| 138 |
async def async_paginated_status(request): |
| 139 |
return Status( |
| 140 |
201, |
| 141 |
[{"id": 1, "name": "A"}, {"id": 2, "name": "B"}, {"id": 3, "name": "C"}], |
| 142 |
) |
| 143 |
|
| 144 |
|
| 145 |
@async_api.get("/async_paginated_normal", response=List[UserOut]) |
| 146 |
@paginate(LimitOffsetPagination) |
| 147 |
async def async_paginated_normal(request): |
| 148 |
return [{"id": 1, "name": "A"}, {"id": 2, "name": "B"}] |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
client = TestClient(api) |
| 154 |
async_client = TestAsyncClient(async_api) |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
class TestStatusGeneric: |
| 161 |
def test_subscriptable_at_runtime(self): |
| 162 |
"""Status[dict] should not raise TypeError (GitHub #1693).""" |
| 163 |
alias = Status[dict] |
| 164 |
assert alias is not None |
| 165 |
|
| 166 |
|
| 167 |
class TestStatusBasic: |
| 168 |
def test_status_with_dict(self): |
| 169 |
response = client.get("/status_dict") |
| 170 |
assert response.status_code == 200 |
| 171 |
assert response.json() == {"id": 1, "name": "John"} |
| 172 |
|
| 173 |
def test_status_error_code(self): |
| 174 |
response = client.get("/status_error") |
| 175 |
assert response.status_code == 400 |
| 176 |
assert response.json() == {"detail": "bad request"} |
| 177 |
|
| 178 |
def test_status_none_204(self): |
| 179 |
response = client.get("/status_none") |
| 180 |
assert response.status_code == 204 |
| 181 |
assert response.content == b"" |
| 182 |
|
| 183 |
def test_status_ellipsis_200(self): |
| 184 |
response = client.get("/status_ellipsis?code=200") |
| 185 |
assert response.status_code == 200 |
| 186 |
assert response.json() == {"id": 1, "name": "John"} |
| 187 |
|
| 188 |
def test_status_ellipsis_fallback(self): |
| 189 |
response = client.get("/status_ellipsis?code=500") |
| 190 |
assert response.status_code == 500 |
| 191 |
assert response.json() == {"detail": "fallback"} |
| 192 |
|
| 193 |
def test_status_code_groups_2xx(self): |
| 194 |
response = client.get("/status_code_groups?code=200") |
| 195 |
assert response.status_code == 200 |
| 196 |
assert response.json() == {"id": 1, "name": "John"} |
| 197 |
|
| 198 |
def test_status_code_groups_201(self): |
| 199 |
response = client.get("/status_code_groups?code=201") |
| 200 |
assert response.status_code == 201 |
| 201 |
assert response.json() == {"id": 1, "name": "John"} |
| 202 |
|
| 203 |
def test_status_code_groups_3xx(self): |
| 204 |
response = client.get("/status_code_groups?code=300") |
| 205 |
assert response.status_code == 300 |
| 206 |
assert response.json() == {"detail": "redirect"} |
| 207 |
|
| 208 |
def test_status_wrapping_model_instance(self): |
| 209 |
response = client.get("/status_model_instance") |
| 210 |
assert response.status_code == 200 |
| 211 |
assert response.json() == {"id": 1, "name": "John"} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
class TestTupleDeprecation: |
| 218 |
def test_tuple_emits_deprecation_warning(self): |
| 219 |
with pytest.warns(DeprecationWarning, match="deprecated.*Status"): |
| 220 |
client.get("/tuple_return") |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
class TestSkipRevalidation: |
| 227 |
def test_model_instance_skips_validation(self): |
| 228 |
with patch( |
| 229 |
"ninja.operation.ResponseObject", wraps=ResponseObject |
| 230 |
) as mock_resp_obj: |
| 231 |
response = client.get("/model_instance") |
| 232 |
assert response.status_code == 200 |
| 233 |
assert response.json() == {"id": 1, "name": "John"} |
| 234 |
mock_resp_obj.assert_not_called() |
| 235 |
|
| 236 |
def test_subclass_skips_validation(self): |
| 237 |
with patch( |
| 238 |
"ninja.operation.ResponseObject", wraps=ResponseObject |
| 239 |
) as mock_resp_obj: |
| 240 |
response = client.get("/model_subclass") |
| 241 |
assert response.status_code == 200 |
| 242 |
assert response.json() == {"id": 1, "name": "John", "extra": "bonus"} |
| 243 |
mock_resp_obj.assert_not_called() |
| 244 |
|
| 245 |
def test_dict_goes_through_validation(self): |
| 246 |
with patch( |
| 247 |
"ninja.operation.ResponseObject", wraps=ResponseObject |
| 248 |
) as mock_resp_obj: |
| 249 |
response = client.get("/dict_result") |
| 250 |
assert response.status_code == 200 |
| 251 |
assert response.json() == {"id": 1, "name": "John"} |
| 252 |
mock_resp_obj.assert_called_once() |
| 253 |
|
| 254 |
def test_union_no_skip(self): |
| 255 |
|
| 256 |
with patch( |
| 257 |
"ninja.operation.ResponseObject", wraps=ResponseObject |
| 258 |
) as mock_resp_obj: |
| 259 |
response = client.get("/union_response?q=1") |
| 260 |
assert response.status_code == 200 |
| 261 |
assert response.json() == {"id": 1, "name": "John"} |
| 262 |
mock_resp_obj.assert_called_once() |
| 263 |
|
| 264 |
def test_list_no_skip(self): |
| 265 |
|
| 266 |
with patch( |
| 267 |
"ninja.operation.ResponseObject", wraps=ResponseObject |
| 268 |
) as mock_resp_obj: |
| 269 |
response = client.get("/list_response") |
| 270 |
assert response.status_code == 200 |
| 271 |
assert response.json() == [{"id": 1, "name": "John"}] |
| 272 |
mock_resp_obj.assert_called_once() |
| 273 |
|
| 274 |
def test_by_alias_serialization(self): |
| 275 |
response = client.get("/by_alias_response") |
| 276 |
assert response.status_code == 200 |
| 277 |
assert response.json() == {"userName": "Alice"} |
| 278 |
|
| 279 |
def test_status_wrapping_model_skips_validation(self): |
| 280 |
with patch( |
| 281 |
"ninja.operation.ResponseObject", wraps=ResponseObject |
| 282 |
) as mock_resp_obj: |
| 283 |
response = client.get("/status_model_instance") |
| 284 |
assert response.status_code == 200 |
| 285 |
assert response.json() == {"id": 1, "name": "John"} |
| 286 |
mock_resp_obj.assert_not_called() |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
class TestPaginationStatus: |
| 293 |
def test_sync_pagination_with_status(self): |
| 294 |
response = client.get("/paginated_status?limit=2&offset=0") |
| 295 |
assert response.status_code == 201 |
| 296 |
data = response.json() |
| 297 |
assert data["count"] == 3 |
| 298 |
assert len(data["items"]) == 2 |
| 299 |
assert data["items"][0] == {"id": 1, "name": "A"} |
| 300 |
|
| 301 |
def test_sync_pagination_without_status(self): |
| 302 |
response = client.get("/paginated_normal?limit=2&offset=0") |
| 303 |
assert response.status_code == 200 |
| 304 |
data = response.json() |
| 305 |
assert data["count"] == 2 |
| 306 |
assert len(data["items"]) == 2 |
| 307 |
|
| 308 |
@pytest.mark.asyncio |
| 309 |
async def test_async_pagination_with_status(self): |
| 310 |
response = await async_client.get("/async_paginated_status?limit=2&offset=0") |
| 311 |
assert response.status_code == 201 |
| 312 |
data = response.json() |
| 313 |
assert data["count"] == 3 |
| 314 |
assert len(data["items"]) == 2 |
| 315 |
assert data["items"][0] == {"id": 1, "name": "A"} |
| 316 |
|
| 317 |
@pytest.mark.asyncio |
| 318 |
async def test_async_pagination_without_status(self): |
| 319 |
response = await async_client.get("/async_paginated_normal?limit=2&offset=0") |
| 320 |
assert response.status_code == 200 |
| 321 |
data = response.json() |
| 322 |
assert data["count"] == 2 |
| 323 |
assert len(data["items"]) == 2 |