Skip to content

Commit

Permalink
Fix TestCase | only "models" works as app_label
Browse files Browse the repository at this point in the history
    - in the current implementation only "models" works as app_label
    - there is no way to change that during run time as it's hard coded
    - the fix introduces another _APP_LABEL global variable that is set
       in the initializer
  • Loading branch information
esrehmki committed Aug 21, 2023
1 parent 0cb82bb commit 4cb5bd2
Showing 1 changed file with 5 additions and 2 deletions.
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

0 comments on commit 4cb5bd2

Please sign in to comment.