mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-11-30 13:20:40 +03:00
for AsyncEngine created in function scope, close and clean-up pooled connections adding await engine.dispose()
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import NullPool
|
||||
|
||||
from the_app import config
|
||||
from the_app.database import get_db
|
||||
from the_app.main import app
|
||||
from the_app.models.base import Base
|
||||
from the_app.database import engine
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
@@ -20,32 +14,13 @@ def anyio_backend(request):
|
||||
return request.param
|
||||
|
||||
|
||||
global_settings = config.get_settings()
|
||||
url = global_settings.asyncpg_test_url
|
||||
engine = create_async_engine(url, poolclass=NullPool, future=True)
|
||||
|
||||
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
||||
|
||||
|
||||
async def get_test_db():
|
||||
session = async_session()
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
await session.rollback()
|
||||
raise ex
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
|
||||
app.dependency_overrides[get_db] = get_test_db
|
||||
|
||||
|
||||
async def start_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
# for AsyncEngine created in function scope, close and
|
||||
# clean-up pooled connections
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
Reference in New Issue
Block a user