-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_openapi_encoders.py
244 lines (158 loc) · 6 KB
/
test_openapi_encoders.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from __future__ import annotations
import math
from collections import deque
from dataclasses import dataclass
import pytest
from pydantic import BaseModel
from aws_lambda_powertools.event_handler.openapi.encoders import jsonable_encoder
from aws_lambda_powertools.event_handler.openapi.exceptions import SerializationError
def test_openapi_encode_include():
class User(BaseModel):
name: str
age: int
result = jsonable_encoder(User(name="John", age=20), include=["name"])
assert result == {"name": "John"}
def test_openapi_encode_exclude():
class User(BaseModel):
name: str
age: int
result = jsonable_encoder(User(name="John", age=20), exclude=["age"])
assert result == {"name": "John"}
def test_openapi_encode_pydantic():
class Order(BaseModel):
quantity: int
class User(BaseModel):
name: str
order: Order
result = jsonable_encoder(User(name="John", order=Order(quantity=2)))
assert result == {"name": "John", "order": {"quantity": 2}}
def test_openapi_encode_dataclass():
@dataclass
class Order:
quantity: int
@dataclass
class User:
name: str
order: Order
result = jsonable_encoder(User(name="John", order=Order(quantity=2)))
assert result == {"name": "John", "order": {"quantity": 2}}
def test_openapi_encode_enum():
from enum import Enum
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
result = jsonable_encoder(Color.RED)
assert result == "red"
def test_openapi_encode_purepath():
from pathlib import PurePath
result = jsonable_encoder(PurePath("/foo/bar"))
assert result == "/foo/bar"
def test_openapi_encode_scalars():
result = jsonable_encoder("foo")
assert result == "foo"
result = jsonable_encoder(1)
assert result == 1
result = jsonable_encoder(1.0)
assert math.isclose(result, 1.0)
result = jsonable_encoder(True)
assert result is True
result = jsonable_encoder(None)
assert result is None
def test_openapi_encode_dict():
result = jsonable_encoder({"foo": "bar"})
assert result == {"foo": "bar"}
def test_openapi_encode_dict_with_include():
result = jsonable_encoder({"foo": "bar", "bar": "foo"}, include=["foo"])
assert result == {"foo": "bar"}
def test_openapi_encode_dict_with_exclude():
result = jsonable_encoder({"foo": "bar", "bar": "foo"}, exclude=["bar"])
assert result == {"foo": "bar"}
def test_openapi_encode_sequences():
result = jsonable_encoder(["foo", "bar"])
assert result == ["foo", "bar"]
result = jsonable_encoder(("foo", "bar"))
assert result == ["foo", "bar"]
result = jsonable_encoder({"foo", "bar"})
assert set(result) == {"foo", "bar"}
result = jsonable_encoder(frozenset(("foo", "bar")))
assert set(result) == {"foo", "bar"}
def test_openapi_encode_bytes():
result = jsonable_encoder(b"foo")
assert result == "foo"
def test_openapi_encode_timedelta():
from datetime import timedelta
result = jsonable_encoder(timedelta(seconds=1))
assert result == 1
def test_openapi_encode_decimal():
from decimal import Decimal
result = jsonable_encoder(Decimal("1.0"))
assert math.isclose(result, 1.0)
result = jsonable_encoder(Decimal("1"))
assert result == 1
def test_openapi_encode_uuid():
from uuid import UUID
result = jsonable_encoder(UUID("123e4567-e89b-12d3-a456-426614174000"))
assert result == "123e4567-e89b-12d3-a456-426614174000"
def test_openapi_encode_encodable():
from datetime import date, datetime, time
result = jsonable_encoder(date(2021, 1, 1))
assert result == "2021-01-01"
result = jsonable_encoder(datetime(2021, 1, 1, 0, 0, 0))
assert result == "2021-01-01T00:00:00"
result = jsonable_encoder(time(0, 0, 0))
assert result == "00:00:00"
def test_openapi_encode_subclasses():
class MyCustomSubclass(deque):
pass
result = jsonable_encoder(MyCustomSubclass(["red"]))
assert result == ["red"]
def test_openapi_encode_other():
class User:
def __init__(self, name: str):
self.name = name
result = jsonable_encoder(User(name="John"))
assert result == {"name": "John"}
def test_openapi_encode_with_error():
class MyClass:
__slots__ = []
with pytest.raises(SerializationError, match="Unable to serialize the object*"):
jsonable_encoder(MyClass())
def test_openapi_encode_custom_serializer_nested_dict():
# GIVEN a nested dictionary with a custom class
class CustomClass: ...
nested_dict = {"a": {"b": CustomClass()}}
# AND a custom serializer
def serializer(value):
return "serialized"
# WHEN we call jsonable_encoder with the nested dictionary and unserializable value
result = jsonable_encoder(nested_dict, custom_serializer=serializer)
# THEN we should get the custom serializer output
assert result == {"a": {"b": "serialized"}}
def test_openapi_encode_custom_serializer_sequences():
# GIVEN a sequence with a custom class
class CustomClass:
__slots__ = []
seq = [CustomClass()]
# AND a custom serializer
def serializer(value):
return "serialized"
# WHEN we call jsonable_encoder with the nested dictionary and unserializable value
result = jsonable_encoder(seq, custom_serializer=serializer)
# THEN we should get the custom serializer output
assert result == ["serialized"]
def test_openapi_encode_custom_serializer_dataclasses():
# GIVEN a sequence with a custom class
class CustomClass:
__slots__ = []
@dataclass
class Order:
kind: CustomClass
order = Order(kind=CustomClass())
# AND a custom serializer
def serializer(value):
return "serialized"
# WHEN we call jsonable_encoder with the nested dictionary and unserializable value
result = jsonable_encoder(order, custom_serializer=serializer)
# THEN we should get the custom serializer output
assert result == {"kind": "serialized"}