Im mocking dynamo db client using @mock_dynamodb2
provided by moto
.
In my tests Im creating tables and test some input - straight forward.
When I run the code to create the table more than once (even when turning off the computer) I see that the table already exist.
I dont want this behavior and I dont want to depend on deletion of this table as well, as sometimes thrown exceptions might miss the deletion code for some reason and the table will still exist.
The code im using to create the mock is:
@mock.patch.dict(os.environ, {"REGION_NAME": "us-east-1"})
@mock_dynamodb2 # type: ignore
@pytest.mark.asyncio
async def mock_dynamo_db_call_method(
param: Any, self_copy: _AutoRefreshingAioObject, name: str, *args: Any, **kwargs: Any
) -> Any:
dynamodb_client_mock = client("dynamodb", region_name=os.environ.get("REGION_NAME"))
if name == "create_table":
dynamodb_client_mock.create_table(
TableName=kwargs["TableName"],
KeySchema=kwargs["KeySchema"],
AttributeDefinitions=kwargs["AttributeDefinitions"],
ProvisionedThroughput=kwargs["ProvisionedThroughput"],
)
return None
elif name == "delete_table":
dynamodb_client_mock.delete_table(TableName=kwargs["TableName"])
else:
raise Exception("Method is not mocked")