@@ -663,3 +663,125 @@ class Family(Schema):
663
663
"daughter" : {"value" : {"age" : ["Missing data for required field." ]}}
664
664
}
665
665
}
666
+
667
+
668
+ class TestFieldPreAndPostLoad :
669
+ def test_field_pre_load (self ):
670
+ class UserSchema (Schema ):
671
+ name = fields .Str (pre_load = str )
672
+
673
+ schema = UserSchema ()
674
+ result = schema .load ({"name" : 808 })
675
+ assert result ["name" ] == "808"
676
+
677
+ def test_field_pre_load_multiple (self ):
678
+ def decrement (value ):
679
+ return value - 1
680
+
681
+ def add_prefix (value ):
682
+ return "test_" + value
683
+
684
+ class UserSchema (Schema ):
685
+ name = fields .Str (pre_load = [decrement , str , add_prefix ])
686
+
687
+ schema = UserSchema ()
688
+ result = schema .load ({"name" : 809 })
689
+ assert result ["name" ] == "test_808"
690
+
691
+ def test_field_post_load (self ):
692
+ class UserSchema (Schema ):
693
+ age = fields .Int (post_load = str )
694
+
695
+ schema = UserSchema ()
696
+ result = schema .load ({"age" : 42 })
697
+ assert result ["age" ] == "42"
698
+
699
+ def test_field_post_load_multiple (self ):
700
+ def to_string (value ):
701
+ return str (value )
702
+
703
+ def add_suffix (value ):
704
+ return value + " years"
705
+
706
+ class UserSchema (Schema ):
707
+ age = fields .Int (post_load = [to_string , add_suffix ])
708
+
709
+ schema = UserSchema ()
710
+ result = schema .load ({"age" : 42 })
711
+ assert result ["age" ] == "42 years"
712
+
713
+ def test_field_pre_and_post_load (self ):
714
+ def multiply_by_2 (value ):
715
+ return value * 2
716
+
717
+ class UserSchema (Schema ):
718
+ age = fields .Int (pre_load = [str .strip , int ], post_load = [multiply_by_2 ])
719
+
720
+ schema = UserSchema ()
721
+ result = schema .load ({"age" : " 21 " })
722
+ assert result ["age" ] == 42
723
+
724
+ def test_field_pre_load_validation_error (self ):
725
+ def always_fail (value ):
726
+ raise ValidationError ("oops" )
727
+
728
+ class UserSchema (Schema ):
729
+ age = fields .Int (pre_load = always_fail )
730
+
731
+ schema = UserSchema ()
732
+ with pytest .raises (ValidationError ) as exc :
733
+ schema .load ({"age" : 42 })
734
+ assert exc .value .messages == {"age" : ["oops" ]}
735
+
736
+ def test_field_post_load_validation_error (self ):
737
+ def always_fail (value ):
738
+ raise ValidationError ("oops" )
739
+
740
+ class UserSchema (Schema ):
741
+ age = fields .Int (post_load = always_fail )
742
+
743
+ schema = UserSchema ()
744
+ with pytest .raises (ValidationError ) as exc :
745
+ schema .load ({"age" : 42 })
746
+ assert exc .value .messages == {"age" : ["oops" ]}
747
+
748
+ def test_field_pre_load_none (self ):
749
+ def handle_none (value ):
750
+ if value is None :
751
+ return 0
752
+ return value
753
+
754
+ class UserSchema (Schema ):
755
+ age = fields .Int (pre_load = handle_none , allow_none = True )
756
+
757
+ schema = UserSchema ()
758
+ result = schema .load ({"age" : None })
759
+ assert result ["age" ] == 0
760
+
761
+ def test_field_post_load_not_called_with_none_input_when_not_allowed (self ):
762
+ def handle_none (value ):
763
+ if value is None :
764
+ return 0
765
+ return value
766
+
767
+ class UserSchema (Schema ):
768
+ age = fields .Int (post_load = handle_none , allow_none = False )
769
+
770
+ schema = UserSchema ()
771
+ with pytest .raises (ValidationError ) as exc :
772
+ schema .load ({"age" : None })
773
+ assert exc .value .messages == {"age" : ["Field may not be null." ]}
774
+
775
+ def test_invalid_type_passed_to_pre_load (self ):
776
+ with pytest .raises (
777
+ ValueError ,
778
+ match = "The 'pre_load' parameter must be a callable or an iterable of callables." ,
779
+ ):
780
+ fields .Int (pre_load = "not_callable" )
781
+
782
+ def test_invalid_type_passed_to_post_load (self ):
783
+ with pytest .raises (
784
+ ValueError ,
785
+ match = "The 'post_load' parameter must be a callable or an iterable of callables." ,
786
+ ):
787
+ fields .Int (post_load = "not_callable" )
0 commit comments