Skip to content

Commit 44c1943

Browse files
Adding more tests
1 parent 2515093 commit 44c1943

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

aws_lambda_powertools/utilities/idempotency/idempotency.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
1212
from aws_lambda_powertools.shared import constants
13+
from aws_lambda_powertools.shared.functions import strtobool
1314
from aws_lambda_powertools.shared.types import AnyCallableT
1415
from aws_lambda_powertools.utilities.idempotency.base import IdempotencyHandler
1516
from aws_lambda_powertools.utilities.idempotency.config import IdempotencyConfig
1617
from aws_lambda_powertools.utilities.idempotency.persistence.base import (
1718
BasePersistenceLayer,
1819
)
19-
from aws_lambda_powertools.shared.functions import strtobool
2020
from aws_lambda_powertools.utilities.idempotency.serialization.base import (
2121
BaseIdempotencyModelSerializer,
2222
BaseIdempotencySerializer,
@@ -67,7 +67,9 @@ def idempotent(
6767
>>> return {"StatusCode": 200}
6868
"""
6969

70-
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "0")):
70+
# Check if the IDEMPOTENCY_DISABLED_ENV environment variable is set to True
71+
# If IDEMPOTENCY_DISABLED_ENV is not set or set to any false value, the function will be idempotent by default
72+
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")):
7173
return handler(event, context, **kwargs)
7274

7375
config = config or IdempotencyConfig()
@@ -151,6 +153,8 @@ def process_order(customer_id: str, order: dict, **kwargs):
151153

152154
@functools.wraps(function)
153155
def decorate(*args, **kwargs):
156+
# Check if the IDEMPOTENCY_DISABLED_ENV environment variable is set to True
157+
# If IDEMPOTENCY_DISABLED_ENV is not set or set to any false value, the function will be idempotent by default
154158
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "0")):
155159
return function(*args, **kwargs)
156160

tests/functional/idempotency/test_idempotency.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -1667,13 +1667,19 @@ def dummy(payload):
16671667
dummy(payload=data_two)
16681668

16691669

1670-
def test_idempotency_disabled_envvar(monkeypatch, lambda_context, persistence_store: DynamoDBPersistenceLayer):
1670+
@pytest.mark.parametrize("idempotency_disabled_value", ["1", "y", "yes", "t", "true", "on"])
1671+
def test_idempotency_enabled_envvar(
1672+
monkeypatch,
1673+
lambda_context,
1674+
persistence_store: DynamoDBPersistenceLayer,
1675+
idempotency_disabled_value,
1676+
):
16711677
# Scenario to validate no requests sent to dynamodb table when 'POWERTOOLS_IDEMPOTENCY_DISABLED' is set
16721678
mock_event = {"data": "value"}
16731679

16741680
persistence_store.client = MagicMock()
16751681

1676-
monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", "1")
1682+
monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", str(idempotency_disabled_value))
16771683

16781684
@idempotent_function(data_keyword_argument="data", persistence_store=persistence_store)
16791685
def dummy(data):
@@ -1689,6 +1695,34 @@ def dummy_handler(event, context):
16891695
assert len(persistence_store.client.method_calls) == 0
16901696

16911697

1698+
@pytest.mark.parametrize("idempotency_disabled_value", ["0", "n", "no", "f", "false", "off"])
1699+
def test_idempotency_disabled_envvar(
1700+
monkeypatch,
1701+
lambda_context,
1702+
persistence_store: DynamoDBPersistenceLayer,
1703+
idempotency_disabled_value,
1704+
):
1705+
# Scenario to validate no requests sent to dynamodb table when 'POWERTOOLS_IDEMPOTENCY_DISABLED' is false
1706+
mock_event = {"data": "value"}
1707+
1708+
persistence_store.client = MagicMock()
1709+
1710+
monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", str(idempotency_disabled_value))
1711+
1712+
@idempotent_function(data_keyword_argument="data", persistence_store=persistence_store)
1713+
def dummy(data):
1714+
return {"message": "hello"}
1715+
1716+
@idempotent(persistence_store=persistence_store)
1717+
def dummy_handler(event, context):
1718+
return {"message": "hi"}
1719+
1720+
dummy(data=mock_event)
1721+
dummy_handler(mock_event, lambda_context)
1722+
1723+
assert len(persistence_store.client.method_calls) == 4
1724+
1725+
16921726
@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": True}], indirect=True)
16931727
def test_idempotent_function_duplicates(
16941728
idempotency_config: IdempotencyConfig,

0 commit comments

Comments
 (0)