Fixed a bug where calling batch_write_item with non-existing tables names would not raise ResourceNotFoundException (#4402)

This commit is contained in:
Maksymilian Babarowski 2021-10-13 11:52:29 +02:00 committed by GitHub
parent 36e66d32b9
commit 5cf6f9b2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 1 deletions

View File

@ -379,7 +379,12 @@ class DynamoHandler(BaseResponse):
request = list(table_request.values())[0]
if request_type == "PutRequest":
item = request["Item"]
self.dynamodb_backend.put_item(table_name, item)
res = self.dynamodb_backend.put_item(table_name, item)
if not res:
return self.error(
"com.amazonaws.dynamodb.v20111205#ResourceNotFoundException",
"Requested resource not found",
)
elif request_type == "DeleteRequest":
keys = request["Key"]
item = self.dynamodb_backend.delete_item(table_name, keys)

View File

@ -5920,3 +5920,44 @@ def test_update_non_existing_item_raises_error_and_does_not_contain_item_afterwa
err.value.response["Error"]["Code"].should.equal("ValidationException")
conn.scan(TableName=name)["Items"].should.have.length_of(0)
@mock_dynamodb2
def test_batch_write_item():
conn = boto3.resource("dynamodb", region_name="us-west-2")
tables = [f"table-{i}" for i in range(3)]
for name in tables:
conn.create_table(
TableName=name,
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
conn.batch_write_item(
RequestItems={
tables[0]: [{"PutRequest": {"Item": {"id": "0"}}}],
tables[1]: [{"PutRequest": {"Item": {"id": "1"}}}],
tables[2]: [{"PutRequest": {"Item": {"id": "2"}}}],
}
)
for idx, name in enumerate(tables):
table = conn.Table(f"table-{idx}")
res = table.get_item(Key={"id": str(idx)})
assert res["Item"].should.equal({"id": str(idx)})
scan = table.scan()
assert scan["Count"].should.equal(1)
conn.batch_write_item(
RequestItems={
tables[0]: [{"DeleteRequest": {"Key": {"id": "0"}}}],
tables[1]: [{"DeleteRequest": {"Key": {"id": "1"}}}],
tables[2]: [{"DeleteRequest": {"Key": {"id": "2"}}}],
}
)
for idx, name in enumerate(tables):
table = conn.Table(f"table-{idx}")
scan = table.scan()
assert scan["Count"].should.equal(0)

View File

@ -169,3 +169,17 @@ def test_update_item_range_key_set():
err["Message"].should.equal(
'Invalid UpdateExpression: The "ADD" section can only be used once in an update expression;'
)
@mock_dynamodb2
def test_batch_write_item_non_existing_table():
client = boto3.client("dynamodb", region_name="us-west-2")
with pytest.raises(client.exceptions.ResourceNotFoundException) as exc:
# Table my-table does not exist
client.batch_write_item(
RequestItems={"my-table": [{"PutRequest": {"Item": {}}}]}
)
err = exc.value.response["Error"]
assert err["Code"].should.equal("ResourceNotFoundException")
assert err["Message"].should.equal("Requested resource not found")