feat: přidat kostru aplikace, SQLite schéma a Docker nasazení
This commit is contained in:
1 parent
f21161b25f
commit
bd41d53ce8
18 files changed
+1359
-2
No files matched your search
@@ -0,0 +1,132 @@
|
||||
"""Testy SQLite pragma nastavení a verzovaného migračního runneru."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from poznavacky import create_app, db as db_module
|
||||
|
||||
|
||||
def test_sqlite_pragmas_enabled_on_file_db(app, db: sqlite3.Connection) -> None:
|
||||
journal_mode = db.execute("PRAGMA journal_mode").fetchone()[0]
|
||||
foreign_keys = db.execute("PRAGMA foreign_keys").fetchone()[0]
|
||||
busy_timeout = db.execute("PRAGMA busy_timeout").fetchone()[0]
|
||||
|
||||
assert str(journal_mode).lower() == "wal"
|
||||
assert foreign_keys == 1
|
||||
assert busy_timeout > 0
|
||||
|
||||
|
||||
def test_migrations_applied_once_and_skipped_on_restart(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
data_dir = tmp_path / "data"
|
||||
env = {
|
||||
"POZNAVACKY_SECRET_KEY": "klic",
|
||||
"POZNAVACKY_DATA_DIR": str(data_dir),
|
||||
}
|
||||
|
||||
first_app = create_app(environ=env)
|
||||
conn = db_module.connect(first_app.config["DB_PATH"])
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT INTO tridy (nazev, rocnik) VALUES (?, ?)",
|
||||
("7.Z", 7),
|
||||
)
|
||||
conn.commit()
|
||||
first_versions = [
|
||||
row["version"]
|
||||
for row in conn.execute(
|
||||
"SELECT version FROM schema_migrations ORDER BY version"
|
||||
).fetchall()
|
||||
]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
assert first_versions == ["0001_initial_schema.sql"]
|
||||
|
||||
# Opakovaný start nesmí selhat ani smazat data.
|
||||
applied_again = db_module.apply_migrations(first_app.config["DB_PATH"])
|
||||
assert applied_again == []
|
||||
|
||||
second_app = create_app(environ=env)
|
||||
conn = db_module.connect(second_app.config["DB_PATH"])
|
||||
try:
|
||||
rows = conn.execute("SELECT nazev, rocnik FROM tridy").fetchall()
|
||||
assert len(rows) == 1
|
||||
assert rows[0]["nazev"] == "7.Z"
|
||||
assert rows[0]["rocnik"] == 7
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_migrations_run_in_deterministic_order(tmp_path: Path) -> None:
|
||||
migrations_dir = tmp_path / "migrations"
|
||||
migrations_dir.mkdir()
|
||||
(migrations_dir / "0002_seed.sql").write_text(
|
||||
"INSERT INTO test_log (krok) VALUES ('druhy');\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
(migrations_dir / "0001_table.sql").write_text(
|
||||
"CREATE TABLE test_log (id INTEGER PRIMARY KEY, krok TEXT NOT NULL);\n"
|
||||
"INSERT INTO test_log (krok) VALUES ('prvni');\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
db_path = tmp_path / "order.db"
|
||||
applied = db_module.apply_migrations(db_path, migrations_dir=migrations_dir)
|
||||
assert applied == ["0001_table.sql", "0002_seed.sql"]
|
||||
|
||||
conn = db_module.connect(db_path)
|
||||
try:
|
||||
steps = [
|
||||
row["krok"]
|
||||
for row in conn.execute(
|
||||
"SELECT krok FROM test_log ORDER BY id"
|
||||
).fetchall()
|
||||
]
|
||||
assert steps == ["prvni", "druhy"]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_failed_migration_rolls_back_atomically(tmp_path: Path) -> None:
|
||||
migrations_dir = tmp_path / "migrations"
|
||||
migrations_dir.mkdir()
|
||||
(migrations_dir / "0001_ok.sql").write_text(
|
||||
"CREATE TABLE zaklad (id INTEGER PRIMARY KEY);\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
(migrations_dir / "0002_broken.sql").write_text(
|
||||
"CREATE TABLE docasna (id INTEGER PRIMARY KEY);\n"
|
||||
"INSERT INTO neexistujici_tabulka VALUES (1);\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
db_path = tmp_path / "rollback.db"
|
||||
with pytest.raises(sqlite3.OperationalError):
|
||||
db_module.apply_migrations(db_path, migrations_dir=migrations_dir)
|
||||
|
||||
conn = db_module.connect(db_path)
|
||||
try:
|
||||
tables = {
|
||||
row["name"]
|
||||
for row in conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table'"
|
||||
).fetchall()
|
||||
}
|
||||
versions = [
|
||||
row["version"]
|
||||
for row in conn.execute(
|
||||
"SELECT version FROM schema_migrations ORDER BY version"
|
||||
).fetchall()
|
||||
]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
assert "zaklad" in tables
|
||||
assert "docasna" not in tables
|
||||
assert versions == ["0001_ok.sql"]
|
||||
Reference in new issue
Block a user