Add basic support for AttributeUpdates in Dynamo update_item. Closes #449.

This commit is contained in:
Steve Pulec 2015-11-07 16:45:24 -05:00
parent 18d63a6cfe
commit 8d41d0019b
3 changed files with 47 additions and 4 deletions

View File

@ -121,6 +121,14 @@ class Item(object):
# TODO deal with other types # TODO deal with other types
self.attrs[key] = DynamoType({"S": value}) self.attrs[key] = DynamoType({"S": value})
def update_with_attribute_updates(self, attribute_updates):
for attribute_name, update_action in attribute_updates.items():
action = update_action['Action']
new_value = update_action['Value'].values()[0]
if action == 'PUT':
# TODO deal with other types
self.attrs[attribute_name] = DynamoType({"S": new_value})
class Table(object): class Table(object):
@ -411,12 +419,19 @@ class DynamoDBBackend(BaseBackend):
return table.scan(scan_filters) return table.scan(scan_filters)
def update_item(self, table_name, key, update_expression): def update_item(self, table_name, key, update_expression, attribute_updates):
table = self.get_table(table_name) table = self.get_table(table_name)
if table.hash_key_attr in key:
# Sometimes the key is wrapped in a dict with the key name
key = key[table.hash_key_attr]
hash_value = DynamoType(key) hash_value = DynamoType(key)
item = table.get_item(hash_value) item = table.get_item(hash_value)
if update_expression:
item.update(update_expression) item.update(update_expression)
else:
item.update_with_attribute_updates(attribute_updates)
return item return item
def delete_item(self, table_name, keys): def delete_item(self, table_name, keys):

View File

@ -373,8 +373,9 @@ class DynamoHandler(BaseResponse):
def update_item(self): def update_item(self):
name = self.body['TableName'] name = self.body['TableName']
key = self.body['Key'] key = self.body['Key']
update_expression = self.body['UpdateExpression'] update_expression = self.body.get('UpdateExpression')
item = dynamodb_backend2.update_item(name, key, update_expression) attribute_updates = self.body.get('AttributeUpdates')
item = dynamodb_backend2.update_item(name, key, update_expression, attribute_updates)
item_dict = item.to_json() item_dict = item.to_json()
item_dict['ConsumedCapacityUnits'] = 0.5 item_dict['ConsumedCapacityUnits'] = 0.5

View File

@ -122,6 +122,33 @@ def test_item_add_and_describe_and_update():
}) })
@requires_boto_gte("2.9")
@mock_dynamodb2
def test_item_partial_save():
table = create_table()
data = {
'forum_name': 'LOLCat Forum',
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
}
table.put_item(data=data)
returned_item = table.get_item(forum_name="LOLCat Forum")
returned_item['SentBy'] = 'User B'
returned_item.partial_save()
returned_item = table.get_item(
forum_name='LOLCat Forum'
)
dict(returned_item).should.equal({
'forum_name': 'LOLCat Forum',
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User B',
})
@requires_boto_gte("2.9") @requires_boto_gte("2.9")
@mock_dynamodb2 @mock_dynamodb2
def test_item_put_without_table(): def test_item_put_without_table():