mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-18 07:51:27 +00:00
109 lines
3.7 KiB
Python
109 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
from scene_search.database import connect, init_db
|
|
from scene_search.jellyfin_index import reconcile_selected_libraries
|
|
|
|
|
|
class LibrarySelectionReconciliationTests(unittest.TestCase):
|
|
def _insert_media(
|
|
self,
|
|
db_path: Path,
|
|
media_id: str,
|
|
library_id: str,
|
|
) -> None:
|
|
with connect(db_path) as con:
|
|
con.execute(
|
|
"""
|
|
INSERT INTO media (
|
|
id, source_path, title, content_sha256, size_bytes,
|
|
mtime_ns, interval_ms, model_id, embedding_dim,
|
|
source_kind, jellyfin_item_id, jellyfin_library_id
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
""",
|
|
(
|
|
media_id,
|
|
f"jellyfin://{media_id}",
|
|
media_id,
|
|
"hash",
|
|
0,
|
|
0,
|
|
10000,
|
|
"model",
|
|
2,
|
|
"jellyfin",
|
|
media_id,
|
|
library_id,
|
|
),
|
|
)
|
|
|
|
def test_deselected_library_is_removed(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
db_path = root / "scenes.sqlite3"
|
|
thumbs_dir = root / "thumbs"
|
|
init_db(db_path)
|
|
self._insert_media(db_path, "keep", "lib-a")
|
|
self._insert_media(db_path, "remove", "lib-b")
|
|
|
|
for media_id in ("keep", "remove"):
|
|
folder = thumbs_dir / media_id
|
|
folder.mkdir(parents=True)
|
|
(folder / "frame.jpg").write_bytes(b"x")
|
|
|
|
fake_settings = SimpleNamespace(
|
|
db_path=db_path,
|
|
thumbs_dir=thumbs_dir,
|
|
)
|
|
with patch("scene_search.jellyfin_index.settings", fake_settings):
|
|
result = reconcile_selected_libraries({"lib-a"})
|
|
|
|
self.assertEqual(result["removed_library_ids"], ["lib-b"])
|
|
self.assertEqual(result["removed_libraries"], 1)
|
|
self.assertEqual(result["removed_media"], 1)
|
|
|
|
with connect(db_path) as con:
|
|
remaining = {
|
|
row["id"]
|
|
for row in con.execute("SELECT id FROM media").fetchall()
|
|
}
|
|
|
|
self.assertEqual(remaining, {"keep"})
|
|
self.assertTrue((thumbs_dir / "keep").exists())
|
|
self.assertFalse((thumbs_dir / "remove").exists())
|
|
|
|
def test_empty_selection_removes_all_jellyfin_media(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
db_path = root / "scenes.sqlite3"
|
|
thumbs_dir = root / "thumbs"
|
|
init_db(db_path)
|
|
self._insert_media(db_path, "a", "lib-a")
|
|
self._insert_media(db_path, "b", "lib-b")
|
|
|
|
fake_settings = SimpleNamespace(
|
|
db_path=db_path,
|
|
thumbs_dir=thumbs_dir,
|
|
)
|
|
with patch("scene_search.jellyfin_index.settings", fake_settings):
|
|
result = reconcile_selected_libraries(set())
|
|
|
|
self.assertEqual(result["removed_libraries"], 2)
|
|
self.assertEqual(result["removed_media"], 2)
|
|
with connect(db_path) as con:
|
|
count = con.execute(
|
|
"SELECT COUNT(*) FROM media WHERE source_kind = 'jellyfin'"
|
|
).fetchone()[0]
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|