Skip to content

docs(idempotency): fixes to testing your code section #2073

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

Merged
18 changes: 10 additions & 8 deletions docs/utilities/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ with a truthy value. If you prefer setting this for specific tests, and are usin

### Testing with DynamoDB Local

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.
To test with [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html), you can replace the `DynamoDB client` used by the persistence layer with one you create inside your tests. This allows you to set the endpoint_url.

=== "tests.py"

Expand Down Expand Up @@ -1249,10 +1249,12 @@ To test with [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/
return LambdaContext()

def test_idempotent_lambda(lambda_context):
# Create our own Table resource using the endpoint for our DynamoDB Local instance
resource = boto3.resource("dynamodb", endpoint_url='http://localhost:8000')
table = resource.Table(app.persistence_layer.table_name)
app.persistence_layer.table = table
# Configure the boto3 to use the endpoint for the DynamoDB Local instance
dynamodb_local_client = boto3.client("dynamodb", endpoint_url='http://localhost:8000')
app.persistence_layer.client = dynamodb_local_client

# If desired, you can use a different DynamoDB Local table name than what your code already uses
# app.persistence_layer.table_name = "another table name"

result = app.handler({'testkey': 'testvalue'}, lambda_context)
assert result['payment_id'] == 12345
Expand Down Expand Up @@ -1310,10 +1312,10 @@ This means it is possible to pass a mocked Table resource, or stub various metho


def test_idempotent_lambda(lambda_context):
table = MagicMock()
app.persistence_layer.table = table
mock_client = MagicMock()
app.persistence_layer.client = mock_client
result = app.handler({'testkey': 'testvalue'}, lambda_context)
table.put_item.assert_called()
mock_client.put_item.assert_called()
...
```

Expand Down