Skip to content

fix(idempotency): static pk is overwritten by hashed_idempotency_key #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def _get_record(self, idempotency_key) -> DataRecord:
def _put_record(self, data_record: DataRecord) -> None:
item = {
**self._get_key(data_record.idempotency_key),
self.key_attr: {"S": data_record.idempotency_key},
self.expiry_attr: {"N": str(data_record.expiry_timestamp)},
self.status_attr: {"S": data_record.status},
}
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/idempotency/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ def expected_params_update_item(serialized_lambda_response, hashed_idempotency_k
}


@pytest.fixture
def expected_params_update_item_compound_key_static_pk_value(
expected_params_update_item, hashed_idempotency_key, static_pk_value
):
return {
**expected_params_update_item,
"Key": {"id": {"S": static_pk_value}, "sk": {"S": hashed_idempotency_key}},
}


@pytest.fixture
def expected_params_update_item_with_validation(
serialized_lambda_response, hashed_idempotency_key, hashed_validation_key
Expand Down Expand Up @@ -150,6 +160,22 @@ def expected_params_put_item(hashed_idempotency_key):
}


@pytest.fixture
def expected_params_put_item_compound_key_static_pk_value(
expected_params_put_item, hashed_idempotency_key, static_pk_value
):
return {
**expected_params_put_item,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! I want to completely overhaul the way functional tests are written - get rid of fixtures and use builders for all of this error-prone boilerplate in stubs.

Migrating this over to the other PR.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

"Item": {
"expiration": {"N": stub.ANY},
"in_progress_expiration": {"N": stub.ANY},
"id": {"S": static_pk_value},
"sk": {"S": hashed_idempotency_key},
"status": {"S": "INPROGRESS"},
},
}


@pytest.fixture
def expected_params_put_item_with_validation(hashed_idempotency_key, hashed_validation_key):
return {
Expand Down Expand Up @@ -200,6 +226,11 @@ def hashed_idempotency_key_with_envelope(request, lambda_apigw_event):
)


@pytest.fixture
def static_pk_value():
return "static-value"


@pytest.fixture
def hashed_validation_key(lambda_apigw_event):
return hash_idempotency_key(lambda_apigw_event["requestContext"])
Expand All @@ -215,6 +246,13 @@ def persistence_store_compound(config):
return DynamoDBPersistenceLayer(table_name=TABLE_NAME, boto_config=config, key_attr="id", sort_key_attr="sk")


@pytest.fixture
def persistence_store_compound_static_pk_value(config, static_pk_value):
return DynamoDBPersistenceLayer(
table_name=TABLE_NAME, boto_config=config, key_attr="id", sort_key_attr="sk", static_pk_value=static_pk_value
)


@pytest.fixture
def idempotency_config(config, request, default_jmespath):
return IdempotencyConfig(
Expand Down
31 changes: 31 additions & 0 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,3 +1504,34 @@ def lambda_handler(event, context):

stubber.assert_no_pending_responses()
stubber.deactivate()


@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": False}], indirect=True)
def test_idempotent_lambda_compound_static_pk_value_has_correct_pk(
idempotency_config: IdempotencyConfig,
persistence_store_compound_static_pk_value: DynamoDBPersistenceLayer,
lambda_apigw_event,
expected_params_put_item_compound_key_static_pk_value,
expected_params_update_item_compound_key_static_pk_value,
lambda_response,
lambda_context,
):
"""
Test idempotent decorator having a DynamoDBPersistenceLayer with a compound key and a static PK value
"""

stubber = stub.Stubber(persistence_store_compound_static_pk_value._client)
ddb_response = {}

stubber.add_response("put_item", ddb_response, expected_params_put_item_compound_key_static_pk_value)
stubber.add_response("update_item", ddb_response, expected_params_update_item_compound_key_static_pk_value)
stubber.activate()

@idempotent(config=idempotency_config, persistence_store=persistence_store_compound_static_pk_value)
def lambda_handler(event, context):
return lambda_response

lambda_handler(lambda_apigw_event, lambda_context)

stubber.assert_no_pending_responses()
stubber.deactivate()