|
30 | 30 | IdempotencyInconsistentStateError,
|
31 | 31 | IdempotencyInvalidStatusError,
|
32 | 32 | IdempotencyKeyError,
|
| 33 | + IdempotencyModelTypeError, |
| 34 | + IdempotencyNoSerializationModelError, |
33 | 35 | IdempotencyPersistenceLayerError,
|
34 | 36 | IdempotencyValidationError,
|
35 | 37 | )
|
@@ -1322,6 +1324,62 @@ def collect_payment(payment: PaymentInput) -> PaymentOutput:
|
1322 | 1324 | assert second_call.transaction_id == payment.transaction_id
|
1323 | 1325 |
|
1324 | 1326 |
|
| 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 | + |
1325 | 1383 | def test_idempotent_function_arbitrary_args_kwargs():
|
1326 | 1384 | # Scenario to validate we can use idempotent_function with a function
|
1327 | 1385 | # with an arbitrary number of args and kwargs
|
|
0 commit comments