DynamoDB - DELETE item from nested sets

This commit is contained in:
Bert Blommers 2019-11-28 13:22:20 +00:00
parent 6d7ad717df
commit e41bc9fc58
2 changed files with 35 additions and 3 deletions

View File

@ -420,12 +420,18 @@ class Item(BaseModel):
if not dyn_value.is_set(): if not dyn_value.is_set():
raise TypeError raise TypeError
existing = self.attrs.get(key, None) key_head = key.split(".")[0]
key_tail = ".".join(key.split(".")[1:])
existing = self.attrs.get(key_head)
existing = existing.get(key_tail)
if existing: if existing:
if not existing.same_type(dyn_value): if not existing.same_type(dyn_value):
raise TypeError raise TypeError
new_set = set(existing.value).difference(dyn_value.value) new_set = set(existing.value).difference(dyn_value.value)
self.attrs[key] = DynamoType({existing.type: list(new_set)}) existing.set(
key=None,
new_value=DynamoType({existing.type: list(new_set)}),
)
else: else:
raise NotImplementedError( raise NotImplementedError(
"{} update action not yet supported".format(action) "{} update action not yet supported".format(action)

View File

@ -1347,7 +1347,7 @@ def test_update_item_add_with_expression():
@mock_dynamodb2 @mock_dynamodb2
def test_update_item_add_with_nested_sets_and_expression_names(): def test_update_item_add_with_nested_sets():
table = _create_table_with_range_key() table = _create_table_with_range_key()
item_key = {"forum_name": "the-key", "subject": "123"} item_key = {"forum_name": "the-key", "subject": "123"}
@ -1383,6 +1383,32 @@ def test_update_item_add_with_nested_sets_and_expression_names():
dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item)
@mock_dynamodb2
def test_update_item_delete_with_nested_sets():
table = _create_table_with_range_key()
item_key = {"forum_name": "the-key", "subject": "123"}
current_item = {
"forum_name": "the-key",
"subject": "123",
"nested": {"str_set": {"item1", "item2", "item3"}},
}
# Put an entry in the DB to play with
table.put_item(Item=current_item)
# Update item to add a string value to a nested string set
table.update_item(
Key=item_key,
UpdateExpression="DELETE nested.str_set :v",
ExpressionAttributeValues={":v": {"item3"}},
)
current_item["nested"]["str_set"] = current_item["nested"]["str_set"].difference(
{"item3"}
)
dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item)
@mock_dynamodb2 @mock_dynamodb2
def test_update_item_delete_with_expression(): def test_update_item_delete_with_expression():
table = _create_table_with_range_key() table = _create_table_with_range_key()