6
6
from flask_restful import Api
7
7
from parameterized import parameterized
8
8
9
- from flask_request_validator .error_formatter import demo_error_formatter
10
9
from flask_request_validator .rules import *
11
10
from flask_request_validator .validator import *
12
11
@@ -348,8 +347,12 @@ def test_default_value(self):
348
347
349
348
350
349
@_app2 .errorhandler (RequestError )
351
- def handler (e ):
352
- return flask .jsonify (demo_error_formatter (e )), 400
350
+ def handler (e : InvalidRequestError ):
351
+ if isinstance (e , InvalidRequestError ):
352
+ return str (e .to_dict ()), 400
353
+ if isinstance (e , AfterParamError ):
354
+ return str (e ), 400
355
+ raise e
353
356
354
357
355
358
@_app2 .route ('/' , methods = ['POST' ])
@@ -414,28 +417,16 @@ class TestNestedJson(TestCase):
414
417
],
415
418
}
416
419
},
417
- [
418
- {
419
- 'errors' : [
420
- {'keys' : {'description' : 'invalid length, min length = 5' ,
421
- 'status' : 'not allowed, allowed values: active|not_active' },
422
- 'path' : 'root|music|bands|details' },
423
- {'list_items' : {'0' : {'name' : 'invalid length, min length = 3' },
424
- '1' : {'name' : 'invalid length, min length = 3' }},
425
- 'path' : 'root|music|bands|persons' }, {
426
- 'keys' : {'description' : 'invalid length, min length = 5' ,
427
- 'status' : 'not allowed, allowed values: active|not_active' },
428
- 'path' : 'root|music|bands|details' },
429
- {'list_items' : {'0' : {'name' : 'invalid length, min length = 3' }},
430
- 'path' : 'root|music|bands|persons' },
431
- {'list_items' : {'0' : {'name' : 'invalid length, min length = 2' },
432
- '1' : {'name' : 'invalid length, min length = 2' }},
433
- 'path' : 'root|music|bands' }, {
434
- 'keys' : {'island' : 'value does not match pattern ^[a-z]{4,20}$' ,
435
- 'iso' : 'expected a datetime in ISO format' }, 'path' : 'root' }],
436
- 'message' : 'invalid JSON parameters'
437
- }
438
- ],
420
+ b"{'json': [JsonError(['root', 'music', 'bands', 'details'], {'description': "
421
+ b"RulesError(ValueMinLengthError(5)), 'status': RulesError(ValueEnumError(('active', 'not_active')))}, "
422
+ b"False), JsonError(['root', 'music', 'bands', 'persons'], {0: "
423
+ b"{'name': RulesError(ValueMinLengthError(3))}, 1: {'name': RulesError(ValueMinLengthError(3))}}, True), "
424
+ b"JsonError(['root', 'music', 'bands', 'details'], {'description': RulesError(ValueMinLengthError(5)), "
425
+ b"'status': RulesError(ValueEnumError(('active', 'not_active')))}, False), "
426
+ b"JsonError(['root', 'music', 'bands', 'persons'], {0: {'name': RulesError(ValueMinLengthError(3))}}, "
427
+ b"True), JsonError(['root', 'music', 'bands'], {0: {'name': RulesError(ValueMinLengthError(2))}, 1: "
428
+ b"{'name': RulesError(ValueMinLengthError(2))}}, True), JsonError(['root'], {'island': "
429
+ b"RulesError(ValuePatternError('^[a-z]{4,20}$')), 'iso': RulesError(ValueDtIsoFormatError())}, False)]}" ,
439
430
'400 BAD REQUEST' ,
440
431
),
441
432
# valid
@@ -477,91 +468,40 @@ class TestNestedJson(TestCase):
477
468
],
478
469
}
479
470
},
480
- {
481
- 'json' : {
482
- 'island' : 'valid' , 'iso' : '2021-01-02' , 'music' : {
483
- 'bands' : [
484
- {
485
- 'name' : 'Metallica' ,
486
- 'details' : {
487
- 'details' : 'Los Angeles, California, U.S.' ,
488
- 'description' : 'very long description' ,
489
- 'status' : 'active' ,
490
- },
491
- 'persons' : [
492
- {'name' : 'James Hetfield' },
493
- {'name' : 'Lars Ulrich' },
494
- {'name' : 'Kirk Hammett' },
495
- {'name' : 'Robert Trujillo' },
496
- ]
497
- },
498
- {
499
- 'name' : 'AC/DC' ,
500
- 'details' : {
501
- 'details' : 'Sydney, Australia' ,
502
- 'status' : 'active' ,
503
- 'description' : 'positive' ,
504
- },
505
- 'persons' : [
506
- {'name' : 'Angus Young' },
507
- {'name' : 'Stevie Young' },
508
- {'name' : 'Brian Johnson' },
509
- {'name' : 'Phil Rudd' },
510
- {'name' : 'Cliff Williams' },
511
- ],
512
- },
513
- ]
514
- }
515
- }
516
- },
471
+ b'{"json":{"island":"valid","iso":"2021-01-02","music":{"bands":[{"details":{"description":"very '
472
+ b'long description","details":"Los Angeles, California, U.S.","status":"active"},"name":"Metallica",'
473
+ b'"persons":[{"name":"James Hetfield"},{"name":"Lars Ulrich"},{"name":"Kirk Hammett"},'
474
+ b'{"name":"Robert Trujillo"}]},{"details":{"description":"positive","details":"Sydney, '
475
+ b'Australia","status":"active"},"name":"AC/DC","persons":[{"name":"Angus Young"},{"name":"Stevie Young"},'
476
+ b'{"name":"Brian Johnson"},{"name":"Phil Rudd"},{"name":"Cliff Williams"}]}]}}}\n ' ,
517
477
'200 OK'
518
478
),
519
479
])
520
480
def test_json_route_with_error_formatter (self , data , expected , status ):
521
481
with _app2 .test_client () as client :
522
482
response = client .post ('/' , data = json .dumps (data ), content_type = 'application/json' )
523
483
self .assertEqual (response .status , status )
524
- self .assertEqual (response .json , expected )
484
+ self .assertEqual (response .data , expected )
525
485
526
486
@parameterized .expand ([
527
487
(
528
488
[{'key' : 'testKey' , 'value' : 'testValue' }],
529
- [{
530
- 'errors' : [{'list_items' : {'0' : {'namespace' : 'key is required' }}, 'path' : 'root' }],
531
- 'message' : 'invalid JSON parameters' ,
532
- }],
489
+ b"{'json': [JsonError(['root'], {0: {'namespace': RulesError(MissingJsonKeyError('namespace'))}}, True)]}"
533
490
),
534
491
(
535
492
[{}, {'unknown_field' : 'value' }],
536
- [
537
- {
538
- 'errors' : [
539
- {
540
- 'list_items' : {
541
- '0' : {
542
- 'key' : 'key is required' ,
543
- 'namespace' : 'key is required' ,
544
- 'value' : 'key is required' ,
545
- },
546
- '1' : {
547
- 'key' : 'key is required' ,
548
- 'namespace' : 'key is required' ,
549
- 'value' : 'key is required' ,
550
- },
551
- },
552
- 'path' : 'root' ,
553
- },
554
- ],
555
- 'message' : 'invalid JSON parameters' ,
556
- },
557
- ],
493
+ b"{'json': [JsonError(['root'], {0: {'namespace': RulesError(MissingJsonKeyError('namespace')), "
494
+ b"'key': RulesError(MissingJsonKeyError('key')), 'value': RulesError(MissingJsonKeyError('value'))}, "
495
+ b"1: {'namespace': RulesError(MissingJsonKeyError('namespace')), "
496
+ b"'key': RulesError(MissingJsonKeyError('key')), "
497
+ b"'value': RulesError(MissingJsonKeyError('value'))}}, True)]}"
558
498
)
559
499
])
560
500
def test_issue_82_negative (self , data , expected ):
561
501
with _app2 .test_client () as client :
562
502
response = client .post ('/issue/82' , data = json .dumps (data ), content_type = 'application/json' )
563
503
self .assertEqual (response .status , '400 BAD REQUEST' )
564
- self .assertEqual (response .json , expected )
504
+ self .assertEqual (response .data , expected )
565
505
566
506
def test_issue_82_positive (self ):
567
507
with _app2 .test_client () as client :
@@ -616,20 +556,16 @@ class TestAfterParam(TestCase):
616
556
@parameterized .expand ([
617
557
(
618
558
['2021-01-01' , '2021-01-' ],
619
- [{'errors' : [
620
- {'list_items' : {'1' : 'expected a datetime in %Y-%m-%d format' },
621
- 'path' : 'root|dates' }], 'message' : 'invalid JSON parameters' }]
559
+ b"{'json': [JsonError(['root', 'dates'], {1: RulesError(ValueDatetimeError('%Y-%m-%d'))}, True)]}" ,
622
560
),
623
561
(
624
562
['2021' ],
625
- [{'errors' : [{'list_items' : {'0' : 'expected a datetime in %Y-%m-%d format' },
626
- 'path' : 'root|dates' }],
627
- 'message' : 'invalid JSON parameters' }]
563
+ b"{'json': [JsonError(['root', 'dates'], {0: RulesError(ValueDatetimeError('%Y-%m-%d'))}, True)]}" ,
628
564
),
629
565
# valid
630
566
(
631
567
['2021-01-01' , '2021-01-02' , '2021-01-03' , '2021-01-04' ],
632
- [ ' 2021-01-01' , ' 2021-01-02' , ' 2021-01-03' , ' 2021-01-04' ] ,
568
+ b'[" 2021-01-01"," 2021-01-02"," 2021-01-03"," 2021-01-04"] \n ' ,
633
569
),
634
570
])
635
571
def test_after_param_rules (self , dates , expected ):
@@ -638,28 +574,39 @@ def test_after_param_rules(self, dates, expected):
638
574
'/after_param' ,
639
575
data = json .dumps ({'dates' : dates }),
640
576
content_type = 'application/json' ,
641
- ). json
577
+ )
642
578
643
- self .assertEqual (result , expected )
579
+ self .assertEqual (result . data , expected )
644
580
645
581
@parameterized .expand ([
646
582
# valid
647
583
(
648
584
['2021-01-01' , '2021-01-02' , '2021-01-03' , '2021-01-04' ],
649
- ['2021-01-01' , '2021-01-02' , '2021-01-03' , '2021-01-04' ],
585
+ '200 OK' ,
586
+ b'["2021-01-01","2021-01-02","2021-01-03","2021-01-04"]\n ' ,
650
587
),
651
588
# invalid
652
- (['2021-01-01' , '2021-01-02' , '2021-01-03' , '2021-01-01' ], ['2021-01-01 < 2021-01-03' ]),
653
- (['2021-01-10' , '2021-01-01' , ], ['2021-01-01 < 2021-01-10' ]),
589
+ (
590
+ ['2021-01-01' , '2021-01-02' , '2021-01-03' , '2021-01-01' ],
591
+ '400 BAD REQUEST' ,
592
+ b'2021-01-01 < 2021-01-03' ,
593
+ ),
594
+ (
595
+ ['2021-01-10' , '2021-01-01' ],
596
+ '400 BAD REQUEST' ,
597
+ b'2021-01-01 < 2021-01-10' ,
598
+ ),
654
599
])
655
- def test_after_params (self , dates , expected ):
600
+ def test_after_params (self , dates , expected_status , expected ):
656
601
with _app2 .test_client () as client :
657
- data = client .post (
602
+ response = client .post (
658
603
'/after_param' ,
659
604
data = json .dumps ({'dates' : dates }),
660
605
content_type = 'application/json' ,
661
- ).json
662
- self .assertEqual (data , expected )
606
+ )
607
+
608
+ self .assertEqual (response .status , expected_status )
609
+ self .assertEqual (response .data , expected )
663
610
664
611
665
612
@_app2 .route ('/issue/83' , methods = ['POST' ])
0 commit comments