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
This commit is contained in:
Oide Brett 2020-11-17 07:41:54 +00:00 committed by GitHub
parent d29475ed19
commit 62d382ff70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 4 deletions

View File

@ -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(

View File

@ -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")