Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1f92c59

Browse files
leandrodamascenaheitorlessa
andauthoredApr 5, 2023
docs(idempotency): fixes to testing your code section (#2073)
Co-authored-by: heitorlessa <[email protected]>
1 parent 50ac176 commit 1f92c59

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed
 

‎docs/utilities/idempotency.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ with a truthy value. If you prefer setting this for specific tests, and are usin
12211221

12221222
### Testing with DynamoDB Local
12231223

1224-
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.
1224+
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.
12251225

12261226
=== "tests.py"
12271227

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

12511251
def test_idempotent_lambda(lambda_context):
1252-
# Create our own Table resource using the endpoint for our DynamoDB Local instance
1253-
resource = boto3.resource("dynamodb", endpoint_url='http://localhost:8000')
1254-
table = resource.Table(app.persistence_layer.table_name)
1255-
app.persistence_layer.table = table
1252+
# Configure the boto3 to use the endpoint for the DynamoDB Local instance
1253+
dynamodb_local_client = boto3.client("dynamodb", endpoint_url='http://localhost:8000')
1254+
app.persistence_layer.client = dynamodb_local_client
1255+
1256+
# If desired, you can use a different DynamoDB Local table name than what your code already uses
1257+
# app.persistence_layer.table_name = "another table name"
12561258

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

13111313

13121314
def test_idempotent_lambda(lambda_context):
1313-
table = MagicMock()
1314-
app.persistence_layer.table = table
1315+
mock_client = MagicMock()
1316+
app.persistence_layer.client = mock_client
13151317
result = app.handler({'testkey': 'testvalue'}, lambda_context)
1316-
table.put_item.assert_called()
1318+
mock_client.put_item.assert_called()
13171319
...
13181320
```
13191321

0 commit comments

Comments
 (0)
Please sign in to comment.