Give up on migrations in tests

pull/115/head
stijndcl 2022-06-30 15:20:54 +02:00
parent b4a3a87e6e
commit 60382b8eab
5 changed files with 6 additions and 18 deletions

3
.gitignore vendored
View File

@ -154,6 +154,3 @@ cython_debug/
# PyCharm # PyCharm
.idea/ .idea/
# SQLite testing database
tests.db

View File

@ -68,10 +68,4 @@ async def run_migrations_online() -> None:
if context.is_offline_mode(): if context.is_offline_mode():
run_migrations_offline() run_migrations_offline()
else: else:
# Wonky way to use the Pytest event loop instead of another one
try:
loop = asyncio.get_running_loop()
if loop and loop.is_running():
loop.create_task(run_migrations_online())
except RuntimeError:
asyncio.run(run_migrations_online()) asyncio.run(run_migrations_online())

View File

@ -17,6 +17,7 @@ engine = create_async_engine(
database=settings.DB_NAME, database=settings.DB_NAME,
), ),
pool_pre_ping=True, pool_pre_ping=True,
future=True,
) )
DBSession = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession, expire_on_commit=False) DBSession = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession, expire_on_commit=False)

View File

@ -1,4 +1,3 @@
aiosqlite==0.17.0
black==22.3.0 black==22.3.0
mypy==0.961 mypy==0.961
pylint==2.14.1 pylint==2.14.1

View File

@ -5,7 +5,6 @@ from unittest.mock import MagicMock
import pytest import pytest
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from alembic import config, command
from database.engine import engine from database.engine import engine
from database.models import Base from database.models import Base
from didier import Didier from didier import Didier
@ -19,16 +18,14 @@ def event_loop() -> Generator:
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
async def tables(event_loop): async def tables():
"""Initialize a database before the tests, and then tear it down again""" """Initialize a database before the tests, and then tear it down again"""
alembic_config: config.Config = config.Config("alembic.ini") async with engine.begin() as connection:
command.upgrade(alembic_config, "head") await connection.run_sync(Base.metadata.create_all)
yield
command.downgrade(alembic_config, "base")
@pytest.fixture @pytest.fixture
async def database_session(tables, event_loop) -> AsyncGenerator[AsyncSession, None]: async def database_session(tables) -> AsyncGenerator[AsyncSession, None]:
"""Fixture to create a session for every test """Fixture to create a session for every test
Rollbacks the transaction afterwards so that the future tests start with a clean database Rollbacks the transaction afterwards so that the future tests start with a clean database
""" """