Skip to content

Commit fdf7219

Browse files
committed
update packages
1 parent 205366e commit fdf7219

File tree

2 files changed

+106
-90
lines changed

2 files changed

+106
-90
lines changed

tests/conftest.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import logging
21
import typing
32

3+
import modern_di
44
import modern_di_litestar
55
import pytest
66
from httpx import ASGITransport, AsyncClient
@@ -10,9 +10,6 @@
1010
from app.application import application
1111

1212

13-
logger = logging.getLogger(__name__)
14-
15-
1613
@pytest.fixture
1714
async def client() -> typing.AsyncIterator[AsyncClient]:
1815
async with AsyncClient(
@@ -22,20 +19,25 @@ async def client() -> typing.AsyncIterator[AsyncClient]:
2219
yield client
2320

2421

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

0 commit comments

Comments
 (0)