Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
0.24
====

0.24.2
------
Added
^^^^^
- Allow mapping of modules for multiple apps to be provided to tortoise.contrib.test.initializer (#1895)

0.24.1
------
Added
Expand Down
2 changes: 2 additions & 0 deletions docs/contrib/unittest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ To get ``test.TestCase`` to work as expected, you need to configure your test en
initializer(['module.a', 'module.b.c'])
# With optional db_url, app_label and loop parameters
initializer(['module.a', 'module.b.c'], db_url='...', app_label="someapp", loop=loop)
# With multiple apps
initializer({'app1': ['app1.models'], 'app2': ['app2.models']})
# Or env-var driven → See Green test runner section below.
env_initializer()

Expand Down
23 changes: 13 additions & 10 deletions tortoise/contrib/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,23 @@
_CONFIG: dict = {}
_CONNECTIONS: dict = {}
_LOOP: AbstractEventLoop = None # type: ignore
_MODULES: Iterable[str | ModuleType] = []
_MODULES: Union[Iterable[str | ModuleType], dict[str, Iterable[Union[str, ModuleType]]]] = []
_CONN_CONFIG: dict = {}


def getDBConfig(app_label: str, modules: Iterable[str | ModuleType]) -> dict:
def getDBConfig(app_label: str, modules: Union[Iterable[str | ModuleType], dict[str, Iterable[Union[str, ModuleType]]]]) -> dict:
"""
DB Config factory, for use in testing.

:param app_label: Label of the app (must be distinct for multiple apps).
:param modules: List of modules to look for models in.
:param app_label: Default label of the app if modules is a list (must be distinct for multiple apps).
:param modules: List of modules to look for models in, or dictionary of apps and list of modules
"""

modules = {app_label: modules} if not isinstance(modules, dict) else modules

return _generate_config(
_TORTOISE_TEST_DB,
app_modules={app_label: modules},
app_modules=modules,
testing=True,
connection_label=app_label,
)
Expand Down Expand Up @@ -104,17 +107,17 @@ async def truncate_all_models() -> None:


def initializer(
modules: Iterable[str | ModuleType],
modules: Union[Iterable[str | ModuleType], dict[str, Iterable[Union[str, ModuleType]]]],
db_url: str | None = None,
app_label: str = "models",
loop: AbstractEventLoop | None = None,
) -> None:
"""
Sets up the DB for testing. Must be called as part of test environment setup.

:param modules: List of modules to look for models in.
:param modules: List of modules to look for models in, or a dict of app labels and module lists
:param db_url: The db_url, defaults to ``sqlite://:memory``.
:param app_label: The name of the APP to initialise the modules in, defaults to "models"
:param app_label: The name of the APP to initialise the modules in if modules is a list, defaults to "models"
:param loop: Optional event loop.
"""
# pylint: disable=W0603
Expand Down Expand Up @@ -264,10 +267,10 @@ class IsolatedTestCase(SimpleTestCase):
It will create and destroy a new DB instance for every test.
This is obviously slow, but guarantees a fresh DB.

If you define a ``tortoise_test_modules`` list, it overrides the DB setup module for the tests.
If you define a ``tortoise_test_modules`` list or dict, it overrides the DB setup module for the tests.
"""

tortoise_test_modules: Iterable[str | ModuleType] = []
tortoise_test_modules: Union[Iterable[str | ModuleType], dict[str, Iterable[Union[str, ModuleType]]]] = []

async def _setUpDB(self) -> None:
await super()._setUpDB()
Expand Down
Loading