tortoise / tortoise/tortoise-orm

TestCase connection_name error

Open
#758 1 comment 0 reactions 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
In tortoise/contrib/test/__init__.py(line:397),there is an error:

    async def _run_outcome(self, outcome, expecting_failure, testMethod) -> None:
        ...
        self.__db__ = Tortoise.get_connection("models") # That's a problem?
        ...

It can be fix to:


class TestCase(TruncationTestCase):
    connection_name:str="models"

    async def _run_outcome(self, outcome, expecting_failure, testMethod) -> None:
        _restore_default()
        self.__db__ = Tortoise.get_connection(self.connection_name)
        if self.__db__.capabilities.supports_transactions:
            connection = self.__db__._in_transaction().connection
            async with TransactionTestContext(connection):
                await super()._run_outcome(outcome, expecting_failure, testMethod)
        else:
            await super()._run_outcome(outcome, expecting_failure, testMethod)

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

Read tortoise/contrib/test/init.py around line 397 and inspect how TestCase selects its database connection. Make the connection name configurable instead of fixed to "models", then run the relevant test suite and verify TestCase uses the configured connection while preserving transaction handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.