Skip to content

Commit

Permalink
DynamoDB: UpdateExpressions can contain a new-line
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Dec 15, 2023
1 parent 9c58c68 commit 81f4496
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
4 changes: 3 additions & 1 deletion moto/dynamodb/parsing/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Token:
CLOSE_ROUND_BRACKET = ")"
COMMA = ","
SPACE = " "
NEW_LINE = "\n"
DOT = "."
OPEN_SQUARE_BRACKET = "["
CLOSE_SQUARE_BRACKET = "]"
Expand All @@ -30,6 +31,7 @@ class Token:
CLOSE_ROUND_BRACKET,
COMMA,
SPACE,
NEW_LINE,
DOT,
OPEN_SQUARE_BRACKET,
CLOSE_SQUARE_BRACKET,
Expand Down Expand Up @@ -193,7 +195,7 @@ def _make_list(self) -> List[Token]:
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
Expand Down
39 changes: 24 additions & 15 deletions tests/test_dynamodb/test_dynamodb_update_expressions.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 81f4496

Please sign in to comment.