Files
SteamBase/tests/test_hltb.py
T
strsv 987e52931f feat: cache HLTB search responses
Store search results in hltb_cache (schema v8) with a 7-day TTL, cache misses too, prune rows older than 90 days, and add --cache-ttl, --no-cache and --clear-cache.
2026-11-07 00:47:30 +03:00

114 lines
4.6 KiB
Python

"""Tests for the optional HLTB enrichment helpers."""
import os
import sys
import tempfile
import unittest
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import db
import fetch_hltb
class HltbTests(unittest.TestCase):
def test_normalize(self):
self.assertEqual(fetch_hltb.normalize("Hades™ (R)"), "hades")
self.assertEqual(fetch_hltb.normalize("Half-Life 2"), "half life 2")
self.assertEqual(fetch_hltb.normalize(""), "")
def test_find_exact_match_only(self):
entries = [SimpleNamespace(game_name="Hades"), SimpleNamespace(game_name="Hades II")]
self.assertEqual(fetch_hltb.find_match(entries, "hades").game_name, "Hades")
self.assertIsNone(fetch_hltb.find_match(entries, "Hade"))
def test_matches_case_roman_and_editions(self):
cases = [
("SOMA", "Soma"),
("INSIDE", "Inside"),
("Divinity: Original Sin 2", "Divinity: Original Sin II"),
("Stronghold Crusader HD", "Stronghold: Crusader"),
("Batman: Arkham Asylum GOTY Edition", "Batman: Arkham Asylum"),
("STAR WARS™ Republic Commando", "Star Wars: Republic Commando"),
("Age of Empires II (2013)", "Age of Empires II"),
("Dishonored RHCP", "Dishonored"),
("Serious Sam Classic: The First Encounter", "Serious Sam: The First Encounter"),
]
for query, title in cases:
entry = SimpleNamespace(game_name=title)
self.assertIs(fetch_hltb.find_match([entry], query), entry)
def test_search_variants_include_year(self):
variants = fetch_hltb.search_variants("DOOM", 2016)
self.assertIn("doom", variants)
self.assertIn("doom 2016", variants)
def test_positive(self):
self.assertEqual(fetch_hltb.positive("23.5"), 23.5)
self.assertIsNone(fetch_hltb.positive(0))
self.assertIsNone(fetch_hltb.positive("n/a"))
self.assertIsNone(fetch_hltb.positive(None))
class HltbCacheTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.con = db.connect(os.path.join(self.tmp.name, "test.db"))
db.init(self.con)
self.addCleanup(self.con.close)
def test_entry_roundtrip(self):
entry = SimpleNamespace(
game_name="Hades", release_world=2020, game_id=1,
main_story=20.5, main_extra=30.0, completionist=50.0,
extra_field="ignored")
data = fetch_hltb.entry_to_dict(entry)
self.assertNotIn("extra_field", data)
restored = fetch_hltb.entry_from_dict(data)
self.assertEqual(restored.game_name, "Hades")
self.assertEqual(restored.main_story, 20.5)
self.assertEqual(restored.game_id, 1)
def test_cache_roundtrip(self):
entries = [{"game_name": "Hades", "release_world": 2020, "game_id": 1,
"main_story": 20.5, "main_extra": 30.0, "completionist": 50.0}]
fetch_hltb.cache_put(self.con, "hades", entries)
self.assertEqual(fetch_hltb.cache_get(self.con, "hades", 7), entries)
def test_cache_miss_returns_none(self):
self.assertIsNone(fetch_hltb.cache_get(self.con, "unknown", 7))
def test_cache_expires(self):
old = (datetime.now(timezone.utc) - timedelta(days=10)).isoformat(timespec="seconds")
self.con.execute(
"INSERT INTO hltb_cache (query, fetched_at, results) VALUES (?, ?, ?)",
("hades", old, "[]"))
self.con.commit()
self.assertIsNone(fetch_hltb.cache_get(self.con, "hades", 7))
self.assertEqual(fetch_hltb.cache_get(self.con, "hades", 0), None)
def test_cache_clear(self):
fetch_hltb.cache_put(self.con, "a", [])
fetch_hltb.cache_put(self.con, "b", [])
self.assertEqual(fetch_hltb.cache_clear(self.con), 2)
self.assertEqual(self.con.execute("SELECT COUNT(*) FROM hltb_cache").fetchone()[0], 0)
def test_cache_prune_removes_old_rows(self):
old = (datetime.now(timezone.utc) - timedelta(days=120)).isoformat(timespec="seconds")
self.con.execute(
"INSERT INTO hltb_cache (query, fetched_at, results) VALUES (?, ?, ?)",
("old", old, "[]"))
self.con.commit()
fetch_hltb.cache_put(self.con, "new", [])
self.assertEqual(fetch_hltb.cache_prune(self.con), 1)
rows = {r[0] for r in self.con.execute("SELECT query FROM hltb_cache")}
self.assertEqual(rows, {"new"})
if __name__ == "__main__":
unittest.main()