feat: add FTS5 search
This commit is contained in:
1 parent
c441d770e2
commit
f1624cfde2
17 files changed
+232
-26
No files matched your search
+18
-1
@@ -9,6 +9,7 @@ import threading
|
||||
import time
|
||||
import unittest
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from http.server import ThreadingHTTPServer
|
||||
from unittest import mock
|
||||
@@ -38,6 +39,7 @@ class ApiTests(unittest.TestCase):
|
||||
db.init(cls.con)
|
||||
db.seed_moods(cls.con)
|
||||
cls._seed(cls.con)
|
||||
db.refresh_search_index(cls.con)
|
||||
cls.con.close()
|
||||
|
||||
cls.db_patch = mock.patch.object(db, "DEFAULT_DB", cls.db_path)
|
||||
@@ -103,8 +105,9 @@ class ApiTests(unittest.TestCase):
|
||||
con.commit()
|
||||
|
||||
def get(self, path, expect=200):
|
||||
url = self.base + urllib.parse.quote(path, safe="/?=&%:,")
|
||||
try:
|
||||
with urllib.request.urlopen(self.base + path, timeout=10) as resp:
|
||||
with urllib.request.urlopen(url, timeout=10) as resp:
|
||||
status = resp.status
|
||||
body = resp.read().decode("utf-8")
|
||||
except urllib.error.HTTPError as exc:
|
||||
@@ -189,6 +192,20 @@ class ApiTests(unittest.TestCase):
|
||||
self.assertEqual({g["appid"] for g in self.get("/api/games?unplayed=1")["items"]}, {2})
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?favorite=1")["items"]], [1])
|
||||
|
||||
def test_search_by_name_description_tags_and_translation(self):
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?q=alph")["items"]], [1])
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?q=Short")["items"]], [1])
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?q=About")["items"]], [1])
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?q=Party")["items"]], [1, 2])
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?q=Кратко")["items"]], [1])
|
||||
self.assertEqual(self.get("/api/games?q=заметка")["items"], [])
|
||||
self.assertEqual(self.get("/api/games?q=%%%")["items"], [])
|
||||
|
||||
def test_fts_query_builder(self):
|
||||
self.assertEqual(server.fts_query("Baldur's Gate"), '"baldur"* "gate"*')
|
||||
self.assertEqual(server.fts_query(" "), "")
|
||||
self.assertEqual(server.fts_query("!!! ???"), "")
|
||||
|
||||
def test_games_sort_and_pagination(self):
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?sort=review")["items"]], [1, 2, 3])
|
||||
self.assertEqual([g["appid"] for g in self.get("/api/games?sort=hours")["items"]], [1, 3, 2])
|
||||
|
||||
@@ -65,6 +65,7 @@ class SchemaVersionTests(unittest.TestCase):
|
||||
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)
|
||||
@@ -85,6 +86,9 @@ class SchemaVersionTests(unittest.TestCase):
|
||||
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)
|
||||
@@ -98,6 +102,41 @@ class SchemaVersionTests(unittest.TestCase):
|
||||
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', '<p>Deep dungeon</p>', '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)
|
||||
|
||||
@@ -123,6 +123,11 @@ class ImportSteamTests(unittest.TestCase):
|
||||
self.assertEqual(meta["steam_root"], self.root)
|
||||
self.assertIn("imported_at", meta)
|
||||
|
||||
self.assertEqual(con.execute("SELECT COUNT(*) FROM game_fts").fetchone()[0], 2)
|
||||
hits = [r[0] for r in con.execute(
|
||||
"SELECT appid FROM game_fts WHERE game_fts MATCH 'alpha*'")]
|
||||
self.assertEqual(hits, [100])
|
||||
|
||||
def test_incremental_keeps_metadata_and_preserves_personal(self):
|
||||
con = self._run({100: game_info("Old Name", review=80)})
|
||||
con.execute(
|
||||
@@ -157,6 +162,10 @@ class ImportSteamTests(unittest.TestCase):
|
||||
|
||||
self.assertEqual(self._game(con, 300)["name"], "Gamma")
|
||||
self.assertEqual(self._personal(con, 300)["status"], "backlog")
|
||||
self.assertEqual(con.execute("SELECT COUNT(*) FROM game_fts").fetchone()[0], 2)
|
||||
hits = [r[0] for r in con.execute(
|
||||
"SELECT appid FROM game_fts WHERE game_fts MATCH 'gamma*'")]
|
||||
self.assertEqual(hits, [300])
|
||||
|
||||
def test_full_import_preserves_personal_attributes_and_translations(self):
|
||||
con = self._run({100: game_info("Initial", review=80)})
|
||||
|
||||
Reference in new issue
Block a user