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,207 @@
|
||||
"""Tests for the library import: full, incremental and data preservation."""
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
||||
|
||||
import db
|
||||
import import_steam
|
||||
import steam_source
|
||||
|
||||
STEAMID = "76561198000000000"
|
||||
|
||||
|
||||
def game_info(name, genres=None, tags=None, metacritic=None, review=None, release=None):
|
||||
return {
|
||||
"name": name,
|
||||
"type": "game",
|
||||
"genres": genres or {},
|
||||
"tags": tags or {},
|
||||
"metacritic": metacritic,
|
||||
"review_pct": review,
|
||||
"release": release,
|
||||
}
|
||||
|
||||
|
||||
class ImportSteamTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.root = self.tmp.name
|
||||
self.db_path = os.path.join(self.root, "test.db")
|
||||
|
||||
def _run(self, appinfo, playtime=None, installed=None, lib_ids=None,
|
||||
incremental=False, no_network=True):
|
||||
playtime = playtime or {}
|
||||
installed = installed or {}
|
||||
lib_ids = lib_ids if lib_ids is not None else list(appinfo)
|
||||
|
||||
argv = [
|
||||
"import_steam.py",
|
||||
"--db", self.db_path,
|
||||
"--steam-root", self.root,
|
||||
"--steamid", STEAMID,
|
||||
]
|
||||
if incremental:
|
||||
argv.append("--incremental")
|
||||
if no_network:
|
||||
argv.append("--no-network")
|
||||
|
||||
patches = [
|
||||
mock.patch.object(sys, "argv", argv),
|
||||
mock.patch.object(steam_source, "read_users", return_value=[
|
||||
{"steamid": STEAMID, "account": "user", "persona": "User", "most_recent": True},
|
||||
]),
|
||||
mock.patch.object(steam_source, "library_roots", return_value=[self.root]),
|
||||
mock.patch.object(steam_source, "library_appids", return_value=lib_ids),
|
||||
mock.patch.object(steam_source, "read_appinfo", return_value=appinfo),
|
||||
mock.patch.object(steam_source, "read_playtime", return_value=playtime),
|
||||
mock.patch.object(steam_source, "installed_apps", return_value=installed),
|
||||
]
|
||||
with contextlib.ExitStack() as stack:
|
||||
for patch in patches:
|
||||
stack.enter_context(patch)
|
||||
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
|
||||
import_steam.main()
|
||||
|
||||
con = db.connect(self.db_path)
|
||||
self.addCleanup(con.close)
|
||||
return con
|
||||
|
||||
def _game(self, con, appid):
|
||||
return con.execute("SELECT * FROM games WHERE appid = ?", (appid,)).fetchone()
|
||||
|
||||
def _personal(self, con, appid):
|
||||
return con.execute("SELECT * FROM personal WHERE appid = ?", (appid,)).fetchone()
|
||||
|
||||
def test_full_import_writes_all_tables(self):
|
||||
appinfo = {
|
||||
100: game_info("Alpha", genres={"0": 25, "1": 3, "2": 30}, tags={"0": 10, "1": 20},
|
||||
metacritic=90, review=95, release=1600000000),
|
||||
200: game_info("Beta", genres={"0": 23}, tags={"0": 10}, review=80),
|
||||
}
|
||||
playtime = {100: {"minutes": 120, "last_played": 1700000000}}
|
||||
installed = {200: {"name": "Beta", "size_bytes": 1024,
|
||||
"last_played": "0", "library_path": self.root}}
|
||||
|
||||
con = self._run(appinfo, playtime, installed)
|
||||
|
||||
alpha = self._game(con, 100)
|
||||
self.assertEqual(alpha["name"], "Alpha")
|
||||
self.assertEqual(alpha["type"], "game")
|
||||
self.assertEqual(alpha["release_date"], "2020-09-13")
|
||||
self.assertEqual(alpha["metacritic"], 90)
|
||||
self.assertEqual(alpha["review_pct"], 95)
|
||||
|
||||
self.assertEqual({r["genre"] for r in con.execute(
|
||||
"SELECT genre FROM game_genres WHERE appid = 100")}, {"Adventure", "RPG"})
|
||||
self.assertEqual({r["tag_id"] for r in con.execute(
|
||||
"SELECT tag_id FROM game_tags WHERE appid = 100")}, {10, 20})
|
||||
|
||||
pt = con.execute("SELECT * FROM game_playtime WHERE appid = 100").fetchone()
|
||||
self.assertEqual(pt["minutes"], 120)
|
||||
self.assertEqual(pt["last_played"], "2023-11-14")
|
||||
|
||||
self.assertEqual(self._personal(con, 100)["status"], "played")
|
||||
self.assertEqual(self._personal(con, 200)["status"], "backlog")
|
||||
|
||||
alpha_install = con.execute("SELECT * FROM game_install WHERE appid = 100").fetchone()
|
||||
beta_install = con.execute("SELECT * FROM game_install WHERE appid = 200").fetchone()
|
||||
self.assertEqual(alpha_install["installed"], 0)
|
||||
self.assertEqual(beta_install["installed"], 1)
|
||||
self.assertEqual(beta_install["size_bytes"], 1024)
|
||||
|
||||
meta = {r["key"]: r["value"] for r in con.execute("SELECT * FROM schema_meta")}
|
||||
self.assertEqual(meta["steamid"], STEAMID)
|
||||
self.assertEqual(meta["steam_root"], self.root)
|
||||
self.assertIn("imported_at", meta)
|
||||
|
||||
def test_incremental_keeps_metadata_and_preserves_personal(self):
|
||||
con = self._run({100: game_info("Old Name", review=80)})
|
||||
con.execute(
|
||||
"UPDATE personal SET status='ignore', rating=8, favorite=1, notes='keep me' WHERE appid=100")
|
||||
con.commit()
|
||||
|
||||
self._run(
|
||||
{
|
||||
100: game_info("New Name", review=99),
|
||||
300: game_info("Gamma", review=70),
|
||||
},
|
||||
playtime={100: {"minutes": 600, "last_played": 0}},
|
||||
installed={100: {"name": "New Name", "size_bytes": 2048,
|
||||
"last_played": "0", "library_path": self.root}},
|
||||
incremental=True,
|
||||
)
|
||||
|
||||
con = db.connect(self.db_path)
|
||||
self.addCleanup(con.close)
|
||||
self.assertEqual(self._game(con, 100)["name"], "Old Name")
|
||||
self.assertEqual(self._game(con, 100)["review_pct"], 80)
|
||||
self.assertEqual(con.execute(
|
||||
"SELECT minutes FROM game_playtime WHERE appid = 100").fetchone()["minutes"], 600)
|
||||
self.assertEqual(con.execute(
|
||||
"SELECT installed FROM game_install WHERE appid = 100").fetchone()["installed"], 1)
|
||||
|
||||
personal = self._personal(con, 100)
|
||||
self.assertEqual(personal["status"], "ignore")
|
||||
self.assertEqual(personal["rating"], 8)
|
||||
self.assertEqual(personal["favorite"], 1)
|
||||
self.assertEqual(personal["notes"], "keep me")
|
||||
|
||||
self.assertEqual(self._game(con, 300)["name"], "Gamma")
|
||||
self.assertEqual(self._personal(con, 300)["status"], "backlog")
|
||||
|
||||
def test_full_import_preserves_personal_attributes_and_translations(self):
|
||||
con = self._run({100: game_info("Initial", review=80)})
|
||||
con.execute(
|
||||
"UPDATE personal SET status='playing', rating=9, favorite=1, notes='mine' WHERE appid=100")
|
||||
con.execute("INSERT INTO attributes (appid, key, value) VALUES (100, 'mood', 'nostalgia')")
|
||||
con.execute("""INSERT INTO game_translations
|
||||
(appid, lang, short_description, about, provider, translated_at)
|
||||
VALUES (100, 'ru', 'Кратко', 'Полно', 'google', '2026-01-01')""")
|
||||
con.commit()
|
||||
|
||||
self._run({100: game_info("Renamed", review=95)})
|
||||
|
||||
con = db.connect(self.db_path)
|
||||
self.addCleanup(con.close)
|
||||
self.assertEqual(self._game(con, 100)["name"], "Renamed")
|
||||
|
||||
personal = self._personal(con, 100)
|
||||
self.assertEqual(personal["status"], "playing")
|
||||
self.assertEqual(personal["rating"], 9)
|
||||
self.assertEqual(personal["favorite"], 1)
|
||||
self.assertEqual(personal["notes"], "mine")
|
||||
|
||||
self.assertEqual(con.execute(
|
||||
"SELECT value FROM attributes WHERE appid=100 AND key='mood'").fetchone()["value"],
|
||||
"nostalgia")
|
||||
translation = con.execute("SELECT * FROM game_translations WHERE appid=100").fetchone()
|
||||
self.assertEqual(translation["short_description"], "Кратко")
|
||||
self.assertEqual(translation["about"], "Полно")
|
||||
|
||||
def test_tag_names_downloaded_when_network_enabled(self):
|
||||
with mock.patch.object(import_steam, "fetch_tag_names", return_value={10: "RPG"}):
|
||||
con = self._run({100: game_info("Alpha")}, no_network=False)
|
||||
self.assertEqual(
|
||||
con.execute("SELECT name FROM tag_names WHERE tag_id=10").fetchone()["name"], "RPG")
|
||||
|
||||
|
||||
class IsoFromUnixTests(unittest.TestCase):
|
||||
def test_none_and_zero(self):
|
||||
self.assertIsNone(import_steam.iso_from_unix(None))
|
||||
self.assertIsNone(import_steam.iso_from_unix(0))
|
||||
|
||||
def test_utc_date(self):
|
||||
self.assertEqual(import_steam.iso_from_unix(1700000000), "2023-11-14")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user