|
1 | 1 | import copy
|
| 2 | +import json |
2 | 3 | import sys
|
| 4 | +from hashlib import md5 |
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 | from botocore import stub
|
|
8 | 10 | IdempotencyAlreadyInProgressError,
|
9 | 11 | IdempotencyInconsistentStateError,
|
10 | 12 | IdempotencyInvalidStatusError,
|
| 13 | + IdempotencyKeyError, |
11 | 14 | IdempotencyPersistenceLayerError,
|
12 | 15 | IdempotencyValidationError,
|
13 | 16 | )
|
14 | 17 | from aws_lambda_powertools.utilities.idempotency.idempotency import idempotent
|
15 |
| -from aws_lambda_powertools.utilities.idempotency.persistence.base import DataRecord |
| 18 | +from aws_lambda_powertools.utilities.idempotency.persistence.base import BasePersistenceLayer, DataRecord |
16 | 19 | from aws_lambda_powertools.utilities.validation import envelopes, validator
|
17 | 20 |
|
18 | 21 | TABLE_NAME = "TEST_TABLE"
|
@@ -638,3 +641,52 @@ def test_delete_from_cache_when_empty(persistence_store):
|
638 | 641 | except KeyError:
|
639 | 642 | # THEN we should not get a KeyError
|
640 | 643 | pytest.fail("KeyError should not happen")
|
| 644 | + |
| 645 | + |
| 646 | +def test_is_missing_idempotency_key(): |
| 647 | + # GIVEN None THEN is_missing_idempotency_key is True |
| 648 | + assert BasePersistenceLayer.is_missing_idempotency_key(None) |
| 649 | + # GIVEN a list of Nones THEN is_missing_idempotency_key is True |
| 650 | + assert BasePersistenceLayer.is_missing_idempotency_key([None, None]) |
| 651 | + # GIVEN a list of all not None THEN is_missing_idempotency_key is false |
| 652 | + assert BasePersistenceLayer.is_missing_idempotency_key([None, "Value"]) is False |
| 653 | + # GIVEN a str THEN is_missing_idempotency_key is false |
| 654 | + assert BasePersistenceLayer.is_missing_idempotency_key("Value") is False |
| 655 | + # GIVEN an empty tuple THEN is_missing_idempotency_key is false |
| 656 | + assert BasePersistenceLayer.is_missing_idempotency_key(()) |
| 657 | + # GIVEN an empty list THEN is_missing_idempotency_key is false |
| 658 | + assert BasePersistenceLayer.is_missing_idempotency_key([]) |
| 659 | + # GIVEN an empty dictionary THEN is_missing_idempotency_key is false |
| 660 | + assert BasePersistenceLayer.is_missing_idempotency_key({}) |
| 661 | + # GIVEN an empty str THEN is_missing_idempotency_key is false |
| 662 | + assert BasePersistenceLayer.is_missing_idempotency_key("") |
| 663 | + |
| 664 | + |
| 665 | +@pytest.mark.parametrize("persistence_store", [{"use_local_cache": False, "event_key_jmespath": "body"}], indirect=True) |
| 666 | +def test_default_no_raise_on_missing_idempotency_key(persistence_store): |
| 667 | + # GIVEN a persistence_store with use_local_cache = False and event_key_jmespath = "body" |
| 668 | + assert persistence_store.use_local_cache is False |
| 669 | + assert "body" in persistence_store.event_key_jmespath |
| 670 | + |
| 671 | + # WHEN getting the hashed idempotency key for an event with no `body` key |
| 672 | + hashed_key = persistence_store._get_hashed_idempotency_key({}) |
| 673 | + |
| 674 | + # THEN return the hash of None |
| 675 | + assert md5(json.dumps(None).encode()).hexdigest() == hashed_key |
| 676 | + |
| 677 | + |
| 678 | +@pytest.mark.parametrize( |
| 679 | + "persistence_store", [{"use_local_cache": False, "event_key_jmespath": "[body, x]"}], indirect=True |
| 680 | +) |
| 681 | +def test_raise_on_no_idempotency_key(persistence_store): |
| 682 | + # GIVEN a persistence_store with raise_on_no_idempotency_key and no idempotency key in the request |
| 683 | + persistence_store.raise_on_no_idempotency_key = True |
| 684 | + assert persistence_store.use_local_cache is False |
| 685 | + assert "body" in persistence_store.event_key_jmespath |
| 686 | + |
| 687 | + # WHEN getting the hashed idempotency key for an event with no `body` key |
| 688 | + with pytest.raises(IdempotencyKeyError) as excinfo: |
| 689 | + persistence_store._get_hashed_idempotency_key({}) |
| 690 | + |
| 691 | + # THEN raise IdempotencyKeyError error |
| 692 | + assert "No data found to create a hashed idempotency_key" in str(excinfo.value) |
0 commit comments