Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB: UpdateExpressions can contain a new-line #7128

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions moto/dynamodb/parsing/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ 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 = "."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated with the issue, but I noticed SPACE_SIGN on line 14 and 28 can be deleted since they aren't used anywhere throughout the moto repository (it's a duplicate of SPACE).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @abbasyadollahi - I've removed the field. Thanks for the review!

OPEN_SQUARE_BRACKET = "["
CLOSE_SQUARE_BRACKET = "]"

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,
Expand Down Expand Up @@ -193,7 +193,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