Skip to content

Commit a8183d2

Browse files
nanotaboadaclaude
andcommitted
fix(tests): apply migrations before test session via conftest fixture (#2)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 80cc753 commit a8183d2

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ This project uses famous football coaches as release codenames, following an A-Z
5353
- `alembic==1.18.4`, `asyncpg==0.31.0` added to dependencies (#2)
5454
- `tests/test_migrations.py`: integration tests for migration downgrade paths —
5555
verifies each step removes only its seeded rows and restores correctly (#2)
56+
- `tests/conftest.py`: session-scoped `apply_migrations` fixture runs
57+
`alembic upgrade head` once before the test session, ensuring the database
58+
exists and is at head in CI and local environments (#2)
5659
- `codecov.yaml`: excludes `alembic/env.py` from coverage (offline mode is
5760
tooling infrastructure, not application logic) (#2)
5861
- `.sonarcloud.properties`: SonarCloud Automatic Analysis configuration —

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import warnings
2+
from pathlib import Path
23
from typing import Any, Generator
34

45
import pytest
6+
from alembic import command
7+
from alembic.config import Config
58
from fastapi.testclient import TestClient
69
from main import app
710
from tests.player_fake import Player, nonexistent_player
811

912
# Suppress the DeprecationWarning from httpx
1013
warnings.filterwarnings("ignore", category=DeprecationWarning)
1114

15+
ALEMBIC_CONFIG = Config(str(Path(__file__).resolve().parent.parent / "alembic.ini"))
16+
17+
18+
@pytest.fixture(scope="session", autouse=True)
19+
def apply_migrations():
20+
"""Apply Alembic migrations once before the test session starts."""
21+
command.upgrade(ALEMBIC_CONFIG, "head")
22+
1223

1324
@pytest.fixture(scope="function")
1425
def client():

tests/test_migrations.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313

1414
import os
1515
import sqlite3
16-
from pathlib import Path
1716

1817
from alembic import command
19-
from alembic.config import Config
18+
19+
from tests.conftest import ALEMBIC_CONFIG
2020

2121
DB_PATH = os.getenv("STORAGE_PATH", "./players-sqlite3.db")
22-
ALEMBIC_CONFIG = Config(str(Path(__file__).resolve().parent.parent / "alembic.ini"))
2322

2423

2524
def test_migration_downgrade_003_removes_substitutes_only():

0 commit comments

Comments
 (0)