File tree 1 file changed +9
-6
lines changed
tests/functional/event_handler
1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -733,10 +733,7 @@ def get_account(account_id: str):
733
733
734
734
735
735
def test_custom_serializer ():
736
- class Color (Enum ):
737
- RED = 1
738
- BLUE = 2
739
-
736
+ # GIVEN a custom serializer to handle enums and sets
740
737
class CustomEncoder (JSONEncoder ):
741
738
def default (self , data ):
742
739
if isinstance (data , Enum ):
@@ -746,23 +743,29 @@ def default(self, data):
746
743
except TypeError :
747
744
pass
748
745
else :
749
- return list (iterable )
746
+ return sorted (iterable )
750
747
return JSONEncoder .default (self , data )
751
748
752
749
def custom_serializer (data ) -> str :
753
750
return json .dumps (data , cls = CustomEncoder )
754
751
755
752
app = ApiGatewayResolver (serializer = custom_serializer )
756
753
754
+ class Color (Enum ):
755
+ RED = 1
756
+ BLUE = 2
757
+
757
758
@app .get ("/colors" )
758
759
def get_color () -> Dict :
759
760
return {
760
761
"color" : Color .RED ,
761
762
"variations" : {"light" , "dark" },
762
763
}
763
764
765
+ # WHEN calling handler
764
766
response = app ({"httpMethod" : "GET" , "path" : "/colors" }, None )
765
767
768
+ # THEN then use the custom serializer
766
769
body = response ["body" ]
767
- expected = '{"color": 1, "variations": ["light ", "dark "]}'
770
+ expected = '{"color": 1, "variations": ["dark ", "light "]}'
768
771
assert expected == body
You can’t perform that action at this time.
0 commit comments