149 lines
4.6 KiB
Python
149 lines
4.6 KiB
Python
"""Tests for the NFO reader (src.core.nfo)."""
|
|
|
|
from src.core.nfo import NfoData, find_nfo, read_nfo
|
|
|
|
MOVIE_NFO = """<?xml version="1.0" encoding="utf-8"?>
|
|
<movie>
|
|
<title>Matrix</title>
|
|
<year>1999</year>
|
|
<genre>Akční</genre>
|
|
<genre>Sci-Fi</genre>
|
|
<country>USA</country>
|
|
<rating>8.7</rating>
|
|
<uniqueid type="tmdb">603</uniqueid>
|
|
</movie>
|
|
"""
|
|
|
|
|
|
def _movie(tmp_path):
|
|
v = tmp_path / "Matrix (1999) [tmdbid-603].mkv"
|
|
v.write_bytes(b"x")
|
|
return v
|
|
|
|
|
|
def test_read_nfo_sibling(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(MOVIE_NFO, encoding="utf-8")
|
|
|
|
nfo = read_nfo(video)
|
|
assert nfo == NfoData(
|
|
title="Matrix",
|
|
year="1999",
|
|
genres=["Akční", "Sci-Fi"],
|
|
countries=["USA"],
|
|
rating=87,
|
|
tmdb_id=603,
|
|
)
|
|
|
|
|
|
def test_rating_scaled_to_percent(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
"<movie><rating>7.3</rating></movie>", encoding="utf-8"
|
|
)
|
|
assert read_nfo(video).rating == 73
|
|
|
|
|
|
def test_rating_from_ratings_block(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
"<movie><ratings><rating name='tmdb'><value>9.1</value></rating>"
|
|
"</ratings></movie>",
|
|
encoding="utf-8",
|
|
)
|
|
assert read_nfo(video).rating == 91
|
|
|
|
|
|
def test_tmdbid_from_tmdbid_tag(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
"<movie><tmdbid>603</tmdbid></movie>", encoding="utf-8"
|
|
)
|
|
assert read_nfo(video).tmdb_id == 603
|
|
|
|
|
|
def test_csfd_url_from_uniqueid_bare_id(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
'<movie><uniqueid type="csfd">9499</uniqueid></movie>', encoding="utf-8"
|
|
)
|
|
assert read_nfo(video).csfd_url == "https://www.csfd.cz/film/9499/"
|
|
|
|
|
|
def test_csfd_url_from_uniqueid_full_url(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
'<movie><uniqueid type="csfd">https://www.csfd.cz/film/9499-matrix/'
|
|
"</uniqueid></movie>",
|
|
encoding="utf-8",
|
|
)
|
|
assert read_nfo(video).csfd_url == "https://www.csfd.cz/film/9499-matrix/"
|
|
|
|
|
|
def test_csfd_url_from_legacy_csfdid_tag(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
"<movie><csfdid>12345</csfdid></movie>", encoding="utf-8"
|
|
)
|
|
assert read_nfo(video).csfd_url == "https://www.csfd.cz/film/12345/"
|
|
|
|
|
|
def test_csfd_url_absent_is_none(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text(
|
|
"<movie><title>Matrix</title></movie>", encoding="utf-8"
|
|
)
|
|
assert read_nfo(video).csfd_url is None
|
|
|
|
|
|
def test_find_prefers_sibling_over_movie_nfo(tmp_path):
|
|
folder = tmp_path / "Matrix (1999)"
|
|
folder.mkdir()
|
|
video = folder / "Matrix (1999).mkv"
|
|
video.write_bytes(b"x")
|
|
(folder / "movie.nfo").write_text(MOVIE_NFO, encoding="utf-8")
|
|
# no sibling → movie.nfo is used
|
|
assert find_nfo(video).name == "movie.nfo"
|
|
# sibling present → it wins
|
|
video.with_suffix(".nfo").write_text(MOVIE_NFO, encoding="utf-8")
|
|
assert find_nfo(video).name == "Matrix (1999).nfo"
|
|
|
|
|
|
def test_missing_nfo_returns_none(tmp_path):
|
|
assert read_nfo(_movie(tmp_path)) is None
|
|
|
|
|
|
def test_broken_nfo_returns_none(tmp_path):
|
|
video = _movie(tmp_path)
|
|
video.with_suffix(".nfo").write_text("<movie><genre>unclosed", encoding="utf-8")
|
|
assert read_nfo(video) is None
|
|
|
|
|
|
def test_field_values():
|
|
nfo = NfoData(year="1999", genres=["A", "B"], countries=["USA"], rating=87)
|
|
assert nfo.field_values("genres") == ["A", "B"]
|
|
assert nfo.field_values("countries") == ["USA"]
|
|
assert nfo.field_values("year") == ["1999"]
|
|
assert nfo.field_values("rating") == ["87"]
|
|
assert nfo.field_values("directors") == []
|
|
|
|
|
|
def test_refresh_tags_from_nfo(tmp_path, monkeypatch):
|
|
import src.core.config as config_module
|
|
|
|
monkeypatch.setattr(config_module, "GLOBAL_CONFIG_FILE", tmp_path / ".gtag")
|
|
from src.core.file_manager import FileManager
|
|
from src.core.tag_manager import TagManager
|
|
|
|
fm = FileManager(TagManager())
|
|
fm.set_pool_dir(tmp_path / "pool")
|
|
(tmp_path / "raw.mkv").write_bytes(b"x")
|
|
movie = fm.import_movie(tmp_path / "raw.mkv", "Matrix", year=1999, tmdb_id=603)
|
|
movie.file_path.with_suffix(".nfo").write_text(MOVIE_NFO, encoding="utf-8")
|
|
|
|
result = fm.refresh_tags_from_nfo()
|
|
assert result == {"updated": 1, "without_nfo": 0}
|
|
paths = {t.full_path for t in movie.tags}
|
|
assert {"Žánr/Akční", "Žánr/Sci-Fi", "Rok/1999", "Země původu/USA"} <= paths
|
|
assert movie.meta == {"year": "1999", "rating": 87}
|