Skip to content

Commit

Permalink
fix DDB eventName for TransactWriteItems Update inserting data (#10685)
Browse files Browse the repository at this point in the history
  • Loading branch information
bentsku committed Apr 17, 2024
1 parent 90dac79 commit 01ac739
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
4 changes: 2 additions & 2 deletions localstack/services/dynamodb/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,10 +1807,10 @@ def prepare_transact_write_item_records(
record["dynamodb"]["StreamViewType"] = stream_type.stream_view_type

record["eventID"] = short_uid()
record["eventName"] = "MODIFY" if updated_item else "INSERT"
record["eventName"] = "MODIFY" if existing_item else "INSERT"
record["dynamodb"]["Keys"] = keys

if stream_type.needs_old_image:
if existing_item and stream_type.needs_old_image:
record["dynamodb"]["OldImage"] = existing_item
if stream_type.needs_new_image:
record["dynamodb"]["NewImage"] = updated_item
Expand Down
20 changes: 19 additions & 1 deletion tests/aws/services/dynamodb/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,23 @@ def test_transact_write_items_streaming(
)
snapshot.match("transact-write-response-update", response)

# use Update to write a new key
response = aws_client.dynamodb.transact_write_items(
TransactItems=[
{
"Update": {
"TableName": table_name,
"Key": {"id": {"S": "NonExistentKey"}},
"UpdateExpression": "SET attr1 = :v1",
"ExpressionAttributeValues": {
":v1": {"S": "value1"},
},
}
},
]
)
snapshot.match("transact-write-update-insert", response)

# delete the key
response = aws_client.dynamodb.transact_write_items(
TransactItems=[
Expand All @@ -2036,6 +2053,7 @@ def test_transact_write_items_streaming(
# - PutItem
# - TransactWriteItem on NewKey insert
# - TransactWriteItem on NewKey update
# - TransactWriteItem on NonExistentKey insert
# - TransactWriteItem on NewKey delete
# - TransactWriteItem on Fred modify via Put
# don't send an event when Fred is overwritten with the same value
Expand All @@ -2052,7 +2070,7 @@ def _get_records_amount(record_amount: int):

assert len(records) >= record_amount

retry(lambda: _get_records_amount(5), sleep=1, retries=3)
retry(lambda: _get_records_amount(6), sleep=1, retries=3)
snapshot.match("get-records", {"Records": records})

@markers.aws.validated
Expand Down
42 changes: 37 additions & 5 deletions tests/aws/services/dynamodb/test_dynamodb.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@
}
},
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_transact_write_items_streaming": {
"recorded-date": "15-03-2024, 01:54:32",
"recorded-date": "17-04-2024, 22:45:49",
"recorded-content": {
"create-table": {
"TableDescription": {
Expand Down Expand Up @@ -944,6 +944,12 @@
"HTTPStatusCode": 200
}
},
"transact-write-update-insert": {
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"transact-write-response-delete": {
"ResponseMetadata": {
"HTTPHeaders": {},
Expand Down Expand Up @@ -1032,6 +1038,32 @@
"eventSource": "aws:dynamodb",
"eventVersion": "1.1"
},
{
"awsRegion": "<region>",
"dynamodb": {
"ApproximateCreationDateTime": "datetime",
"Keys": {
"id": {
"S": "NonExistentKey"
}
},
"NewImage": {
"attr1": {
"S": "value1"
},
"id": {
"S": "NonExistentKey"
}
},
"SequenceNumber": "<sequence-number:4>",
"SizeBytes": 43,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventID": "<event-i-d:4>",
"eventName": "INSERT",
"eventSource": "aws:dynamodb",
"eventVersion": "1.1"
},
{
"awsRegion": "<region>",
"dynamodb": {
Expand All @@ -1052,11 +1084,11 @@
"S": "NewKey"
}
},
"SequenceNumber": "<sequence-number:4>",
"SequenceNumber": "<sequence-number:5>",
"SizeBytes": 38,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventID": "<event-i-d:4>",
"eventID": "<event-i-d:5>",
"eventName": "REMOVE",
"eventSource": "aws:dynamodb",
"eventVersion": "1.1"
Expand All @@ -1083,11 +1115,11 @@
"S": "Fred"
}
},
"SequenceNumber": "<sequence-number:5>",
"SequenceNumber": "<sequence-number:6>",
"SizeBytes": 26,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventID": "<event-i-d:5>",
"eventID": "<event-i-d:6>",
"eventName": "MODIFY",
"eventSource": "aws:dynamodb",
"eventVersion": "1.1"
Expand Down
2 changes: 1 addition & 1 deletion tests/aws/services/dynamodb/test_dynamodb.validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"last_validated_date": "2023-08-23T14:33:37+00:00"
},
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_transact_write_items_streaming": {
"last_validated_date": "2024-03-15T01:54:32+00:00"
"last_validated_date": "2024-04-17T22:45:49+00:00"
},
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_transact_write_items_streaming_for_different_tables": {
"last_validated_date": "2024-04-02T21:45:36+00:00"
Expand Down

0 comments on commit 01ac739

Please sign in to comment.