diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index 48340405b..dd51ac972 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -400,8 +400,12 @@ class DynamoHandler(BaseResponse): key = self.body['Key'] update_expression = self.body.get('UpdateExpression') attribute_updates = self.body.get('AttributeUpdates') + existing_item = dynamodb_backend2.get_item(name, key) item = dynamodb_backend2.update_item(name, key, update_expression, attribute_updates) item_dict = item.to_json() item_dict['ConsumedCapacityUnits'] = 0.5 + if not existing_item: + item_dict['Attributes'] = {} + return dynamo_json_dump(item_dict) diff --git a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py index 6a5010bb5..df9428913 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py @@ -859,7 +859,7 @@ def test_update_item_does_not_exist_is_created(): table = _create_table_with_range_key() item_key = {'forum_name': 'the-key', 'subject': '123'} - table.update_item( + result = table.update_item( Key=item_key, AttributeUpdates={ 'username': { @@ -875,8 +875,11 @@ def test_update_item_does_not_exist_is_created(): 'Value': {'key': 'value'}, } }, + ReturnValues='ALL_OLD', ) + assert not result.get('Attributes') + returned_item = dict((k, str(v) if isinstance(v, Decimal) else v) for k, v in table.get_item(Key=item_key)['Item'].items()) dict(returned_item).should.equal({ diff --git a/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py index 6baeb8a12..27acae9f7 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py @@ -430,7 +430,7 @@ def test_update_item_remove(): } table.put_item(data=data) key_map = { - "S": "steve" + 'username': {"S": "steve"} } # Then remove the SentBy field @@ -455,7 +455,7 @@ def test_update_item_set(): } table.put_item(data=data) key_map = { - "S": "steve" + 'username': {"S": "steve"} } conn.update_item("messages", key_map, update_expression="SET foo=:bar, blah=:baz REMOVE :SentBy")