|
| 1 | +""" |
| 2 | +Integration tests for Alembic migration downgrade paths. |
| 3 | +
|
| 4 | +These tests exercise the downgrade() functions in each migration version, |
| 5 | +verifying that each step removes exactly the rows it seeded and leaves the |
| 6 | +rest of the database intact. |
| 7 | +
|
| 8 | +Tests run after test_main.py (alphabetical order). Each test downgrades one |
| 9 | +or more steps, asserts the expected state, then restores to head before the |
| 10 | +next test, ensuring the shared SQLite database remains consistent for any |
| 11 | +subsequent test runs. |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +import sqlite3 |
| 16 | + |
| 17 | +from alembic import command |
| 18 | +from alembic.config import Config |
| 19 | + |
| 20 | +_DB_PATH = os.getenv("STORAGE_PATH", "./players-sqlite3.db") |
| 21 | +_ALEMBIC_CFG = Config("alembic.ini") |
| 22 | + |
| 23 | + |
| 24 | +def test_migration_downgrade_003_removes_substitutes_only(): |
| 25 | + """Downgrade 003→002 removes the 15 seeded substitutes, leaves Starting XI.""" |
| 26 | + command.downgrade(_ALEMBIC_CFG, "-1") |
| 27 | + |
| 28 | + conn = sqlite3.connect(_DB_PATH) |
| 29 | + total = conn.execute("SELECT COUNT(*) FROM players").fetchone()[0] |
| 30 | + subs = conn.execute("SELECT COUNT(*) FROM players WHERE starting11=0").fetchone()[0] |
| 31 | + conn.close() |
| 32 | + |
| 33 | + command.upgrade(_ALEMBIC_CFG, "head") |
| 34 | + |
| 35 | + assert total == 11 |
| 36 | + assert subs == 0 |
| 37 | + |
| 38 | + |
| 39 | +def test_migration_downgrade_002_removes_starting11_only(): |
| 40 | + """Downgrade 002→001 removes the 11 seeded Starting XI, leaves table empty.""" |
| 41 | + command.downgrade(_ALEMBIC_CFG, "-2") |
| 42 | + |
| 43 | + conn = sqlite3.connect(_DB_PATH) |
| 44 | + total = conn.execute("SELECT COUNT(*) FROM players").fetchone()[0] |
| 45 | + conn.close() |
| 46 | + |
| 47 | + command.upgrade(_ALEMBIC_CFG, "head") |
| 48 | + |
| 49 | + assert total == 0 |
| 50 | + |
| 51 | + |
| 52 | +def test_migration_downgrade_001_drops_players_table(): |
| 53 | + """Downgrade 001→base drops the players table entirely.""" |
| 54 | + command.downgrade(_ALEMBIC_CFG, "base") |
| 55 | + |
| 56 | + conn = sqlite3.connect(_DB_PATH) |
| 57 | + table = conn.execute( |
| 58 | + "SELECT name FROM sqlite_master WHERE type='table' AND name='players'" |
| 59 | + ).fetchone() |
| 60 | + conn.close() |
| 61 | + |
| 62 | + command.upgrade(_ALEMBIC_CFG, "head") |
| 63 | + |
| 64 | + assert table is None |
0 commit comments