mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 21:15:18 +00:00
199 lines
7.7 KiB
Python
199 lines
7.7 KiB
Python
import pytest
|
|
|
|
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, strip_year, title_ratio
|
|
from unshackle.core.providers.anilist import AniListProvider
|
|
from unshackle.core.providers.imdb import IMDBProvider
|
|
from unshackle.core.providers.tmdb import TMDBProvider
|
|
|
|
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(
|
|
("title", "expected"),
|
|
[
|
|
("The Matrix (1999)", "thematrix1999"),
|
|
("Spider-Man: No Way Home", "spidermannowayhome"),
|
|
("Léon", "leon"),
|
|
("Straße", "strasse"),
|
|
("XYZ 12", "xyz12"),
|
|
("进击的巨人", "进击的巨人"),
|
|
("Брат 2", "брат2"),
|
|
("İstanbul", "istanbul"),
|
|
("Title™", "titletm"),
|
|
("사랑의 불시착", "사랑의불시착"),
|
|
("がっこうぐらし", "がっこうぐらし"),
|
|
("เป็นต่อ", "เป็นต่อ"),
|
|
("진격의 거인", "진격의거인"),
|
|
("進撃の巨人", "進撃の巨人"),
|
|
],
|
|
)
|
|
def test_clean_keeps_letters_and_digits_of_every_script(title: str, expected: str) -> None:
|
|
assert clean(title) == expected
|
|
|
|
|
|
@pytest.mark.parametrize("title", ["เป็นต่อ", "हिंदी", "进击的巨人", "Война и мир", "The Matrix"])
|
|
def test_fuzzy_match_a_title_with_itself(title: str) -> None:
|
|
assert fuzzy_match(title, title)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("a", "b"),
|
|
[
|
|
("เป็นต่อ", "हिंदी"),
|
|
("进击的巨人", "鬼灭之刃"),
|
|
("Война и мир", "Преступление и наказание"),
|
|
("เป็นต่อ", "The Matrix"),
|
|
("사랑", "사람"),
|
|
("슬기로운 의사생활", "슬기로운 감빵생활"),
|
|
("がっこうぐらし", "かっこうくらし"),
|
|
("ガンダム", "カンタム"),
|
|
("कहानी", "कहना"),
|
|
],
|
|
)
|
|
def test_fuzzy_match_rejects_different_non_latin_titles(a: str, b: str) -> None:
|
|
assert not fuzzy_match(a, b)
|
|
|
|
|
|
@pytest.mark.parametrize(("a", "b"), [("!!!", "!!!"), ("", ""), ("---", "The Matrix")])
|
|
def test_a_title_with_no_letters_or_digits_matches_nothing(a: str, b: str) -> None:
|
|
assert title_ratio(a, b) == 0.0
|
|
assert not fuzzy_match(a, b)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("a", "b", "expected"),
|
|
[
|
|
("The Matrix", "the matrix", True),
|
|
("Spider-Man: No Way Home", "Spider Man No Way Home", True),
|
|
("Leon", "Léon", True),
|
|
("The Matrix", "The Godfather", False),
|
|
],
|
|
)
|
|
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)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("title", "year", "expected"),
|
|
[
|
|
("EXAMPLE (2019)", None, "EXAMPLE"),
|
|
("EXAMPLE 2011", 2011, "EXAMPLE"),
|
|
("1917", None, "1917"),
|
|
("EXAMPLE 2049", 2017, "EXAMPLE 2049"),
|
|
("EXAMPLE 12023", 2023, "EXAMPLE 12023"),
|
|
],
|
|
)
|
|
def test_strip_year_keeps_a_title_that_is_only_a_year_or_ends_in_another_number(
|
|
title: str, year: int | None, expected: str
|
|
) -> None:
|
|
assert strip_year(title, year) == expected
|
|
|
|
|
|
class TMDBResponse:
|
|
def __init__(self, results: list[dict]) -> None:
|
|
self.results = results
|
|
|
|
def raise_for_status(self) -> None:
|
|
pass
|
|
|
|
def json(self) -> dict:
|
|
return {"results": self.results}
|
|
|
|
|
|
def tmdb_search(monkeypatch: pytest.MonkeyPatch, results: list[dict], year: int | None) -> tuple[int | None, dict]:
|
|
sent: dict = {}
|
|
|
|
def get(url: str, params: dict, timeout: int) -> TMDBResponse:
|
|
sent.update(params)
|
|
return TMDBResponse(results)
|
|
|
|
p = TMDBProvider()
|
|
monkeypatch.setattr(config, "tmdb_api_key", "key")
|
|
monkeypatch.setattr(p, "_session", type("S", (), {"get": staticmethod(get)})())
|
|
monkeypatch.setattr(p, "fetch_detail", lambda tmdb_id, kind: None)
|
|
monkeypatch.setattr(p, "fetch_external_ids_raw", lambda tmdb_id, kind: None)
|
|
result = p.search("EXAMPLE", year, "tv")
|
|
return (result.external_ids.tmdb_id if result else None), sent
|
|
|
|
|
|
def test_tmdb_tv_search_matches_the_year_on_any_air_date(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_, sent = tmdb_search(monkeypatch, [{"id": 1, "name": "EXAMPLE", "first_air_date": "2016-04-04"}], 2020)
|
|
assert sent["year"] == 2020 and "first_air_date_year" not in sent
|
|
|
|
|
|
@pytest.mark.parametrize(("year", "expected"), [(2023, 2), (1999, 1), (2024, 1), (None, 1)])
|
|
def test_tmdb_prefers_the_series_that_first_aired_in_the_year_on_a_title_tie(
|
|
monkeypatch: pytest.MonkeyPatch, year: int | None, expected: int
|
|
) -> None:
|
|
results = [
|
|
{"id": 1, "name": "EXAMPLE", "first_air_date": "1999-10-20"},
|
|
{"id": 2, "name": "EXAMPLE", "first_air_date": "2023-08-31"},
|
|
{"id": 3, "name": "EXAMPLE OTHER", "first_air_date": "2024-01-01"},
|
|
]
|
|
assert tmdb_search(monkeypatch, results, year)[0] == expected
|