fix: sync fixture for alembic stamp, E2E CI race fix, ruff I rule removal
Some checks failed
ci/woodpecker/pr/pipeline Pipeline failed
Some checks failed
ci/woodpecker/pr/pipeline Pipeline failed
- Rewrite setup_database fixture to use sync engine with alembic stamp - Fix E2E CI race condition (test-e2e depends on test-integration) - Remove ruff I rule to resolve isort conflict
This commit is contained in:
@@ -104,7 +104,7 @@ steps:
|
||||
UV_PYTHON: "3.13"
|
||||
DB_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/blog_test
|
||||
SKIP_INIT_DB: "1"
|
||||
depends_on: [deps]
|
||||
depends_on: [test-integration]
|
||||
commands:
|
||||
- pip install uv
|
||||
- uv run --no-sync alembic upgrade head
|
||||
|
||||
@@ -82,7 +82,7 @@ target-version = "py313"
|
||||
line-length = 100
|
||||
|
||||
[tool.ruff.lint]
|
||||
select = ["E", "F", "I", "W", "B", "C4", "SIM"]
|
||||
select = ["E", "F", "W", "B", "C4", "SIM"]
|
||||
ignore = ["E501"]
|
||||
|
||||
[tool.isort]
|
||||
|
||||
@@ -4,12 +4,25 @@ from collections.abc import AsyncGenerator
|
||||
from typing import Generator
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from app.infrastructure.config import settings
|
||||
from app.infrastructure.database.models import Base
|
||||
|
||||
|
||||
def _sync_url(db_url: str) -> str:
|
||||
return db_url.replace("+aiosqlite", "").replace("+asyncpg", "")
|
||||
|
||||
|
||||
def _build_alembic_config(db_url: str) -> Config:
|
||||
alembic_cfg = Config("alembic.ini")
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", db_url)
|
||||
return alembic_cfg
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def event_loop() -> Generator[asyncio.AbstractEventLoop]:
|
||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||
@@ -18,20 +31,17 @@ def event_loop() -> Generator[asyncio.AbstractEventLoop]:
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
async def setup_database() -> AsyncGenerator[None]:
|
||||
def setup_database() -> Generator[None]:
|
||||
db_url = os.environ.get("DB_URL", settings.database_url)
|
||||
test_engine = create_async_engine(db_url)
|
||||
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sync_engine = create_engine(_sync_url(db_url))
|
||||
Base.metadata.drop_all(sync_engine)
|
||||
Base.metadata.create_all(sync_engine)
|
||||
sync_engine.dispose()
|
||||
|
||||
alembic_cfg = _build_alembic_config(db_url)
|
||||
command.stamp(alembic_cfg, "head")
|
||||
yield
|
||||
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
|
||||
await test_engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def db_session() -> AsyncGenerator[AsyncSession]:
|
||||
|
||||
Reference in New Issue
Block a user