feat: add SQLite migration runner
This commit is contained in:
parent
bfa5b2e8d0
commit
627675fff1
2 changed files with 90 additions and 0 deletions
42
tests/test_store/test_migrations.py
Normal file
42
tests/test_store/test_migrations.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from pathlib import Path
|
||||
|
||||
import aiosqlite
|
||||
|
||||
from fastapi_oidc_op.store.sqlite.migrations import run_migrations
|
||||
|
||||
MIGRATIONS_DIR = (
|
||||
Path(__file__).resolve().parent.parent.parent / "src" / "fastapi_oidc_op" / "store" / "sqlite" / "migrations"
|
||||
)
|
||||
|
||||
|
||||
async def test_run_migrations_applies_initial() -> None:
|
||||
async with aiosqlite.connect(":memory:") as db:
|
||||
await db.execute("PRAGMA foreign_keys=ON")
|
||||
count = await run_migrations(db, MIGRATIONS_DIR)
|
||||
assert count == 1
|
||||
async with db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users'") as cursor:
|
||||
row = await cursor.fetchone()
|
||||
assert row is not None
|
||||
|
||||
|
||||
async def test_run_migrations_skips_already_applied() -> None:
|
||||
async with aiosqlite.connect(":memory:") as db:
|
||||
await db.execute("PRAGMA foreign_keys=ON")
|
||||
first_count = await run_migrations(db, MIGRATIONS_DIR)
|
||||
second_count = await run_migrations(db, MIGRATIONS_DIR)
|
||||
assert first_count == 1
|
||||
assert second_count == 0
|
||||
|
||||
|
||||
async def test_run_migrations_creates_all_tables() -> None:
|
||||
async with aiosqlite.connect(":memory:") as db:
|
||||
await db.execute("PRAGMA foreign_keys=ON")
|
||||
await run_migrations(db, MIGRATIONS_DIR)
|
||||
async with db.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name") as cursor:
|
||||
tables = [row[0] async for row in cursor]
|
||||
assert "users" in tables
|
||||
assert "user_groups" in tables
|
||||
assert "webauthn_credentials" in tables
|
||||
assert "password_credentials" in tables
|
||||
assert "magic_links" in tables
|
||||
assert "_migrations" in tables
|
||||
Loading…
Add table
Add a link
Reference in a new issue