DynamoDB: UpdateExpressions can contain a new-line (#7128)

This commit is contained in:
Bert Blommers 2023-12-16 10:53:38 -01:00 committed by GitHub
parent 9c58c689ad
commit 2228f07b80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 18 deletions

View File

@ -11,12 +11,12 @@ class Token:
_TOKEN_INSTANCE = None
MINUS_SIGN = "-"
PLUS_SIGN = "+"
SPACE_SIGN = " "
EQUAL_SIGN = "="
OPEN_ROUND_BRACKET = "("
CLOSE_ROUND_BRACKET = ")"
COMMA = ","
SPACE = " "
NEW_LINE = "\n"
DOT = "."
OPEN_SQUARE_BRACKET = "["
CLOSE_SQUARE_BRACKET = "]"
@ -24,12 +24,12 @@ class Token:
SPECIAL_CHARACTERS = [
MINUS_SIGN,
PLUS_SIGN,
SPACE_SIGN,
EQUAL_SIGN,
OPEN_ROUND_BRACKET,
CLOSE_ROUND_BRACKET,
COMMA,
SPACE,
NEW_LINE,
DOT,
OPEN_SQUARE_BRACKET,
CLOSE_SQUARE_BRACKET,
@ -193,7 +193,7 @@ class ExpressionTokenizer(object):
else:
self.process_staged_characters()
if character == Token.SPACE:
if character in [Token.SPACE, Token.NEW_LINE]:
if (
len(self.token_list) > 0
and self.token_list[-1].type == Token.WHITESPACE

View File

@ -1,33 +1,42 @@
import boto3
import pytest
from moto import mock_dynamodb
from . import dynamodb_aws_verified
@mock_dynamodb
def test_update_different_map_elements_in_single_request():
@pytest.mark.aws_verified
@dynamodb_aws_verified()
def test_update_different_map_elements_in_single_request(table_name=None):
# https://github.com/getmoto/moto/issues/5552
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="example_table",
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[
{"AttributeName": "id", "AttributeType": "S"},
],
BillingMode="PAY_PER_REQUEST",
)
record = {
"id": "example_id",
"pk": "example_id",
"d": {"hello": "h", "world": "w"},
}
table = dynamodb.Table("example_table")
table = dynamodb.Table(table_name)
table.put_item(Item=record)
updated = table.update_item(
Key={"id": "example_id"},
Key={"pk": "example_id"},
UpdateExpression="set d.hello = :h, d.world = :w",
ExpressionAttributeValues={":h": "H", ":w": "W"},
ReturnValues="ALL_NEW",
)
assert updated["Attributes"] == {
"id": "example_id",
"pk": "example_id",
"d": {"hello": "H", "world": "W"},
}
# Use UpdateExpression that contains a new-line
# https://github.com/getmoto/moto/issues/7127
table.update_item(
Key={"pk": "example_id"},
UpdateExpression=(
"""
ADD
MyTotalCount :MyCount
"""
),
ExpressionAttributeValues={":MyCount": 5},
)
assert table.get_item(Key={"pk": "example_id"})["Item"]["MyTotalCount"] == 5