1
from dirty_equals import IsDict
2
from fastapi.testclient import TestClient
3
from fastapi.utils import match_pydantic_error_url
7
client = TestClient(app)
11
response = client.get("/text")
12
assert response.status_code == 200, response.text
13
assert response.json() == "Hello World"
16
def test_nonexistent():
17
response = client.get("/nonexistent")
18
assert response.status_code == 404, response.text
19
assert response.json() == {"detail": "Not Found"}
22
def test_path_foobar():
23
response = client.get("/path/foobar")
24
assert response.status_code == 200
25
assert response.json() == "foobar"
28
def test_path_str_foobar():
29
response = client.get("/path/str/foobar")
30
assert response.status_code == 200
31
assert response.json() == "foobar"
34
def test_path_str_42():
35
response = client.get("/path/str/42")
36
assert response.status_code == 200
37
assert response.json() == "42"
40
def test_path_str_True():
41
response = client.get("/path/str/True")
42
assert response.status_code == 200
43
assert response.json() == "True"
46
def test_path_int_foobar():
47
response = client.get("/path/int/foobar")
48
assert response.status_code == 422
49
assert response.json() == IsDict(
53
"type": "int_parsing",
54
"loc": ["path", "item_id"],
55
"msg": "Input should be a valid integer, unable to parse string as an integer",
57
"url": match_pydantic_error_url("int_parsing"),
66
"loc": ["path", "item_id"],
67
"msg": "value is not a valid integer",
68
"type": "type_error.integer",
75
def test_path_int_True():
76
response = client.get("/path/int/True")
77
assert response.status_code == 422
78
assert response.json() == IsDict(
82
"type": "int_parsing",
83
"loc": ["path", "item_id"],
84
"msg": "Input should be a valid integer, unable to parse string as an integer",
86
"url": match_pydantic_error_url("int_parsing"),
95
"loc": ["path", "item_id"],
96
"msg": "value is not a valid integer",
97
"type": "type_error.integer",
104
def test_path_int_42():
105
response = client.get("/path/int/42")
106
assert response.status_code == 200
107
assert response.json() == 42
110
def test_path_int_42_5():
111
response = client.get("/path/int/42.5")
112
assert response.status_code == 422
113
assert response.json() == IsDict(
117
"type": "int_parsing",
118
"loc": ["path", "item_id"],
119
"msg": "Input should be a valid integer, unable to parse string as an integer",
121
"url": match_pydantic_error_url("int_parsing"),
130
"loc": ["path", "item_id"],
131
"msg": "value is not a valid integer",
132
"type": "type_error.integer",
139
def test_path_float_foobar():
140
response = client.get("/path/float/foobar")
141
assert response.status_code == 422
142
assert response.json() == IsDict(
146
"type": "float_parsing",
147
"loc": ["path", "item_id"],
148
"msg": "Input should be a valid number, unable to parse string as a number",
150
"url": match_pydantic_error_url("float_parsing"),
159
"loc": ["path", "item_id"],
160
"msg": "value is not a valid float",
161
"type": "type_error.float",
168
def test_path_float_True():
169
response = client.get("/path/float/True")
170
assert response.status_code == 422
171
assert response.json() == IsDict(
175
"type": "float_parsing",
176
"loc": ["path", "item_id"],
177
"msg": "Input should be a valid number, unable to parse string as a number",
179
"url": match_pydantic_error_url("float_parsing"),
188
"loc": ["path", "item_id"],
189
"msg": "value is not a valid float",
190
"type": "type_error.float",
197
def test_path_float_42():
198
response = client.get("/path/float/42")
199
assert response.status_code == 200
200
assert response.json() == 42
203
def test_path_float_42_5():
204
response = client.get("/path/float/42.5")
205
assert response.status_code == 200
206
assert response.json() == 42.5
209
def test_path_bool_foobar():
210
response = client.get("/path/bool/foobar")
211
assert response.status_code == 422
212
assert response.json() == IsDict(
216
"type": "bool_parsing",
217
"loc": ["path", "item_id"],
218
"msg": "Input should be a valid boolean, unable to interpret input",
220
"url": match_pydantic_error_url("bool_parsing"),
229
"loc": ["path", "item_id"],
230
"msg": "value could not be parsed to a boolean",
231
"type": "type_error.bool",
238
def test_path_bool_True():
239
response = client.get("/path/bool/True")
240
assert response.status_code == 200
241
assert response.json() is True
244
def test_path_bool_42():
245
response = client.get("/path/bool/42")
246
assert response.status_code == 422
247
assert response.json() == IsDict(
251
"type": "bool_parsing",
252
"loc": ["path", "item_id"],
253
"msg": "Input should be a valid boolean, unable to interpret input",
255
"url": match_pydantic_error_url("bool_parsing"),
264
"loc": ["path", "item_id"],
265
"msg": "value could not be parsed to a boolean",
266
"type": "type_error.bool",
273
def test_path_bool_42_5():
274
response = client.get("/path/bool/42.5")
275
assert response.status_code == 422
276
assert response.json() == IsDict(
280
"type": "bool_parsing",
281
"loc": ["path", "item_id"],
282
"msg": "Input should be a valid boolean, unable to interpret input",
284
"url": match_pydantic_error_url("bool_parsing"),
293
"loc": ["path", "item_id"],
294
"msg": "value could not be parsed to a boolean",
295
"type": "type_error.bool",
302
def test_path_bool_1():
303
response = client.get("/path/bool/1")
304
assert response.status_code == 200
305
assert response.json() is True
308
def test_path_bool_0():
309
response = client.get("/path/bool/0")
310
assert response.status_code == 200
311
assert response.json() is False
314
def test_path_bool_true():
315
response = client.get("/path/bool/true")
316
assert response.status_code == 200
317
assert response.json() is True
320
def test_path_bool_False():
321
response = client.get("/path/bool/False")
322
assert response.status_code == 200
323
assert response.json() is False
326
def test_path_bool_false():
327
response = client.get("/path/bool/false")
328
assert response.status_code == 200
329
assert response.json() is False
332
def test_path_param_foo():
333
response = client.get("/path/param/foo")
334
assert response.status_code == 200
335
assert response.json() == "foo"
338
def test_path_param_minlength_foo():
339
response = client.get("/path/param-minlength/foo")
340
assert response.status_code == 200
341
assert response.json() == "foo"
344
def test_path_param_minlength_fo():
345
response = client.get("/path/param-minlength/fo")
346
assert response.status_code == 422
347
assert response.json() == IsDict(
351
"type": "string_too_short",
352
"loc": ["path", "item_id"],
353
"msg": "String should have at least 3 characters",
355
"ctx": {"min_length": 3},
356
"url": match_pydantic_error_url("string_too_short"),
365
"loc": ["path", "item_id"],
366
"msg": "ensure this value has at least 3 characters",
367
"type": "value_error.any_str.min_length",
368
"ctx": {"limit_value": 3},
375
def test_path_param_maxlength_foo():
376
response = client.get("/path/param-maxlength/foo")
377
assert response.status_code == 200
378
assert response.json() == "foo"
381
def test_path_param_maxlength_foobar():
382
response = client.get("/path/param-maxlength/foobar")
383
assert response.status_code == 422
384
assert response.json() == IsDict(
388
"type": "string_too_long",
389
"loc": ["path", "item_id"],
390
"msg": "String should have at most 3 characters",
392
"ctx": {"max_length": 3},
393
"url": match_pydantic_error_url("string_too_long"),
402
"loc": ["path", "item_id"],
403
"msg": "ensure this value has at most 3 characters",
404
"type": "value_error.any_str.max_length",
405
"ctx": {"limit_value": 3},
412
def test_path_param_min_maxlength_foo():
413
response = client.get("/path/param-min_maxlength/foo")
414
assert response.status_code == 200
415
assert response.json() == "foo"
418
def test_path_param_min_maxlength_foobar():
419
response = client.get("/path/param-min_maxlength/foobar")
420
assert response.status_code == 422
421
assert response.json() == IsDict(
425
"type": "string_too_long",
426
"loc": ["path", "item_id"],
427
"msg": "String should have at most 3 characters",
429
"ctx": {"max_length": 3},
430
"url": match_pydantic_error_url("string_too_long"),
439
"loc": ["path", "item_id"],
440
"msg": "ensure this value has at most 3 characters",
441
"type": "value_error.any_str.max_length",
442
"ctx": {"limit_value": 3},
449
def test_path_param_min_maxlength_f():
450
response = client.get("/path/param-min_maxlength/f")
451
assert response.status_code == 422
452
assert response.json() == IsDict(
456
"type": "string_too_short",
457
"loc": ["path", "item_id"],
458
"msg": "String should have at least 2 characters",
460
"ctx": {"min_length": 2},
461
"url": match_pydantic_error_url("string_too_short"),
469
"loc": ["path", "item_id"],
470
"msg": "ensure this value has at least 2 characters",
471
"type": "value_error.any_str.min_length",
472
"ctx": {"limit_value": 2},
479
def test_path_param_gt_42():
480
response = client.get("/path/param-gt/42")
481
assert response.status_code == 200
482
assert response.json() == 42
485
def test_path_param_gt_2():
486
response = client.get("/path/param-gt/2")
487
assert response.status_code == 422
488
assert response.json() == IsDict(
492
"type": "greater_than",
493
"loc": ["path", "item_id"],
494
"msg": "Input should be greater than 3",
497
"url": match_pydantic_error_url("greater_than"),
506
"loc": ["path", "item_id"],
507
"msg": "ensure this value is greater than 3",
508
"type": "value_error.number.not_gt",
509
"ctx": {"limit_value": 3},
516
def test_path_param_gt0_0_05():
517
response = client.get("/path/param-gt0/0.05")
518
assert response.status_code == 200
519
assert response.json() == 0.05
522
def test_path_param_gt0_0():
523
response = client.get("/path/param-gt0/0")
524
assert response.status_code == 422
525
assert response.json() == IsDict(
529
"type": "greater_than",
530
"loc": ["path", "item_id"],
531
"msg": "Input should be greater than 0",
534
"url": match_pydantic_error_url("greater_than"),
543
"loc": ["path", "item_id"],
544
"msg": "ensure this value is greater than 0",
545
"type": "value_error.number.not_gt",
546
"ctx": {"limit_value": 0},
553
def test_path_param_ge_42():
554
response = client.get("/path/param-ge/42")
555
assert response.status_code == 200
556
assert response.json() == 42
559
def test_path_param_ge_3():
560
response = client.get("/path/param-ge/3")
561
assert response.status_code == 200
562
assert response.json() == 3
565
def test_path_param_ge_2():
566
response = client.get("/path/param-ge/2")
567
assert response.status_code == 422
568
assert response.json() == IsDict(
572
"type": "greater_than_equal",
573
"loc": ["path", "item_id"],
574
"msg": "Input should be greater than or equal to 3",
577
"url": match_pydantic_error_url("greater_than_equal"),
586
"loc": ["path", "item_id"],
587
"msg": "ensure this value is greater than or equal to 3",
588
"type": "value_error.number.not_ge",
589
"ctx": {"limit_value": 3},
596
def test_path_param_lt_42():
597
response = client.get("/path/param-lt/42")
598
assert response.status_code == 422
599
assert response.json() == IsDict(
604
"loc": ["path", "item_id"],
605
"msg": "Input should be less than 3",
608
"url": match_pydantic_error_url("less_than"),
617
"loc": ["path", "item_id"],
618
"msg": "ensure this value is less than 3",
619
"type": "value_error.number.not_lt",
620
"ctx": {"limit_value": 3},
627
def test_path_param_lt_2():
628
response = client.get("/path/param-lt/2")
629
assert response.status_code == 200
630
assert response.json() == 2
633
def test_path_param_lt0__1():
634
response = client.get("/path/param-lt0/-1")
635
assert response.status_code == 200
636
assert response.json() == -1
639
def test_path_param_lt0_0():
640
response = client.get("/path/param-lt0/0")
641
assert response.status_code == 422
642
assert response.json() == IsDict(
647
"loc": ["path", "item_id"],
648
"msg": "Input should be less than 0",
651
"url": match_pydantic_error_url("less_than"),
660
"loc": ["path", "item_id"],
661
"msg": "ensure this value is less than 0",
662
"type": "value_error.number.not_lt",
663
"ctx": {"limit_value": 0},
670
def test_path_param_le_42():
671
response = client.get("/path/param-le/42")
672
assert response.status_code == 422
673
assert response.json() == IsDict(
677
"type": "less_than_equal",
678
"loc": ["path", "item_id"],
679
"msg": "Input should be less than or equal to 3",
682
"url": match_pydantic_error_url("less_than_equal"),
691
"loc": ["path", "item_id"],
692
"msg": "ensure this value is less than or equal to 3",
693
"type": "value_error.number.not_le",
694
"ctx": {"limit_value": 3},
701
def test_path_param_le_3():
702
response = client.get("/path/param-le/3")
703
assert response.status_code == 200
704
assert response.json() == 3
707
def test_path_param_le_2():
708
response = client.get("/path/param-le/2")
709
assert response.status_code == 200
710
assert response.json() == 2
713
def test_path_param_lt_gt_2():
714
response = client.get("/path/param-lt-gt/2")
715
assert response.status_code == 200
716
assert response.json() == 2
719
def test_path_param_lt_gt_4():
720
response = client.get("/path/param-lt-gt/4")
721
assert response.status_code == 422
722
assert response.json() == IsDict(
727
"loc": ["path", "item_id"],
728
"msg": "Input should be less than 3",
731
"url": match_pydantic_error_url("less_than"),
740
"loc": ["path", "item_id"],
741
"msg": "ensure this value is less than 3",
742
"type": "value_error.number.not_lt",
743
"ctx": {"limit_value": 3},
750
def test_path_param_lt_gt_0():
751
response = client.get("/path/param-lt-gt/0")
752
assert response.status_code == 422
753
assert response.json() == IsDict(
757
"type": "greater_than",
758
"loc": ["path", "item_id"],
759
"msg": "Input should be greater than 1",
762
"url": match_pydantic_error_url("greater_than"),
771
"loc": ["path", "item_id"],
772
"msg": "ensure this value is greater than 1",
773
"type": "value_error.number.not_gt",
774
"ctx": {"limit_value": 1},
781
def test_path_param_le_ge_2():
782
response = client.get("/path/param-le-ge/2")
783
assert response.status_code == 200
784
assert response.json() == 2
787
def test_path_param_le_ge_1():
788
response = client.get("/path/param-le-ge/1")
789
assert response.status_code == 200
792
def test_path_param_le_ge_3():
793
response = client.get("/path/param-le-ge/3")
794
assert response.status_code == 200
795
assert response.json() == 3
798
def test_path_param_le_ge_4():
799
response = client.get("/path/param-le-ge/4")
800
assert response.status_code == 422
801
assert response.json() == IsDict(
805
"type": "less_than_equal",
806
"loc": ["path", "item_id"],
807
"msg": "Input should be less than or equal to 3",
810
"url": match_pydantic_error_url("less_than_equal"),
819
"loc": ["path", "item_id"],
820
"msg": "ensure this value is less than or equal to 3",
821
"type": "value_error.number.not_le",
822
"ctx": {"limit_value": 3},
829
def test_path_param_lt_int_2():
830
response = client.get("/path/param-lt-int/2")
831
assert response.status_code == 200
832
assert response.json() == 2
835
def test_path_param_lt_int_42():
836
response = client.get("/path/param-lt-int/42")
837
assert response.status_code == 422
838
assert response.json() == IsDict(
843
"loc": ["path", "item_id"],
844
"msg": "Input should be less than 3",
847
"url": match_pydantic_error_url("less_than"),
856
"loc": ["path", "item_id"],
857
"msg": "ensure this value is less than 3",
858
"type": "value_error.number.not_lt",
859
"ctx": {"limit_value": 3},
866
def test_path_param_lt_int_2_7():
867
response = client.get("/path/param-lt-int/2.7")
868
assert response.status_code == 422
869
assert response.json() == IsDict(
873
"type": "int_parsing",
874
"loc": ["path", "item_id"],
875
"msg": "Input should be a valid integer, unable to parse string as an integer",
877
"url": match_pydantic_error_url("int_parsing"),
886
"loc": ["path", "item_id"],
887
"msg": "value is not a valid integer",
888
"type": "type_error.integer",
895
def test_path_param_gt_int_42():
896
response = client.get("/path/param-gt-int/42")
897
assert response.status_code == 200
898
assert response.json() == 42
901
def test_path_param_gt_int_2():
902
response = client.get("/path/param-gt-int/2")
903
assert response.status_code == 422
904
assert response.json() == IsDict(
908
"type": "greater_than",
909
"loc": ["path", "item_id"],
910
"msg": "Input should be greater than 3",
913
"url": match_pydantic_error_url("greater_than"),
922
"loc": ["path", "item_id"],
923
"msg": "ensure this value is greater than 3",
924
"type": "value_error.number.not_gt",
925
"ctx": {"limit_value": 3},
932
def test_path_param_gt_int_2_7():
933
response = client.get("/path/param-gt-int/2.7")
934
assert response.status_code == 422
935
assert response.json() == IsDict(
939
"type": "int_parsing",
940
"loc": ["path", "item_id"],
941
"msg": "Input should be a valid integer, unable to parse string as an integer",
943
"url": match_pydantic_error_url("int_parsing"),
952
"loc": ["path", "item_id"],
953
"msg": "value is not a valid integer",
954
"type": "type_error.integer",
961
def test_path_param_le_int_42():
962
response = client.get("/path/param-le-int/42")
963
assert response.status_code == 422
964
assert response.json() == IsDict(
968
"type": "less_than_equal",
969
"loc": ["path", "item_id"],
970
"msg": "Input should be less than or equal to 3",
973
"url": match_pydantic_error_url("less_than_equal"),
982
"loc": ["path", "item_id"],
983
"msg": "ensure this value is less than or equal to 3",
984
"type": "value_error.number.not_le",
985
"ctx": {"limit_value": 3},
992
def test_path_param_le_int_3():
993
response = client.get("/path/param-le-int/3")
994
assert response.status_code == 200
995
assert response.json() == 3
998
def test_path_param_le_int_2():
999
response = client.get("/path/param-le-int/2")
1000
assert response.status_code == 200
1001
assert response.json() == 2
1004
def test_path_param_le_int_2_7():
1005
response = client.get("/path/param-le-int/2.7")
1006
assert response.status_code == 422
1007
assert response.json() == IsDict(
1011
"type": "int_parsing",
1012
"loc": ["path", "item_id"],
1013
"msg": "Input should be a valid integer, unable to parse string as an integer",
1015
"url": match_pydantic_error_url("int_parsing"),
1024
"loc": ["path", "item_id"],
1025
"msg": "value is not a valid integer",
1026
"type": "type_error.integer",
1033
def test_path_param_ge_int_42():
1034
response = client.get("/path/param-ge-int/42")
1035
assert response.status_code == 200
1036
assert response.json() == 42
1039
def test_path_param_ge_int_3():
1040
response = client.get("/path/param-ge-int/3")
1041
assert response.status_code == 200
1042
assert response.json() == 3
1045
def test_path_param_ge_int_2():
1046
response = client.get("/path/param-ge-int/2")
1047
assert response.status_code == 422
1048
assert response.json() == IsDict(
1052
"type": "greater_than_equal",
1053
"loc": ["path", "item_id"],
1054
"msg": "Input should be greater than or equal to 3",
1057
"url": match_pydantic_error_url("greater_than_equal"),
1066
"loc": ["path", "item_id"],
1067
"msg": "ensure this value is greater than or equal to 3",
1068
"type": "value_error.number.not_ge",
1069
"ctx": {"limit_value": 3},
1076
def test_path_param_ge_int_2_7():
1077
response = client.get("/path/param-ge-int/2.7")
1078
assert response.status_code == 422
1079
assert response.json() == IsDict(
1083
"type": "int_parsing",
1084
"loc": ["path", "item_id"],
1085
"msg": "Input should be a valid integer, unable to parse string as an integer",
1087
"url": match_pydantic_error_url("int_parsing"),
1096
"loc": ["path", "item_id"],
1097
"msg": "value is not a valid integer",
1098
"type": "type_error.integer",
1105
def test_path_param_lt_gt_int_2():
1106
response = client.get("/path/param-lt-gt-int/2")
1107
assert response.status_code == 200
1108
assert response.json() == 2
1111
def test_path_param_lt_gt_int_4():
1112
response = client.get("/path/param-lt-gt-int/4")
1113
assert response.status_code == 422
1114
assert response.json() == IsDict(
1118
"type": "less_than",
1119
"loc": ["path", "item_id"],
1120
"msg": "Input should be less than 3",
1123
"url": match_pydantic_error_url("less_than"),
1132
"loc": ["path", "item_id"],
1133
"msg": "ensure this value is less than 3",
1134
"type": "value_error.number.not_lt",
1135
"ctx": {"limit_value": 3},
1142
def test_path_param_lt_gt_int_0():
1143
response = client.get("/path/param-lt-gt-int/0")
1144
assert response.status_code == 422
1145
assert response.json() == IsDict(
1149
"type": "greater_than",
1150
"loc": ["path", "item_id"],
1151
"msg": "Input should be greater than 1",
1154
"url": match_pydantic_error_url("greater_than"),
1163
"loc": ["path", "item_id"],
1164
"msg": "ensure this value is greater than 1",
1165
"type": "value_error.number.not_gt",
1166
"ctx": {"limit_value": 1},
1173
def test_path_param_lt_gt_int_2_7():
1174
response = client.get("/path/param-lt-gt-int/2.7")
1175
assert response.status_code == 422
1176
assert response.json() == IsDict(
1180
"type": "int_parsing",
1181
"loc": ["path", "item_id"],
1182
"msg": "Input should be a valid integer, unable to parse string as an integer",
1184
"url": match_pydantic_error_url("int_parsing"),
1193
"loc": ["path", "item_id"],
1194
"msg": "value is not a valid integer",
1195
"type": "type_error.integer",
1202
def test_path_param_le_ge_int_2():
1203
response = client.get("/path/param-le-ge-int/2")
1204
assert response.status_code == 200
1205
assert response.json() == 2
1208
def test_path_param_le_ge_int_1():
1209
response = client.get("/path/param-le-ge-int/1")
1210
assert response.status_code == 200
1211
assert response.json() == 1
1214
def test_path_param_le_ge_int_3():
1215
response = client.get("/path/param-le-ge-int/3")
1216
assert response.status_code == 200
1217
assert response.json() == 3
1220
def test_path_param_le_ge_int_4():
1221
response = client.get("/path/param-le-ge-int/4")
1222
assert response.status_code == 422
1223
assert response.json() == IsDict(
1227
"type": "less_than_equal",
1228
"loc": ["path", "item_id"],
1229
"msg": "Input should be less than or equal to 3",
1232
"url": match_pydantic_error_url("less_than_equal"),
1241
"loc": ["path", "item_id"],
1242
"msg": "ensure this value is less than or equal to 3",
1243
"type": "value_error.number.not_le",
1244
"ctx": {"limit_value": 3},
1251
def test_path_param_le_ge_int_2_7():
1252
response = client.get("/path/param-le-ge-int/2.7")
1253
assert response.status_code == 422
1254
assert response.json() == IsDict(
1258
"type": "int_parsing",
1259
"loc": ["path", "item_id"],
1260
"msg": "Input should be a valid integer, unable to parse string as an integer",
1262
"url": match_pydantic_error_url("int_parsing"),
1271
"loc": ["path", "item_id"],
1272
"msg": "value is not a valid integer",
1273
"type": "type_error.integer",