| 1 |
from sys import version_info |
| 2 |
|
| 3 |
import pytest |
| 4 |
from main import router |
| 5 |
|
| 6 |
from ninja import Router |
| 7 |
from ninja.testing import TestClient |
| 8 |
|
| 9 |
client = TestClient(router) |
| 10 |
|
| 11 |
|
| 12 |
def test_text_get(): |
| 13 |
response = client.get("/text") |
| 14 |
assert response.status_code == 200, response.text |
| 15 |
assert response.json() == "Hello World" |
| 16 |
|
| 17 |
|
| 18 |
response_not_valid_bool = { |
| 19 |
"detail": [ |
| 20 |
{ |
| 21 |
"type": "bool_parsing", |
| 22 |
"loc": ["path", "item_id"], |
| 23 |
"msg": "Input should be a valid boolean, unable to interpret input", |
| 24 |
} |
| 25 |
] |
| 26 |
} |
| 27 |
|
| 28 |
response_not_valid_int = { |
| 29 |
"detail": [ |
| 30 |
{ |
| 31 |
"type": "int_parsing", |
| 32 |
"loc": ["path", "item_id"], |
| 33 |
"msg": "Input should be a valid integer, unable to parse string as an integer", |
| 34 |
} |
| 35 |
] |
| 36 |
} |
| 37 |
|
| 38 |
response_not_valid_custom = { |
| 39 |
"detail": [ |
| 40 |
{ |
| 41 |
"ctx": {"error": "Input should pass this custom validator"}, |
| 42 |
"loc": ["path", "item_id"], |
| 43 |
"msg": "Value error, Input should pass this custom validator", |
| 44 |
"type": "value_error", |
| 45 |
} |
| 46 |
] |
| 47 |
} |
| 48 |
|
| 49 |
response_not_valid_int_float = { |
| 50 |
"detail": [ |
| 51 |
{ |
| 52 |
"type": "int_parsing", |
| 53 |
"loc": ["path", "item_id"], |
| 54 |
"msg": "Input should be a valid integer, unable to parse string as an integer", |
| 55 |
} |
| 56 |
] |
| 57 |
} |
| 58 |
|
| 59 |
response_not_valid_float = { |
| 60 |
"detail": [ |
| 61 |
{ |
| 62 |
"type": "float_parsing", |
| 63 |
"loc": ["path", "item_id"], |
| 64 |
"msg": "Input should be a valid number, unable to parse string as a number", |
| 65 |
} |
| 66 |
] |
| 67 |
} |
| 68 |
|
| 69 |
response_at_least_3 = { |
| 70 |
"detail": [ |
| 71 |
{ |
| 72 |
"type": "string_too_short", |
| 73 |
"loc": ["path", "item_id"], |
| 74 |
"msg": "String should have at least 3 characters", |
| 75 |
"ctx": {"min_length": 3}, |
| 76 |
} |
| 77 |
] |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
response_at_least_2 = { |
| 82 |
"detail": [ |
| 83 |
{ |
| 84 |
"type": "string_too_short", |
| 85 |
"loc": ["path", "item_id"], |
| 86 |
"msg": "String should have at least 2 characters", |
| 87 |
"ctx": {"min_length": 2}, |
| 88 |
} |
| 89 |
] |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
response_maximum_3 = { |
| 94 |
"detail": [ |
| 95 |
{ |
| 96 |
"type": "string_too_long", |
| 97 |
"loc": ["path", "item_id"], |
| 98 |
"msg": "String should have at most 3 characters", |
| 99 |
"ctx": {"max_length": 3}, |
| 100 |
} |
| 101 |
] |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
response_greater_than_3 = { |
| 106 |
"detail": [ |
| 107 |
{ |
| 108 |
"type": "greater_than", |
| 109 |
"loc": ["path", "item_id"], |
| 110 |
"msg": "Input should be greater than 3", |
| 111 |
"ctx": {"gt": 3.0}, |
| 112 |
} |
| 113 |
] |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
response_greater_than_0 = { |
| 118 |
"detail": [ |
| 119 |
{ |
| 120 |
"type": "greater_than", |
| 121 |
"loc": ["path", "item_id"], |
| 122 |
"msg": "Input should be greater than 0", |
| 123 |
"ctx": {"gt": 0.0}, |
| 124 |
} |
| 125 |
] |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
response_greater_than_1 = { |
| 130 |
"detail": [ |
| 131 |
{ |
| 132 |
"type": "greater_than", |
| 133 |
"loc": ["path", "item_id"], |
| 134 |
"msg": "Input should be greater than 1", |
| 135 |
"ctx": {"gt": 1}, |
| 136 |
} |
| 137 |
] |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
response_greater_than_equal_3 = { |
| 142 |
"detail": [ |
| 143 |
{ |
| 144 |
"type": "greater_than_equal", |
| 145 |
"loc": ["path", "item_id"], |
| 146 |
"msg": "Input should be greater than or equal to 3", |
| 147 |
"ctx": {"ge": 3.0}, |
| 148 |
} |
| 149 |
] |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
response_less_than_3 = { |
| 154 |
"detail": [ |
| 155 |
{ |
| 156 |
"type": "less_than", |
| 157 |
"loc": ["path", "item_id"], |
| 158 |
"msg": "Input should be less than 3", |
| 159 |
"ctx": {"lt": 3.0}, |
| 160 |
} |
| 161 |
] |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
response_less_than_0 = { |
| 166 |
"detail": [ |
| 167 |
{ |
| 168 |
"type": "less_than", |
| 169 |
"loc": ["path", "item_id"], |
| 170 |
"msg": "Input should be less than 0", |
| 171 |
"ctx": {"lt": 0.0}, |
| 172 |
} |
| 173 |
] |
| 174 |
} |
| 175 |
|
| 176 |
response_less_than_equal_3 = { |
| 177 |
"detail": [ |
| 178 |
{ |
| 179 |
"type": "less_than_equal", |
| 180 |
"loc": ["path", "item_id"], |
| 181 |
"msg": "Input should be less than or equal to 3", |
| 182 |
"ctx": {"le": 3.0}, |
| 183 |
} |
| 184 |
] |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
response_not_valid_pattern = { |
| 189 |
"detail": [ |
| 190 |
{ |
| 191 |
"ctx": { |
| 192 |
"pattern": "^foo", |
| 193 |
}, |
| 194 |
"loc": ["path", "item_id"], |
| 195 |
"msg": "String should match pattern '^foo'", |
| 196 |
"type": "string_pattern_mismatch", |
| 197 |
} |
| 198 |
] |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.parametrize( |
| 203 |
"path,expected_status,expected_response", |
| 204 |
[ |
| 205 |
("/path/foobar", 200, "foobar"), |
| 206 |
("/path/str/foobar", 200, "foobar"), |
| 207 |
("/path/str/42", 200, "42"), |
| 208 |
("/path/str/True", 200, "True"), |
| 209 |
("/path/int/foobar", 422, response_not_valid_int), |
| 210 |
("/path/int/True", 422, response_not_valid_int), |
| 211 |
("/path/int/42", 200, 42), |
| 212 |
("/path/int/42.5", 422, response_not_valid_int_float), |
| 213 |
("/path/float/foobar", 422, response_not_valid_float), |
| 214 |
("/path/float/True", 422, response_not_valid_float), |
| 215 |
("/path/float/42", 200, 42), |
| 216 |
("/path/float/42.5", 200, 42.5), |
| 217 |
("/path/bool/foobar", 422, response_not_valid_bool), |
| 218 |
("/path/bool/True", 200, True), |
| 219 |
("/path/bool/42", 422, response_not_valid_bool), |
| 220 |
("/path/bool/42.5", 422, response_not_valid_bool), |
| 221 |
("/path/bool/1", 200, True), |
| 222 |
("/path/bool/0", 200, False), |
| 223 |
("/path/bool/true", 200, True), |
| 224 |
("/path/bool/False", 200, False), |
| 225 |
("/path/bool/false", 200, False), |
| 226 |
("/path/param/foo", 200, "foo"), |
| 227 |
("/path/param-required/foo", 200, "foo"), |
| 228 |
("/path/param-minlength/foo", 200, "foo"), |
| 229 |
("/path/param-minlength/fo", 422, response_at_least_3), |
| 230 |
("/path/param-maxlength/foo", 200, "foo"), |
| 231 |
("/path/param-maxlength/foobar", 422, response_maximum_3), |
| 232 |
("/path/param-min_maxlength/foo", 200, "foo"), |
| 233 |
("/path/param-min_maxlength/foobar", 422, response_maximum_3), |
| 234 |
("/path/param-min_maxlength/f", 422, response_at_least_2), |
| 235 |
("/path/param-gt/42", 200, 42), |
| 236 |
("/path/param-gt/2", 422, response_greater_than_3), |
| 237 |
("/path/param-gt0/0.05", 200, 0.05), |
| 238 |
("/path/param-gt0/0", 422, response_greater_than_0), |
| 239 |
("/path/param-ge/42", 200, 42), |
| 240 |
("/path/param-ge/3", 200, 3), |
| 241 |
("/path/param-ge/2", 422, response_greater_than_equal_3), |
| 242 |
("/path/param-lt/42", 422, response_less_than_3), |
| 243 |
("/path/param-lt/2", 200, 2), |
| 244 |
("/path/param-lt0/-1", 200, -1), |
| 245 |
("/path/param-lt0/0", 422, response_less_than_0), |
| 246 |
("/path/param-le/42", 422, response_less_than_equal_3), |
| 247 |
("/path/param-le/3", 200, 3), |
| 248 |
("/path/param-le/2", 200, 2), |
| 249 |
("/path/param-lt-gt/2", 200, 2), |
| 250 |
("/path/param-lt-gt/4", 422, response_less_than_3), |
| 251 |
("/path/param-lt-gt/0", 422, response_greater_than_1), |
| 252 |
("/path/param-le-ge/2", 200, 2), |
| 253 |
("/path/param-le-ge/1", 200, 1), |
| 254 |
("/path/param-le-ge/3", 200, 3), |
| 255 |
("/path/param-le-ge/4", 422, response_less_than_equal_3), |
| 256 |
("/path/param-lt-int/2", 200, 2), |
| 257 |
("/path/param-lt-int/42", 422, response_less_than_3), |
| 258 |
("/path/param-lt-int/2.7", 422, response_not_valid_int_float), |
| 259 |
("/path/param-gt-int/42", 200, 42), |
| 260 |
("/path/param-gt-int/2", 422, response_greater_than_3), |
| 261 |
("/path/param-gt-int/2.7", 422, response_not_valid_int_float), |
| 262 |
("/path/param-le-int/42", 422, response_less_than_equal_3), |
| 263 |
("/path/param-le-int/3", 200, 3), |
| 264 |
("/path/param-le-int/2", 200, 2), |
| 265 |
("/path/param-le-int/2.7", 422, response_not_valid_int_float), |
| 266 |
("/path/param-ge-int/42", 200, 42), |
| 267 |
("/path/param-ge-int/3", 200, 3), |
| 268 |
("/path/param-ge-int/2", 422, response_greater_than_equal_3), |
| 269 |
("/path/param-ge-int/2.7", 422, response_not_valid_int_float), |
| 270 |
("/path/param-lt-gt-int/2", 200, 2), |
| 271 |
("/path/param-lt-gt-int/4", 422, response_less_than_3), |
| 272 |
("/path/param-lt-gt-int/0", 422, response_greater_than_1), |
| 273 |
("/path/param-lt-gt-int/2.7", 422, response_not_valid_int_float), |
| 274 |
("/path/param-le-ge-int/2", 200, 2), |
| 275 |
("/path/param-le-ge-int/1", 200, 1), |
| 276 |
("/path/param-le-ge-int/3", 200, 3), |
| 277 |
("/path/param-le-ge-int/4", 422, response_less_than_equal_3), |
| 278 |
("/path/param-le-ge-int/2.7", 422, response_not_valid_int_float), |
| 279 |
("/path/param-pattern/foo", 200, "foo"), |
| 280 |
("/path/param-pattern/fo", 422, response_not_valid_pattern), |
| 281 |
], |
| 282 |
) |
| 283 |
def test_get_path(path, expected_status, expected_response): |
| 284 |
response = client.get(path) |
| 285 |
print(path, response.json()) |
| 286 |
assert response.status_code == expected_status |
| 287 |
assert response.json() == expected_response |
| 288 |
|
| 289 |
|
| 290 |
@pytest.mark.skipif( |
| 291 |
version_info < (3, 9), |
| 292 |
reason="requires py3.9+ for Annotated[] at the route definition site", |
| 293 |
) |
| 294 |
@pytest.mark.parametrize( |
| 295 |
"path,expected_status,expected_response", |
| 296 |
[ |
| 297 |
("/path/param_ex/True", 422, response_not_valid_int), |
| 298 |
("/path/param_ex/0", 422, response_not_valid_custom), |
| 299 |
("/path/param_ex/42", 200, 42), |
| 300 |
], |
| 301 |
) |
| 302 |
def test_get_pathex(path, expected_status, expected_response): |
| 303 |
response = client.get(path) |
| 304 |
print(path, response.json()) |
| 305 |
assert response.status_code == expected_status |
| 306 |
assert response.json() == expected_response |
| 307 |
|
| 308 |
|
| 309 |
@pytest.mark.parametrize( |
| 310 |
"path,expected_status,expected_response", |
| 311 |
[ |
| 312 |
("/path/param-django-str/42", 200, "42"), |
| 313 |
("/path/param-django-str/-1", 200, "-1"), |
| 314 |
("/path/param-django-str/foobar", 200, "foobar"), |
| 315 |
("/path/param-django-int/0", 200, 0), |
| 316 |
("/path/param-django-int/42", 200, 42), |
| 317 |
("/path/param-django-int/42.5", "Cannot resolve", Exception), |
| 318 |
("/path/param-django-int/-1", "Cannot resolve", Exception), |
| 319 |
("/path/param-django-int/True", "Cannot resolve", Exception), |
| 320 |
("/path/param-django-int/foobar", "Cannot resolve", Exception), |
| 321 |
("/path/param-django-int/not-an-int", 200, "Found not-an-int"), |
| 322 |
|
| 323 |
("/path/param-django-int-str/42.5", "Cannot resolve", Exception), |
| 324 |
( |
| 325 |
"/path/param-django-slug/django-ninja-is-the-best", |
| 326 |
200, |
| 327 |
"django-ninja-is-the-best", |
| 328 |
), |
| 329 |
("/path/param-django-slug/42.5", "Cannot resolve", Exception), |
| 330 |
( |
| 331 |
"/path/param-django-uuid/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a", |
| 332 |
200, |
| 333 |
"31ea378c-c052-4b4c-bf0b-679ce5cfcc2a", |
| 334 |
), |
| 335 |
( |
| 336 |
"/path/param-django-uuid/31ea378c-c052-4b4c-bf0b-679ce5cfcc2", |
| 337 |
"Cannot resolve", |
| 338 |
Exception, |
| 339 |
), |
| 340 |
( |
| 341 |
"/path/param-django-uuid-notype/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a", |
| 342 |
200, |
| 343 |
"31ea378c-c052-4b4c-bf0b-679ce5cfcc2a", |
| 344 |
), |
| 345 |
( |
| 346 |
"/path/param-django-uuid-typestr/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a", |
| 347 |
200, |
| 348 |
"31ea378c-c052-4b4c-bf0b-679ce5cfcc2a", |
| 349 |
), |
| 350 |
("/path/param-django-path/some/path/things/after", 200, "some/path/things"), |
| 351 |
("/path/param-django-path/less/path/after", 200, "less/path"), |
| 352 |
("/path/param-django-path/plugh/after", 200, "plugh"), |
| 353 |
("/path/param-django-path//after", "Cannot resolve", Exception), |
| 354 |
("/path/param-django-custom-int/42", 200, 24), |
| 355 |
("/path/param-django-custom-int/x42", "Cannot resolve", Exception), |
| 356 |
("/path/param-django-custom-float/42", 200, 0.24), |
| 357 |
("/path/param-django-custom-float/x42", "Cannot resolve", Exception), |
| 358 |
], |
| 359 |
) |
| 360 |
def test_get_path_django(path, expected_status, expected_response): |
| 361 |
if expected_response is Exception: |
| 362 |
with pytest.raises(Exception, match=expected_status): |
| 363 |
client.get(path) |
| 364 |
else: |
| 365 |
response = client.get(path) |
| 366 |
print(response.json()) |
| 367 |
assert response.status_code == expected_status |
| 368 |
assert response.json() == expected_response |
| 369 |
|
| 370 |
|
| 371 |
def test_path_signature_asserts_default(): |
| 372 |
test_router = Router() |
| 373 |
|
| 374 |
match = "'item_id' is a path param, default not allowed" |
| 375 |
with pytest.raises(AssertionError, match=match): |
| 376 |
|
| 377 |
@test_router.get("/path/{item_id}") |
| 378 |
def get_path_item_id(request, item_id="1"): |
| 379 |
pass |
| 380 |
|
| 381 |
|
| 382 |
def test_path_signature_warns_missing(): |
| 383 |
test_router = Router() |
| 384 |
|
| 385 |
match = ( |
| 386 |
r"Field\(s\) \('a_path_param', 'another_path_param'\) are in " |
| 387 |
r"the view path, but were not found in the view signature." |
| 388 |
) |
| 389 |
with pytest.warns(UserWarning, match=match): |
| 390 |
|
| 391 |
@test_router.get("/path/{a_path_param}/{another_path_param}") |
| 392 |
def get_path_item_id(request): |
| 393 |
pass |