From 640df048405bc7c3e5b8fce8caec69c05f75e43c Mon Sep 17 00:00:00 2001 From: Brian Pandola Date: Sun, 10 Jan 2021 06:20:41 -0800 Subject: [PATCH] Implement Add to List for dynamodb:UpdateItem (#3585) This handles the add-to-list case using the legacy `AttributeUpdates` parameter. * Added test coverage. * Verified against real AWS backend. Closes #3561 --- moto/dynamodb2/models/__init__.py | 4 ++++ tests/test_dynamodb2/test_dynamodb.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/moto/dynamodb2/models/__init__.py b/moto/dynamodb2/models/__init__.py index bcc70ab0..2772dacf 100644 --- a/moto/dynamodb2/models/__init__.py +++ b/moto/dynamodb2/models/__init__.py @@ -156,6 +156,10 @@ class Item(BaseModel): existing = self.attrs.get(attribute_name, DynamoType({"SS": {}})) new_set = set(existing.value).union(set(new_value)) self.attrs[attribute_name] = DynamoType({"SS": list(new_set)}) + elif set(update_action["Value"].keys()) == {"L"}: + existing = self.attrs.get(attribute_name, DynamoType({"L": []})) + new_list = existing.value + new_value + self.attrs[attribute_name] = DynamoType({"L": new_list}) else: # TODO: implement other data types raise NotImplementedError( diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index cfba7b15..6fc8be00 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -5704,3 +5704,26 @@ def test_dynamodb_update_item_fails_on_string_sets(): Key={"record_id": {"S": "testrecord"}}, AttributeUpdates=attribute, ) + + +@moto.mock_dynamodb2 +def test_update_item_add_to_list_using_legacy_attribute_updates(): + resource = boto3.resource("dynamodb", region_name="us-west-2") + resource.create_table( + AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}], + TableName="TestTable", + KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}], + ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}, + ) + table = resource.Table("TestTable") + table.wait_until_exists() + table.put_item(Item={"id": "list_add", "attr": ["a", "b", "c"]},) + + table.update_item( + TableName="TestTable", + Key={"id": "list_add"}, + AttributeUpdates={"attr": {"Action": "ADD", "Value": ["d", "e"]}}, + ) + + resp = table.get_item(Key={"id": "list_add"}) + resp["Item"]["attr"].should.equal(["a", "b", "c", "d", "e"])