"""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) self.assertIn("game_fts", 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"]) fts_tables = {r[0] for r in con.execute( "SELECT name FROM sqlite_master WHERE name='game_fts'")} self.assertEqual(fts_tables, {"game_fts"}) 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_search_index_rebuild_and_single_game_refresh(self): con = db.connect(self.path) self.addCleanup(con.close) db.init(con) con.executemany( "INSERT INTO games (appid, name, type) VALUES (?, ?, ?)", [(1, "Alpha Quest", "game"), (2, "Toolbox", "tool")]) con.execute("INSERT INTO game_details (appid, short_description, about, status) " "VALUES (1, 'Short', '

Deep dungeon

', 'ok')") con.execute("INSERT INTO game_translations (appid, lang, short_description) " "VALUES (1, 'ru', 'Краткое описание')") con.execute("INSERT INTO tag_names (tag_id, name) VALUES (10, 'RPG')") con.execute("INSERT INTO game_tags (appid, tag_id) VALUES (1, 10)") con.commit() db.refresh_search_index(con) def match(term): return [r[0] for r in con.execute( "SELECT appid FROM game_fts WHERE game_fts MATCH ? ORDER BY appid", (term,))] self.assertEqual(match("alpha*"), [1]) self.assertEqual(match("dungeon*"), [1]) self.assertEqual(match("краткое*"), [1]) self.assertEqual(match("rpg*"), [1]) self.assertEqual(match("toolbox*"), []) con.execute("INSERT INTO games (appid, name, type) VALUES (3, 'Beta Game', 'game')") db.refresh_search_game(con, 3) self.assertEqual(match("beta*"), [3]) con.execute("UPDATE games SET type='tool' WHERE appid=3") db.refresh_search_game(con, 3) self.assertEqual(match("beta*"), []) 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()