Coverage for tests/test_dependency_overrides.py: 100%
156 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Optional 1abcde
3import pytest 1abcde
4from dirty_equals import IsDict 1abcde
5from fastapi import APIRouter, Depends, FastAPI 1abcde
6from fastapi.testclient import TestClient 1abcde
8app = FastAPI() 1abcde
10router = APIRouter() 1abcde
13async def common_parameters(q: str, skip: int = 0, limit: int = 100): 1abcde
14 return {"q": q, "skip": skip, "limit": limit} 1abcde
17@app.get("/main-depends/") 1abcde
18async def main_depends(commons: dict = Depends(common_parameters)): 1abcde
19 return {"in": "main-depends", "params": commons} 1abcde
22@app.get("/decorator-depends/", dependencies=[Depends(common_parameters)]) 1abcde
23async def decorator_depends(): 1abcde
24 return {"in": "decorator-depends"} 1abcde
27@router.get("/router-depends/") 1abcde
28async def router_depends(commons: dict = Depends(common_parameters)): 1abcde
29 return {"in": "router-depends", "params": commons} 1abcde
32@router.get("/router-decorator-depends/", dependencies=[Depends(common_parameters)]) 1abcde
33async def router_decorator_depends(): 1abcde
34 return {"in": "router-decorator-depends"} 1abcde
37app.include_router(router) 1abcde
39client = TestClient(app) 1abcde
42async def overrider_dependency_simple(q: Optional[str] = None): 1abcde
43 return {"q": q, "skip": 5, "limit": 10} 1abcde
46async def overrider_sub_dependency(k: str): 1abcde
47 return {"k": k} 1abcde
50async def overrider_dependency_with_sub(msg: dict = Depends(overrider_sub_dependency)): 1abcde
51 return msg 1abcde
54def test_main_depends(): 1abcde
55 response = client.get("/main-depends/") 1abcde
56 assert response.status_code == 422 1abcde
57 assert response.json() == IsDict( 1abcde
58 {
59 "detail": [
60 {
61 "type": "missing",
62 "loc": ["query", "q"],
63 "msg": "Field required",
64 "input": None,
65 }
66 ]
67 }
68 ) | IsDict(
69 # TODO: remove when deprecating Pydantic v1
70 {
71 "detail": [
72 {
73 "loc": ["query", "q"],
74 "msg": "field required",
75 "type": "value_error.missing",
76 }
77 ]
78 }
79 )
82def test_main_depends_q_foo(): 1abcde
83 response = client.get("/main-depends/?q=foo") 1abcde
84 assert response.status_code == 200 1abcde
85 assert response.json() == { 1abcde
86 "in": "main-depends",
87 "params": {"q": "foo", "skip": 0, "limit": 100},
88 }
91def test_main_depends_q_foo_skip_100_limit_200(): 1abcde
92 response = client.get("/main-depends/?q=foo&skip=100&limit=200") 1abcde
93 assert response.status_code == 200 1abcde
94 assert response.json() == { 1abcde
95 "in": "main-depends",
96 "params": {"q": "foo", "skip": 100, "limit": 200},
97 }
100def test_decorator_depends(): 1abcde
101 response = client.get("/decorator-depends/") 1abcde
102 assert response.status_code == 422 1abcde
103 assert response.json() == IsDict( 1abcde
104 {
105 "detail": [
106 {
107 "type": "missing",
108 "loc": ["query", "q"],
109 "msg": "Field required",
110 "input": None,
111 }
112 ]
113 }
114 ) | IsDict(
115 # TODO: remove when deprecating Pydantic v1
116 {
117 "detail": [
118 {
119 "loc": ["query", "q"],
120 "msg": "field required",
121 "type": "value_error.missing",
122 }
123 ]
124 }
125 )
128def test_decorator_depends_q_foo(): 1abcde
129 response = client.get("/decorator-depends/?q=foo") 1abcde
130 assert response.status_code == 200 1abcde
131 assert response.json() == {"in": "decorator-depends"} 1abcde
134def test_decorator_depends_q_foo_skip_100_limit_200(): 1abcde
135 response = client.get("/decorator-depends/?q=foo&skip=100&limit=200") 1abcde
136 assert response.status_code == 200 1abcde
137 assert response.json() == {"in": "decorator-depends"} 1abcde
140def test_router_depends(): 1abcde
141 response = client.get("/router-depends/") 1abcde
142 assert response.status_code == 422 1abcde
143 assert response.json() == IsDict( 1abcde
144 {
145 "detail": [
146 {
147 "type": "missing",
148 "loc": ["query", "q"],
149 "msg": "Field required",
150 "input": None,
151 }
152 ]
153 }
154 ) | IsDict(
155 # TODO: remove when deprecating Pydantic v1
156 {
157 "detail": [
158 {
159 "loc": ["query", "q"],
160 "msg": "field required",
161 "type": "value_error.missing",
162 }
163 ]
164 }
165 )
168def test_router_depends_q_foo(): 1abcde
169 response = client.get("/router-depends/?q=foo") 1abcde
170 assert response.status_code == 200 1abcde
171 assert response.json() == { 1abcde
172 "in": "router-depends",
173 "params": {"q": "foo", "skip": 0, "limit": 100},
174 }
177def test_router_depends_q_foo_skip_100_limit_200(): 1abcde
178 response = client.get("/router-depends/?q=foo&skip=100&limit=200") 1abcde
179 assert response.status_code == 200 1abcde
180 assert response.json() == { 1abcde
181 "in": "router-depends",
182 "params": {"q": "foo", "skip": 100, "limit": 200},
183 }
186def test_router_decorator_depends(): 1abcde
187 response = client.get("/router-decorator-depends/") 1abcde
188 assert response.status_code == 422 1abcde
189 assert response.json() == IsDict( 1abcde
190 {
191 "detail": [
192 {
193 "type": "missing",
194 "loc": ["query", "q"],
195 "msg": "Field required",
196 "input": None,
197 }
198 ]
199 }
200 ) | IsDict(
201 # TODO remove when deprecating Pydantic v1
202 {
203 "detail": [
204 {
205 "loc": ["query", "q"],
206 "msg": "field required",
207 "type": "value_error.missing",
208 }
209 ]
210 }
211 )
214def test_router_decorator_depends_q_foo(): 1abcde
215 response = client.get("/router-decorator-depends/?q=foo") 1abcde
216 assert response.status_code == 200 1abcde
217 assert response.json() == {"in": "router-decorator-depends"} 1abcde
220def test_router_decorator_depends_q_foo_skip_100_limit_200(): 1abcde
221 response = client.get("/router-decorator-depends/?q=foo&skip=100&limit=200") 1abcde
222 assert response.status_code == 200 1abcde
223 assert response.json() == {"in": "router-decorator-depends"} 1abcde
226@pytest.mark.parametrize( 1abcde
227 "url,status_code,expected",
228 [
229 (
230 "/main-depends/",
231 200,
232 {"in": "main-depends", "params": {"q": None, "skip": 5, "limit": 10}},
233 ),
234 (
235 "/main-depends/?q=foo",
236 200,
237 {"in": "main-depends", "params": {"q": "foo", "skip": 5, "limit": 10}},
238 ),
239 (
240 "/main-depends/?q=foo&skip=100&limit=200",
241 200,
242 {"in": "main-depends", "params": {"q": "foo", "skip": 5, "limit": 10}},
243 ),
244 ("/decorator-depends/", 200, {"in": "decorator-depends"}),
245 (
246 "/router-depends/",
247 200,
248 {"in": "router-depends", "params": {"q": None, "skip": 5, "limit": 10}},
249 ),
250 (
251 "/router-depends/?q=foo",
252 200,
253 {"in": "router-depends", "params": {"q": "foo", "skip": 5, "limit": 10}},
254 ),
255 (
256 "/router-depends/?q=foo&skip=100&limit=200",
257 200,
258 {"in": "router-depends", "params": {"q": "foo", "skip": 5, "limit": 10}},
259 ),
260 ("/router-decorator-depends/", 200, {"in": "router-decorator-depends"}),
261 ],
262)
263def test_override_simple(url, status_code, expected): 1abcde
264 app.dependency_overrides[common_parameters] = overrider_dependency_simple 1abcde
265 response = client.get(url) 1abcde
266 assert response.status_code == status_code 1abcde
267 assert response.json() == expected 1abcde
268 app.dependency_overrides = {} 1abcde
271def test_override_with_sub_main_depends(): 1abcde
272 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
273 response = client.get("/main-depends/") 1abcde
274 assert response.status_code == 422 1abcde
275 assert response.json() == IsDict( 1abcde
276 {
277 "detail": [
278 {
279 "type": "missing",
280 "loc": ["query", "k"],
281 "msg": "Field required",
282 "input": None,
283 }
284 ]
285 }
286 ) | IsDict(
287 # TODO: remove when deprecating Pydantic v1
288 {
289 "detail": [
290 {
291 "loc": ["query", "k"],
292 "msg": "field required",
293 "type": "value_error.missing",
294 }
295 ]
296 }
297 )
298 app.dependency_overrides = {} 1abcde
301def test_override_with_sub__main_depends_q_foo(): 1abcde
302 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
303 response = client.get("/main-depends/?q=foo") 1abcde
304 assert response.status_code == 422 1abcde
305 assert response.json() == IsDict( 1abcde
306 {
307 "detail": [
308 {
309 "type": "missing",
310 "loc": ["query", "k"],
311 "msg": "Field required",
312 "input": None,
313 }
314 ]
315 }
316 ) | IsDict(
317 # TODO: remove when deprecating Pydantic v1
318 {
319 "detail": [
320 {
321 "loc": ["query", "k"],
322 "msg": "field required",
323 "type": "value_error.missing",
324 }
325 ]
326 }
327 )
328 app.dependency_overrides = {} 1abcde
331def test_override_with_sub_main_depends_k_bar(): 1abcde
332 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
333 response = client.get("/main-depends/?k=bar") 1abcde
334 assert response.status_code == 200 1abcde
335 assert response.json() == {"in": "main-depends", "params": {"k": "bar"}} 1abcde
336 app.dependency_overrides = {} 1abcde
339def test_override_with_sub_decorator_depends(): 1abcde
340 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
341 response = client.get("/decorator-depends/") 1abcde
342 assert response.status_code == 422 1abcde
343 assert response.json() == IsDict( 1abcde
344 {
345 "detail": [
346 {
347 "type": "missing",
348 "loc": ["query", "k"],
349 "msg": "Field required",
350 "input": None,
351 }
352 ]
353 }
354 ) | IsDict(
355 # TODO: remove when deprecating Pydantic v1
356 {
357 "detail": [
358 {
359 "loc": ["query", "k"],
360 "msg": "field required",
361 "type": "value_error.missing",
362 }
363 ]
364 }
365 )
366 app.dependency_overrides = {} 1abcde
369def test_override_with_sub_decorator_depends_q_foo(): 1abcde
370 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
371 response = client.get("/decorator-depends/?q=foo") 1abcde
372 assert response.status_code == 422 1abcde
373 assert response.json() == IsDict( 1abcde
374 {
375 "detail": [
376 {
377 "type": "missing",
378 "loc": ["query", "k"],
379 "msg": "Field required",
380 "input": None,
381 }
382 ]
383 }
384 ) | IsDict(
385 # TODO: remove when deprecating Pydantic v1
386 {
387 "detail": [
388 {
389 "loc": ["query", "k"],
390 "msg": "field required",
391 "type": "value_error.missing",
392 }
393 ]
394 }
395 )
396 app.dependency_overrides = {} 1abcde
399def test_override_with_sub_decorator_depends_k_bar(): 1abcde
400 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
401 response = client.get("/decorator-depends/?k=bar") 1abcde
402 assert response.status_code == 200 1abcde
403 assert response.json() == {"in": "decorator-depends"} 1abcde
404 app.dependency_overrides = {} 1abcde
407def test_override_with_sub_router_depends(): 1abcde
408 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
409 response = client.get("/router-depends/") 1abcde
410 assert response.status_code == 422 1abcde
411 assert response.json() == IsDict( 1abcde
412 {
413 "detail": [
414 {
415 "type": "missing",
416 "loc": ["query", "k"],
417 "msg": "Field required",
418 "input": None,
419 }
420 ]
421 }
422 ) | IsDict(
423 # TODO remove when deprecating Pydantic v1
424 {
425 "detail": [
426 {
427 "loc": ["query", "k"],
428 "msg": "field required",
429 "type": "value_error.missing",
430 }
431 ]
432 }
433 )
434 app.dependency_overrides = {} 1abcde
437def test_override_with_sub_router_depends_q_foo(): 1abcde
438 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
439 response = client.get("/router-depends/?q=foo") 1abcde
440 assert response.status_code == 422 1abcde
441 assert response.json() == IsDict( 1abcde
442 {
443 "detail": [
444 {
445 "type": "missing",
446 "loc": ["query", "k"],
447 "msg": "Field required",
448 "input": None,
449 }
450 ]
451 }
452 ) | IsDict(
453 # TODO remove when deprecating Pydantic v1
454 {
455 "detail": [
456 {
457 "loc": ["query", "k"],
458 "msg": "field required",
459 "type": "value_error.missing",
460 }
461 ]
462 }
463 )
464 app.dependency_overrides = {} 1abcde
467def test_override_with_sub_router_depends_k_bar(): 1abcde
468 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
469 response = client.get("/router-depends/?k=bar") 1abcde
470 assert response.status_code == 200 1abcde
471 assert response.json() == {"in": "router-depends", "params": {"k": "bar"}} 1abcde
472 app.dependency_overrides = {} 1abcde
475def test_override_with_sub_router_decorator_depends(): 1abcde
476 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
477 response = client.get("/router-decorator-depends/") 1abcde
478 assert response.status_code == 422 1abcde
479 assert response.json() == IsDict( 1abcde
480 {
481 "detail": [
482 {
483 "type": "missing",
484 "loc": ["query", "k"],
485 "msg": "Field required",
486 "input": None,
487 }
488 ]
489 }
490 ) | IsDict(
491 # TODO remove when deprecating Pydantic v1
492 {
493 "detail": [
494 {
495 "loc": ["query", "k"],
496 "msg": "field required",
497 "type": "value_error.missing",
498 }
499 ]
500 }
501 )
502 app.dependency_overrides = {} 1abcde
505def test_override_with_sub_router_decorator_depends_q_foo(): 1abcde
506 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
507 response = client.get("/router-decorator-depends/?q=foo") 1abcde
508 assert response.status_code == 422 1abcde
509 assert response.json() == IsDict( 1abcde
510 {
511 "detail": [
512 {
513 "type": "missing",
514 "loc": ["query", "k"],
515 "msg": "Field required",
516 "input": None,
517 }
518 ]
519 }
520 ) | IsDict(
521 # TODO remove when deprecating Pydantic v1
522 {
523 "detail": [
524 {
525 "loc": ["query", "k"],
526 "msg": "field required",
527 "type": "value_error.missing",
528 }
529 ]
530 }
531 )
532 app.dependency_overrides = {} 1abcde
535def test_override_with_sub_router_decorator_depends_k_bar(): 1abcde
536 app.dependency_overrides[common_parameters] = overrider_dependency_with_sub 1abcde
537 response = client.get("/router-decorator-depends/?k=bar") 1abcde
538 assert response.status_code == 200 1abcde
539 assert response.json() == {"in": "router-decorator-depends"} 1abcde
540 app.dependency_overrides = {} 1abcde