Fixture that sets up two separate databases for testing.
()
| 17 | |
| 18 | @pytest_asyncio.fixture(scope="function") |
| 19 | async def two_databases(): |
| 20 | """Fixture that sets up two separate databases for testing.""" |
| 21 | db_url = os.getenv("TORTOISE_TEST_DB", "sqlite://:memory:") |
| 22 | |
| 23 | from tortoise.backends.base.config_generator import expand_db_url |
| 24 | |
| 25 | # Expand the URL with uniqueness if it's a template |
| 26 | # This ensures "models" and "events" get different DB names if TORTOISE_TEST_DB has {} |
| 27 | db1_config = expand_db_url(db_url, testing=True) |
| 28 | db2_config = expand_db_url(db_url, testing=True) |
| 29 | |
| 30 | ctx = TortoiseContext() |
| 31 | async with ctx: |
| 32 | await ctx.init( |
| 33 | config={ |
| 34 | "connections": { |
| 35 | "models": db1_config, |
| 36 | "events": db2_config, |
| 37 | }, |
| 38 | "apps": { |
| 39 | "models": {"models": ["tests.testmodels"], "default_connection": "models"}, |
| 40 | "events": {"models": ["tests.testmodels"], "default_connection": "events"}, |
| 41 | }, |
| 42 | }, |
| 43 | _create_db=True, |
| 44 | ) |
| 45 | await ctx.generate_schemas() |
| 46 | |
| 47 | db = ctx.connections.get("models") |
| 48 | second_db = ctx.connections.get("events") |
| 49 | |
| 50 | yield db, second_db |
| 51 | |
| 52 | |
| 53 | def build_select_sql(db) -> str: |
nothing calls this directly
no test coverage detected
searching dependent graphs…