You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/idempotency.md
+117
Original file line number
Diff line number
Diff line change
@@ -765,6 +765,123 @@ The idempotency utility can be used with the `validator` decorator. Ensure that
765
765
!!! tip "JMESPath Powertools functions are also available"
766
766
Built-in functions known in the validation utility like `powertools_json`, `powertools_base64`, `powertools_base64_gzip` are also available to use in this utility.
767
767
768
+
769
+
## Testing your code
770
+
771
+
The idempotency utility provides several routes to test your code.
772
+
773
+
### Disabling the idempotency utility
774
+
When testing your code, you may wish to disable the idempotency logic altogether and focus on testing your business logic. To do this, you can set the environment variable `POWERTOOLS_IDEMPOTENCY_DISABLED`
775
+
with a truthy value. If you prefer setting this for specific tests, and are using Pytest, you can use [monkeypatch](https://docs.pytest.org/en/latest/monkeypatch.html) fixture:
776
+
777
+
=== "tests.py"
778
+
779
+
```python hl_lines="2 3"
780
+
def test_idempotent_lambda_handler(monkeypatch):
781
+
# Set POWERTOOLS_IDEMPOTENCY_DISABLED before calling decorated functions
To test with [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html), you can replace the `Table` resource used by the persistence layer with one you create inside your tests. This allows you to set the endpoint_url.
809
+
810
+
=== "tests.py"
811
+
812
+
```python hl_lines="6 7 8"
813
+
import boto3
814
+
815
+
import app
816
+
817
+
def test_idempotent_lambda():
818
+
# Create our own Table resource using the endpoint for our DynamoDB Local instance
The idempotency utility lazily creates the dynamodb [Table](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#table) which it uses to access DynamoDB.
849
+
This means it is possible to pass a mocked Table resource, or stub various methods.
850
+
851
+
=== "tests.py"
852
+
853
+
```python hl_lines="6 7 8 9"
854
+
from unittest.mock import MagicMock
855
+
856
+
import app
857
+
858
+
def test_idempotent_lambda():
859
+
table = MagicMock()
860
+
app.persistence_layer.table = table
861
+
result = app.handler({'testkey': 'testvalue'}, {})
862
+
table.put_item.assert_called()
863
+
...
864
+
```
865
+
866
+
=== "app.py"
867
+
868
+
```python
869
+
from aws_lambda_powertools.utilities.idempotency import (
0 commit comments