98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
import json
|
|
|
|
from src.core.pool_index import PoolIndex, INDEX_FILENAME
|
|
from src.core.file import File
|
|
from src.core.tag_manager import TagManager
|
|
|
|
|
|
class TestPoolIndex:
|
|
"""Testy pro unifikovaný metadata index poolu"""
|
|
|
|
def test_empty_index_when_no_file(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
assert index.records == {}
|
|
assert index.get(tmp_path / "Filmy" / "x.mkv") is None
|
|
|
|
def test_set_and_get_by_relative_key(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
path = tmp_path / "Filmy" / "Matrix.mkv"
|
|
index.set(path, {"title": "Matrix"})
|
|
|
|
assert index.get(path) == {"title": "Matrix"}
|
|
# key is the pool-relative POSIX path
|
|
assert "Filmy/Matrix.mkv" in index.records
|
|
|
|
def test_set_persists_to_disk(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
index.set(tmp_path / "Filmy" / "A.mkv", {"title": "A"})
|
|
|
|
on_disk = json.loads((tmp_path / INDEX_FILENAME).read_text(encoding="utf-8"))
|
|
assert on_disk["movies"]["Filmy/A.mkv"]["title"] == "A"
|
|
|
|
def test_reload_reads_existing_index(self, tmp_path):
|
|
PoolIndex(tmp_path).set(tmp_path / "Filmy" / "A.mkv", {"title": "A"})
|
|
|
|
reloaded = PoolIndex(tmp_path)
|
|
assert reloaded.get(tmp_path / "Filmy" / "A.mkv") == {"title": "A"}
|
|
|
|
def test_delete_removes_record(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
path = tmp_path / "Filmy" / "A.mkv"
|
|
index.set(path, {"title": "A"})
|
|
index.delete(path)
|
|
|
|
assert index.get(path) is None
|
|
assert PoolIndex(tmp_path).get(path) is None
|
|
|
|
def test_corrupt_index_loads_empty(self, tmp_path):
|
|
(tmp_path / INDEX_FILENAME).write_text("{ not json", encoding="utf-8")
|
|
index = PoolIndex(tmp_path)
|
|
assert index.records == {}
|
|
|
|
|
|
class TestFileWithIndex:
|
|
"""File používá index místo sidecar souboru, je-li injektován"""
|
|
|
|
def test_index_backed_file_writes_no_sidecar(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
movie = tmp_path / "Filmy" / "Matrix.mkv"
|
|
movie.parent.mkdir(parents=True)
|
|
movie.write_bytes(b"x")
|
|
|
|
f = File(movie, TagManager(), index=index)
|
|
|
|
assert not f.metadata_filename.exists() # no sidecar
|
|
assert index.get(movie) is not None # record created in index
|
|
assert f.tags == [] # no automatic tags
|
|
|
|
def test_index_backed_metadata_persists_across_reload(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
movie = tmp_path / "Filmy" / "Matrix.mkv"
|
|
movie.parent.mkdir(parents=True)
|
|
movie.write_bytes(b"x")
|
|
|
|
tm = TagManager()
|
|
f = File(movie, tm, index=index)
|
|
f.title = "Matrix"
|
|
f.csfd_link = "https://csfd.cz/film/1"
|
|
f.add_tag("Žánr/Akční")
|
|
f.set_date("1999-03-31")
|
|
|
|
# Fresh index + File read from disk
|
|
index2 = PoolIndex(tmp_path)
|
|
f2 = File(movie, TagManager(), index=index2)
|
|
assert f2.title == "Matrix"
|
|
assert f2.csfd_link == "https://csfd.cz/film/1"
|
|
assert f2.date == "1999-03-31"
|
|
assert "Žánr/Akční" in {t.full_path for t in f2.tags}
|
|
|
|
def test_delete_metadata_removes_index_record(self, tmp_path):
|
|
index = PoolIndex(tmp_path)
|
|
movie = tmp_path / "Filmy" / "Matrix.mkv"
|
|
movie.parent.mkdir(parents=True)
|
|
movie.write_bytes(b"x")
|
|
|
|
f = File(movie, TagManager(), index=index)
|
|
f.delete_metadata()
|
|
assert index.get(movie) is None
|