fastapi

Форк
0
/
test_path.py 
1277 строк · 36.7 Кб
1
from dirty_equals import IsDict
2
from fastapi.testclient import TestClient
3
from fastapi.utils import match_pydantic_error_url
4

5
from .main import app
6

7
client = TestClient(app)
8

9

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

15

16
def test_nonexistent():
17
    response = client.get("/nonexistent")
18
    assert response.status_code == 404, response.text
19
    assert response.json() == {"detail": "Not Found"}
20

21

22
def test_path_foobar():
23
    response = client.get("/path/foobar")
24
    assert response.status_code == 200
25
    assert response.json() == "foobar"
26

27

28
def test_path_str_foobar():
29
    response = client.get("/path/str/foobar")
30
    assert response.status_code == 200
31
    assert response.json() == "foobar"
32

33

34
def test_path_str_42():
35
    response = client.get("/path/str/42")
36
    assert response.status_code == 200
37
    assert response.json() == "42"
38

39

40
def test_path_str_True():
41
    response = client.get("/path/str/True")
42
    assert response.status_code == 200
43
    assert response.json() == "True"
44

45

46
def test_path_int_foobar():
47
    response = client.get("/path/int/foobar")
48
    assert response.status_code == 422
49
    assert response.json() == IsDict(
50
        {
51
            "detail": [
52
                {
53
                    "type": "int_parsing",
54
                    "loc": ["path", "item_id"],
55
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
56
                    "input": "foobar",
57
                    "url": match_pydantic_error_url("int_parsing"),
58
                }
59
            ]
60
        }
61
    ) | IsDict(
62
        # TODO: remove when deprecating Pydantic v1
63
        {
64
            "detail": [
65
                {
66
                    "loc": ["path", "item_id"],
67
                    "msg": "value is not a valid integer",
68
                    "type": "type_error.integer",
69
                }
70
            ]
71
        }
72
    )
73

74

75
def test_path_int_True():
76
    response = client.get("/path/int/True")
77
    assert response.status_code == 422
78
    assert response.json() == IsDict(
79
        {
80
            "detail": [
81
                {
82
                    "type": "int_parsing",
83
                    "loc": ["path", "item_id"],
84
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
85
                    "input": "True",
86
                    "url": match_pydantic_error_url("int_parsing"),
87
                }
88
            ]
89
        }
90
    ) | IsDict(
91
        # TODO: remove when deprecating Pydantic v1
92
        {
93
            "detail": [
94
                {
95
                    "loc": ["path", "item_id"],
96
                    "msg": "value is not a valid integer",
97
                    "type": "type_error.integer",
98
                }
99
            ]
100
        }
101
    )
102

103

104
def test_path_int_42():
105
    response = client.get("/path/int/42")
106
    assert response.status_code == 200
107
    assert response.json() == 42
108

109

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(
114
        {
115
            "detail": [
116
                {
117
                    "type": "int_parsing",
118
                    "loc": ["path", "item_id"],
119
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
120
                    "input": "42.5",
121
                    "url": match_pydantic_error_url("int_parsing"),
122
                }
123
            ]
124
        }
125
    ) | IsDict(
126
        # TODO: remove when deprecating Pydantic v1
127
        {
128
            "detail": [
129
                {
130
                    "loc": ["path", "item_id"],
131
                    "msg": "value is not a valid integer",
132
                    "type": "type_error.integer",
133
                }
134
            ]
135
        }
136
    )
137

138

139
def test_path_float_foobar():
140
    response = client.get("/path/float/foobar")
141
    assert response.status_code == 422
142
    assert response.json() == IsDict(
143
        {
144
            "detail": [
145
                {
146
                    "type": "float_parsing",
147
                    "loc": ["path", "item_id"],
148
                    "msg": "Input should be a valid number, unable to parse string as a number",
149
                    "input": "foobar",
150
                    "url": match_pydantic_error_url("float_parsing"),
151
                }
152
            ]
153
        }
154
    ) | IsDict(
155
        # TODO: remove when deprecating Pydantic v1
156
        {
157
            "detail": [
158
                {
159
                    "loc": ["path", "item_id"],
160
                    "msg": "value is not a valid float",
161
                    "type": "type_error.float",
162
                }
163
            ]
164
        }
165
    )
166

167

168
def test_path_float_True():
169
    response = client.get("/path/float/True")
170
    assert response.status_code == 422
171
    assert response.json() == IsDict(
172
        {
173
            "detail": [
174
                {
175
                    "type": "float_parsing",
176
                    "loc": ["path", "item_id"],
177
                    "msg": "Input should be a valid number, unable to parse string as a number",
178
                    "input": "True",
179
                    "url": match_pydantic_error_url("float_parsing"),
180
                }
181
            ]
182
        }
183
    ) | IsDict(
184
        # TODO: remove when deprecating Pydantic v1
185
        {
186
            "detail": [
187
                {
188
                    "loc": ["path", "item_id"],
189
                    "msg": "value is not a valid float",
190
                    "type": "type_error.float",
191
                }
192
            ]
193
        }
194
    )
195

196

197
def test_path_float_42():
198
    response = client.get("/path/float/42")
199
    assert response.status_code == 200
200
    assert response.json() == 42
201

202

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
207

208

209
def test_path_bool_foobar():
210
    response = client.get("/path/bool/foobar")
211
    assert response.status_code == 422
212
    assert response.json() == IsDict(
213
        {
214
            "detail": [
215
                {
216
                    "type": "bool_parsing",
217
                    "loc": ["path", "item_id"],
218
                    "msg": "Input should be a valid boolean, unable to interpret input",
219
                    "input": "foobar",
220
                    "url": match_pydantic_error_url("bool_parsing"),
221
                }
222
            ]
223
        }
224
    ) | IsDict(
225
        # TODO: remove when deprecating Pydantic v1
226
        {
227
            "detail": [
228
                {
229
                    "loc": ["path", "item_id"],
230
                    "msg": "value could not be parsed to a boolean",
231
                    "type": "type_error.bool",
232
                }
233
            ]
234
        }
235
    )
236

237

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
242

243

244
def test_path_bool_42():
245
    response = client.get("/path/bool/42")
246
    assert response.status_code == 422
247
    assert response.json() == IsDict(
248
        {
249
            "detail": [
250
                {
251
                    "type": "bool_parsing",
252
                    "loc": ["path", "item_id"],
253
                    "msg": "Input should be a valid boolean, unable to interpret input",
254
                    "input": "42",
255
                    "url": match_pydantic_error_url("bool_parsing"),
256
                }
257
            ]
258
        }
259
    ) | IsDict(
260
        # TODO: remove when deprecating Pydantic v1
261
        {
262
            "detail": [
263
                {
264
                    "loc": ["path", "item_id"],
265
                    "msg": "value could not be parsed to a boolean",
266
                    "type": "type_error.bool",
267
                }
268
            ]
269
        }
270
    )
271

272

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(
277
        {
278
            "detail": [
279
                {
280
                    "type": "bool_parsing",
281
                    "loc": ["path", "item_id"],
282
                    "msg": "Input should be a valid boolean, unable to interpret input",
283
                    "input": "42.5",
284
                    "url": match_pydantic_error_url("bool_parsing"),
285
                }
286
            ]
287
        }
288
    ) | IsDict(
289
        # TODO: remove when deprecating Pydantic v1
290
        {
291
            "detail": [
292
                {
293
                    "loc": ["path", "item_id"],
294
                    "msg": "value could not be parsed to a boolean",
295
                    "type": "type_error.bool",
296
                }
297
            ]
298
        }
299
    )
300

301

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
306

307

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
312

313

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
318

319

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
324

325

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
330

331

332
def test_path_param_foo():
333
    response = client.get("/path/param/foo")
334
    assert response.status_code == 200
335
    assert response.json() == "foo"
336

337

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"
342

343

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(
348
        {
349
            "detail": [
350
                {
351
                    "type": "string_too_short",
352
                    "loc": ["path", "item_id"],
353
                    "msg": "String should have at least 3 characters",
354
                    "input": "fo",
355
                    "ctx": {"min_length": 3},
356
                    "url": match_pydantic_error_url("string_too_short"),
357
                }
358
            ]
359
        }
360
    ) | IsDict(
361
        # TODO: remove when deprecating Pydantic v1
362
        {
363
            "detail": [
364
                {
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},
369
                }
370
            ]
371
        }
372
    )
373

374

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"
379

380

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(
385
        {
386
            "detail": [
387
                {
388
                    "type": "string_too_long",
389
                    "loc": ["path", "item_id"],
390
                    "msg": "String should have at most 3 characters",
391
                    "input": "foobar",
392
                    "ctx": {"max_length": 3},
393
                    "url": match_pydantic_error_url("string_too_long"),
394
                }
395
            ]
396
        }
397
    ) | IsDict(
398
        # TODO: remove when deprecating Pydantic v1
399
        {
400
            "detail": [
401
                {
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},
406
                }
407
            ]
408
        }
409
    )
410

411

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"
416

417

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(
422
        {
423
            "detail": [
424
                {
425
                    "type": "string_too_long",
426
                    "loc": ["path", "item_id"],
427
                    "msg": "String should have at most 3 characters",
428
                    "input": "foobar",
429
                    "ctx": {"max_length": 3},
430
                    "url": match_pydantic_error_url("string_too_long"),
431
                }
432
            ]
433
        }
434
    ) | IsDict(
435
        # TODO: remove when deprecating Pydantic v1
436
        {
437
            "detail": [
438
                {
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},
443
                }
444
            ]
445
        }
446
    )
447

448

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(
453
        {
454
            "detail": [
455
                {
456
                    "type": "string_too_short",
457
                    "loc": ["path", "item_id"],
458
                    "msg": "String should have at least 2 characters",
459
                    "input": "f",
460
                    "ctx": {"min_length": 2},
461
                    "url": match_pydantic_error_url("string_too_short"),
462
                }
463
            ]
464
        }
465
    ) | IsDict(
466
        {
467
            "detail": [
468
                {
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},
473
                }
474
            ]
475
        }
476
    )
477

478

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
483

484

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(
489
        {
490
            "detail": [
491
                {
492
                    "type": "greater_than",
493
                    "loc": ["path", "item_id"],
494
                    "msg": "Input should be greater than 3",
495
                    "input": "2",
496
                    "ctx": {"gt": 3.0},
497
                    "url": match_pydantic_error_url("greater_than"),
498
                }
499
            ]
500
        }
501
    ) | IsDict(
502
        # TODO: remove when deprecating Pydantic v1
503
        {
504
            "detail": [
505
                {
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},
510
                }
511
            ]
512
        }
513
    )
514

515

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
520

521

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(
526
        {
527
            "detail": [
528
                {
529
                    "type": "greater_than",
530
                    "loc": ["path", "item_id"],
531
                    "msg": "Input should be greater than 0",
532
                    "input": "0",
533
                    "ctx": {"gt": 0.0},
534
                    "url": match_pydantic_error_url("greater_than"),
535
                }
536
            ]
537
        }
538
    ) | IsDict(
539
        # TODO: remove when deprecating Pydantic v1
540
        {
541
            "detail": [
542
                {
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},
547
                }
548
            ]
549
        }
550
    )
551

552

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
557

558

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
563

564

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(
569
        {
570
            "detail": [
571
                {
572
                    "type": "greater_than_equal",
573
                    "loc": ["path", "item_id"],
574
                    "msg": "Input should be greater than or equal to 3",
575
                    "input": "2",
576
                    "ctx": {"ge": 3.0},
577
                    "url": match_pydantic_error_url("greater_than_equal"),
578
                }
579
            ]
580
        }
581
    ) | IsDict(
582
        # TODO: remove when deprecating Pydantic v1
583
        {
584
            "detail": [
585
                {
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},
590
                }
591
            ]
592
        }
593
    )
594

595

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(
600
        {
601
            "detail": [
602
                {
603
                    "type": "less_than",
604
                    "loc": ["path", "item_id"],
605
                    "msg": "Input should be less than 3",
606
                    "input": "42",
607
                    "ctx": {"lt": 3.0},
608
                    "url": match_pydantic_error_url("less_than"),
609
                }
610
            ]
611
        }
612
    ) | IsDict(
613
        # TODO: remove when deprecating Pydantic v1
614
        {
615
            "detail": [
616
                {
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},
621
                }
622
            ]
623
        }
624
    )
625

626

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
631

632

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
637

638

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(
643
        {
644
            "detail": [
645
                {
646
                    "type": "less_than",
647
                    "loc": ["path", "item_id"],
648
                    "msg": "Input should be less than 0",
649
                    "input": "0",
650
                    "ctx": {"lt": 0.0},
651
                    "url": match_pydantic_error_url("less_than"),
652
                }
653
            ]
654
        }
655
    ) | IsDict(
656
        # TODO: remove when deprecating Pydantic v1
657
        {
658
            "detail": [
659
                {
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},
664
                }
665
            ]
666
        }
667
    )
668

669

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(
674
        {
675
            "detail": [
676
                {
677
                    "type": "less_than_equal",
678
                    "loc": ["path", "item_id"],
679
                    "msg": "Input should be less than or equal to 3",
680
                    "input": "42",
681
                    "ctx": {"le": 3.0},
682
                    "url": match_pydantic_error_url("less_than_equal"),
683
                }
684
            ]
685
        }
686
    ) | IsDict(
687
        # TODO: remove when deprecating Pydantic v1
688
        {
689
            "detail": [
690
                {
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},
695
                }
696
            ]
697
        }
698
    )
699

700

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
705

706

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
711

712

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
717

718

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(
723
        {
724
            "detail": [
725
                {
726
                    "type": "less_than",
727
                    "loc": ["path", "item_id"],
728
                    "msg": "Input should be less than 3",
729
                    "input": "4",
730
                    "ctx": {"lt": 3.0},
731
                    "url": match_pydantic_error_url("less_than"),
732
                }
733
            ]
734
        }
735
    ) | IsDict(
736
        # TODO: remove when deprecating Pydantic v1
737
        {
738
            "detail": [
739
                {
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},
744
                }
745
            ]
746
        }
747
    )
748

749

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(
754
        {
755
            "detail": [
756
                {
757
                    "type": "greater_than",
758
                    "loc": ["path", "item_id"],
759
                    "msg": "Input should be greater than 1",
760
                    "input": "0",
761
                    "ctx": {"gt": 1.0},
762
                    "url": match_pydantic_error_url("greater_than"),
763
                }
764
            ]
765
        }
766
    ) | IsDict(
767
        # TODO: remove when deprecating Pydantic v1
768
        {
769
            "detail": [
770
                {
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},
775
                }
776
            ]
777
        }
778
    )
779

780

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
785

786

787
def test_path_param_le_ge_1():
788
    response = client.get("/path/param-le-ge/1")
789
    assert response.status_code == 200
790

791

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
796

797

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(
802
        {
803
            "detail": [
804
                {
805
                    "type": "less_than_equal",
806
                    "loc": ["path", "item_id"],
807
                    "msg": "Input should be less than or equal to 3",
808
                    "input": "4",
809
                    "ctx": {"le": 3.0},
810
                    "url": match_pydantic_error_url("less_than_equal"),
811
                }
812
            ]
813
        }
814
    ) | IsDict(
815
        # TODO: remove when deprecating Pydantic v1
816
        {
817
            "detail": [
818
                {
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},
823
                }
824
            ]
825
        }
826
    )
827

828

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
833

834

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(
839
        {
840
            "detail": [
841
                {
842
                    "type": "less_than",
843
                    "loc": ["path", "item_id"],
844
                    "msg": "Input should be less than 3",
845
                    "input": "42",
846
                    "ctx": {"lt": 3},
847
                    "url": match_pydantic_error_url("less_than"),
848
                }
849
            ]
850
        }
851
    ) | IsDict(
852
        # TODO: remove when deprecating Pydantic v1
853
        {
854
            "detail": [
855
                {
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},
860
                }
861
            ]
862
        }
863
    )
864

865

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(
870
        {
871
            "detail": [
872
                {
873
                    "type": "int_parsing",
874
                    "loc": ["path", "item_id"],
875
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
876
                    "input": "2.7",
877
                    "url": match_pydantic_error_url("int_parsing"),
878
                }
879
            ]
880
        }
881
    ) | IsDict(
882
        # TODO: remove when deprecating Pydantic v1
883
        {
884
            "detail": [
885
                {
886
                    "loc": ["path", "item_id"],
887
                    "msg": "value is not a valid integer",
888
                    "type": "type_error.integer",
889
                }
890
            ]
891
        }
892
    )
893

894

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
899

900

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(
905
        {
906
            "detail": [
907
                {
908
                    "type": "greater_than",
909
                    "loc": ["path", "item_id"],
910
                    "msg": "Input should be greater than 3",
911
                    "input": "2",
912
                    "ctx": {"gt": 3},
913
                    "url": match_pydantic_error_url("greater_than"),
914
                }
915
            ]
916
        }
917
    ) | IsDict(
918
        # TODO: remove when deprecating Pydantic v1
919
        {
920
            "detail": [
921
                {
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},
926
                }
927
            ]
928
        }
929
    )
930

931

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(
936
        {
937
            "detail": [
938
                {
939
                    "type": "int_parsing",
940
                    "loc": ["path", "item_id"],
941
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
942
                    "input": "2.7",
943
                    "url": match_pydantic_error_url("int_parsing"),
944
                }
945
            ]
946
        }
947
    ) | IsDict(
948
        # TODO: remove when deprecating Pydantic v1
949
        {
950
            "detail": [
951
                {
952
                    "loc": ["path", "item_id"],
953
                    "msg": "value is not a valid integer",
954
                    "type": "type_error.integer",
955
                }
956
            ]
957
        }
958
    )
959

960

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(
965
        {
966
            "detail": [
967
                {
968
                    "type": "less_than_equal",
969
                    "loc": ["path", "item_id"],
970
                    "msg": "Input should be less than or equal to 3",
971
                    "input": "42",
972
                    "ctx": {"le": 3},
973
                    "url": match_pydantic_error_url("less_than_equal"),
974
                }
975
            ]
976
        }
977
    ) | IsDict(
978
        # TODO: remove when deprecating Pydantic v1
979
        {
980
            "detail": [
981
                {
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},
986
                }
987
            ]
988
        }
989
    )
990

991

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
996

997

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
1002

1003

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(
1008
        {
1009
            "detail": [
1010
                {
1011
                    "type": "int_parsing",
1012
                    "loc": ["path", "item_id"],
1013
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
1014
                    "input": "2.7",
1015
                    "url": match_pydantic_error_url("int_parsing"),
1016
                }
1017
            ]
1018
        }
1019
    ) | IsDict(
1020
        # TODO: remove when deprecating Pydantic v1
1021
        {
1022
            "detail": [
1023
                {
1024
                    "loc": ["path", "item_id"],
1025
                    "msg": "value is not a valid integer",
1026
                    "type": "type_error.integer",
1027
                }
1028
            ]
1029
        }
1030
    )
1031

1032

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
1037

1038

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
1043

1044

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(
1049
        {
1050
            "detail": [
1051
                {
1052
                    "type": "greater_than_equal",
1053
                    "loc": ["path", "item_id"],
1054
                    "msg": "Input should be greater than or equal to 3",
1055
                    "input": "2",
1056
                    "ctx": {"ge": 3},
1057
                    "url": match_pydantic_error_url("greater_than_equal"),
1058
                }
1059
            ]
1060
        }
1061
    ) | IsDict(
1062
        # TODO: remove when deprecating Pydantic v1
1063
        {
1064
            "detail": [
1065
                {
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},
1070
                }
1071
            ]
1072
        }
1073
    )
1074

1075

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(
1080
        {
1081
            "detail": [
1082
                {
1083
                    "type": "int_parsing",
1084
                    "loc": ["path", "item_id"],
1085
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
1086
                    "input": "2.7",
1087
                    "url": match_pydantic_error_url("int_parsing"),
1088
                }
1089
            ]
1090
        }
1091
    ) | IsDict(
1092
        # TODO: remove when deprecating Pydantic v1
1093
        {
1094
            "detail": [
1095
                {
1096
                    "loc": ["path", "item_id"],
1097
                    "msg": "value is not a valid integer",
1098
                    "type": "type_error.integer",
1099
                }
1100
            ]
1101
        }
1102
    )
1103

1104

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
1109

1110

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(
1115
        {
1116
            "detail": [
1117
                {
1118
                    "type": "less_than",
1119
                    "loc": ["path", "item_id"],
1120
                    "msg": "Input should be less than 3",
1121
                    "input": "4",
1122
                    "ctx": {"lt": 3},
1123
                    "url": match_pydantic_error_url("less_than"),
1124
                }
1125
            ]
1126
        }
1127
    ) | IsDict(
1128
        # TODO: remove when deprecating Pydantic v1
1129
        {
1130
            "detail": [
1131
                {
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},
1136
                }
1137
            ]
1138
        }
1139
    )
1140

1141

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(
1146
        {
1147
            "detail": [
1148
                {
1149
                    "type": "greater_than",
1150
                    "loc": ["path", "item_id"],
1151
                    "msg": "Input should be greater than 1",
1152
                    "input": "0",
1153
                    "ctx": {"gt": 1},
1154
                    "url": match_pydantic_error_url("greater_than"),
1155
                }
1156
            ]
1157
        }
1158
    ) | IsDict(
1159
        # TODO: remove when deprecating Pydantic v1
1160
        {
1161
            "detail": [
1162
                {
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},
1167
                }
1168
            ]
1169
        }
1170
    )
1171

1172

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(
1177
        {
1178
            "detail": [
1179
                {
1180
                    "type": "int_parsing",
1181
                    "loc": ["path", "item_id"],
1182
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
1183
                    "input": "2.7",
1184
                    "url": match_pydantic_error_url("int_parsing"),
1185
                }
1186
            ]
1187
        }
1188
    ) | IsDict(
1189
        # TODO: remove when deprecating Pydantic v1
1190
        {
1191
            "detail": [
1192
                {
1193
                    "loc": ["path", "item_id"],
1194
                    "msg": "value is not a valid integer",
1195
                    "type": "type_error.integer",
1196
                }
1197
            ]
1198
        }
1199
    )
1200

1201

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
1206

1207

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
1212

1213

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
1218

1219

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(
1224
        {
1225
            "detail": [
1226
                {
1227
                    "type": "less_than_equal",
1228
                    "loc": ["path", "item_id"],
1229
                    "msg": "Input should be less than or equal to 3",
1230
                    "input": "4",
1231
                    "ctx": {"le": 3},
1232
                    "url": match_pydantic_error_url("less_than_equal"),
1233
                }
1234
            ]
1235
        }
1236
    ) | IsDict(
1237
        # TODO: remove when deprecating Pydantic v1
1238
        {
1239
            "detail": [
1240
                {
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},
1245
                }
1246
            ]
1247
        }
1248
    )
1249

1250

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(
1255
        {
1256
            "detail": [
1257
                {
1258
                    "type": "int_parsing",
1259
                    "loc": ["path", "item_id"],
1260
                    "msg": "Input should be a valid integer, unable to parse string as an integer",
1261
                    "input": "2.7",
1262
                    "url": match_pydantic_error_url("int_parsing"),
1263
                }
1264
            ]
1265
        }
1266
    ) | IsDict(
1267
        # TODO: remove when deprecating Pydantic v1
1268
        {
1269
            "detail": [
1270
                {
1271
                    "loc": ["path", "item_id"],
1272
                    "msg": "value is not a valid integer",
1273
                    "type": "type_error.integer",
1274
                }
1275
            ]
1276
        }
1277
    )
1278

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.