From e1fc3a95966c925824e22104194567a3d37a0709 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Mon, 7 Dec 2020 01:31:53 -0800 Subject: [PATCH] #3506 - DynamoDB - Allow StringSets to be passed to update_item() (#3519) --- moto/dynamodb2/models/__init__.py | 6 +++--- tests/test_dynamodb2/test_dynamodb.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/moto/dynamodb2/models/__init__.py b/moto/dynamodb2/models/__init__.py index 7218fe0c..bcc70ab0 100644 --- a/moto/dynamodb2/models/__init__.py +++ b/moto/dynamodb2/models/__init__.py @@ -128,10 +128,10 @@ class Item(BaseModel): new_value = list(update_action["Value"].values())[0] if action == "PUT": # TODO deal with other types - if isinstance(new_value, list): - self.attrs[attribute_name] = DynamoType({"L": new_value}) - elif isinstance(new_value, set): + if set(update_action["Value"].keys()) == set(["SS"]): self.attrs[attribute_name] = DynamoType({"SS": new_value}) + elif isinstance(new_value, list): + self.attrs[attribute_name] = DynamoType({"L": new_value}) elif isinstance(new_value, dict): self.attrs[attribute_name] = DynamoType({"M": new_value}) elif set(update_action["Value"].keys()) == set(["N"]): diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index 0e0fcb08..cfba7b15 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -5683,3 +5683,24 @@ def test_transact_get_items_should_return_empty_map_for_non_existent_item(): items.should.have.length_of(2) items[0].should.equal({"Item": item}) items[1].should.equal({}) + + +@mock_dynamodb2 +def test_dynamodb_update_item_fails_on_string_sets(): + dynamodb = boto3.resource("dynamodb", region_name="eu-west-1") + client = boto3.client("dynamodb", region_name="eu-west-1") + + table = dynamodb.create_table( + TableName="test", + KeySchema=[{"AttributeName": "record_id", "KeyType": "HASH"},], + AttributeDefinitions=[{"AttributeName": "record_id", "AttributeType": "S"},], + BillingMode="PAY_PER_REQUEST", + ) + table.meta.client.get_waiter("table_exists").wait(TableName="test") + attribute = {"test_field": {"Value": {"SS": ["test1", "test2"],}, "Action": "PUT"}} + + client.update_item( + TableName="test", + Key={"record_id": {"S": "testrecord"}}, + AttributeUpdates=attribute, + )