diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index f48519681..081afc2c4 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -419,6 +419,12 @@ class DynamoHandler(BaseResponse): expression_attribute_names = self.body.get('ExpressionAttributeNames', {}) expression_attribute_values = self.body.get('ExpressionAttributeValues', {}) 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_dict = item.to_json() 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 476ae14f4..7e4403daa 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py @@ -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 def test_boto3_query_gsi_range_comparison(): table = _create_table_with_range_key()