From bc1c9a27f1fd2eaf3b41e0e6e5a11da0deab88b0 Mon Sep 17 00:00:00 2001 From: Heyder Date: Wed, 4 Mar 2020 23:17:03 -0300 Subject: [PATCH 1/4] fix use of _get_default result on _get_appended_list --- moto/dynamodb2/models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 88f750775..747fa93a7 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -450,9 +450,7 @@ class Item(BaseModel): old_list_key = list_append_re.group(1) # old_key could be a function itself (if_not_exists) if old_list_key.startswith("if_not_exists"): - old_list = DynamoType( - expression_attribute_values[self._get_default(old_list_key)] - ) + old_list = DynamoType(self._get_default(old_list_key)) else: old_list = self.attrs[old_list_key.split(".")[0]] if "." in old_list_key: From f8dd5a13c65c8d30f235e99b334c6d00f00a0dac Mon Sep 17 00:00:00 2001 From: Heyder Date: Wed, 4 Mar 2020 23:56:30 -0300 Subject: [PATCH 2/4] fix case if don't have attrs --- moto/dynamodb2/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 747fa93a7..7d995486d 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -450,7 +450,11 @@ class Item(BaseModel): old_list_key = list_append_re.group(1) # old_key could be a function itself (if_not_exists) if old_list_key.startswith("if_not_exists"): - old_list = DynamoType(self._get_default(old_list_key)) + old_list = self._get_default(old_list_key) + if not isinstance(old_list, DynamoType): + old_list = DynamoType( + expression_attribute_values[old_list] + ) else: old_list = self.attrs[old_list_key.split(".")[0]] if "." in old_list_key: From fa7f83bc2f10c9157e8b9484e5ef1219e54b2af1 Mon Sep 17 00:00:00 2001 From: "heyder.dias" Date: Thu, 5 Mar 2020 17:05:00 -0300 Subject: [PATCH 3/4] add test to nested if_not_exists and property already exists --- tests/test_dynamodb2/test_dynamodb.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index 180f460c0..d36fdc7fa 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -3634,6 +3634,31 @@ def test_update_supports_list_append_with_nested_if_not_exists_operation(): ) +@mock_dynamodb2 +def test_update_supports_list_append_with_nested_if_not_exists_operation_and_property_already_exists(): + dynamo = boto3.resource("dynamodb", region_name="us-west-1") + table_name = "test" + + dynamo.create_table( + TableName=table_name, + AttributeDefinitions=[{"AttributeName": "Id", "AttributeType": "S"}], + KeySchema=[{"AttributeName": "Id", "KeyType": "HASH"}], + ProvisionedThroughput={"ReadCapacityUnits": 20, "WriteCapacityUnits": 20}, + ) + + table = dynamo.Table(table_name) + + table.put_item(Item={"Id": "item-id", "event_history":["other_value"]}) + table.update_item( + Key={"Id": "item-id"}, + UpdateExpression="SET event_history = list_append(if_not_exists(event_history, :empty_list), :new_value)", + ExpressionAttributeValues={":empty_list": [], ":new_value": ["some_value"]}, + ) + table.get_item(Key={"Id": "item-id"})["Item"].should.equal( + {"Id": "item-id", "event_history": ["other_value", "some_value"]} + ) + + @mock_dynamodb2 def test_update_catches_invalid_list_append_operation(): client = boto3.client("dynamodb", region_name="us-east-1") From f5080e539dd140e556c0a280d9770b552d9aeaba Mon Sep 17 00:00:00 2001 From: "heyder.dias" Date: Thu, 5 Mar 2020 18:39:20 -0300 Subject: [PATCH 4/4] fix lint check --- moto/dynamodb2/models.py | 4 +--- tests/test_dynamodb2/test_dynamodb.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 7d995486d..8e5a61755 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -452,9 +452,7 @@ class Item(BaseModel): if old_list_key.startswith("if_not_exists"): old_list = self._get_default(old_list_key) if not isinstance(old_list, DynamoType): - old_list = DynamoType( - expression_attribute_values[old_list] - ) + old_list = DynamoType(expression_attribute_values[old_list]) else: old_list = self.attrs[old_list_key.split(".")[0]] if "." in old_list_key: diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index d36fdc7fa..428b58f81 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -3648,7 +3648,7 @@ def test_update_supports_list_append_with_nested_if_not_exists_operation_and_pro table = dynamo.Table(table_name) - table.put_item(Item={"Id": "item-id", "event_history":["other_value"]}) + table.put_item(Item={"Id": "item-id", "event_history": ["other_value"]}) table.update_item( Key={"Id": "item-id"}, UpdateExpression="SET event_history = list_append(if_not_exists(event_history, :empty_list), :new_value)",