Skip to content

Commit f6e61c1

Browse files
committed
chore: add tests for static_pk_value feature
1 parent 8296945 commit f6e61c1

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

tests/functional/idempotency/conftest.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ def persistence_store_compound(config):
215215
return DynamoDBPersistenceLayer(table_name=TABLE_NAME, boto_config=config, key_attr="id", sort_key_attr="sk")
216216

217217

218+
@pytest.fixture
219+
def persistence_store_compound_static_pk_value(config, static_pk_value):
220+
return DynamoDBPersistenceLayer(
221+
table_name=TABLE_NAME, boto_config=config, key_attr="id", sort_key_attr="sk", static_pk_value=static_pk_value
222+
)
223+
224+
218225
@pytest.fixture
219226
def idempotency_config(config, request, default_jmespath):
220227
return IdempotencyConfig(
@@ -246,3 +253,58 @@ def _func_echo_decoder(self, value):
246253
@pytest.fixture
247254
def mock_function():
248255
return mock.MagicMock()
256+
257+
258+
@pytest.fixture
259+
def static_pk_value():
260+
return "static-value"
261+
262+
263+
@pytest.fixture
264+
def expected_params_update_item_compound_key_static_pk_value(
265+
serialized_lambda_response, hashed_idempotency_key, static_pk_value
266+
):
267+
return {
268+
"ExpressionAttributeNames": {
269+
"#expiry": "expiration",
270+
"#response_data": "data",
271+
"#status": "status",
272+
},
273+
"ExpressionAttributeValues": {
274+
":expiry": {"N": stub.ANY},
275+
":response_data": {"S": serialized_lambda_response},
276+
":status": {"S": "COMPLETED"},
277+
},
278+
"Key": {"id": {"S": static_pk_value}, "sk": {"S": hashed_idempotency_key}},
279+
"TableName": "TEST_TABLE",
280+
"UpdateExpression": "SET #response_data = :response_data, " "#expiry = :expiry, #status = :status",
281+
}
282+
283+
284+
@pytest.fixture
285+
def expected_params_put_item_compound_key_static_pk_value(hashed_idempotency_key, static_pk_value):
286+
return {
287+
"ConditionExpression": (
288+
"attribute_not_exists(#id) OR #expiry < :now OR "
289+
"(#status = :inprogress AND attribute_exists(#in_progress_expiry) AND #in_progress_expiry < :now_in_millis)"
290+
),
291+
"ExpressionAttributeNames": {
292+
"#id": "id",
293+
"#expiry": "expiration",
294+
"#status": "status",
295+
"#in_progress_expiry": "in_progress_expiration",
296+
},
297+
"ExpressionAttributeValues": {
298+
":now": {"N": stub.ANY},
299+
":now_in_millis": {"N": stub.ANY},
300+
":inprogress": {"S": "INPROGRESS"},
301+
},
302+
"Item": {
303+
"expiration": {"N": stub.ANY},
304+
"in_progress_expiration": {"N": stub.ANY},
305+
"id": {"S": static_pk_value},
306+
"sk": {"S": hashed_idempotency_key},
307+
"status": {"S": "INPROGRESS"},
308+
},
309+
"TableName": "TEST_TABLE",
310+
}

tests/functional/idempotency/test_idempotency.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,3 +1504,34 @@ def lambda_handler(event, context):
15041504

15051505
stubber.assert_no_pending_responses()
15061506
stubber.deactivate()
1507+
1508+
1509+
@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": False}], indirect=True)
1510+
def test_idempotent_lambda_compound_static_pk_value_has_correct_pk(
1511+
idempotency_config: IdempotencyConfig,
1512+
persistence_store_compound_static_pk_value: DynamoDBPersistenceLayer,
1513+
lambda_apigw_event,
1514+
expected_params_put_item_compound_key_static_pk_value,
1515+
expected_params_update_item_compound_key_static_pk_value,
1516+
lambda_response,
1517+
lambda_context,
1518+
):
1519+
"""
1520+
Test idempotent decorator having a DynamoDBPersistenceLayer with a compound key and a static PK value
1521+
"""
1522+
1523+
stubber = stub.Stubber(persistence_store_compound_static_pk_value._client)
1524+
ddb_response = {}
1525+
1526+
stubber.add_response("put_item", ddb_response, expected_params_put_item_compound_key_static_pk_value)
1527+
stubber.add_response("update_item", ddb_response, expected_params_update_item_compound_key_static_pk_value)
1528+
stubber.activate()
1529+
1530+
@idempotent(config=idempotency_config, persistence_store=persistence_store_compound_static_pk_value)
1531+
def lambda_handler(event, context):
1532+
return lambda_response
1533+
1534+
lambda_handler(lambda_apigw_event, lambda_context)
1535+
1536+
stubber.assert_no_pending_responses()
1537+
stubber.deactivate()

0 commit comments

Comments
 (0)