Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TestCase | only "models" works as app_label #1457

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tortoise/contrib/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
_LOOP: AbstractEventLoop = None # type: ignore
_MODULES: Iterable[Union[str, ModuleType]] = []
_CONN_CONFIG: dict = {}
_APP_LABEL = None


def getDBConfig(app_label: str, modules: Iterable[Union[str, ModuleType]]) -> dict:
Expand Down Expand Up @@ -103,7 +104,9 @@ def initializer(
global _TORTOISE_TEST_DB
global _MODULES
global _CONN_CONFIG
global _APP_LABEL
_MODULES = modules
_APP_LABEL = app_label
if db_url is not None: # pragma: nobranch
_TORTOISE_TEST_DB = db_url
_CONFIG = getDBConfig(app_label=app_label, modules=_MODULES)
Expand Down Expand Up @@ -247,7 +250,7 @@ class IsolatedTestCase(SimpleTestCase):

async def _setUpDB(self) -> None:
await super()._setUpDB()
config = getDBConfig(app_label="models", modules=self.tortoise_test_modules or _MODULES)
config = getDBConfig(app_label=_APP_LABEL, modules=self.tortoise_test_modules or _MODULES)
await Tortoise.init(config, _create_db=True)
await Tortoise.generate_schemas(safe=False)

Expand Down Expand Up @@ -327,7 +330,7 @@ class TestCase(TruncationTestCase):

async def asyncSetUp(self) -> None:
await super().asyncSetUp()
self._db = connections.get("models")
self._db = connections.get(_APP_LABEL)
self._transaction = TransactionTestContext(self._db._in_transaction().connection)
await self._transaction.__aenter__() # type: ignore

Expand Down
Loading