From 62d382ff706066251bae967ef28a495864941988 Mon Sep 17 00:00:00 2001 From: Oide Brett <32061073+oidebrett@users.noreply.github.com> Date: Tue, 17 Nov 2020 07:41:54 +0000 Subject: [PATCH] Fixed issue 3448 for DynamoDB update_item (#3463) * Fixed issue 3448 for DynamoDB update_item * Tidied up fix for issue 3448 for DynamoDB update_item * Reformatted fix for issue 3448 for DynamoDB update_item * removed use of f-strings in test case as it fails in Travis CI build due to Python 2.7 support of f strings --- moto/dynamodb2/responses.py | 5 +--- tests/test_dynamodb2/test_dynamodb.py | 38 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index 8eb1023b6..d67994ced 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -813,7 +813,6 @@ class DynamoHandler(BaseResponse): item_dict["Attributes"] = self._build_updated_new_attributes( existing_attributes, item_dict["Attributes"] ) - return dynamo_json_dump(item_dict) def _build_updated_new_attributes(self, original, changed): @@ -838,10 +837,8 @@ class DynamoHandler(BaseResponse): ) for index in range(len(changed)) ] - elif changed != original: - return changed else: - return None + return changed def describe_limits(self): return json.dumps( diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index de9811df6..731f4466d 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -2437,6 +2437,44 @@ def test_update_return_attributes(): r = update("col1", "val6", "WRONG") +# https://github.com/spulec/moto/issues/3448 +@mock_dynamodb2 +def test_update_return_updated_new_attributes_when_same(): + dynamo_client = boto3.resource("dynamodb", region_name="us-east-1") + dynamo_client.create_table( + TableName="moto-test", + KeySchema=[{"AttributeName": "HashKey1", "KeyType": "HASH"}], + AttributeDefinitions=[{"AttributeName": "HashKey1", "AttributeType": "S"}], + ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1}, + ) + + dynamodb_table = dynamo_client.Table("moto-test") + dynamodb_table.put_item( + Item={"HashKey1": "HashKeyValue1", "listValuedAttribute1": ["a", "b"]} + ) + + def update(col, to, rv): + return dynamodb_table.update_item( + TableName="moto-test", + Key={"HashKey1": "HashKeyValue1"}, + UpdateExpression="SET listValuedAttribute1=:" + col, + ExpressionAttributeValues={":" + col: to}, + ReturnValues=rv, + ) + + r = update("a", ["a", "c"], "UPDATED_NEW") + assert r["Attributes"] == {"listValuedAttribute1": ["a", "c"]} + + r = update("a", {"a", "c"}, "UPDATED_NEW") + assert r["Attributes"] == {"listValuedAttribute1": {"a", "c"}} + + r = update("a", {1, 2}, "UPDATED_NEW") + assert r["Attributes"] == {"listValuedAttribute1": {1, 2}} + + with pytest.raises(ClientError) as ex: + r = update("a", ["a", "c"], "WRONG") + + @mock_dynamodb2 def test_put_return_attributes(): dynamodb = boto3.client("dynamodb", region_name="us-east-1")