feat: add tests, CI and P0 reliability features
This commit is contained in:
1 parent
0754065dae
commit
626650e44a
22 files changed
+1576
-49
No files matched your search
@@ -0,0 +1,113 @@
|
||||
"""Tests for schema initialization and versioned migrations."""
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
||||
|
||||
import db
|
||||
|
||||
LEGACY_SCHEMA = """
|
||||
CREATE TABLE schema_meta (key TEXT PRIMARY KEY, value TEXT);
|
||||
CREATE TABLE games (
|
||||
appid INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT,
|
||||
release_date TEXT,
|
||||
metacritic INTEGER,
|
||||
review_pct INTEGER,
|
||||
updated_at TEXT
|
||||
);
|
||||
CREATE TABLE game_details (
|
||||
appid INTEGER PRIMARY KEY REFERENCES games(appid) ON DELETE CASCADE,
|
||||
short_description TEXT,
|
||||
about TEXT,
|
||||
header_image TEXT,
|
||||
developers TEXT,
|
||||
publishers TEXT,
|
||||
release_date TEXT,
|
||||
lang TEXT,
|
||||
fetched_at TEXT,
|
||||
status TEXT
|
||||
);
|
||||
INSERT INTO games (appid, name, type) VALUES (100, 'Legacy', 'game');
|
||||
INSERT INTO game_details (appid, short_description, status)
|
||||
VALUES (100, 'Old description', 'ok');
|
||||
"""
|
||||
|
||||
|
||||
class SchemaVersionTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.path = os.path.join(self.tmp.name, "test.db")
|
||||
|
||||
def _version(self, con):
|
||||
row = con.execute("SELECT value FROM schema_meta WHERE key='schema_version'").fetchone()
|
||||
return row[0] if row else None
|
||||
|
||||
def _columns(self, con, table):
|
||||
return {row[1] for row in con.execute(f"PRAGMA table_info({table})")}
|
||||
|
||||
def test_migrations_cover_current_version(self):
|
||||
self.assertEqual(set(db.MIGRATIONS), set(range(1, db.SCHEMA_VERSION + 1)))
|
||||
|
||||
def test_new_database_is_stamped(self):
|
||||
con = db.connect(self.path)
|
||||
self.addCleanup(con.close)
|
||||
db.init(con)
|
||||
self.assertEqual(self._version(con), str(db.SCHEMA_VERSION))
|
||||
self.assertIn("source_appid", self._columns(con, "game_details"))
|
||||
tables = {r[0] for r in con.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
self.assertIn("personal", tables)
|
||||
self.assertIn("game_translations", tables)
|
||||
|
||||
def test_legacy_database_is_migrated_and_data_preserved(self):
|
||||
con = sqlite3.connect(self.path)
|
||||
con.executescript(LEGACY_SCHEMA)
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
con = db.connect(self.path)
|
||||
self.addCleanup(con.close)
|
||||
self.assertNotIn("source_appid", self._columns(con, "game_details"))
|
||||
|
||||
db.init(con)
|
||||
|
||||
self.assertEqual(self._version(con), str(db.SCHEMA_VERSION))
|
||||
self.assertIn("source_appid", self._columns(con, "game_details"))
|
||||
row = con.execute("SELECT name FROM games WHERE appid=100").fetchone()
|
||||
self.assertEqual(row["name"], "Legacy")
|
||||
details = con.execute("SELECT short_description, source_appid FROM game_details WHERE appid=100").fetchone()
|
||||
self.assertEqual(details["short_description"], "Old description")
|
||||
self.assertIsNone(details["source_appid"])
|
||||
|
||||
def test_init_is_idempotent(self):
|
||||
con = db.connect(self.path)
|
||||
self.addCleanup(con.close)
|
||||
db.init(con)
|
||||
con.execute("INSERT INTO games (appid, name, type) VALUES (1, 'One', 'game')")
|
||||
con.commit()
|
||||
|
||||
db.init(con)
|
||||
|
||||
self.assertEqual(self._version(con), str(db.SCHEMA_VERSION))
|
||||
self.assertEqual(con.execute("SELECT COUNT(*) c FROM games").fetchone()["c"], 1)
|
||||
|
||||
def test_newer_database_is_rejected(self):
|
||||
con = db.connect(self.path)
|
||||
self.addCleanup(con.close)
|
||||
con.execute("CREATE TABLE schema_meta (key TEXT PRIMARY KEY, value TEXT)")
|
||||
con.execute("INSERT INTO schema_meta (key, value) VALUES ('schema_version', '999')")
|
||||
con.commit()
|
||||
|
||||
with self.assertRaises(RuntimeError):
|
||||
db.init(con)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user