Add DynamoDB tests for update_item() with UpdateExpression, support spaces in the UpdateExpression. (#758)

Fixes #745.
This commit is contained in:
Nuno Santos 2016-11-11 23:04:14 +01:00 committed by Steve Pulec
parent 4942e74ab1
commit 71c1fbadbe
2 changed files with 39 additions and 0 deletions

View File

@ -419,6 +419,12 @@ class DynamoHandler(BaseResponse):
expression_attribute_names = self.body.get('ExpressionAttributeNames', {}) expression_attribute_names = self.body.get('ExpressionAttributeNames', {})
expression_attribute_values = self.body.get('ExpressionAttributeValues', {}) expression_attribute_values = self.body.get('ExpressionAttributeValues', {})
existing_item = dynamodb_backend2.get_item(name, key) existing_item = dynamodb_backend2.get_item(name, key)
# Support spaces between operators in an update expression
# E.g. `a = b + c` -> `a=b+c`
if update_expression:
update_expression = re.sub('\s*([=\+-])\s*', '\\1', update_expression)
item = dynamodb_backend2.update_item(name, key, update_expression, attribute_updates, expression_attribute_names, expression_attribute_values) item = dynamodb_backend2.update_item(name, key, update_expression, attribute_updates, expression_attribute_names, expression_attribute_values)
item_dict = item.to_json() item_dict = item.to_json()

View File

@ -1267,6 +1267,39 @@ def test_update_item_add_value_does_not_exist_is_created():
}) })
@mock_dynamodb2
def test_update_item_with_expression():
table = _create_table_with_range_key()
table.put_item(Item={
'forum_name': 'the-key',
'subject': '123',
'field': '1'
})
item_key = {'forum_name': 'the-key', 'subject': '123'}
table.update_item(
Key=item_key,
UpdateExpression='SET field=2',
)
dict(table.get_item(Key=item_key)['Item']).should.equal({
'field': '2',
'forum_name': 'the-key',
'subject': '123',
})
table.update_item(
Key=item_key,
UpdateExpression='SET field = 3',
)
dict(table.get_item(Key=item_key)['Item']).should.equal({
'field': '3',
'forum_name': 'the-key',
'subject': '123',
})
@mock_dynamodb2 @mock_dynamodb2
def test_boto3_query_gsi_range_comparison(): def test_boto3_query_gsi_range_comparison():
table = _create_table_with_range_key() table = _create_table_with_range_key()