mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 13:05:08 +00:00
534 lines
21 KiB
Python
534 lines
21 KiB
Python
"""Precedence of user-supplied external IDs in providers.resolve_by_ids."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from unshackle.core import providers
|
|
from unshackle.core.config import config
|
|
from unshackle.core.providers import anilist
|
|
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult
|
|
|
|
search_metadata = providers.search_metadata
|
|
|
|
ANIME_MAP = {
|
|
"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797},
|
|
"199": {"imdb": "tt0245429", "tmdb_movie": 129},
|
|
"16498": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 1, "tvdb_season": 1},
|
|
"20958": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 2, "tvdb_season": 2},
|
|
}
|
|
|
|
|
|
class FakeProvider(MetadataProvider):
|
|
"""Stands in for a real provider; records the ID it was asked for."""
|
|
|
|
def __init__(self, name: str, result: MetadataResult | None = None, raises: bool = False) -> None:
|
|
self.NAME = name
|
|
self._result = result
|
|
self._raises = raises
|
|
self.asked: list[int | str] = []
|
|
super().__init__()
|
|
|
|
def is_available(self) -> bool:
|
|
return True
|
|
|
|
def search(self, title: str, year: int | None, kind: str) -> MetadataResult | None:
|
|
raise AssertionError("resolve_by_ids must not search when an ID was supplied")
|
|
|
|
def get_by_id(self, provider_id: int | str, kind: str) -> MetadataResult | None:
|
|
self.asked.append(provider_id)
|
|
if self._raises:
|
|
raise ValueError("provider is down")
|
|
return self._result
|
|
|
|
def get_external_ids(self, provider_id: int | str, kind: str) -> ExternalIds:
|
|
return ExternalIds()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def isolate(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Keep every test off the network and on the default provider order."""
|
|
monkeypatch.setattr(config, "metadata_providers", [])
|
|
monkeypatch.setattr(providers, "fetch_external_ids", lambda *a, **kw: ExternalIds())
|
|
monkeypatch.setattr(providers, "enrich_ids", lambda result: None)
|
|
monkeypatch.setattr(
|
|
providers,
|
|
"search_metadata",
|
|
lambda *a, **kw: pytest.fail("search_metadata must not run when an ID was supplied"),
|
|
)
|
|
|
|
|
|
def patch_providers(monkeypatch: pytest.MonkeyPatch, available: dict[str, FakeProvider]) -> None:
|
|
monkeypatch.setattr(providers, "get_provider", lambda name: available.get(name))
|
|
|
|
|
|
def test_supplied_ids_survive_a_providers_own_answer(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
# tmdb answers with a different imdb id; the one the user gave must win
|
|
tmdb = FakeProvider(
|
|
"tmdb",
|
|
MetadataResult(title="Inception", year=2010, kind="movie", external_ids=ExternalIds(imdb_id="tt9999999")),
|
|
)
|
|
patch_providers(monkeypatch, {"tmdb": tmdb})
|
|
|
|
result = providers.resolve_by_ids(27205, "tt1375666", kind="movie")
|
|
|
|
assert result is not None
|
|
assert result.external_ids.tmdb_id == 27205
|
|
assert result.external_ids.tmdb_kind == "movie"
|
|
assert result.external_ids.imdb_id == "tt1375666"
|
|
|
|
|
|
def test_imdb_falls_through_to_omdb(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
imdb = FakeProvider("imdb", raises=True)
|
|
omdb = FakeProvider("omdb", MetadataResult(title="Inception", year=2010, kind="movie"))
|
|
patch_providers(monkeypatch, {"imdb": imdb, "omdb": omdb})
|
|
|
|
result = providers.resolve_by_ids(imdb_id="tt1375666", kind="movie")
|
|
|
|
assert result is not None
|
|
assert result.title == "Inception"
|
|
assert imdb.asked == ["tt1375666"]
|
|
assert omdb.asked == ["tt1375666"]
|
|
|
|
|
|
def test_all_providers_down_synthesizes_a_result(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
patch_providers(monkeypatch, {})
|
|
|
|
result = providers.resolve_by_ids(tvdb_id=73871, title="Futurama", year=1999, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.title == "Futurama"
|
|
assert result.year == 1999
|
|
assert result.external_ids.tvdb_id == 73871
|
|
|
|
|
|
def test_no_ids_searches(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[tuple] = []
|
|
monkeypatch.setattr(providers, "search_metadata", lambda *a, **kw: calls.append(a) or None)
|
|
patch_providers(monkeypatch, {})
|
|
|
|
providers.resolve_by_ids(title="Futurama", year=1999, kind="tv")
|
|
|
|
assert calls and calls[0][:3] == ("Futurama", 1999, "tv")
|
|
|
|
|
|
def test_no_ids_and_no_title_returns_none(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
patch_providers(monkeypatch, {})
|
|
assert providers.resolve_by_ids(kind="tv") is None
|
|
|
|
|
|
def test_per_kind_order_puts_tvdb_first_for_tv(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(config, "metadata_providers", {"tv": ["tvdb", "tmdb", "imdb", "omdb"]})
|
|
tvdb = FakeProvider("tvdb", MetadataResult(title="Futurama", year=1999, kind="tv"))
|
|
tmdb = FakeProvider("tmdb", MetadataResult(title="Wrong Show", year=2020, kind="tv"))
|
|
patch_providers(monkeypatch, {"tvdb": tvdb, "tmdb": tmdb})
|
|
|
|
result = providers.resolve_by_ids(615, tvdb_id=73871, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.title == "Futurama"
|
|
assert tmdb.asked == []
|
|
# movies keep the default order, so tmdb is consulted before tvdb there
|
|
assert [cls.NAME for cls in providers.provider_order("movie")] == list(providers.DEFAULT_ORDER)
|
|
|
|
|
|
def test_flat_list_config_applies_to_both_kinds(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(config, "metadata_providers", ["tvdb", "tmdb"])
|
|
assert [cls.NAME for cls in providers.provider_order("tv")] == ["tvdb", "tmdb"]
|
|
assert [cls.NAME for cls in providers.provider_order("movie")] == ["tvdb", "tmdb"]
|
|
assert [cls.NAME for cls in providers.provider_order()] == ["tvdb", "tmdb"]
|
|
|
|
|
|
def test_an_anilist_id_reaches_the_anilist_provider(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
anilist = FakeProvider(
|
|
"anilist",
|
|
MetadataResult(title="One Piece", year=1999, kind="tv", external_ids=ExternalIds(anilist_id=21)),
|
|
)
|
|
tmdb = FakeProvider("tmdb", MetadataResult(title="Wrong Show", year=2020, kind="tv"))
|
|
patch_providers(monkeypatch, {"anilist": anilist, "tmdb": tmdb})
|
|
|
|
result = providers.resolve_by_ids(anilist_id=21, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.title == "One Piece"
|
|
assert result.external_ids.anilist_id == 21
|
|
assert anilist.asked == [21]
|
|
assert tmdb.asked == []
|
|
|
|
|
|
def test_a_supplied_anilist_id_survives_a_provider_miss(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
patch_providers(monkeypatch, {})
|
|
|
|
result = providers.resolve_by_ids(anilist_id="21", title="One Piece", kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids.anilist_id == 21
|
|
|
|
|
|
def test_a_mal_reference_carries_only_what_the_lookup_answered(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
anilist = FakeProvider("anilist", MetadataResult(title="One Piece", external_ids=ExternalIds(anilist_id=21)))
|
|
patch_providers(monkeypatch, {"anilist": anilist})
|
|
|
|
result = providers.resolve_by_ids(anilist_id="mal:21", kind="tv")
|
|
|
|
assert result is not None
|
|
assert anilist.asked == ["mal:21"]
|
|
assert result.external_ids.anilist_id == 21
|
|
|
|
|
|
def test_a_mal_reference_that_misses_leaves_no_anilist_id(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
patch_providers(monkeypatch, {})
|
|
|
|
result = providers.resolve_by_ids(anilist_id="mal:21", title="One Piece", kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids.anilist_id is None
|
|
|
|
|
|
def test_anilist_combines_with_a_western_id(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
tmdb = FakeProvider("tmdb", MetadataResult(title="One Piece", year=1999, kind="tv"))
|
|
anilist = FakeProvider("anilist", MetadataResult(title="One Piece", external_ids=ExternalIds(anilist_id=21)))
|
|
patch_providers(monkeypatch, {"tmdb": tmdb, "anilist": anilist})
|
|
|
|
result = providers.resolve_by_ids(37854, anilist_id=21, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids.tmdb_id == 37854
|
|
assert result.external_ids.anilist_id == 21
|
|
|
|
|
|
def test_anime_puts_anilist_first_with_the_rest_behind_it() -> None:
|
|
ordered = [cls.NAME for cls in providers.provider_order("tv", anime=True)]
|
|
assert ordered[0] == "anilist"
|
|
assert ordered[1:] == [name for name in providers.DEFAULT_ORDER if name != "anilist"]
|
|
|
|
|
|
def test_without_the_hint_anilist_stays_last() -> None:
|
|
assert [cls.NAME for cls in providers.provider_order("tv")] == list(providers.DEFAULT_ORDER)
|
|
assert providers.DEFAULT_ORDER[-1] == "anilist"
|
|
|
|
|
|
def test_the_hint_cannot_add_a_provider_the_config_left_out(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(config, "metadata_providers", ["tmdb", "tvdb"])
|
|
assert [cls.NAME for cls in providers.provider_order("tv", anime=True)] == ["tmdb", "tvdb"]
|
|
|
|
|
|
def test_an_anime_hint_falls_through_to_the_normal_order_on_a_miss(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""anilist is asked first but knows no imdb ids, so imdb still answers."""
|
|
anilist = FakeProvider("anilist", None)
|
|
imdb = FakeProvider("imdb", MetadataResult(title="One Piece", year=1999, kind="tv"))
|
|
patch_providers(monkeypatch, {"anilist": anilist, "imdb": imdb})
|
|
|
|
result = providers.resolve_by_ids(imdb_id="tt0388629", kind="tv", anime=True)
|
|
|
|
assert result is not None
|
|
assert result.title == "One Piece"
|
|
# the imdb id is in no namespace anilist consumes, so it is never asked
|
|
assert anilist.asked == []
|
|
assert imdb.asked == ["tt0388629"]
|
|
|
|
|
|
def test_the_anime_hint_reaches_search(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[dict] = []
|
|
monkeypatch.setattr(providers, "search_metadata", lambda *a, **kw: calls.append({"args": a}) or None)
|
|
patch_providers(monkeypatch, {})
|
|
|
|
providers.resolve_by_ids(title="One Piece", year=1999, kind="tv", anime=True)
|
|
|
|
assert calls and calls[0]["args"][-1] is True
|
|
|
|
|
|
def test_the_service_provider_goes_first_with_the_rest_behind_it() -> None:
|
|
ordered = [cls.NAME for cls in providers.provider_order("movie", prefer="tmdb")]
|
|
assert ordered == ["tmdb", *(name for name in providers.DEFAULT_ORDER if name != "tmdb")]
|
|
|
|
|
|
def test_the_service_provider_cannot_add_one_the_config_left_out(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(config, "metadata_providers", ["imdb"])
|
|
assert [cls.NAME for cls in providers.provider_order("movie", prefer="tmdb")] == ["imdb"]
|
|
|
|
|
|
def test_the_service_provider_goes_before_anilist_on_an_anime_title() -> None:
|
|
ordered = [cls.NAME for cls in providers.provider_order("tv", anime=True, prefer="tmdb")]
|
|
assert ordered[:2] == ["tmdb", "anilist"]
|
|
assert ordered[2:] == [name for name in providers.DEFAULT_ORDER if name not in ("anilist", "tmdb")]
|
|
|
|
|
|
def test_the_user_order_stays_behind_both_hints(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(config, "metadata_providers", ["imdb", "tvdb", "anilist", "tmdb"])
|
|
ordered = [cls.NAME for cls in providers.provider_order("tv", anime=True, prefer="tmdb")]
|
|
assert ordered == ["tmdb", "anilist", "imdb", "tvdb"]
|
|
|
|
|
|
def test_an_anilist_service_provider_on_an_anime_title_keeps_anilist_first() -> None:
|
|
ordered = [cls.NAME for cls in providers.provider_order("tv", anime=True, prefer="anilist")]
|
|
assert ordered == ["anilist", *(name for name in providers.DEFAULT_ORDER if name != "anilist")]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("prefer", "expected"),
|
|
[
|
|
("IMDB", ["imdb", "tmdb"]),
|
|
("imdbapi", ["imdb", "tmdb"]),
|
|
("nope", ["tmdb", "imdb"]),
|
|
(5, ["tmdb", "imdb"]),
|
|
(None, ["tmdb", "imdb"]),
|
|
],
|
|
)
|
|
def test_the_service_provider_is_read_like_a_config_entry(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, prefer: Any, expected: list[str]
|
|
) -> None:
|
|
monkeypatch.setattr(config, "metadata_providers", ["tmdb", "imdb"])
|
|
with caplog.at_level("WARNING"):
|
|
assert [cls.NAME for cls in providers.provider_order("movie", prefer=prefer)] == expected
|
|
assert "Ignoring unknown" not in caplog.text
|
|
|
|
|
|
def test_the_service_provider_reaches_search(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[dict] = []
|
|
monkeypatch.setattr(providers, "search_metadata", lambda *a, **kw: calls.append({"args": a, "kw": kw}) or None)
|
|
patch_providers(monkeypatch, {})
|
|
|
|
providers.resolve_by_ids(title="Some Show", year=1999, kind="tv", prefer="tmdb")
|
|
|
|
assert calls and calls[0]["kw"]["prefer"] == "tmdb"
|
|
|
|
|
|
def test_the_service_provider_orders_a_supplied_id(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
imdb = FakeProvider("imdb", MetadataResult(title="Wrong Film", year=2020, kind="movie"))
|
|
tmdb = FakeProvider("tmdb", MetadataResult(title="Right Film", year=2010, kind="movie"))
|
|
patch_providers(monkeypatch, {"imdb": imdb, "tmdb": tmdb})
|
|
|
|
result = providers.resolve_by_ids(27205, "tt1375666", kind="movie", prefer="tmdb")
|
|
|
|
assert result is not None and result.title == "Right Film"
|
|
assert imdb.asked == []
|
|
|
|
|
|
def test_search_metadata_orders_by_the_service_provider(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[tuple] = []
|
|
monkeypatch.setattr(config, "disable_metadata", False)
|
|
monkeypatch.setattr(providers, "provider_order", lambda *a: calls.append(a) or [])
|
|
|
|
search_metadata("X", None, "movie", prefer="tmdb")
|
|
|
|
assert calls == [("movie", False, "tmdb")]
|
|
|
|
|
|
def test_all_supplied_ids_survive_together(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""anilist_id combined with a western trio member: every supplied ID reaches external_ids."""
|
|
tvdb = FakeProvider(
|
|
"tvdb",
|
|
MetadataResult(title="One Piece", year=1999, kind="tv", external_ids=ExternalIds(tvdb_id=81797)),
|
|
)
|
|
patch_providers(monkeypatch, {"tvdb": tvdb})
|
|
|
|
result = providers.resolve_by_ids(tvdb_id=81797, anilist_id="mal:21", imdb_id="tt0388629", kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids.tvdb_id == 81797
|
|
assert result.external_ids.imdb_id == "tt0388629"
|
|
# a mal: ref the provider never resolved contributes no anilist_id; a bare id
|
|
# would have been forced back in
|
|
assert result.external_ids.anilist_id is None
|
|
|
|
result = providers.resolve_by_ids(tvdb_id=81797, anilist_id=21, kind="tv")
|
|
assert result is not None
|
|
assert result.external_ids.tvdb_id == 81797
|
|
assert result.external_ids.anilist_id == 21
|
|
|
|
|
|
@pytest.fixture
|
|
def anime_map(monkeypatch: pytest.MonkeyPatch) -> list[bool]:
|
|
"""A stubbed AniList ID map; returns one entry per map read."""
|
|
reads: list[bool] = []
|
|
monkeypatch.setattr(anilist, "western_id_map", lambda: reads.append(True) or ANIME_MAP)
|
|
monkeypatch.setattr(providers, "get_provider", lambda name: None)
|
|
return reads
|
|
|
|
|
|
def anilist_hit(anilist_id: int = 21, kind: str = "tv", **ids: Any) -> MetadataResult:
|
|
return MetadataResult(title="One Piece", kind=kind, external_ids=ExternalIds(anilist_id=anilist_id, **ids))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("hit", "kind", "expected"),
|
|
[
|
|
(anilist_hit(), "tv", ExternalIds("tt0388629", 37854, "tv", 81797, 21)),
|
|
(anilist_hit(199, "movie"), "movie", ExternalIds("tt0245429", 129, "movie", None, 199)),
|
|
(anilist_hit(tmdb_id=37854, tmdb_kind="tv"), "tv", ExternalIds("tt0388629", 37854, "tv", 81797, 21)),
|
|
(anilist_hit(tvdb_id=1), "tv", ExternalIds(tvdb_id=1, anilist_id=21)),
|
|
(anilist_hit(), "movie", ExternalIds(anilist_id=21)),
|
|
(anilist_hit(199, "movie"), "tv", ExternalIds(anilist_id=199)),
|
|
],
|
|
ids=["series", "movie", "agreeing-id-fills-the-rest", "other-id-wins", "tv-node-for-a-movie", "movie-node-for-tv"],
|
|
)
|
|
def test_an_anilist_match_takes_the_mapped_western_ids(
|
|
anime_map: list[bool], hit: MetadataResult, kind: str, expected: ExternalIds
|
|
) -> None:
|
|
providers.fill_anime_ids(hit, kind)
|
|
|
|
assert hit.external_ids == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"ids",
|
|
[ExternalIds("tt1", 5, "tv", 7, 21), ExternalIds(imdb_id="tt1"), ExternalIds()],
|
|
ids=["all-western-ids", "not-anilist", "no-ids"],
|
|
)
|
|
def test_the_map_is_not_read_without_need(anime_map: list[bool], ids: ExternalIds) -> None:
|
|
before = ExternalIds(**vars(ids))
|
|
|
|
providers.fill_anime_ids(MetadataResult(kind="tv", external_ids=ids), "tv")
|
|
|
|
assert (anime_map, ids) == ([], before)
|
|
|
|
|
|
def test_a_supplied_anilist_id_takes_the_mapped_western_ids(
|
|
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
|
) -> None:
|
|
patch_providers(monkeypatch, {"anilist": FakeProvider("anilist", anilist_hit())})
|
|
|
|
result = providers.resolve_by_ids(anilist_id=21, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)
|
|
|
|
|
|
def test_the_tagging_lookup_fills_the_rest_without_a_tmdb_key(
|
|
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
|
) -> None:
|
|
patch_providers(monkeypatch, {"anilist": FakeProvider("anilist", anilist_hit())})
|
|
|
|
result = providers.resolve_by_ids(37854, anilist_id=21, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)
|
|
|
|
|
|
def test_a_supplied_western_id_wins_over_the_map(monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]) -> None:
|
|
patch_providers(monkeypatch, {"anilist": FakeProvider("anilist", anilist_hit())})
|
|
|
|
result = providers.resolve_by_ids(tvdb_id=1, anilist_id=21, kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids == ExternalIds(tvdb_id=1, anilist_id=21)
|
|
|
|
|
|
class SearchHit(FakeProvider):
|
|
def search(self, title: str, year: int | None, kind: str) -> MetadataResult | None:
|
|
return self._result
|
|
|
|
|
|
def test_a_search_hit_on_anilist_takes_the_mapped_western_ids(
|
|
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
|
) -> None:
|
|
monkeypatch.setattr(config, "disable_metadata", False)
|
|
monkeypatch.setattr(providers, "provider_order", lambda *a: [lambda: SearchHit("anilist", anilist_hit())])
|
|
|
|
result = search_metadata("One Piece", None, "tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)
|
|
|
|
|
|
def western_hit(language: str | None = "ja", kind: str = "tv", **ids: Any) -> MetadataResult:
|
|
ids = {"tmdb_id": 37854, "tmdb_kind": kind, **ids}
|
|
return MetadataResult(title="One Piece", kind=kind, original_language=language, external_ids=ExternalIds(**ids))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("hit", "kind", "anime", "season", "expected"),
|
|
[
|
|
(western_hit(), "tv", False, None, 21),
|
|
(western_hit("jpn"), "tv", False, None, 21),
|
|
(western_hit(None), "tv", True, None, 21),
|
|
(western_hit(tmdb_id=1429), "tv", False, 2, 20958),
|
|
(western_hit(tmdb_id=1429), "tv", False, None, None),
|
|
(western_hit(tmdb_id=129, kind="movie"), "movie", False, None, 199),
|
|
(western_hit(tmdb_kind="movie"), "tv", False, None, None),
|
|
(
|
|
MetadataResult(kind="movie", original_language="ja", external_ids=ExternalIds(tmdb_id=37854)),
|
|
"tv",
|
|
False,
|
|
None,
|
|
None,
|
|
),
|
|
(western_hit(anilist_id=7), "tv", False, None, 7),
|
|
],
|
|
ids=[
|
|
"ja",
|
|
"alpha-3",
|
|
"anime-run",
|
|
"season",
|
|
"no-season",
|
|
"movie",
|
|
"movie-id-on-tv",
|
|
"movie-match-for-tv",
|
|
"own-anilist-id-kept",
|
|
],
|
|
)
|
|
def test_a_western_match_takes_its_mapped_anilist_id(
|
|
anime_map: list[bool], hit: MetadataResult, kind: str, anime: bool, season: int | None, expected: int | None
|
|
) -> None:
|
|
providers.fill_anilist_id(hit, kind, anime, season)
|
|
|
|
assert hit.external_ids.anilist_id == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"hit",
|
|
[western_hit("en"), western_hit(None), western_hit(anilist_id=7), western_hit(tmdb_id=None, tmdb_kind=None)],
|
|
ids=["english", "no-language", "has-anilist-id", "no-western-id"],
|
|
)
|
|
def test_a_match_outside_anime_never_reads_the_map(anime_map: list[bool], hit: MetadataResult) -> None:
|
|
providers.fill_anilist_id(hit, "tv")
|
|
|
|
assert anime_map == []
|
|
|
|
|
|
def test_the_tagging_lookup_picks_the_anilist_entry_of_the_season(
|
|
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
|
) -> None:
|
|
patch_providers(monkeypatch, {"tmdb": FakeProvider("tmdb", western_hit(tmdb_id=1429))})
|
|
|
|
result = providers.resolve_by_ids(1429, kind="tv", prefer="tmdb", season=2)
|
|
|
|
assert result is not None
|
|
assert result.external_ids.anilist_id == 20958
|
|
|
|
|
|
def test_a_supplied_anilist_id_wins_over_the_mapped_one(monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]) -> None:
|
|
patch_providers(monkeypatch, {"tmdb": FakeProvider("tmdb", western_hit(tmdb_id=1429))})
|
|
|
|
result = providers.resolve_by_ids(1429, anilist_id=16498, kind="tv", prefer="tmdb", season=2)
|
|
|
|
assert result is not None
|
|
assert result.external_ids.anilist_id == 16498
|
|
|
|
|
|
def test_a_search_hit_on_tmdb_takes_the_mapped_anilist_id(
|
|
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
|
) -> None:
|
|
monkeypatch.setattr(config, "disable_metadata", False)
|
|
monkeypatch.setattr(providers, "provider_order", lambda *a: [lambda: SearchHit("tmdb", western_hit())])
|
|
monkeypatch.setattr(providers, "search_metadata", search_metadata)
|
|
|
|
result = providers.resolve_by_ids(title="One Piece", kind="tv")
|
|
|
|
assert result is not None
|
|
assert result.external_ids == ExternalIds(None, 37854, "tv", None, 21)
|
|
|
|
|
|
def test_the_tagging_search_picks_the_anilist_entry_of_the_season(
|
|
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
|
) -> None:
|
|
monkeypatch.setattr(providers, "search_metadata", lambda *a, **kw: western_hit(tmdb_id=1429))
|
|
|
|
result = providers.resolve_by_ids(title="EXAMPLE", kind="tv", season=2)
|
|
|
|
assert result is not None
|
|
assert result.external_ids.anilist_id == 20958
|