From 5cf6f9b2b4eed0e84395228ae176fd465773fbcb Mon Sep 17 00:00:00 2001 From: Maksymilian Babarowski Date: Wed, 13 Oct 2021 11:52:29 +0200 Subject: [PATCH] Fixed a bug where calling batch_write_item with non-existing tables names would not raise ResourceNotFoundException (#4402) --- moto/dynamodb2/responses.py | 7 +++- tests/test_dynamodb2/test_dynamodb.py | 41 +++++++++++++++++++ .../test_dynamodb_exceptions.py | 14 +++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index 884a7ea71..14f4ad3af 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -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) diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index 39c264c05..ae2873a25 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -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) diff --git a/tests/test_dynamodb2/test_dynamodb_exceptions.py b/tests/test_dynamodb2/test_dynamodb_exceptions.py index 377ef2e0e..e67841805 100644 --- a/tests/test_dynamodb2/test_dynamodb_exceptions.py +++ b/tests/test_dynamodb2/test_dynamodb_exceptions.py @@ -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")