Files
unshackle/tests/core/test_download_archive_loop.py
T

340 lines
12 KiB
Python

"""Exercise the real selection, publication and completion loop with an in-memory service."""
import inspect
import logging
from pathlib import Path
from types import SimpleNamespace
import pytest
import requests
from unshackle.commands.dl import dl
from unshackle.core.config import config
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY
from unshackle.core.dl_options import DEFAULT_DOWNLOAD_PARAMS
from unshackle.core.download_archive import DownloadArchive
from unshackle.core.title_source import TitleSource
from unshackle.core.titles import Episode, Series
from unshackle.core.tracks import Audio, Tracks, Video
from unshackle.core.vaults import Vaults
class EXAMPLE(TitleSource):
NO_SUBTITLES = True
def __init__(self, height=720, count=1):
self.height = height
self.count = count
self.session = requests.Session()
self.ctx = None
def authenticate(self, *args):
pass
def get_titles(self):
return Series(
Episode(id_=f"example-episode-{n}", service=EXAMPLE, title="Example", season=1, number=n, language="en")
for n in range(1, self.count + 1)
)
def get_tracks(self, title):
return Tracks(
[
Video(
id_=f"v-{title.number}",
url="https://example.invalid/video",
language="en",
codec=Video.Codec.AVC,
range_=Video.Range.SDR,
height=self.height,
width=self.height * 16 // 9,
bitrate=1_000_000,
),
Audio(
id_=f"a-{title.number}",
url="https://example.invalid/audio",
language="en",
codec=Audio.Codec.AAC,
bitrate=128_000,
),
]
)
def get_chapters(self, title):
return []
def close(self):
self.session.close()
def __getattr__(self, name):
if name.startswith(("on_", "get_")):
return lambda *a, **k: None
raise AttributeError(name)
class Harness(dl):
vaults = Vaults()
def __getattr__(self, name):
return None
@pytest.fixture
def run(monkeypatch, tmp_path):
import unshackle.commands.dl as module
monkeypatch.setattr(config.directories, "cache", tmp_path / "cache")
monkeypatch.setattr(config.directories, "downloads", tmp_path / "downloads")
monkeypatch.setattr(config.directories, "temp", tmp_path / "temp")
monkeypatch.setattr(config, "subtitle", {})
monkeypatch.setattr("unshackle.core.providers.search_metadata", lambda *a, **k: None)
monkeypatch.setattr(
module, "load_tracks", lambda service, title, **k: setattr(title, "tracks", service.get_tracks(title))
)
monkeypatch.setattr(module.tags, "tag_file", lambda *a, **k: None)
monkeypatch.setattr(module.MediaInfo, "parse", lambda *a, **k: SimpleNamespace(general_tracks=[]))
monkeypatch.setattr(
Episode,
"get_filename",
lambda self, *a, folder=False, **k: "Example" if folder else f"Example.S01E{self.number:02}",
)
monkeypatch.setattr(module, "dispatch", lambda *a, **k: None)
downloads = []
muxes = []
def download(tracks, *a, **k):
for track in tracks:
config.directories.temp.mkdir(parents=True, exist_ok=True)
track.path = config.directories.temp / f"{track.id}.bin"
track.path.write_bytes(b"test")
downloads.append(track.id)
monkeypatch.setattr(module, "download_tracks_in_passes", download)
def mux(self, *a, **k):
muxes.append(list(self.videos))
path = config.directories.temp / f"mux-{len(muxes)}.mkv"
path.write_bytes(b"test mux")
return path, 0, []
monkeypatch.setattr(Tracks, "mux", mux)
def invoke(height=720, count=1, scope="cli", **flags):
cmd = Harness.__new__(Harness)
cmd.log = logging.getLogger("archive-test")
cmd.service = "EXAMPLE"
cmd.completed_files = []
cmd.already_present = []
cmd.skipped_titles = []
cmd.skipped_subtitles = []
cmd.archive_scope = scope
cmd.archive_cache = None
kwargs = {}
for name, param in inspect.signature(dl.result).parameters.items():
if name in DEFAULT_DOWNLOAD_PARAMS:
kwargs[name] = DEFAULT_DOWNLOAD_PARAMS[name]
elif name not in ("self", "service"):
kwargs[name] = None if param.default is inspect.Parameter.empty else param.default
kwargs.update(
quality=[], vcodec=[], acodec=[], range_=[], wanted=[], cdm_only=None, downloads=1, skip_existing=True
)
kwargs.update(flags)
try:
cmd.result(EXAMPLE(height, count), **kwargs)
finally:
DOWNLOAD_CANCELLED.clear()
DOWNLOAD_LICENCE_ONLY.clear()
return cmd
invoke.downloads = downloads
invoke.muxes = muxes
return invoke
def test_second_run_skips_and_force_downloads(run):
first = run()
assert len(first.completed_files) == 1 and not first.already_present
count = len(run.downloads)
progress = []
second = run(progress_sink=progress.append)
assert len(run.downloads) == count
assert not second.completed_files and not second.skipped_titles
assert second.already_present[0]["reason"] == "already_present"
assert any(event.get("already_present") for event in progress)
forced = run(force=True)
assert len(run.downloads) > count
assert forced.completed_files and not forced.already_present
def test_new_best_quality_downloads_instead_of_skipping(run):
run(height=720)
count = len(run.downloads)
assert run(height=1080).completed_files
assert len(run.downloads) > count
assert run(height=1080).already_present
def test_failed_publication_does_not_record(monkeypatch, run):
import unshackle.commands.dl as module
monkeypatch.setattr(module.os, "replace", lambda *a: (_ for _ in ()).throw(OSError("publication failed")))
monkeypatch.setattr(module.shutil, "move", lambda *a: (_ for _ in ()).throw(OSError("publication failed")))
with pytest.raises(OSError, match="publication failed"):
run()
assert not DownloadArchive(config.directories.cache).path.exists()
def test_api_scopes_and_unowned_worker_do_not_use_cli_archive(run):
run()
assert run(scope="api-" + "a" * 64).completed_files
assert run(scope="api-" + "b" * 64).completed_files
assert run(scope="api-" + "a" * 64).already_present
assert run(scope="api-unowned").completed_files
def test_mux_failure_does_not_record(monkeypatch, run):
monkeypatch.setattr(Tracks, "mux", lambda *a, **k: (Path("unused.mkv"), 2, ["mux failed"]))
with pytest.raises(SystemExit) as exc:
run()
assert exc.value.code == 1
assert not DownloadArchive(config.directories.cache).path.exists()
def test_download_failure_does_not_record(monkeypatch, run):
import unshackle.commands.dl as module
def fail(*a, **k):
raise RuntimeError("test download failure")
monkeypatch.setattr(module, "download_tracks_in_passes", fail)
result = run()
assert result.download_error is not None
assert not DownloadArchive(config.directories.cache).path.exists()
def test_opt_out_does_not_read_or_write_archive(run):
run()
result = run(skip_existing=False)
assert result.completed_files and not result.already_present
def test_missing_requested_quality_still_fails(run):
run(height=720)
with pytest.raises(SystemExit) as exc:
run(height=720, quality=[1080])
assert exc.value.code == 1
def test_partial_publication_of_multiple_variants_does_not_record(monkeypatch, run):
import unshackle.commands.dl as module
get_tracks = EXAMPLE.get_tracks
replace_file = module.os.replace
def variants(self, title):
tracks = get_tracks(self, title)
tracks.add(
Video(
id_="v-higher",
url="https://example.invalid/video",
language="en",
codec=Video.Codec.AVC,
range_=Video.Range.SDR,
height=1080,
width=1920,
bitrate=2_000_000,
)
)
return tracks
monkeypatch.setattr(EXAMPLE, "get_tracks", variants)
published = []
def replace(source, target):
if published:
raise OSError("second variant failed")
replace_file(source, target)
published.append(target)
def fail_move(*a, **k):
raise OSError("second variant failed")
monkeypatch.setattr(module.os, "replace", replace)
monkeypatch.setattr(module.shutil, "move", fail_move)
with pytest.raises(OSError, match="second variant failed"):
run(quality=[720, 1080])
assert len(published) == 1 and published[0].exists()
assert not DownloadArchive(config.directories.cache).path.exists()
@pytest.fixture
def get_tracks_calls(monkeypatch):
calls = []
get_tracks = EXAMPLE.get_tracks
def counted(self, title):
calls.append(title.number)
return get_tracks(self, title)
monkeypatch.setattr(EXAMPLE, "get_tracks", counted)
return calls
def test_max_downloads_counts_only_downloaded_titles_and_leaves_the_rest_untouched(run, get_tracks_calls):
run(count=1)
get_tracks_calls.clear()
result = run(count=4, max_downloads=2)
assert get_tracks_calls == [1, 2, 3]
assert [path.stem for path in result.completed_files] == ["Example.S01E02", "Example.S01E03"]
assert [entry["title"] for entry in result.already_present] == ["Example S01E01"]
assert not result.skipped_titles
get_tracks_calls.clear()
following = run(count=4, max_downloads=2)
assert get_tracks_calls == [1, 2, 3, 4]
assert [path.stem for path in following.completed_files] == ["Example.S01E04"]
@pytest.mark.parametrize("flags", [{}, {"skip_dl": True}, {"no_mux": True}])
def test_max_downloads_without_the_archive_takes_the_first_titles(run, get_tracks_calls, flags):
run(count=3, max_downloads=2, skip_existing=False, **flags)
assert get_tracks_calls == [1, 2]
def test_a_title_skipped_for_a_missing_track_does_not_count(monkeypatch, run):
get_tracks = EXAMPLE.get_tracks
monkeypatch.setattr(
EXAMPLE, "get_tracks", lambda self, title: get_tracks(self, title) if title.number > 1 else Tracks()
)
result = run(count=3, max_downloads=2, skip_missing=True)
assert [entry["title"] for entry in result.skipped_titles] == ["Example S01E01"]
assert len(result.completed_files) == 2
def test_a_title_that_published_no_file_does_not_count(monkeypatch, run, get_tracks_calls):
import unshackle.commands.dl as module
download = module.download_tracks_in_passes
def lose_first_title(tracks, *a, **k):
download(tracks, *a, **k)
for track in tracks:
if track.id.endswith("-1"):
track.path.unlink()
monkeypatch.setattr(module, "download_tracks_in_passes", lose_first_title)
result = run(count=3, max_downloads=2, no_mux=True)
assert get_tracks_calls == [1, 2, 3]
assert len(result.completed_files) == 4
def test_a_season_cut_short_by_max_downloads_gets_no_season_event(monkeypatch, run):
import unshackle.commands.dl as module
events = []
monkeypatch.setattr(module, "dispatch", lambda status, scope, *a, **k: events.append(scope))
run(count=3, max_downloads=2)
assert events == ["file", "file", "run"]
events.clear()
run(count=3, max_downloads=3, skip_existing=False)
assert events == ["file", "file", "file", "season", "run"]