forked from kenzuya/unshackle
fix(providers): accept an IMDb or AniList match on any title the ranker used
This commit is contained in:
@@ -1,7 +1,26 @@
|
||||
import pytest
|
||||
|
||||
from unshackle.core.providers import fuzzy_match
|
||||
from unshackle.core import providers
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.providers import MetadataResult, fuzzy_match, result_matches
|
||||
from unshackle.core.providers._base import clean, title_ratio
|
||||
from unshackle.core.providers.anilist import AniListProvider
|
||||
from unshackle.core.providers.imdb import IMDBProvider
|
||||
|
||||
IMDB_NODE = {
|
||||
"id": "tt2560140",
|
||||
"titleText": {"text": "Attack on Titan"},
|
||||
"originalTitleText": {"text": "Shingeki no Kyojin"},
|
||||
"releaseYear": {"year": 2013},
|
||||
}
|
||||
|
||||
ANILIST_NODE = {
|
||||
"id": 16498,
|
||||
"title": {"romaji": "Shingeki no Kyojin", "english": "Attack on Titan", "native": "進撃の巨人"},
|
||||
"format": "TV",
|
||||
"startDate": {"year": 2013},
|
||||
"countryOfOrigin": "JP",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -67,3 +86,52 @@ def test_a_title_with_no_letters_or_digits_matches_nothing(a: str, b: str) -> No
|
||||
)
|
||||
def test_fuzzy_match_latin_titles(a: str, b: str, expected: bool) -> None:
|
||||
assert fuzzy_match(a, b) is expected
|
||||
|
||||
|
||||
def test_a_result_matches_on_any_of_its_names() -> None:
|
||||
result = MetadataResult(title="Attack on Titan", names=("Attack on Titan", "Shingeki no Kyojin"))
|
||||
assert result_matches(result, "Shingeki no Kyojin")
|
||||
assert not result_matches(result, "The Matrix")
|
||||
|
||||
|
||||
def test_a_result_with_no_names_matches_on_its_title_only() -> None:
|
||||
assert result_matches(MetadataResult(title="Attack on Titan"), "attack on titan")
|
||||
assert not result_matches(MetadataResult(title="Attack on Titan"), "Shingeki no Kyojin")
|
||||
assert not result_matches(MetadataResult(), "Shingeki no Kyojin")
|
||||
|
||||
|
||||
def test_imdb_search_accepts_a_result_that_matches_on_its_original_title(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
p = IMDBProvider()
|
||||
edges = {"advancedTitleSearch": {"edges": [{"node": {"title": IMDB_NODE}}]}}
|
||||
monkeypatch.setattr(p, "graphql", lambda operation, query: edges)
|
||||
result = p.search("Shingeki no Kyojin", 2013, "tv")
|
||||
assert result is not None and result.title == "Attack on Titan"
|
||||
|
||||
|
||||
class FakeCacher:
|
||||
def get_cached_provider(self, provider_name: str, *args: object) -> dict:
|
||||
return IMDB_NODE
|
||||
|
||||
|
||||
def test_search_metadata_uses_a_cached_imdb_result_that_matches_on_its_original_title(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(config, "disable_metadata", False)
|
||||
monkeypatch.setattr(providers, "provider_order", lambda *args: [IMDBProvider])
|
||||
monkeypatch.setattr(IMDBProvider, "search", lambda *args: pytest.fail("a cache hit must not search again"))
|
||||
|
||||
result = providers.search_metadata("Shingeki no Kyojin", None, "tv", FakeCacher(), "show-1") # type: ignore[arg-type]
|
||||
assert result is not None and result.external_ids.imdb_id == "tt2560140"
|
||||
|
||||
|
||||
def test_search_metadata_accepts_an_anilist_result_found_by_another_title_variant(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(config, "disable_metadata", False)
|
||||
monkeypatch.setattr(config, "anilist_title_language", "english")
|
||||
monkeypatch.setattr(providers, "provider_order", lambda *args: [AniListProvider])
|
||||
monkeypatch.setattr(providers, "enrich_ids", lambda result: None)
|
||||
monkeypatch.setattr(AniListProvider, "graphql", lambda self, query, variables: {"Page": {"media": [ANILIST_NODE]}})
|
||||
|
||||
result = providers.search_metadata("Shingeki no Kyojin", None, "tv", anime=True)
|
||||
assert result is not None and (result.title, result.external_ids.anilist_id) == ("Attack on Titan", 16498)
|
||||
|
||||
@@ -128,7 +128,7 @@ def test_the_tvdb_order_resolves_a_tvdb_id_and_keeps_it_for_the_tags(monkeypatch
|
||||
|
||||
def test_a_series_is_searched_once_and_each_title_fetches_its_external_ids(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
calls = Providers(monkeypatch, found(title="The Show"))
|
||||
monkeypatch.setattr(providers, "fuzzy_match", lambda a, b: a == b)
|
||||
monkeypatch.setattr(providers, "result_matches", lambda result, title: result.title == title)
|
||||
metadata = TitleMetadata(cache=CACHE)
|
||||
metadata.search(episode(1, 1))
|
||||
metadata.search(episode(1, 2))
|
||||
@@ -141,12 +141,19 @@ def test_a_series_is_searched_once_and_each_title_fetches_its_external_ids(monke
|
||||
|
||||
def test_a_search_that_matches_another_show_sets_no_id(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
calls = Providers(monkeypatch, found(title="Another Show"))
|
||||
monkeypatch.setattr(providers, "fuzzy_match", lambda a, b: a == b)
|
||||
monkeypatch.setattr(providers, "result_matches", lambda result, title: result.title == title)
|
||||
metadata = TitleMetadata()
|
||||
metadata.search(episode(1, 1))
|
||||
assert (metadata.tmdb_id, metadata.searched, calls.fetched) == (None, True, [])
|
||||
|
||||
|
||||
def test_a_search_matches_on_another_name_of_the_result(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
Providers(monkeypatch, found(title="Another Show", names=("Another Show", "The Show")))
|
||||
metadata = TitleMetadata()
|
||||
metadata.search(episode(1, 1))
|
||||
assert metadata.tmdb_id == 1396
|
||||
|
||||
|
||||
def test_a_given_tmdb_id_is_kept(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
Providers(monkeypatch, found(tmdb_id=1, title="Other"))
|
||||
metadata = TitleMetadata(tmdb_id=5)
|
||||
@@ -224,7 +231,7 @@ def test_enrich_fills_the_air_dates_of_a_daily_series(monkeypatch: pytest.Monkey
|
||||
|
||||
def test_a_listing_prints_the_search_match(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
Providers(monkeypatch, found(title="The Show"))
|
||||
monkeypatch.setattr(providers, "fuzzy_match", lambda a, b: a == b)
|
||||
monkeypatch.setattr(providers, "result_matches", lambda result, title: result.title == title)
|
||||
with console.capture() as captured:
|
||||
TitleMetadata().search(episode(1, 1), listing=True)
|
||||
assert "Search -> The Show (ID 1396)" in captured.get()
|
||||
|
||||
Reference in New Issue
Block a user