Coverage for tests / test_path.py: 100%
302 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from fastapi.testclient import TestClient 1abcd
3from .main import app 1abcd
5client = TestClient(app) 1abcd
8def test_text_get(): 1abcd
9 response = client.get("/text") 1efg
10 assert response.status_code == 200, response.text 1efg
11 assert response.json() == "Hello World" 1efg
14def test_nonexistent(): 1abcd
15 response = client.get("/nonexistent") 1hij
16 assert response.status_code == 404, response.text 1hij
17 assert response.json() == {"detail": "Not Found"} 1hij
20def test_path_foobar(): 1abcd
21 response = client.get("/path/foobar") 1klm
22 assert response.status_code == 200 1klm
23 assert response.json() == "foobar" 1klm
26def test_path_str_foobar(): 1abcd
27 response = client.get("/path/str/foobar") 1nop
28 assert response.status_code == 200 1nop
29 assert response.json() == "foobar" 1nop
32def test_path_str_42(): 1abcd
33 response = client.get("/path/str/42") 1qrs
34 assert response.status_code == 200 1qrs
35 assert response.json() == "42" 1qrs
38def test_path_str_True(): 1abcd
39 response = client.get("/path/str/True") 1tuv
40 assert response.status_code == 200 1tuv
41 assert response.json() == "True" 1tuv
44def test_path_int_foobar(): 1abcd
45 response = client.get("/path/int/foobar") 1wxy
46 assert response.status_code == 422 1wxy
47 assert response.json() == { 1wxy
48 "detail": [
49 {
50 "type": "int_parsing",
51 "loc": ["path", "item_id"],
52 "msg": "Input should be a valid integer, unable to parse string as an integer",
53 "input": "foobar",
54 }
55 ]
56 }
59def test_path_int_True(): 1abcd
60 response = client.get("/path/int/True") 1zAB
61 assert response.status_code == 422 1zAB
62 assert response.json() == { 1zAB
63 "detail": [
64 {
65 "type": "int_parsing",
66 "loc": ["path", "item_id"],
67 "msg": "Input should be a valid integer, unable to parse string as an integer",
68 "input": "True",
69 }
70 ]
71 }
74def test_path_int_42(): 1abcd
75 response = client.get("/path/int/42") 1CDE
76 assert response.status_code == 200 1CDE
77 assert response.json() == 42 1CDE
80def test_path_int_42_5(): 1abcd
81 response = client.get("/path/int/42.5") 1FGH
82 assert response.status_code == 422 1FGH
83 assert response.json() == { 1FGH
84 "detail": [
85 {
86 "type": "int_parsing",
87 "loc": ["path", "item_id"],
88 "msg": "Input should be a valid integer, unable to parse string as an integer",
89 "input": "42.5",
90 }
91 ]
92 }
95def test_path_float_foobar(): 1abcd
96 response = client.get("/path/float/foobar") 1IJK
97 assert response.status_code == 422 1IJK
98 assert response.json() == { 1IJK
99 "detail": [
100 {
101 "type": "float_parsing",
102 "loc": ["path", "item_id"],
103 "msg": "Input should be a valid number, unable to parse string as a number",
104 "input": "foobar",
105 }
106 ]
107 }
110def test_path_float_True(): 1abcd
111 response = client.get("/path/float/True") 1LMN
112 assert response.status_code == 422 1LMN
113 assert response.json() == { 1LMN
114 "detail": [
115 {
116 "type": "float_parsing",
117 "loc": ["path", "item_id"],
118 "msg": "Input should be a valid number, unable to parse string as a number",
119 "input": "True",
120 }
121 ]
122 }
125def test_path_float_42(): 1abcd
126 response = client.get("/path/float/42") 1OPQ
127 assert response.status_code == 200 1OPQ
128 assert response.json() == 42 1OPQ
131def test_path_float_42_5(): 1abcd
132 response = client.get("/path/float/42.5") 1RST
133 assert response.status_code == 200 1RST
134 assert response.json() == 42.5 1RST
137def test_path_bool_foobar(): 1abcd
138 response = client.get("/path/bool/foobar") 1UVW
139 assert response.status_code == 422 1UVW
140 assert response.json() == { 1UVW
141 "detail": [
142 {
143 "type": "bool_parsing",
144 "loc": ["path", "item_id"],
145 "msg": "Input should be a valid boolean, unable to interpret input",
146 "input": "foobar",
147 }
148 ]
149 }
152def test_path_bool_True(): 1abcd
153 response = client.get("/path/bool/True") 1XYZ
154 assert response.status_code == 200 1XYZ
155 assert response.json() is True 1XYZ
158def test_path_bool_42(): 1abcd
159 response = client.get("/path/bool/42") 1012
160 assert response.status_code == 422 1012
161 assert response.json() == { 1012
162 "detail": [
163 {
164 "type": "bool_parsing",
165 "loc": ["path", "item_id"],
166 "msg": "Input should be a valid boolean, unable to interpret input",
167 "input": "42",
168 }
169 ]
170 }
173def test_path_bool_42_5(): 1abcd
174 response = client.get("/path/bool/42.5") 1345
175 assert response.status_code == 422 1345
176 assert response.json() == { 1345
177 "detail": [
178 {
179 "type": "bool_parsing",
180 "loc": ["path", "item_id"],
181 "msg": "Input should be a valid boolean, unable to interpret input",
182 "input": "42.5",
183 }
184 ]
185 }
188def test_path_bool_1(): 1abcd
189 response = client.get("/path/bool/1") 1678
190 assert response.status_code == 200 1678
191 assert response.json() is True 1678
194def test_path_bool_0(): 1abcd
195 response = client.get("/path/bool/0") 19!#
196 assert response.status_code == 200 19!#
197 assert response.json() is False 19!#
200def test_path_bool_true(): 1abcd
201 response = client.get("/path/bool/true") 1$%'
202 assert response.status_code == 200 1$%'
203 assert response.json() is True 1$%'
206def test_path_bool_False(): 1abcd
207 response = client.get("/path/bool/False") 1()*
208 assert response.status_code == 200 1()*
209 assert response.json() is False 1()*
212def test_path_bool_false(): 1abcd
213 response = client.get("/path/bool/false") 1+,-
214 assert response.status_code == 200 1+,-
215 assert response.json() is False 1+,-
218def test_path_param_foo(): 1abcd
219 response = client.get("/path/param/foo") 1./:
220 assert response.status_code == 200 1./:
221 assert response.json() == "foo" 1./:
224def test_path_param_minlength_foo(): 1abcd
225 response = client.get("/path/param-minlength/foo") 1;=?
226 assert response.status_code == 200 1;=?
227 assert response.json() == "foo" 1;=?
230def test_path_param_minlength_fo(): 1abcd
231 response = client.get("/path/param-minlength/fo") 1@[]
232 assert response.status_code == 422 1@[]
233 assert response.json() == { 1@[]
234 "detail": [
235 {
236 "type": "string_too_short",
237 "loc": ["path", "item_id"],
238 "msg": "String should have at least 3 characters",
239 "input": "fo",
240 "ctx": {"min_length": 3},
241 }
242 ]
243 }
246def test_path_param_maxlength_foo(): 1abcd
247 response = client.get("/path/param-maxlength/foo") 1^_`
248 assert response.status_code == 200 1^_`
249 assert response.json() == "foo" 1^_`
252def test_path_param_maxlength_foobar(): 1abcd
253 response = client.get("/path/param-maxlength/foobar") 1{|}
254 assert response.status_code == 422 1{|}
255 assert response.json() == { 1{|}
256 "detail": [
257 {
258 "type": "string_too_long",
259 "loc": ["path", "item_id"],
260 "msg": "String should have at most 3 characters",
261 "input": "foobar",
262 "ctx": {"max_length": 3},
263 }
264 ]
265 }
268def test_path_param_min_maxlength_foo(): 1abcd
269 response = client.get("/path/param-min_maxlength/foo") 2~ abbb
270 assert response.status_code == 200 2~ abbb
271 assert response.json() == "foo" 2~ abbb
274def test_path_param_min_maxlength_foobar(): 1abcd
275 response = client.get("/path/param-min_maxlength/foobar") 2cbdbeb
276 assert response.status_code == 422 2cbdbeb
277 assert response.json() == { 2cbdbeb
278 "detail": [
279 {
280 "type": "string_too_long",
281 "loc": ["path", "item_id"],
282 "msg": "String should have at most 3 characters",
283 "input": "foobar",
284 "ctx": {"max_length": 3},
285 }
286 ]
287 }
290def test_path_param_min_maxlength_f(): 1abcd
291 response = client.get("/path/param-min_maxlength/f") 2fbgbhb
292 assert response.status_code == 422 2fbgbhb
293 assert response.json() == { 2fbgbhb
294 "detail": [
295 {
296 "type": "string_too_short",
297 "loc": ["path", "item_id"],
298 "msg": "String should have at least 2 characters",
299 "input": "f",
300 "ctx": {"min_length": 2},
301 }
302 ]
303 }
306def test_path_param_gt_42(): 1abcd
307 response = client.get("/path/param-gt/42") 2ibjbkb
308 assert response.status_code == 200 2ibjbkb
309 assert response.json() == 42 2ibjbkb
312def test_path_param_gt_2(): 1abcd
313 response = client.get("/path/param-gt/2") 2lbmbnb
314 assert response.status_code == 422 2lbmbnb
315 assert response.json() == { 2lbmbnb
316 "detail": [
317 {
318 "type": "greater_than",
319 "loc": ["path", "item_id"],
320 "msg": "Input should be greater than 3",
321 "input": "2",
322 "ctx": {"gt": 3.0},
323 }
324 ]
325 }
328def test_path_param_gt0_0_05(): 1abcd
329 response = client.get("/path/param-gt0/0.05") 2obpbqb
330 assert response.status_code == 200 2obpbqb
331 assert response.json() == 0.05 2obpbqb
334def test_path_param_gt0_0(): 1abcd
335 response = client.get("/path/param-gt0/0") 2rbsbtb
336 assert response.status_code == 422 2rbsbtb
337 assert response.json() == { 2rbsbtb
338 "detail": [
339 {
340 "type": "greater_than",
341 "loc": ["path", "item_id"],
342 "msg": "Input should be greater than 0",
343 "input": "0",
344 "ctx": {"gt": 0.0},
345 }
346 ]
347 }
350def test_path_param_ge_42(): 1abcd
351 response = client.get("/path/param-ge/42") 2ubvbwb
352 assert response.status_code == 200 2ubvbwb
353 assert response.json() == 42 2ubvbwb
356def test_path_param_ge_3(): 1abcd
357 response = client.get("/path/param-ge/3") 2xbybzb
358 assert response.status_code == 200 2xbybzb
359 assert response.json() == 3 2xbybzb
362def test_path_param_ge_2(): 1abcd
363 response = client.get("/path/param-ge/2") 2AbBbCb
364 assert response.status_code == 422 2AbBbCb
365 assert response.json() == { 2AbBbCb
366 "detail": [
367 {
368 "type": "greater_than_equal",
369 "loc": ["path", "item_id"],
370 "msg": "Input should be greater than or equal to 3",
371 "input": "2",
372 "ctx": {"ge": 3.0},
373 }
374 ]
375 }
378def test_path_param_lt_42(): 1abcd
379 response = client.get("/path/param-lt/42") 2DbEbFb
380 assert response.status_code == 422 2DbEbFb
381 assert response.json() == { 2DbEbFb
382 "detail": [
383 {
384 "type": "less_than",
385 "loc": ["path", "item_id"],
386 "msg": "Input should be less than 3",
387 "input": "42",
388 "ctx": {"lt": 3.0},
389 }
390 ]
391 }
394def test_path_param_lt_2(): 1abcd
395 response = client.get("/path/param-lt/2") 2GbHbIb
396 assert response.status_code == 200 2GbHbIb
397 assert response.json() == 2 2GbHbIb
400def test_path_param_lt0__1(): 1abcd
401 response = client.get("/path/param-lt0/-1") 2JbKbLb
402 assert response.status_code == 200 2JbKbLb
403 assert response.json() == -1 2JbKbLb
406def test_path_param_lt0_0(): 1abcd
407 response = client.get("/path/param-lt0/0") 2MbNbOb
408 assert response.status_code == 422 2MbNbOb
409 assert response.json() == { 2MbNbOb
410 "detail": [
411 {
412 "type": "less_than",
413 "loc": ["path", "item_id"],
414 "msg": "Input should be less than 0",
415 "input": "0",
416 "ctx": {"lt": 0.0},
417 }
418 ]
419 }
422def test_path_param_le_42(): 1abcd
423 response = client.get("/path/param-le/42") 2PbQbRb
424 assert response.status_code == 422 2PbQbRb
425 assert response.json() == { 2PbQbRb
426 "detail": [
427 {
428 "type": "less_than_equal",
429 "loc": ["path", "item_id"],
430 "msg": "Input should be less than or equal to 3",
431 "input": "42",
432 "ctx": {"le": 3.0},
433 }
434 ]
435 }
438def test_path_param_le_3(): 1abcd
439 response = client.get("/path/param-le/3") 2SbTbUb
440 assert response.status_code == 200 2SbTbUb
441 assert response.json() == 3 2SbTbUb
444def test_path_param_le_2(): 1abcd
445 response = client.get("/path/param-le/2") 2VbWbXb
446 assert response.status_code == 200 2VbWbXb
447 assert response.json() == 2 2VbWbXb
450def test_path_param_lt_gt_2(): 1abcd
451 response = client.get("/path/param-lt-gt/2") 2YbZb0b
452 assert response.status_code == 200 2YbZb0b
453 assert response.json() == 2 2YbZb0b
456def test_path_param_lt_gt_4(): 1abcd
457 response = client.get("/path/param-lt-gt/4") 21b2b3b
458 assert response.status_code == 422 21b2b3b
459 assert response.json() == { 21b2b3b
460 "detail": [
461 {
462 "type": "less_than",
463 "loc": ["path", "item_id"],
464 "msg": "Input should be less than 3",
465 "input": "4",
466 "ctx": {"lt": 3.0},
467 }
468 ]
469 }
472def test_path_param_lt_gt_0(): 1abcd
473 response = client.get("/path/param-lt-gt/0") 24b5b6b
474 assert response.status_code == 422 24b5b6b
475 assert response.json() == { 24b5b6b
476 "detail": [
477 {
478 "type": "greater_than",
479 "loc": ["path", "item_id"],
480 "msg": "Input should be greater than 1",
481 "input": "0",
482 "ctx": {"gt": 1.0},
483 }
484 ]
485 }
488def test_path_param_le_ge_2(): 1abcd
489 response = client.get("/path/param-le-ge/2") 27b8b9b
490 assert response.status_code == 200 27b8b9b
491 assert response.json() == 2 27b8b9b
494def test_path_param_le_ge_1(): 1abcd
495 response = client.get("/path/param-le-ge/1") 2WcXcYc
496 assert response.status_code == 200 2WcXcYc
499def test_path_param_le_ge_3(): 1abcd
500 response = client.get("/path/param-le-ge/3") 2!b#b$b
501 assert response.status_code == 200 2!b#b$b
502 assert response.json() == 3 2!b#b$b
505def test_path_param_le_ge_4(): 1abcd
506 response = client.get("/path/param-le-ge/4") 2%b'b(b
507 assert response.status_code == 422 2%b'b(b
508 assert response.json() == { 2%b'b(b
509 "detail": [
510 {
511 "type": "less_than_equal",
512 "loc": ["path", "item_id"],
513 "msg": "Input should be less than or equal to 3",
514 "input": "4",
515 "ctx": {"le": 3.0},
516 }
517 ]
518 }
521def test_path_param_lt_int_2(): 1abcd
522 response = client.get("/path/param-lt-int/2") 2)b*b+b
523 assert response.status_code == 200 2)b*b+b
524 assert response.json() == 2 2)b*b+b
527def test_path_param_lt_int_42(): 1abcd
528 response = client.get("/path/param-lt-int/42") 2,b-b.b
529 assert response.status_code == 422 2,b-b.b
530 assert response.json() == { 2,b-b.b
531 "detail": [
532 {
533 "type": "less_than",
534 "loc": ["path", "item_id"],
535 "msg": "Input should be less than 3",
536 "input": "42",
537 "ctx": {"lt": 3},
538 }
539 ]
540 }
543def test_path_param_lt_int_2_7(): 1abcd
544 response = client.get("/path/param-lt-int/2.7") 2/b:b;b
545 assert response.status_code == 422 2/b:b;b
546 assert response.json() == { 2/b:b;b
547 "detail": [
548 {
549 "type": "int_parsing",
550 "loc": ["path", "item_id"],
551 "msg": "Input should be a valid integer, unable to parse string as an integer",
552 "input": "2.7",
553 }
554 ]
555 }
558def test_path_param_gt_int_42(): 1abcd
559 response = client.get("/path/param-gt-int/42") 2=b?b@b
560 assert response.status_code == 200 2=b?b@b
561 assert response.json() == 42 2=b?b@b
564def test_path_param_gt_int_2(): 1abcd
565 response = client.get("/path/param-gt-int/2") 2[b]b^b
566 assert response.status_code == 422 2[b]b^b
567 assert response.json() == { 2[b]b^b
568 "detail": [
569 {
570 "type": "greater_than",
571 "loc": ["path", "item_id"],
572 "msg": "Input should be greater than 3",
573 "input": "2",
574 "ctx": {"gt": 3},
575 }
576 ]
577 }
580def test_path_param_gt_int_2_7(): 1abcd
581 response = client.get("/path/param-gt-int/2.7") 2_b`b{b
582 assert response.status_code == 422 2_b`b{b
583 assert response.json() == { 2_b`b{b
584 "detail": [
585 {
586 "type": "int_parsing",
587 "loc": ["path", "item_id"],
588 "msg": "Input should be a valid integer, unable to parse string as an integer",
589 "input": "2.7",
590 }
591 ]
592 }
595def test_path_param_le_int_42(): 1abcd
596 response = client.get("/path/param-le-int/42") 2|b}b~b
597 assert response.status_code == 422 2|b}b~b
598 assert response.json() == { 2|b}b~b
599 "detail": [
600 {
601 "type": "less_than_equal",
602 "loc": ["path", "item_id"],
603 "msg": "Input should be less than or equal to 3",
604 "input": "42",
605 "ctx": {"le": 3},
606 }
607 ]
608 }
611def test_path_param_le_int_3(): 1abcd
612 response = client.get("/path/param-le-int/3") 2acbccc
613 assert response.status_code == 200 2acbccc
614 assert response.json() == 3 2acbccc
617def test_path_param_le_int_2(): 1abcd
618 response = client.get("/path/param-le-int/2") 2dcecfc
619 assert response.status_code == 200 2dcecfc
620 assert response.json() == 2 2dcecfc
623def test_path_param_le_int_2_7(): 1abcd
624 response = client.get("/path/param-le-int/2.7") 2gchcic
625 assert response.status_code == 422 2gchcic
626 assert response.json() == { 2gchcic
627 "detail": [
628 {
629 "type": "int_parsing",
630 "loc": ["path", "item_id"],
631 "msg": "Input should be a valid integer, unable to parse string as an integer",
632 "input": "2.7",
633 }
634 ]
635 }
638def test_path_param_ge_int_42(): 1abcd
639 response = client.get("/path/param-ge-int/42") 2jckclc
640 assert response.status_code == 200 2jckclc
641 assert response.json() == 42 2jckclc
644def test_path_param_ge_int_3(): 1abcd
645 response = client.get("/path/param-ge-int/3") 2mcncoc
646 assert response.status_code == 200 2mcncoc
647 assert response.json() == 3 2mcncoc
650def test_path_param_ge_int_2(): 1abcd
651 response = client.get("/path/param-ge-int/2") 2pcqcrc
652 assert response.status_code == 422 2pcqcrc
653 assert response.json() == { 2pcqcrc
654 "detail": [
655 {
656 "type": "greater_than_equal",
657 "loc": ["path", "item_id"],
658 "msg": "Input should be greater than or equal to 3",
659 "input": "2",
660 "ctx": {"ge": 3},
661 }
662 ]
663 }
666def test_path_param_ge_int_2_7(): 1abcd
667 response = client.get("/path/param-ge-int/2.7") 2sctcuc
668 assert response.status_code == 422 2sctcuc
669 assert response.json() == { 2sctcuc
670 "detail": [
671 {
672 "type": "int_parsing",
673 "loc": ["path", "item_id"],
674 "msg": "Input should be a valid integer, unable to parse string as an integer",
675 "input": "2.7",
676 }
677 ]
678 }
681def test_path_param_lt_gt_int_2(): 1abcd
682 response = client.get("/path/param-lt-gt-int/2") 2vcwcxc
683 assert response.status_code == 200 2vcwcxc
684 assert response.json() == 2 2vcwcxc
687def test_path_param_lt_gt_int_4(): 1abcd
688 response = client.get("/path/param-lt-gt-int/4") 2yczcAc
689 assert response.status_code == 422 2yczcAc
690 assert response.json() == { 2yczcAc
691 "detail": [
692 {
693 "type": "less_than",
694 "loc": ["path", "item_id"],
695 "msg": "Input should be less than 3",
696 "input": "4",
697 "ctx": {"lt": 3},
698 }
699 ]
700 }
703def test_path_param_lt_gt_int_0(): 1abcd
704 response = client.get("/path/param-lt-gt-int/0") 2BcCcDc
705 assert response.status_code == 422 2BcCcDc
706 assert response.json() == { 2BcCcDc
707 "detail": [
708 {
709 "type": "greater_than",
710 "loc": ["path", "item_id"],
711 "msg": "Input should be greater than 1",
712 "input": "0",
713 "ctx": {"gt": 1},
714 }
715 ]
716 }
719def test_path_param_lt_gt_int_2_7(): 1abcd
720 response = client.get("/path/param-lt-gt-int/2.7") 2EcFcGc
721 assert response.status_code == 422 2EcFcGc
722 assert response.json() == { 2EcFcGc
723 "detail": [
724 {
725 "type": "int_parsing",
726 "loc": ["path", "item_id"],
727 "msg": "Input should be a valid integer, unable to parse string as an integer",
728 "input": "2.7",
729 }
730 ]
731 }
734def test_path_param_le_ge_int_2(): 1abcd
735 response = client.get("/path/param-le-ge-int/2") 2HcIcJc
736 assert response.status_code == 200 2HcIcJc
737 assert response.json() == 2 2HcIcJc
740def test_path_param_le_ge_int_1(): 1abcd
741 response = client.get("/path/param-le-ge-int/1") 2KcLcMc
742 assert response.status_code == 200 2KcLcMc
743 assert response.json() == 1 2KcLcMc
746def test_path_param_le_ge_int_3(): 1abcd
747 response = client.get("/path/param-le-ge-int/3") 2NcOcPc
748 assert response.status_code == 200 2NcOcPc
749 assert response.json() == 3 2NcOcPc
752def test_path_param_le_ge_int_4(): 1abcd
753 response = client.get("/path/param-le-ge-int/4") 2QcRcSc
754 assert response.status_code == 422 2QcRcSc
755 assert response.json() == { 2QcRcSc
756 "detail": [
757 {
758 "type": "less_than_equal",
759 "loc": ["path", "item_id"],
760 "msg": "Input should be less than or equal to 3",
761 "input": "4",
762 "ctx": {"le": 3},
763 }
764 ]
765 }
768def test_path_param_le_ge_int_2_7(): 1abcd
769 response = client.get("/path/param-le-ge-int/2.7") 2TcUcVc
770 assert response.status_code == 422 2TcUcVc
771 assert response.json() == { 2TcUcVc
772 "detail": [
773 {
774 "type": "int_parsing",
775 "loc": ["path", "item_id"],
776 "msg": "Input should be a valid integer, unable to parse string as an integer",
777 "input": "2.7",
778 }
779 ]
780 }