Skip to content

Commit 9f08bf0

Browse files
committed
add negative tests
1 parent fc38f63 commit 9f08bf0

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/functional/idempotency/test_idempotency.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
IdempotencyInconsistentStateError,
3131
IdempotencyInvalidStatusError,
3232
IdempotencyKeyError,
33+
IdempotencyModelTypeError,
34+
IdempotencyNoSerializationModelError,
3335
IdempotencyPersistenceLayerError,
3436
IdempotencyValidationError,
3537
)
@@ -1322,6 +1324,62 @@ def collect_payment(payment: PaymentInput) -> PaymentOutput:
13221324
assert second_call.transaction_id == payment.transaction_id
13231325

13241326

1327+
def test_idempotent_function_serialization_pydantic_failure_no_return_type():
1328+
# GIVEN
1329+
config = IdempotencyConfig(use_local_cache=True)
1330+
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
1331+
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
1332+
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
1333+
1334+
class PaymentInput(BaseModel):
1335+
customer_id: str
1336+
transaction_id: str
1337+
1338+
class PaymentOutput(BaseModel):
1339+
customer_id: str
1340+
transaction_id: str
1341+
1342+
idempotent_function_decorator = idempotent_function(
1343+
data_keyword_argument="payment",
1344+
persistence_store=persistence_layer,
1345+
config=config,
1346+
output_serializer=PydanticSerializer,
1347+
)
1348+
with pytest.raises(IdempotencyNoSerializationModelError, match="No serialization model was supplied"):
1349+
1350+
@idempotent_function_decorator
1351+
def collect_payment(payment: PaymentInput):
1352+
return PaymentOutput(**payment.dict())
1353+
1354+
1355+
def test_idempotent_function_serialization_pydantic_failure_bad_type():
1356+
# GIVEN
1357+
config = IdempotencyConfig(use_local_cache=True)
1358+
mock_event = {"customer_id": "fake", "transaction_id": "fake-id"}
1359+
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
1360+
persistence_layer = MockPersistenceLayer(expected_idempotency_key=idempotency_key)
1361+
1362+
class PaymentInput(BaseModel):
1363+
customer_id: str
1364+
transaction_id: str
1365+
1366+
class PaymentOutput(BaseModel):
1367+
customer_id: str
1368+
transaction_id: str
1369+
1370+
idempotent_function_decorator = idempotent_function(
1371+
data_keyword_argument="payment",
1372+
persistence_store=persistence_layer,
1373+
config=config,
1374+
output_serializer=PydanticSerializer,
1375+
)
1376+
with pytest.raises(IdempotencyModelTypeError, match="Model type is not inherited from pydantic BaseModel"):
1377+
1378+
@idempotent_function_decorator
1379+
def collect_payment(payment: PaymentInput) -> dict:
1380+
return PaymentOutput(**payment.dict())
1381+
1382+
13251383
def test_idempotent_function_arbitrary_args_kwargs():
13261384
# Scenario to validate we can use idempotent_function with a function
13271385
# with an arbitrary number of args and kwargs

0 commit comments

Comments
 (0)