Add evaluation of ConditionExpression to DynamoDB2 delete_item
This commit is contained in:
parent
e71c06738c
commit
c9d69681ec
@ -1092,12 +1092,23 @@ class DynamoDBBackend(BaseBackend):
|
||||
item.update_with_attribute_updates(attribute_updates)
|
||||
return item
|
||||
|
||||
def delete_item(self, table_name, keys):
|
||||
def delete_item(self, table_name, key, expression_attribute_names=None, expression_attribute_values=None,
|
||||
condition_expression=None):
|
||||
table = self.get_table(table_name)
|
||||
if not table:
|
||||
return None
|
||||
hash_key, range_key = self.get_keys_value(table, keys)
|
||||
return table.delete_item(hash_key, range_key)
|
||||
|
||||
hash_value, range_value = self.get_keys_value(table, key)
|
||||
item = table.get_item(hash_value, range_value)
|
||||
|
||||
condition_op = get_filter_expression(
|
||||
condition_expression,
|
||||
expression_attribute_names,
|
||||
expression_attribute_values)
|
||||
if not condition_op.expr(item):
|
||||
raise ValueError('The conditional request failed')
|
||||
|
||||
return table.delete_item(hash_value, range_value)
|
||||
|
||||
def update_ttl(self, table_name, ttl_spec):
|
||||
table = self.tables.get(table_name)
|
||||
|
@ -579,7 +579,7 @@ class DynamoHandler(BaseResponse):
|
||||
|
||||
def delete_item(self):
|
||||
name = self.body['TableName']
|
||||
keys = self.body['Key']
|
||||
key = self.body['Key']
|
||||
return_values = self.body.get('ReturnValues', 'NONE')
|
||||
if return_values not in ('ALL_OLD', 'NONE'):
|
||||
er = 'com.amazonaws.dynamodb.v20111205#ValidationException'
|
||||
@ -590,7 +590,21 @@ class DynamoHandler(BaseResponse):
|
||||
er = 'com.amazonaws.dynamodb.v20120810#ConditionalCheckFailedException'
|
||||
return self.error(er, 'A condition specified in the operation could not be evaluated.')
|
||||
|
||||
item = self.dynamodb_backend.delete_item(name, keys)
|
||||
# Attempt to parse simple ConditionExpressions into an Expected
|
||||
# expression
|
||||
condition_expression = self.body.get('ConditionExpression')
|
||||
expression_attribute_names = self.body.get('ExpressionAttributeNames', {})
|
||||
expression_attribute_values = self.body.get('ExpressionAttributeValues', {})
|
||||
|
||||
try:
|
||||
item = self.dynamodb_backend.delete_item(
|
||||
name, key, expression_attribute_names, expression_attribute_values,
|
||||
condition_expression
|
||||
)
|
||||
except ValueError:
|
||||
er = 'com.amazonaws.dynamodb.v20111205#ConditionalCheckFailedException'
|
||||
return self.error(er, 'A condition specified in the operation could not be evaluated.')
|
||||
|
||||
if item and return_values == 'ALL_OLD':
|
||||
item_dict = item.to_json()
|
||||
else:
|
||||
|
@ -1995,6 +1995,23 @@ def test_condition_expressions():
|
||||
},
|
||||
)
|
||||
|
||||
with assert_raises(client.exceptions.ConditionalCheckFailedException):
|
||||
client.delete_item(
|
||||
TableName = 'test1',
|
||||
Key = {
|
||||
'client': {'S': 'client1'},
|
||||
'app': {'S': 'app1'},
|
||||
},
|
||||
ConditionExpression = 'attribute_not_exists(#existing)',
|
||||
ExpressionAttributeValues = {
|
||||
':match': {'S': 'match'}
|
||||
},
|
||||
ExpressionAttributeNames = {
|
||||
'#existing': 'existing',
|
||||
'#match': 'match',
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@mock_dynamodb2
|
||||
def test_condition_expression__attr_doesnt_exist():
|
||||
|
Loading…
Reference in New Issue
Block a user