Skip to content

Commit cf11493

Browse files
committed
add di_container ficture
1 parent 58b0c47 commit cf11493

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

tests/conftest.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typing
22

3+
import modern_di
34
import modern_di_fastapi
45
import pytest
56
from httpx import ASGITransport, AsyncClient
@@ -18,19 +19,25 @@ async def client() -> typing.AsyncIterator[AsyncClient]:
1819
yield client
1920

2021

22+
@pytest.fixture
23+
async def di_container() -> typing.AsyncIterator[modern_di.Container]:
24+
di_container_: typing.Final = modern_di_fastapi.fetch_di_container(application)
25+
async with di_container_:
26+
yield di_container_
27+
28+
2129
@pytest.fixture(autouse=True)
22-
async def db_session() -> typing.AsyncIterator[AsyncSession]:
23-
async with modern_di_fastapi.fetch_di_container(application) as di_container:
24-
engine = await ioc.Dependencies.database_engine.async_resolve(di_container)
25-
connection = await engine.connect()
26-
transaction = await connection.begin()
27-
await connection.begin_nested()
28-
ioc.Dependencies.database_engine.override(connection, di_container)
29-
30-
try:
31-
yield AsyncSession(connection, expire_on_commit=False, autoflush=False)
32-
finally:
33-
if connection.in_transaction():
34-
await transaction.rollback()
35-
await connection.close()
36-
await engine.dispose()
30+
async def db_session(di_container: modern_di.Container) -> typing.AsyncIterator[AsyncSession]:
31+
engine = await ioc.Dependencies.database_engine.async_resolve(di_container)
32+
connection = await engine.connect()
33+
transaction = await connection.begin()
34+
await connection.begin_nested()
35+
ioc.Dependencies.database_engine.override(connection, di_container)
36+
37+
try:
38+
yield AsyncSession(connection, expire_on_commit=False, autoflush=False)
39+
finally:
40+
if connection.in_transaction():
41+
await transaction.rollback()
42+
await connection.close()
43+
await engine.dispose()

uv.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)