File: test_path.py

package info (click to toggle)
django-ninja 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,280 kB
  • sloc: python: 15,956; javascript: 1,689; makefile: 39; sh: 25
file content (393 lines) | stat: -rw-r--r-- 12,629 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from sys import version_info

import pytest
from main import router

from ninja import Router
from ninja.testing import TestClient

client = TestClient(router)


def test_text_get():
    response = client.get("/text")
    assert response.status_code == 200, response.text
    assert response.json() == "Hello World"


response_not_valid_bool = {
    "detail": [
        {
            "type": "bool_parsing",
            "loc": ["path", "item_id"],
            "msg": "Input should be a valid boolean, unable to interpret input",
        }
    ]
}

response_not_valid_int = {
    "detail": [
        {
            "type": "int_parsing",
            "loc": ["path", "item_id"],
            "msg": "Input should be a valid integer, unable to parse string as an integer",
        }
    ]
}

response_not_valid_custom = {
    "detail": [
        {
            "ctx": {"error": "Input should pass this custom validator"},
            "loc": ["path", "item_id"],
            "msg": "Value error, Input should pass this custom validator",
            "type": "value_error",
        }
    ]
}

response_not_valid_int_float = {
    "detail": [
        {
            "type": "int_parsing",
            "loc": ["path", "item_id"],
            "msg": "Input should be a valid integer, unable to parse string as an integer",
        }
    ]
}

response_not_valid_float = {
    "detail": [
        {
            "type": "float_parsing",
            "loc": ["path", "item_id"],
            "msg": "Input should be a valid number, unable to parse string as a number",
        }
    ]
}

response_at_least_3 = {
    "detail": [
        {
            "type": "string_too_short",
            "loc": ["path", "item_id"],
            "msg": "String should have at least 3 characters",
            "ctx": {"min_length": 3},
        }
    ]
}


response_at_least_2 = {
    "detail": [
        {
            "type": "string_too_short",
            "loc": ["path", "item_id"],
            "msg": "String should have at least 2 characters",
            "ctx": {"min_length": 2},
        }
    ]
}


response_maximum_3 = {
    "detail": [
        {
            "type": "string_too_long",
            "loc": ["path", "item_id"],
            "msg": "String should have at most 3 characters",
            "ctx": {"max_length": 3},
        }
    ]
}


response_greater_than_3 = {
    "detail": [
        {
            "type": "greater_than",
            "loc": ["path", "item_id"],
            "msg": "Input should be greater than 3",
            "ctx": {"gt": 3.0},
        }
    ]
}


response_greater_than_0 = {
    "detail": [
        {
            "type": "greater_than",
            "loc": ["path", "item_id"],
            "msg": "Input should be greater than 0",
            "ctx": {"gt": 0.0},
        }
    ]
}


response_greater_than_1 = {
    "detail": [
        {
            "type": "greater_than",
            "loc": ["path", "item_id"],
            "msg": "Input should be greater than 1",
            "ctx": {"gt": 1},
        }
    ]
}


response_greater_than_equal_3 = {
    "detail": [
        {
            "type": "greater_than_equal",
            "loc": ["path", "item_id"],
            "msg": "Input should be greater than or equal to 3",
            "ctx": {"ge": 3.0},
        }
    ]
}


response_less_than_3 = {
    "detail": [
        {
            "type": "less_than",
            "loc": ["path", "item_id"],
            "msg": "Input should be less than 3",
            "ctx": {"lt": 3.0},
        }
    ]
}


response_less_than_0 = {
    "detail": [
        {
            "type": "less_than",
            "loc": ["path", "item_id"],
            "msg": "Input should be less than 0",
            "ctx": {"lt": 0.0},
        }
    ]
}

response_less_than_equal_3 = {
    "detail": [
        {
            "type": "less_than_equal",
            "loc": ["path", "item_id"],
            "msg": "Input should be less than or equal to 3",
            "ctx": {"le": 3.0},
        }
    ]
}


response_not_valid_pattern = {
    "detail": [
        {
            "ctx": {
                "pattern": "^foo",
            },
            "loc": ["path", "item_id"],
            "msg": "String should match pattern '^foo'",
            "type": "string_pattern_mismatch",
        }
    ]
}


@pytest.mark.parametrize(
    "path,expected_status,expected_response",
    [
        ("/path/foobar", 200, "foobar"),
        ("/path/str/foobar", 200, "foobar"),
        ("/path/str/42", 200, "42"),
        ("/path/str/True", 200, "True"),
        ("/path/int/foobar", 422, response_not_valid_int),
        ("/path/int/True", 422, response_not_valid_int),
        ("/path/int/42", 200, 42),
        ("/path/int/42.5", 422, response_not_valid_int_float),
        ("/path/float/foobar", 422, response_not_valid_float),
        ("/path/float/True", 422, response_not_valid_float),
        ("/path/float/42", 200, 42),
        ("/path/float/42.5", 200, 42.5),
        ("/path/bool/foobar", 422, response_not_valid_bool),
        ("/path/bool/True", 200, True),
        ("/path/bool/42", 422, response_not_valid_bool),
        ("/path/bool/42.5", 422, response_not_valid_bool),
        ("/path/bool/1", 200, True),
        ("/path/bool/0", 200, False),
        ("/path/bool/true", 200, True),
        ("/path/bool/False", 200, False),
        ("/path/bool/false", 200, False),
        ("/path/param/foo", 200, "foo"),
        ("/path/param-required/foo", 200, "foo"),
        ("/path/param-minlength/foo", 200, "foo"),
        ("/path/param-minlength/fo", 422, response_at_least_3),
        ("/path/param-maxlength/foo", 200, "foo"),
        ("/path/param-maxlength/foobar", 422, response_maximum_3),
        ("/path/param-min_maxlength/foo", 200, "foo"),
        ("/path/param-min_maxlength/foobar", 422, response_maximum_3),
        ("/path/param-min_maxlength/f", 422, response_at_least_2),
        ("/path/param-gt/42", 200, 42),
        ("/path/param-gt/2", 422, response_greater_than_3),
        ("/path/param-gt0/0.05", 200, 0.05),
        ("/path/param-gt0/0", 422, response_greater_than_0),
        ("/path/param-ge/42", 200, 42),
        ("/path/param-ge/3", 200, 3),
        ("/path/param-ge/2", 422, response_greater_than_equal_3),
        ("/path/param-lt/42", 422, response_less_than_3),
        ("/path/param-lt/2", 200, 2),
        ("/path/param-lt0/-1", 200, -1),
        ("/path/param-lt0/0", 422, response_less_than_0),
        ("/path/param-le/42", 422, response_less_than_equal_3),
        ("/path/param-le/3", 200, 3),
        ("/path/param-le/2", 200, 2),
        ("/path/param-lt-gt/2", 200, 2),
        ("/path/param-lt-gt/4", 422, response_less_than_3),
        ("/path/param-lt-gt/0", 422, response_greater_than_1),
        ("/path/param-le-ge/2", 200, 2),
        ("/path/param-le-ge/1", 200, 1),
        ("/path/param-le-ge/3", 200, 3),
        ("/path/param-le-ge/4", 422, response_less_than_equal_3),
        ("/path/param-lt-int/2", 200, 2),
        ("/path/param-lt-int/42", 422, response_less_than_3),
        ("/path/param-lt-int/2.7", 422, response_not_valid_int_float),
        ("/path/param-gt-int/42", 200, 42),
        ("/path/param-gt-int/2", 422, response_greater_than_3),
        ("/path/param-gt-int/2.7", 422, response_not_valid_int_float),
        ("/path/param-le-int/42", 422, response_less_than_equal_3),
        ("/path/param-le-int/3", 200, 3),
        ("/path/param-le-int/2", 200, 2),
        ("/path/param-le-int/2.7", 422, response_not_valid_int_float),
        ("/path/param-ge-int/42", 200, 42),
        ("/path/param-ge-int/3", 200, 3),
        ("/path/param-ge-int/2", 422, response_greater_than_equal_3),
        ("/path/param-ge-int/2.7", 422, response_not_valid_int_float),
        ("/path/param-lt-gt-int/2", 200, 2),
        ("/path/param-lt-gt-int/4", 422, response_less_than_3),
        ("/path/param-lt-gt-int/0", 422, response_greater_than_1),
        ("/path/param-lt-gt-int/2.7", 422, response_not_valid_int_float),
        ("/path/param-le-ge-int/2", 200, 2),
        ("/path/param-le-ge-int/1", 200, 1),
        ("/path/param-le-ge-int/3", 200, 3),
        ("/path/param-le-ge-int/4", 422, response_less_than_equal_3),
        ("/path/param-le-ge-int/2.7", 422, response_not_valid_int_float),
        ("/path/param-pattern/foo", 200, "foo"),
        ("/path/param-pattern/fo", 422, response_not_valid_pattern),
    ],
)
def test_get_path(path, expected_status, expected_response):
    response = client.get(path)
    print(path, response.json())
    assert response.status_code == expected_status
    assert response.json() == expected_response


@pytest.mark.skipif(
    version_info < (3, 9),
    reason="requires py3.9+ for Annotated[] at the route definition site",
)
@pytest.mark.parametrize(
    "path,expected_status,expected_response",
    [
        ("/path/param_ex/True", 422, response_not_valid_int),
        ("/path/param_ex/0", 422, response_not_valid_custom),
        ("/path/param_ex/42", 200, 42),
    ],
)
def test_get_pathex(path, expected_status, expected_response):
    response = client.get(path)
    print(path, response.json())
    assert response.status_code == expected_status
    assert response.json() == expected_response


@pytest.mark.parametrize(
    "path,expected_status,expected_response",
    [
        ("/path/param-django-str/42", 200, "42"),
        ("/path/param-django-str/-1", 200, "-1"),
        ("/path/param-django-str/foobar", 200, "foobar"),
        ("/path/param-django-int/0", 200, 0),
        ("/path/param-django-int/42", 200, 42),
        ("/path/param-django-int/42.5", "Cannot resolve", Exception),
        ("/path/param-django-int/-1", "Cannot resolve", Exception),
        ("/path/param-django-int/True", "Cannot resolve", Exception),
        ("/path/param-django-int/foobar", "Cannot resolve", Exception),
        ("/path/param-django-int/not-an-int", 200, "Found not-an-int"),
        # ("/path/param-django-int-str/42", 200, "42"), # https://github.com/pydantic/pydantic/issues/5993
        ("/path/param-django-int-str/42.5", "Cannot resolve", Exception),
        (
            "/path/param-django-slug/django-ninja-is-the-best",
            200,
            "django-ninja-is-the-best",
        ),
        ("/path/param-django-slug/42.5", "Cannot resolve", Exception),
        (
            "/path/param-django-uuid/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
            200,
            "31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
        ),
        (
            "/path/param-django-uuid/31ea378c-c052-4b4c-bf0b-679ce5cfcc2",  # invalid UUID (missing last digit)
            "Cannot resolve",
            Exception,
        ),
        (
            "/path/param-django-uuid-notype/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
            200,
            "31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
        ),
        (
            "/path/param-django-uuid-typestr/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
            200,
            "31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
        ),
        ("/path/param-django-path/some/path/things/after", 200, "some/path/things"),
        ("/path/param-django-path/less/path/after", 200, "less/path"),
        ("/path/param-django-path/plugh/after", 200, "plugh"),
        ("/path/param-django-path//after", "Cannot resolve", Exception),
        ("/path/param-django-custom-int/42", 200, 24),
        ("/path/param-django-custom-int/x42", "Cannot resolve", Exception),
        ("/path/param-django-custom-float/42", 200, 0.24),
        ("/path/param-django-custom-float/x42", "Cannot resolve", Exception),
    ],
)
def test_get_path_django(path, expected_status, expected_response):
    if expected_response is Exception:
        with pytest.raises(Exception, match=expected_status):
            client.get(path)
    else:
        response = client.get(path)
        print(response.json())
        assert response.status_code == expected_status
        assert response.json() == expected_response


def test_path_signature_asserts_default():
    test_router = Router()

    match = "'item_id' is a path param, default not allowed"
    with pytest.raises(AssertionError, match=match):

        @test_router.get("/path/{item_id}")
        def get_path_item_id(request, item_id="1"):
            pass


def test_path_signature_warns_missing():
    test_router = Router()

    match = (
        r"Field\(s\) \('a_path_param', 'another_path_param'\) are in "
        r"the view path, but were not found in the view signature."
    )
    with pytest.warns(UserWarning, match=match):

        @test_router.get("/path/{a_path_param}/{another_path_param}")
        def get_path_item_id(request):
            pass