-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathtest_openapi_serialization.py
63 lines (44 loc) · 1.65 KB
/
test_openapi_serialization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
from typing import Dict
import pytest
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
def test_openapi_duplicated_serialization():
# GIVEN APIGatewayRestResolver is initialized with enable_validation=True
app = APIGatewayRestResolver(enable_validation=True)
# WHEN we have duplicated operations
@app.get("/")
def handler():
pass
@app.get("/")
def handler(): # noqa: F811
pass
# THEN we should get a warning
with pytest.warns(UserWarning, match="Duplicate Operation*"):
app.get_openapi_schema()
def test_openapi_serialize_json():
# GIVEN APIGatewayRestResolver is initialized with enable_validation=True
app = APIGatewayRestResolver(enable_validation=True)
@app.get("/")
def handler():
pass
# WHEN we serialize as json_schema
schema = json.loads(app.get_openapi_json_schema())
# THEN we should get a dictionary
assert isinstance(schema, Dict)
def test_openapi_serialize_other(gw_event):
# GIVEN a custom serializer
def serializer(_):
return "hello world"
# GIVEN APIGatewayRestResolver is initialized with enable_validation=True and the custom serializer
app = APIGatewayRestResolver(enable_validation=True, serializer=serializer)
# GIVEN a custom class
class CustomClass(object):
__slots__ = []
# GIVEN a handler that returns an instance of that class
@app.get("/my/path")
def handler():
return CustomClass()
# WHEN we invoke the handler
response = app(gw_event, {})
# THEN we the custom serializer should be used
assert response["body"] == "hello world"