-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathtest_idempotency_with_pydantic.py
267 lines (212 loc) · 9.89 KB
/
test_idempotency_with_pydantic.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from typing import Optional
import pytest
from pydantic import BaseModel
from aws_lambda_powertools.utilities.idempotency import (
IdempotencyConfig,
idempotent_function,
)
from aws_lambda_powertools.utilities.idempotency.base import (
_prepare_data,
)
from aws_lambda_powertools.utilities.idempotency.exceptions import (
IdempotencyModelTypeError,
IdempotencyNoSerializationModelError,
)
from aws_lambda_powertools.utilities.idempotency.persistence.base import (
BasePersistenceLayer,
DataRecord,
)
from aws_lambda_powertools.utilities.idempotency.serialization.pydantic import (
PydanticSerializer,
)
from tests.functional.idempotency.utils import (
hash_idempotency_key,
)
TESTS_MODULE_PREFIX = "test-func.tests.functional.idempotency._pydantic.test_idempotency_with_pydantic"
def get_dataclasses_lib():
"""Python 3.6 doesn't support dataclasses natively"""
import dataclasses
return dataclasses
class MockPersistenceLayer(BasePersistenceLayer):
def __init__(self, expected_idempotency_key: str):
self.expected_idempotency_key = expected_idempotency_key
super().__init__()
def _put_record(self, data_record: DataRecord) -> None:
assert data_record.idempotency_key == self.expected_idempotency_key
def _update_record(self, data_record: DataRecord) -> None:
assert data_record.idempotency_key == self.expected_idempotency_key
def _get_record(self, idempotency_key) -> DataRecord: ...
def _delete_record(self, data_record: DataRecord) -> None: ...
@pytest.mark.parametrize("output_serializer_type", ["explicit", "deduced"])
def test_idempotent_function_serialization_pydantic(output_serializer_type: str):
# GIVEN
config = IdempotencyConfig(use_local_cache=True)
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
idempotency_key = f"{TESTS_MODULE_PREFIX}.test_idempotent_function_serialization_pydantic.<locals>.collect_payment#{hash_idempotency_key(mock_event)}" # noqa E501
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
class PaymentInput(BaseModel):
customer_id: str
transaction_id: str
class PaymentOutput(BaseModel):
customer_id: str
transaction_id: str
if output_serializer_type == "explicit":
output_serializer = PydanticSerializer(
model=PaymentOutput,
)
else:
output_serializer = PydanticSerializer
@idempotent_function(
data_keyword_argument="payment",
persistence_store=persistence_layer,
config=config,
output_serializer=output_serializer,
)
def collect_payment(payment: PaymentInput) -> PaymentOutput:
return PaymentOutput(**payment.dict())
# WHEN
payment = PaymentInput(**mock_event)
first_call: PaymentOutput = collect_payment(payment=payment)
assert first_call.customer_id == payment.customer_id
assert first_call.transaction_id == payment.transaction_id
assert isinstance(first_call, PaymentOutput)
second_call: PaymentOutput = collect_payment(payment=payment)
assert isinstance(second_call, PaymentOutput)
assert second_call.customer_id == payment.customer_id
assert second_call.transaction_id == payment.transaction_id
def test_idempotent_function_serialization_pydantic_failure_no_return_type():
# GIVEN
config = IdempotencyConfig(use_local_cache=True)
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
idempotency_key = f"{TESTS_MODULE_PREFIX}.test_idempotent_function_serialization_pydantic_failure_no_return_type.<locals>.collect_payment#{hash_idempotency_key(mock_event)}" # noqa E501
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
class PaymentInput(BaseModel):
customer_id: str
transaction_id: str
class PaymentOutput(BaseModel):
customer_id: str
transaction_id: str
idempotent_function_decorator = idempotent_function(
data_keyword_argument="payment",
persistence_store=persistence_layer,
config=config,
output_serializer=PydanticSerializer,
)
with pytest.raises(IdempotencyNoSerializationModelError, match="No serialization model was supplied"):
@idempotent_function_decorator
def collect_payment(payment: PaymentInput):
return PaymentOutput(**payment.dict())
def test_idempotent_function_serialization_pydantic_failure_bad_type():
# GIVEN
config = IdempotencyConfig(use_local_cache=True)
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
idempotency_key = f"{TESTS_MODULE_PREFIX}.test_idempotent_function_serialization_pydantic_failure_no_return_type.<locals>.collect_payment#{hash_idempotency_key(mock_event)}" # noqa E501
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
class PaymentInput(BaseModel):
customer_id: str
transaction_id: str
class PaymentOutput(BaseModel):
customer_id: str
transaction_id: str
idempotent_function_decorator = idempotent_function(
data_keyword_argument="payment",
persistence_store=persistence_layer,
config=config,
output_serializer=PydanticSerializer,
)
with pytest.raises(IdempotencyModelTypeError, match="Model type is not inherited from pydantic BaseModel"):
@idempotent_function_decorator
def collect_payment(payment: PaymentInput) -> dict:
return PaymentOutput(**payment.dict())
def test_idempotent_function_serialization_dataclass_failure_bad_type():
# GIVEN
dataclasses = get_dataclasses_lib()
config = IdempotencyConfig(use_local_cache=True)
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
idempotency_key = f"{TESTS_MODULE_PREFIX}.test_idempotent_function_serialization_pydantic_failure_no_return_type.<locals>.collect_payment#{hash_idempotency_key(mock_event)}" # noqa E501
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
@dataclasses.dataclass
class PaymentInput:
customer_id: str
transaction_id: str
@dataclasses.dataclass
class PaymentOutput:
customer_id: str
transaction_id: str
idempotent_function_decorator = idempotent_function(
data_keyword_argument="payment",
persistence_store=persistence_layer,
config=config,
output_serializer=PydanticSerializer,
)
with pytest.raises(IdempotencyModelTypeError, match="Model type is not inherited from pydantic BaseModel"):
@idempotent_function_decorator
def collect_payment(payment: PaymentInput) -> dict:
return PaymentOutput(**payment.dict())
def test_idempotent_function_pydantic():
# Scenario _prepare_data should convert a pydantic to a dict
class Foo(BaseModel):
name: str
expected_result = {"name": "Bar"}
data = Foo(name="Bar")
as_dict = _prepare_data(data)
assert as_dict == data.dict()
assert as_dict == expected_result
@pytest.mark.parametrize("data", [None, "foo", ["foo"], 1, True, {}])
def test_idempotent_function_other(data):
# All other data types should be left as is
assert _prepare_data(data) == data
def test_idempotent_function_pydantic_with_jmespath():
# GIVEN
config = IdempotencyConfig(event_key_jmespath="transaction_id", use_local_cache=True)
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
idempotency_key = f"{TESTS_MODULE_PREFIX}.test_idempotent_function_pydantic_with_jmespath.<locals>.collect_payment#{hash_idempotency_key(mock_event['transaction_id'])}" # noqa E501
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
class Payment(BaseModel):
customer_id: str
transaction_id: str
@idempotent_function(data_keyword_argument="payment", persistence_store=persistence_layer, config=config)
def collect_payment(payment: Payment):
return payment.transaction_id
# WHEN
payment = Payment(**mock_event)
result = collect_payment(payment=payment)
# THEN idempotency key assertion happens at MockPersistenceLayer
assert result == payment.transaction_id
@pytest.mark.parametrize("output_serializer_type", ["explicit", "deduced"])
def test_idempotent_function_serialization_pydantic_with_optional_return(output_serializer_type: str):
# GIVEN
config = IdempotencyConfig(use_local_cache=True)
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
idempotency_key = f"{TESTS_MODULE_PREFIX}.test_idempotent_function_serialization_pydantic_with_optional_return.<locals>.collect_payment#{hash_idempotency_key(mock_event)}" # noqa E501
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
class PaymentInput(BaseModel):
customer_id: str
transaction_id: str
class PaymentOutput(BaseModel):
customer_id: str
transaction_id: str
if output_serializer_type == "explicit":
output_serializer = PydanticSerializer(
model=PaymentOutput,
)
else:
output_serializer = PydanticSerializer
@idempotent_function(
data_keyword_argument="payment",
persistence_store=persistence_layer,
config=config,
output_serializer=output_serializer,
)
def collect_payment(payment: PaymentInput) -> Optional[PaymentOutput]:
return PaymentOutput(**payment.dict())
# WHEN
payment = PaymentInput(**mock_event)
first_call: PaymentOutput = collect_payment(payment=payment)
assert first_call.customer_id == payment.customer_id
assert first_call.transaction_id == payment.transaction_id
assert isinstance(first_call, PaymentOutput)
second_call: PaymentOutput = collect_payment(payment=payment)
assert isinstance(second_call, PaymentOutput)
assert second_call.customer_id == payment.customer_id
assert second_call.transaction_id == payment.transaction_id