tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Not possible to use a non default name for apps when testing using TestCase

Open Cikmo opened this issue 3 years ago • 0 comments

Describe the bug It is currently not possible to use a different app name from the default "models" when using TestCase.

The error raised:

tortoise.exceptions.ConfigurationError: Unable to get db settings for alias 'models'. Please check if the config dict contains this alias and try again

Having looked around in the source code, I'm pretty sure it stems from this hard coded piece of code, referencing "models":

class TestCase(TruncationTestCase):
    """
    An asyncio capable test class that will ensure that each test will be run at
    separate transaction that will rollback on finish.

    This is a fast test runner. Don't use it if your test uses transactions.
    """

    async def asyncSetUp(self) -> None:
        await super(TestCase, self).asyncSetUp()
        self._db = connections.get("models") # Here it has hardcoded the model name "models"
        self._transaction = TransactionTestContext(
            self._db._in_transaction().connection)
        await self._transaction.__aenter__()

To reproduce

@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
    db_url = "sqlite://:memory:"
    initializer(["tests.testmodels"], db_url=db_url, app_label="notmodels") # any app name thats not "models"
    request.addfinalizer(finalizer)

Cikmo avatar Jul 30 '22 16:07 Cikmo