Skip to content

Commit

Permalink
DynamoDB: update_item() now throws the appriopriate exception when ad…
Browse files Browse the repository at this point in the history
…ding an empty set (#7684)
  • Loading branch information
bblommers committed May 11, 2024
1 parent cf8e560 commit 44310b5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions moto/dynamodb/parsing/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from moto.dynamodb.exceptions import (
IncorrectDataType,
IncorrectOperandType,
MockValidationException,
ProvidedKeyDoesNotExist,
)
from moto.dynamodb.models.dynamo_type import (
Expand Down Expand Up @@ -226,6 +227,10 @@ def execute(self, item: Item) -> None:
value_to_add = self.get_action_value()
if isinstance(value_to_add, DynamoType):
if value_to_add.is_set():
if len(value_to_add.value) == 0:
raise MockValidationException(
"ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An string set may not be empty"
)
try:
current_string_set = self.get_item_at_end_of_path(item)
except ProvidedKeyDoesNotExist:
Expand Down
47 changes: 47 additions & 0 deletions tests/test_dynamodb/test_dynamodb_update_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import boto3
import pytest
from botocore.exceptions import ClientError

from . import dynamodb_aws_verified

Expand Down Expand Up @@ -140,3 +141,49 @@ def test_delete_non_existing_item(table_name=None):
ReturnValues="ALL_NEW",
)
assert table.get_item(Key={"pk": name})["Item"] == {"pk": "name"}


@pytest.mark.aws_verified
@dynamodb_aws_verified()
def test_update_item_add_empty_set(table_name=None):
dynamodb = boto3.client("dynamodb", "us-east-1")

# Add to non-existing attribute
dynamodb.put_item(TableName=table_name, Item={"pk": {"S": "foo"}})
with pytest.raises(ClientError) as exc:
dynamodb.update_item(
TableName=table_name,
Key={"pk": {"S": "foo"}},
UpdateExpression="ADD stringset :emptySet",
ExpressionAttributeValues={":emptySet": {"SS": ()}},
)
err = exc.value.response["Error"]
assert err["Code"] == "ValidationException"
assert (
err["Message"]
== "ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An string set may not be empty"
)

assert dynamodb.scan(TableName=table_name)["Items"] == [{"pk": {"S": "foo"}}]

# Still not allowed when the attribute exists
dynamodb.put_item(
TableName=table_name, Item={"pk": {"S": "foo"}, "stringset": {"SS": ("item1",)}}
)
with pytest.raises(ClientError) as exc:
dynamodb.update_item(
TableName=table_name,
Key={"pk": {"S": "foo"}},
UpdateExpression="ADD stringset :emptySet",
ExpressionAttributeValues={":emptySet": {"SS": ()}},
)
err = exc.value.response["Error"]
assert err["Code"] == "ValidationException"
assert (
err["Message"]
== "ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An string set may not be empty"
)

assert dynamodb.scan(TableName=table_name)["Items"] == [
{"pk": {"S": "foo"}, "stringset": {"SS": ["item1"]}}
]

0 comments on commit 44310b5

Please sign in to comment.