11import typing
22
3+ import modern_di
34import modern_di_fastapi
45import pytest
56from 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 ()
0 commit comments