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"
|
UV_PYTHON: "3.13"
|
||||||
DB_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/blog_test
|
DB_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/blog_test
|
||||||
SKIP_INIT_DB: "1"
|
SKIP_INIT_DB: "1"
|
||||||
depends_on: [deps]
|
depends_on: [test-integration]
|
||||||
commands:
|
commands:
|
||||||
- pip install uv
|
- pip install uv
|
||||||
- uv run --no-sync alembic upgrade head
|
- uv run --no-sync alembic upgrade head
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ target-version = "py313"
|
|||||||
line-length = 100
|
line-length = 100
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
select = ["E", "F", "I", "W", "B", "C4", "SIM"]
|
select = ["E", "F", "W", "B", "C4", "SIM"]
|
||||||
ignore = ["E501"]
|
ignore = ["E501"]
|
||||||
|
|
||||||
[tool.isort]
|
[tool.isort]
|
||||||
|
|||||||
@@ -4,12 +4,25 @@ from collections.abc import AsyncGenerator
|
|||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_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.config import settings
|
||||||
from app.infrastructure.database.models import Base
|
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")
|
@pytest.fixture(scope="session")
|
||||||
def event_loop() -> Generator[asyncio.AbstractEventLoop]:
|
def event_loop() -> Generator[asyncio.AbstractEventLoop]:
|
||||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
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)
|
@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)
|
db_url = os.environ.get("DB_URL", settings.database_url)
|
||||||
test_engine = create_async_engine(db_url)
|
sync_engine = create_engine(_sync_url(db_url))
|
||||||
|
Base.metadata.drop_all(sync_engine)
|
||||||
async with test_engine.begin() as conn:
|
Base.metadata.create_all(sync_engine)
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
sync_engine.dispose()
|
||||||
|
|
||||||
|
alembic_cfg = _build_alembic_config(db_url)
|
||||||
|
command.stamp(alembic_cfg, "head")
|
||||||
yield
|
yield
|
||||||
|
|
||||||
async with test_engine.begin() as conn:
|
|
||||||
await conn.run_sync(Base.metadata.drop_all)
|
|
||||||
|
|
||||||
await test_engine.dispose()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
async def db_session() -> AsyncGenerator[AsyncSession]:
|
async def db_session() -> AsyncGenerator[AsyncSession]:
|
||||||
|
|||||||
Reference in New Issue
Block a user