180 lines
7.0 KiB
Python
180 lines
7.0 KiB
Python
"""Tests for mood parsing, recommendations and similar games."""
|
|
|
|
import os
|
|
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
|
|
import reco
|
|
|
|
|
|
class RecoFixture(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.tmp.cleanup)
|
|
self.con = db.connect(os.path.join(self.tmp.name, "reco.db"))
|
|
self.addCleanup(self.con.close)
|
|
db.init(self.con)
|
|
db.seed_moods(self.con)
|
|
self._seed()
|
|
|
|
def _seed(self):
|
|
con = self.con
|
|
games = [
|
|
# appid, name, type, review, metacritic, release
|
|
(1, "Alpha", "game", 90, 90, "2020-05-01"),
|
|
(2, "Beta", "game", 85, 85, "2019-01-01"),
|
|
(3, "Gamma", "game", 95, 95, "2018-01-01"),
|
|
(4, "Delta", "game", 70, 70, "2021-01-01"),
|
|
(5, "Ignored", "game", 99, 99, "2017-01-01"),
|
|
(6, "Unknown", "game", None, None, None),
|
|
(7, "Tool", "tool", 99, 99, "2016-01-01"),
|
|
(8, "NoTags", "game", 80, 80, "2015-01-01"),
|
|
]
|
|
con.executemany(
|
|
"INSERT INTO games (appid, name, type, review_pct, metacritic, release_date) VALUES (?,?,?,?,?,?)",
|
|
games,
|
|
)
|
|
playtime = [
|
|
(1, 0, None), (2, 0, None), (3, 0, None), (4, 600, "2026-01-01"),
|
|
(5, 0, None), (6, 0, None), (7, 0, None), (8, 0, None),
|
|
]
|
|
con.executemany("INSERT INTO game_playtime (appid, minutes, last_played) VALUES (?,?,?)", playtime)
|
|
con.execute("INSERT INTO game_install (appid, installed) VALUES (1, 1)")
|
|
personal = [(5, "ignore", None, 0), (4, "played", 8, 0), (1, "backlog", None, 0)]
|
|
con.executemany(
|
|
"INSERT INTO personal (appid, status, rating, favorite) VALUES (?,?,?,?)", personal)
|
|
|
|
tags = {1: [1, 2, 3], 2: [1, 2], 3: [4], 4: [1, 2, 3],
|
|
5: [1, 2, 3], 6: [1, 2, 3], 7: [1, 2, 3]}
|
|
con.executemany(
|
|
"INSERT INTO game_tags (appid, tag_id) VALUES (?,?)",
|
|
[(appid, tag) for appid, ids in tags.items() for tag in ids])
|
|
con.executemany(
|
|
"INSERT INTO tag_names (tag_id, name) VALUES (?,?)",
|
|
[(1, "RPG"), (2, "Party-Based RPG"), (3, "Dungeon Crawler"), (4, "Horror")])
|
|
|
|
genres = {1: ["RPG", "Adventure"], 2: ["RPG"], 3: ["Horror"], 4: ["RPG"],
|
|
5: ["RPG"], 6: ["RPG"], 7: ["RPG"]}
|
|
con.executemany(
|
|
"INSERT INTO game_genres (appid, genre) VALUES (?,?)",
|
|
[(appid, genre) for appid, names in genres.items() for genre in names])
|
|
con.commit()
|
|
|
|
def mood(self):
|
|
return {
|
|
"description": "test",
|
|
"genres": ["RPG", "Adventure"],
|
|
"tags": {"RPG": 1.0, "Party-Based RPG": 1.3, "Dungeon Crawler": 1.2},
|
|
}
|
|
|
|
def names(self, results):
|
|
return [g["name"] for g in results]
|
|
|
|
|
|
class MoodTests(RecoFixture):
|
|
def test_russian_text(self):
|
|
mood = reco.mood_from_text("рпг про подземелья и отряд")
|
|
self.assertEqual(mood["tags"]["RPG"], 1.0)
|
|
self.assertEqual(mood["tags"]["Dungeon Crawler"], 1.2)
|
|
self.assertEqual(mood["tags"]["Party-Based RPG"], 1.3)
|
|
self.assertEqual(mood["genres"], ["RPG", "Adventure"])
|
|
|
|
def test_english_text(self):
|
|
mood = reco.mood_from_text("dark fantasy coop horror")
|
|
self.assertIn("Dark Fantasy", mood["tags"])
|
|
self.assertIn("Co-op", mood["tags"])
|
|
self.assertIn("Horror", mood["tags"])
|
|
self.assertEqual(mood["description"], "dark fantasy coop horror")
|
|
|
|
def test_empty_text(self):
|
|
mood = reco.mood_from_text("")
|
|
self.assertEqual(mood, {"description": "", "genres": [], "tags": {}})
|
|
|
|
def test_load_preset(self):
|
|
mood = reco.load_mood(self.con, preset="dark-fantasy")
|
|
self.assertEqual(mood["preset"], "dark-fantasy")
|
|
self.assertIn("Dark Fantasy", mood["tags"])
|
|
|
|
def test_load_missing_preset(self):
|
|
with self.assertRaises(KeyError):
|
|
reco.load_mood(self.con, preset="nope")
|
|
|
|
def test_load_text_and_empty(self):
|
|
mood = reco.load_mood(self.con, text="кооп")
|
|
self.assertIn("Co-op", mood["tags"])
|
|
with self.assertRaises(ValueError):
|
|
reco.load_mood(self.con)
|
|
|
|
|
|
class RecommendTests(RecoFixture):
|
|
def test_default_excludes_unknown_ignored_and_non_games(self):
|
|
_, results = reco.recommend(self.con, self.mood(), limit=10)
|
|
names = self.names(results)
|
|
self.assertEqual(names[0], "Alpha")
|
|
self.assertEqual(set(names), {"Alpha", "Beta", "Gamma", "Delta", "NoTags"})
|
|
self.assertNotIn("Ignored", names)
|
|
self.assertNotIn("Unknown", names)
|
|
self.assertNotIn("Tool", names)
|
|
alpha = results[0]
|
|
self.assertEqual(alpha["matched"], ["RPG", "Party-Based RPG", "Dungeon Crawler"])
|
|
self.assertTrue(alpha["installed"])
|
|
self.assertEqual(alpha["review"], 90)
|
|
self.assertEqual(alpha["hours"], 0.0)
|
|
|
|
def test_unplayed_only(self):
|
|
_, results = reco.recommend(self.con, self.mood(), unplayed_only=True)
|
|
self.assertEqual(set(self.names(results)), {"Alpha", "Beta", "Gamma", "NoTags"})
|
|
|
|
def test_min_review(self):
|
|
_, results = reco.recommend(self.con, self.mood(), min_review=95)
|
|
self.assertEqual(self.names(results), ["Gamma"])
|
|
|
|
def test_allowed_statuses(self):
|
|
_, results = reco.recommend(self.con, self.mood(), allowed_statuses=["ignore"])
|
|
self.assertEqual(self.names(results), ["Ignored"])
|
|
|
|
def test_allow_unknown(self):
|
|
_, results = reco.recommend(self.con, self.mood(), allow_unknown=True, limit=10)
|
|
self.assertIn("Unknown", self.names(results))
|
|
|
|
def test_limit(self):
|
|
_, results = reco.recommend(self.con, self.mood(), limit=2)
|
|
self.assertEqual(len(results), 2)
|
|
|
|
def test_no_eligible_games(self):
|
|
mood, results = reco.recommend(self.con, self.mood(), allowed_statuses=["tribute"])
|
|
self.assertEqual(mood["description"], "test")
|
|
self.assertEqual(results, [])
|
|
|
|
|
|
class SimilarGamesTests(RecoFixture):
|
|
def test_similar_games_by_shared_tags(self):
|
|
results = reco.similar_games(self.con, 1, limit=6)
|
|
ids = [g["appid"] for g in results]
|
|
self.assertEqual(ids[0], 4)
|
|
self.assertEqual(set(ids), {2, 4, 6})
|
|
self.assertNotIn(1, ids)
|
|
self.assertNotIn(5, ids)
|
|
self.assertNotIn(7, ids)
|
|
delta = results[0]
|
|
self.assertEqual(delta["matched"], ["RPG", "Party-Based RPG", "Dungeon Crawler"])
|
|
self.assertEqual(delta["year"], 2021)
|
|
self.assertEqual(delta["hours"], 10.0)
|
|
|
|
def test_requires_two_shared_tags(self):
|
|
ids = [g["appid"] for g in reco.similar_games(self.con, 3)]
|
|
self.assertEqual(ids, [])
|
|
|
|
def test_unknown_or_untagged_game(self):
|
|
self.assertEqual(reco.similar_games(self.con, 999), [])
|
|
self.assertEqual(reco.similar_games(self.con, 8), [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|