Files
Curator/tests/test_series.py
T

149 lines
5.9 KiB
Python

"""Tests for series naming/scanning (src.core.series) + FileManager episodes."""
import pytest
from src.core.series import (
episode_filename,
is_canonical_episode,
parse_episode,
scan_series,
season_folder_name,
series_folder_stem,
)
@pytest.mark.parametrize("name,expected", [
("Breaking Bad S01E02.mkv", (1, 2)),
("bb.s1e2.mkv", (1, 2)),
("Show 01x02 x264.mkv", (1, 2)),
("Series.S01E02E03.mkv", (1, 2)), # multi-ep → first
("Series S12E145.mkv", (12, 145)),
("No code here.mkv", None),
])
def test_parse_episode(name, expected):
assert parse_episode(name) == expected
def test_season_folder_name():
assert season_folder_name(0) == "Specials"
assert season_folder_name(1) == "Season 01"
assert season_folder_name(12) == "Season 12"
def test_episode_filename_keeps_original_name():
assert episode_filename(1, 2, "bb.pilot.hdtv", ".mkv") == "S01E02 - bb.pilot.hdtv.mkv"
# already-canonical stem → no doubled prefix
assert episode_filename(1, 2, "S01E02 - bb.pilot.hdtv", ".mkv") == \
"S01E02 - bb.pilot.hdtv.mkv"
# stem that was only the code → collapses to just the code
assert episode_filename(3, 4, "S03E04", ".mkv") == "S03E04.mkv"
def test_series_folder_stem():
assert series_folder_stem("Breaking Bad", 2008, 1396) == "Breaking Bad (2008) [tmdbid-1396]"
assert series_folder_stem("Neznámý", None, 42) == "Neznámý [tmdbid-42]"
assert series_folder_stem("Jen název", None, None) == "Jen název"
def test_is_canonical_episode(tmp_path):
folder = tmp_path / "Breaking Bad (2008) [tmdbid-1396]" / "Season 01"
folder.mkdir(parents=True)
good = folder / "S01E02 - bb.pilot.mkv"
good.write_bytes(b"x")
bad = folder / "bb.s01e02.mkv"
bad.write_bytes(b"x")
assert is_canonical_episode(good)
assert not is_canonical_episode(bad)
def test_scan_series(tmp_path):
lib = tmp_path / "Seriály"
s1 = lib / "Breaking Bad (2008) [tmdbid-1396]" / "Season 01"
s1.mkdir(parents=True)
(s1 / "Breaking Bad S01E01.mkv").write_bytes(b"x")
(s1 / "Breaking Bad S01E02.mkv").write_bytes(b"x")
(s1 / "poster.jpg").write_bytes(b"img") # sidecar → skipped
found = scan_series(lib, "Seriály")
assert len(found) == 1
ser = found[0]
assert ser.title == "Breaking Bad" and ser.year == "2008" and ser.tmdb_id == 1396
assert ser.library == "Seriály"
assert len(ser.episodes) == 2
seasons = ser.seasons()
assert list(seasons.keys()) == [1] and len(seasons[1]) == 2
class TestFileManagerSeries:
@pytest.fixture
def file_manager(self, tmp_path, monkeypatch):
import src.core.config as config_module
monkeypatch.setattr(config_module, "GLOBAL_CONFIG_FILE", tmp_path / ".gtag")
from src.core.tag_manager import TagManager
from src.core.file_manager import FileManager
fm = FileManager(TagManager())
fm.set_pool_dir(tmp_path / "pool")
return fm
def test_import_episodes_places_them(self, file_manager, tmp_path):
for n in ("bb.S01E01.mkv", "bb.S01E02.mkv", "notes.txt_S00.dummy"):
(tmp_path / n).write_bytes(b"x")
result = file_manager.import_episodes(
[tmp_path / "bb.S01E01.mkv", tmp_path / "bb.S01E02.mkv"],
"Breaking Bad", year=2008, tmdb_id=1396)
assert result["imported"] == 2
base = (tmp_path / "pool" / "Seriály"
/ "Breaking Bad (2008) [tmdbid-1396]" / "Season 01")
assert (base / "S01E01 - bb.mkv").exists()
assert (base / "S01E02 - bb.mkv").exists()
def test_import_episodes_into_mixed_library(self, file_manager, tmp_path):
file_manager.set_libraries([{"name": "Hlavní", "kind": "mixed"}])
(tmp_path / "bb.S01E01.mkv").write_bytes(b"x")
result = file_manager.import_episodes(
[tmp_path / "bb.S01E01.mkv"], "Breaking Bad", year=2008, tmdb_id=1396,
library="Hlavní")
assert result["imported"] == 1
assert (tmp_path / "pool" / "Hlavní" / "Breaking Bad (2008) [tmdbid-1396]"
/ "Season 01" / "S01E01 - bb.mkv").exists()
# a mixed library is scanned for series too
found = file_manager.load_series()
assert [s.title for s in found] == ["Breaking Bad"]
def test_import_episodes_skips_uncoded(self, file_manager, tmp_path):
(tmp_path / "random.mkv").write_bytes(b"x")
result = file_manager.import_episodes(
[tmp_path / "random.mkv"], "Breaking Bad", year=2008)
assert result["imported"] == 0
assert result["skipped_no_code"] == ["random.mkv"]
def test_rename_episodes_to_canonical(self, file_manager, tmp_path):
# a mis-named episode sitting in the wrong place
folder = (tmp_path / "pool" / "Seriály"
/ "Breaking Bad (2008) [tmdbid-1396]")
folder.mkdir(parents=True)
(folder / "bb.1x03.HDTV.mkv").write_bytes(b"x")
series_list = file_manager.load_series()
assert len(series_list) == 1
result = file_manager.rename_episodes_to_canonical(series_list[0])
assert result["renamed"] == [("bb.1x03.HDTV.mkv", "S01E03 - bb.HDTV.mkv")]
assert (folder / "Season 01" / "S01E03 - bb.HDTV.mkv").exists()
def test_apply_series_edits_renames_folder_and_episodes(self, file_manager, tmp_path):
folder = tmp_path / "pool" / "Seriály" / "Wrong Name"
(folder / "Season 01").mkdir(parents=True)
(folder / "Season 01" / "old S01E01.mkv").write_bytes(b"x")
ser = file_manager.load_series()[0]
result = file_manager.apply_series_edits(
ser, title="Breaking Bad", year=2008, tmdb_id=1396)
new_folder = (tmp_path / "pool" / "Seriály"
/ "Breaking Bad (2008) [tmdbid-1396]")
assert result["folder"] == new_folder
assert (new_folder / "Season 01" / "S01E01 - old.mkv").exists()
assert not folder.exists()