|
22 | 22 | from aws_lambda_powertools.utilities.idempotency.idempotency import idempotent, idempotent_function
|
23 | 23 | from aws_lambda_powertools.utilities.idempotency.persistence.base import BasePersistenceLayer, DataRecord
|
24 | 24 | from aws_lambda_powertools.utilities.validation import envelopes, validator
|
25 |
| -from tests.functional.utils import hash_idempotency_key, json_serialize, load_event |
| 25 | +from tests.functional.idempotency.utils import ( |
| 26 | + build_idempotency_put_item_stub, |
| 27 | + build_idempotency_update_item_stub, |
| 28 | + hash_idempotency_key, |
| 29 | +) |
| 30 | +from tests.functional.utils import json_serialize, load_event |
26 | 31 |
|
27 | 32 | TABLE_NAME = "TEST_TABLE"
|
28 | 33 |
|
@@ -275,6 +280,40 @@ def lambda_handler(event, context):
|
275 | 280 | stubber.deactivate()
|
276 | 281 |
|
277 | 282 |
|
| 283 | +@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": True, "event_key_jmespath": "body"}], indirect=True) |
| 284 | +def test_idempotent_lambda_first_execution_event_mutation( |
| 285 | + idempotency_config: IdempotencyConfig, |
| 286 | + persistence_store: DynamoDBPersistenceLayer, |
| 287 | + lambda_apigw_event, |
| 288 | + lambda_response, |
| 289 | + lambda_context, |
| 290 | +): |
| 291 | + """ |
| 292 | + Test idempotent decorator where lambda_handler mutates the event. |
| 293 | + Ensures we're passing data by value, not reference. |
| 294 | + """ |
| 295 | + event = copy.deepcopy(lambda_apigw_event) |
| 296 | + stubber = stub.Stubber(persistence_store.table.meta.client) |
| 297 | + ddb_response = {} |
| 298 | + stubber.add_response("put_item", ddb_response, build_idempotency_put_item_stub(data=event["body"])) |
| 299 | + stubber.add_response( |
| 300 | + "update_item", |
| 301 | + ddb_response, |
| 302 | + build_idempotency_update_item_stub(data=event["body"], handler_response=lambda_response), |
| 303 | + ) |
| 304 | + stubber.activate() |
| 305 | + |
| 306 | + @idempotent(config=idempotency_config, persistence_store=persistence_store) |
| 307 | + def lambda_handler(event, context): |
| 308 | + event.pop("body") # remove exact key we're using for idempotency |
| 309 | + return lambda_response |
| 310 | + |
| 311 | + lambda_handler(event, lambda_context) |
| 312 | + |
| 313 | + stubber.assert_no_pending_responses() |
| 314 | + stubber.deactivate() |
| 315 | + |
| 316 | + |
278 | 317 | @pytest.mark.parametrize("idempotency_config", [{"use_local_cache": False}, {"use_local_cache": True}], indirect=True)
|
279 | 318 | def test_idempotent_lambda_expired(
|
280 | 319 | idempotency_config: IdempotencyConfig,
|
|
0 commit comments