Skip to content

Commit 406f160

Browse files
committed
reuse lifespan connection in tests
1 parent 96b8fa6 commit 406f160

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

exmaples/fastapi_example/tests/conftest.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
from httpx import AsyncClient, ASGITransport
1010
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
1111

12-
from context_async_sqlalchemy import master_connect, replica_connect
13-
from exmaples.fastapi_example.database import (
14-
create_engine,
15-
create_session_maker,
16-
)
12+
from context_async_sqlalchemy import master_connect
1713
from exmaples.fastapi_example.setup_app import lifespan, setup_app
1814

1915

@@ -50,17 +46,8 @@ async def db_session_test(
5046

5147

5248
@pytest_asyncio.fixture
53-
async def session_maker_test() -> AsyncGenerator[
54-
async_sessionmaker[AsyncSession]
55-
]:
56-
engine = create_engine("127.0.0.1")
57-
session_maker = create_session_maker(engine)
49+
async def session_maker_test(
50+
app: FastAPI, # To make the connection to the database in lifespan
51+
) -> AsyncGenerator[async_sessionmaker[AsyncSession]]:
52+
session_maker = await master_connect.get_session_maker()
5853
yield session_maker
59-
await engine.dispose()
60-
61-
62-
@pytest_asyncio.fixture(autouse=True)
63-
async def close_connect() -> AsyncGenerator[None]:
64-
yield
65-
await master_connect.close()
66-
await replica_connect.close()

exmaples/fastapi_example/tests/non_transactional/conftest.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import pytest_asyncio
1111
from sqlalchemy import text
12+
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
13+
14+
from context_async_sqlalchemy import master_connect
1215
from exmaples.fastapi_example.database import (
1316
create_engine,
1417
create_session_maker,
@@ -36,7 +39,8 @@ async def cleanup_tables_after() -> AsyncGenerator[None]:
3639
database for each worker.
3740
"""
3841
yield
39-
await _cleanup_tables()
42+
session_maker = await master_connect.get_session_maker()
43+
await _cleanup_tables(session_maker)
4044

4145

4246
@pytest_asyncio.fixture(autouse=True, scope="session")
@@ -48,15 +52,18 @@ async def cleanup_tables_before() -> None:
4852
4953
It has its own engine, since the scope is session.
5054
"""
51-
await _cleanup_tables()
52-
53-
54-
async def _cleanup_tables() -> None:
55+
# Since the scope is different, we make a separate engine
5556
engine = create_engine("127.0.0.1")
5657
session_maker = create_session_maker(engine)
58+
await _cleanup_tables(session_maker)
59+
await engine.dispose()
60+
61+
62+
async def _cleanup_tables(
63+
session_maker: async_sessionmaker[AsyncSession],
64+
) -> None:
5765
async with session_maker() as session:
5866
await session.execute(
5967
text("TRUNCATE TABLE example RESTART IDENTITY CASCADE;")
6068
)
6169
await session.commit()
62-
await engine.dispose()

0 commit comments

Comments
 (0)