tortoise / tortoise/tortoise-orm

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

Open
#1,204 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at TestCase.asyncSetUp and the connections.get("models") call shown in the issue, then trace how initializer records the app_label. Reproduce the fixture with app_label="notmodels" and verify that TestCase uses the configured non-default app while the default setup still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlite
Domain
database, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.