dynamodb AttributeUpdate ADD/PUT/DELETE for NS (#6138)
This commit is contained in:
parent
d0cfddebfd
commit
fe1869212f
@ -341,6 +341,8 @@ class Item(BaseModel):
|
|||||||
# TODO deal with other types
|
# TODO deal with other types
|
||||||
if set(update_action["Value"].keys()) == set(["SS"]):
|
if set(update_action["Value"].keys()) == set(["SS"]):
|
||||||
self.attrs[attribute_name] = DynamoType({"SS": new_value})
|
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):
|
elif isinstance(new_value, list):
|
||||||
self.attrs[attribute_name] = DynamoType({"L": new_value})
|
self.attrs[attribute_name] = DynamoType({"L": new_value})
|
||||||
elif isinstance(new_value, dict):
|
elif isinstance(new_value, dict):
|
||||||
@ -367,6 +369,10 @@ class Item(BaseModel):
|
|||||||
existing = self.attrs.get(attribute_name, DynamoType({"SS": {}}))
|
existing = self.attrs.get(attribute_name, DynamoType({"SS": {}}))
|
||||||
new_set = set(existing.value).union(set(new_value))
|
new_set = set(existing.value).union(set(new_value))
|
||||||
self.attrs[attribute_name] = DynamoType({"SS": list(new_set)})
|
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"}:
|
elif set(update_action["Value"].keys()) == {"L"}:
|
||||||
existing = self.attrs.get(attribute_name, DynamoType({"L": []}))
|
existing = self.attrs.get(attribute_name, DynamoType({"L": []}))
|
||||||
new_list = existing.value + new_value
|
new_list = existing.value + new_value
|
||||||
@ -382,6 +388,10 @@ class Item(BaseModel):
|
|||||||
existing = self.attrs.get(attribute_name, DynamoType({"SS": {}}))
|
existing = self.attrs.get(attribute_name, DynamoType({"SS": {}}))
|
||||||
new_set = set(existing.value).difference(set(new_value))
|
new_set = set(existing.value).difference(set(new_value))
|
||||||
self.attrs[attribute_name] = DynamoType({"SS": list(new_set)})
|
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:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"ADD not supported for %s"
|
"ADD not supported for %s"
|
||||||
|
@ -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"])
|
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
|
@mock_dynamodb
|
||||||
def test_get_item_for_non_existent_table_raises_error():
|
def test_get_item_for_non_existent_table_raises_error():
|
||||||
client = boto3.client("dynamodb", "us-east-1")
|
client = boto3.client("dynamodb", "us-east-1")
|
||||||
|
Loading…
Reference in New Issue
Block a user