Debian Codegraph @

tests/main.py

raw symbols SCIP
1 from sys import version_info
2 from typing import List, Optional
3 from uuid import UUID
4
5 import pydantic
6 import pytest
7 from django.urls import register_converter
8 from typing_extensions import Annotated
9
10 from ninja import Field, P, Path, PathEx, Query, Router, Schema
11
12 router = Router()
13
14
15 @router.get("/text")
16 def get_text(request):
17 return "Hello World"
18
19
20 @router.get("/path/{item_id}")
21 def get_id(request, item_id):
22 return item_id
23
24
25 @router.get("/path/str/{item_id}")
26 def get_str_id(request, item_id: str):
27 return item_id
28
29
30 @router.get("/path/int/{item_id}")
31 def get_int_id(request, item_id: int):
32 return item_id
33
34
35 @router.get("/path/float/{item_id}")
36 def get_float_id(request, item_id: float):
37 return item_id
38
39
40 @router.get("/path/bool/{item_id}")
41 def get_bool_id(request, item_id: bool):
42 return item_id
43
44
45 def custom_validator(value: int) -> int:
46 if value != 42:
47 raise ValueError("Input should pass this custom validator")
48 return value
49
50
51 CustomValidatedInt = Annotated[
52 int,
53 pydantic.AfterValidator(custom_validator),
54 pydantic.WithJsonSchema({
55 "type": "int",
56 "example": "42",
57 }),
58 ]
59
60 # TODO: Remove this condition once support for <= 3.8 is dropped
61 if version_info >= (3, 9):
62
63 @router.get("/path/param_ex/{item_id}")
64 def get_path_param_ex_id(
65 request,
66 item_id: PathEx[CustomValidatedInt, P(description="path_ex description")],
67 ):
68 return item_id
69
70 else:
71
72 def test_annotated_path_ex_unsupported():
73 with pytest.raises(NotImplementedError, match="3.9+"):
74
75 @router.get("/path/param_ex/{item_id}")
76 def get_path_param_ex_id(
77 request,
78 item_id: PathEx[
79 CustomValidatedInt, P(description="path_ex description")
80 ],
81 ):
82 return item_id
83
84
85 @router.get("/path/param/{item_id}")
86 def get_path_param_id(request, item_id: str = Path(None)):
87 return item_id
88
89
90 @router.get("/path/param-required/{item_id}")
91 def get_path_param_required_id(request, item_id: str = Path(...)):
92 return item_id
93
94
95 @router.get("/path/param-minlength/{item_id}")
96 def get_path_param_min_length(request, item_id: str = Path(..., min_length=3)):
97 return item_id
98
99
100 @router.get("/path/param-maxlength/{item_id}")
101 def get_path_param_max_length(request, item_id: str = Path(..., max_length=3)):
102 return item_id
103
104
105 @router.get("/path/param-min_maxlength/{item_id}")
106 def get_path_param_min_max_length(
107 request, item_id: str = Path(..., max_length=3, min_length=2)
108 ):
109 return item_id
110
111
112 @router.get("/path/param-gt/{item_id}")
113 def get_path_param_gt(request, item_id: float = Path(..., gt=3)):
114 return item_id
115
116
117 @router.get("/path/param-gt0/{item_id}")
118 def get_path_param_gt0(request, item_id: float = Path(..., gt=0)):
119 return item_id
120
121
122 @router.get("/path/param-ge/{item_id}")
123 def get_path_param_ge(request, item_id: float = Path(..., ge=3)):
124 return item_id
125
126
127 @router.get("/path/param-lt/{item_id}")
128 def get_path_param_lt(request, item_id: float = Path(..., lt=3)):
129 return item_id
130
131
132 @router.get("/path/param-lt0/{item_id}")
133 def get_path_param_lt0(request, item_id: float = Path(..., lt=0)):
134 return item_id
135
136
137 @router.get("/path/param-le/{item_id}")
138 def get_path_param_le(request, item_id: float = Path(..., le=3)):
139 return item_id
140
141
142 @router.get("/path/param-lt-gt/{item_id}")
143 def get_path_param_lt_gt(request, item_id: float = Path(..., lt=3, gt=1)):
144 return item_id
145
146
147 @router.get("/path/param-le-ge/{item_id}")
148 def get_path_param_le_ge(request, item_id: float = Path(..., le=3, ge=1)):
149 return item_id
150
151
152 @router.get("/path/param-lt-int/{item_id}")
153 def get_path_param_lt_int(request, item_id: int = Path(..., lt=3)):
154 return item_id
155
156
157 @router.get("/path/param-gt-int/{item_id}")
158 def get_path_param_gt_int(request, item_id: int = Path(..., gt=3)):
159 return item_id
160
161
162 @router.get("/path/param-le-int/{item_id}")
163 def get_path_param_le_int(request, item_id: int = Path(..., le=3)):
164 return item_id
165
166
167 @router.get("/path/param-ge-int/{item_id}")
168 def get_path_param_ge_int(request, item_id: int = Path(..., ge=3)):
169 return item_id
170
171
172 @router.get("/path/param-lt-gt-int/{item_id}")
173 def get_path_param_lt_gt_int(request, item_id: int = Path(..., lt=3, gt=1)):
174 return item_id
175
176
177 @router.get("/path/param-le-ge-int/{item_id}")
178 def get_path_param_le_ge_int(request, item_id: int = Path(..., le=3, ge=1)):
179 return item_id
180
181
182 @router.get("/path/param-pattern/{item_id}")
183 def get_path_param_pattern(request, item_id: str = Path(..., pattern="^foo")):
184 return item_id
185
186
187 @router.get("/path/param-django-str/{str:item_id}")
188 def get_path_param_django_str(request, item_id):
189 return item_id
190
191
192 @router.get("/path/param-django-int/{int:item_id}")
193 def get_path_param_django_int(request, item_id: int):
194 assert isinstance(item_id, int)
195 return item_id
196
197
198 @router.get("/path/param-django-int/not-an-int")
199 def get_path_param_django_not_an_int(request):
200 """Verify that url resolution for get_path_param_django_int passes non-ints forward"""
201 return "Found not-an-int"
202
203
204 @router.get("/path/param-django-int-str/{int:item_id}")
205 def get_path_param_django_int_str(request, item_id: str):
206 assert isinstance(item_id, str)
207 return item_id
208
209
210 @router.get("/path/param-django-slug/{slug:item_id}")
211 def get_path_param_django_slug(request, item_id):
212 return item_id
213
214
215 @router.get("/path/param-django-uuid/{uuid:item_id}")
216 def get_path_param_django_uuid(request, item_id: UUID):
217 assert isinstance(item_id, UUID)
218 return item_id
219
220
221 @router.get("/path/param-django-uuid-notype/{uuid:item_id}")
222 def get_path_param_django_uuid_notype(request, item_id):
223 # no type annotation defaults to str..............^
224 assert isinstance(item_id, str)
225 return item_id
226
227
228 @router.get("/path/param-django-uuid-typestr/{uuid:item_id}")
229 def get_path_param_django_uuid_typestr(request, item_id: str):
230 assert isinstance(item_id, str)
231 return item_id
232
233
234 @router.get("/path/param-django-path/{path:item_id}/after")
235 def get_path_param_django_path(request, item_id):
236 return item_id
237
238
239 @router.get("/query")
240 def get_query(request, query):
241 return f"foo bar {query}"
242
243
244 @router.get("/query/optional")
245 def get_query_optional(request, query=None):
246 if query is None:
247 return "foo bar"
248 return f"foo bar {query}"
249
250
251 @router.get("/query/int")
252 def get_query_type(request, query: int):
253 return f"foo bar {query}"
254
255
256 @router.get("/query/int/optional")
257 def get_query_type_optional(request, query: int = None):
258 if query is None:
259 return "foo bar"
260 return f"foo bar {query}"
261
262
263 @router.get("/query/str/optional")
264 def get_query_str_optional(request, query: str = None):
265 """Test for issue #1607 - str type with None default should be optional."""
266 if query is None:
267 return "foo bar"
268 return f"foo bar {query}"
269
270
271 @router.get("/query/int/default")
272 def get_query_type_optional_10(request, query: int = 10):
273 return f"foo bar {query}"
274
275
276 @router.get("/query/list")
277 def get_query_list(request, query: List[str] = Query(...)):
278 return ",".join(query)
279
280
281 @router.get("/query/list-optional")
282 def get_query_optional_list(request, query: Optional[List[str]] = Query(None)):
283 if query:
284 return ",".join(query)
285 return query
286
287
288 @router.get("/query/param")
289 def get_query_param(request, query=Query(None)):
290 if query is None:
291 return "foo bar"
292 return f"foo bar {query}"
293
294
295 @router.get("/query/param-required")
296 def get_query_param_required(request, query=Query(...)):
297 return f"foo bar {query}"
298
299
300 @router.get("/query/param-required/int")
301 def get_query_param_required_type(request, query: int = Query(...)):
302 return f"foo bar {query}"
303
304
305 class AliasedSchema(Schema):
306 query: str = Field(..., alias="aliased.-_~name")
307
308
309 @router.get("/query/aliased-name")
310 def get_query_aliased_name(request, query: AliasedSchema = Query(...)):
311 return f"foo bar {query.query}"
312
313
314 class CustomPathConverter1:
315 regex = "[0-9]+"
316
317 def to_python(self, value) -> "int":
318 """reverse the string and convert to int"""
319 return int(value[::-1])
320
321 def to_url(self, value):
322 return str(value)
323
324
325 class CustomPathConverter2:
326 regex = "[0-9]+"
327
328 def to_python(self, value):
329 """reverse the string and convert to float like"""
330 return f"0.{value[::-1]}"
331
332 def to_url(self, value):
333 return str(value)
334
335
336 register_converter(CustomPathConverter1, "custom-int")
337 register_converter(CustomPathConverter2, "custom-float")
338
339
340 @router.get("/path/param-django-custom-int/{custom-int:item_id}")
341 def get_path_param_django_custom_int(request, item_id: int):
342 return item_id
343
344
345 @router.get("/path/param-django-custom-float/{custom-float:item_id}")
346 def get_path_param_django_custom_float(request, item_id: float):
347 return item_id