Skip to content

Commit

Permalink
test(event-handler): fix apigw test
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brewer committed Jul 27, 2021
1 parent b2c4915 commit f5f4123
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,7 @@ def get_account(account_id: str):


def test_custom_serializer():
class Color(Enum):
RED = 1
BLUE = 2

# GIVEN a custom serializer to handle enums and sets
class CustomEncoder(JSONEncoder):
def default(self, data):
if isinstance(data, Enum):
Expand All @@ -746,23 +743,29 @@ def default(self, data):
except TypeError:
pass
else:
return list(iterable)
return sorted(iterable)
return JSONEncoder.default(self, data)

def custom_serializer(data) -> str:
return json.dumps(data, cls=CustomEncoder)

app = ApiGatewayResolver(serializer=custom_serializer)

class Color(Enum):
RED = 1
BLUE = 2

@app.get("/colors")
def get_color() -> Dict:
return {
"color": Color.RED,
"variations": {"light", "dark"},
}

# WHEN calling handler
response = app({"httpMethod": "GET", "path": "/colors"}, None)

# THEN then use the custom serializer
body = response["body"]
expected = '{"color": 1, "variations": ["light", "dark"]}'
expected = '{"color": 1, "variations": ["dark", "light"]}'
assert expected == body

0 comments on commit f5f4123

Please sign in to comment.