dynamodb AttributeUpdate ADD/PUT/DELETE for NS (#6138)

This commit is contained in:
Basti110 2023-04-01 18:47:04 +02:00 committed by GitHub
parent d0cfddebfd
commit fe1869212f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View File

@ -341,6 +341,8 @@ class Item(BaseModel):
# TODO deal with other types
if set(update_action["Value"].keys()) == set(["SS"]):
self.attrs[attribute_name] = DynamoType({"SS": new_value})
elif set(update_action["Value"].keys()) == set(["NS"]):
self.attrs[attribute_name] = DynamoType({"NS": new_value})
elif isinstance(new_value, list):
self.attrs[attribute_name] = DynamoType({"L": new_value})
elif isinstance(new_value, dict):
@ -367,6 +369,10 @@ class Item(BaseModel):
existing = self.attrs.get(attribute_name, DynamoType({"SS": {}}))
new_set = set(existing.value).union(set(new_value))
self.attrs[attribute_name] = DynamoType({"SS": list(new_set)})
elif set(update_action["Value"].keys()) == set(["NS"]):
existing = self.attrs.get(attribute_name, DynamoType({"NS": {}}))
new_set = set(existing.value).union(set(new_value))
self.attrs[attribute_name] = DynamoType({"NS": list(new_set)})
elif set(update_action["Value"].keys()) == {"L"}:
existing = self.attrs.get(attribute_name, DynamoType({"L": []}))
new_list = existing.value + new_value
@ -382,6 +388,10 @@ class Item(BaseModel):
existing = self.attrs.get(attribute_name, DynamoType({"SS": {}}))
new_set = set(existing.value).difference(set(new_value))
self.attrs[attribute_name] = DynamoType({"SS": list(new_set)})
elif set(update_action["Value"].keys()) == set(["NS"]):
existing = self.attrs.get(attribute_name, DynamoType({"NS": {}}))
new_set = set(existing.value).difference(set(new_value))
self.attrs[attribute_name] = DynamoType({"NS": list(new_set)})
else:
raise NotImplementedError(
"ADD not supported for %s"

View File

@ -4887,6 +4887,44 @@ def test_update_item_add_to_list_using_legacy_attribute_updates():
resp["Item"]["attr"].should.equal(["a", "b", "c", "d", "e"])
@mock_dynamodb
def test_update_item_add_to_num_set_using_legacy_attribute_updates():
resource = boto3.resource("dynamodb", region_name="us-west-2")
resource.create_table(
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
TableName="TestTable",
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
)
table = resource.Table("TestTable")
table.wait_until_exists()
table.put_item(Item={"id": "set_add", "attr": {1, 2}})
table.update_item(
TableName="TestTable",
Key={"id": "set_add"},
AttributeUpdates={"attr": {"Action": "PUT", "Value": {1, 2, 3}}},
)
table.update_item(
TableName="TestTable",
Key={"id": "set_add"},
AttributeUpdates={"attr": {"Action": "ADD", "Value": {4, 5}}},
)
resp = table.get_item(Key={"id": "set_add"})
resp["Item"]["attr"].should.equal({1, 2, 3, 4, 5})
table.update_item(
TableName="TestTable",
Key={"id": "set_add"},
AttributeUpdates={"attr": {"Action": "DELETE", "Value": {2, 3}}},
)
resp = table.get_item(Key={"id": "set_add"})
resp["Item"]["attr"].should.equal({1, 4, 5})
@mock_dynamodb
def test_get_item_for_non_existent_table_raises_error():
client = boto3.client("dynamodb", "us-east-1")