130 lines
4.8 KiB
Python
130 lines
4.8 KiB
Python
"""Tests for the TMDb client (src.core.tmdb). Network is monkeypatched."""
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from src.core import tmdb
|
|
from src.core.tmdb import TmdbError, TmdbResult
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _with_key(monkeypatch):
|
|
monkeypatch.setenv("TMDB_API_KEY", "testkey")
|
|
|
|
|
|
def _resp(status=200, payload=None):
|
|
return SimpleNamespace(status_code=status, ok=(200 <= status < 300),
|
|
json=lambda: (payload or {}))
|
|
|
|
|
|
def test_api_available_reflects_key(monkeypatch):
|
|
assert tmdb.api_available()
|
|
monkeypatch.delenv("TMDB_API_KEY", raising=False)
|
|
assert not tmdb.api_available()
|
|
|
|
|
|
def test_lookup_movie_returns_title_and_year(monkeypatch):
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(
|
|
200, {"id": 603, "title": "Matrix", "release_date": "1999-03-31"}))
|
|
result = tmdb.lookup_movie(603)
|
|
assert result == TmdbResult(603, "Matrix", "1999", "movie")
|
|
|
|
|
|
def test_lookup_movie_falls_back_to_original_title(monkeypatch):
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(
|
|
200, {"id": 1, "original_title": "Solyaris", "release_date": "1972-01-01"}))
|
|
assert tmdb.lookup_movie(1).title == "Solyaris"
|
|
|
|
|
|
def test_lookup_movie_missing_date_gives_no_year(monkeypatch):
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(
|
|
200, {"id": 2, "title": "Bez data", "release_date": ""}))
|
|
assert tmdb.lookup_movie(2).year is None
|
|
|
|
|
|
def test_lookup_tv_uses_name_and_first_air(monkeypatch):
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(
|
|
200, {"id": 1396, "name": "Breaking Bad", "first_air_date": "2008-01-20"}))
|
|
result = tmdb.lookup(1396, kind="tv")
|
|
assert result == TmdbResult(1396, "Breaking Bad", "2008", "tv")
|
|
|
|
|
|
def test_search_movie_maps_results(monkeypatch):
|
|
payload = {"results": [
|
|
{"id": 603, "title": "Matrix", "release_date": "1999-03-31"},
|
|
{"id": 605, "title": "Matrix Revolutions", "release_date": "2003-11-05"},
|
|
]}
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(200, payload))
|
|
results = tmdb.search_movie("Matrix", year="1999")
|
|
assert [r.tmdb_id for r in results] == [603, 605]
|
|
assert results[0].year == "1999"
|
|
|
|
|
|
def test_search_movie_empty_query_skips_network(monkeypatch):
|
|
def boom(*a, **k):
|
|
raise AssertionError("should not hit network")
|
|
monkeypatch.setattr(tmdb.requests, "get", boom)
|
|
assert tmdb.search_movie(" ") == []
|
|
|
|
|
|
def test_movie_details_maps_fields(monkeypatch):
|
|
payload = {
|
|
"id": 603, "title": "Matrix", "release_date": "1999-03-31",
|
|
"vote_average": 8.3,
|
|
"genres": [{"id": 28, "name": "Akční"}, {"id": 878, "name": "Vědeckofantastický"}],
|
|
"production_countries": [{"iso_3166_1": "US", "name": "United States of America"}],
|
|
}
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(200, payload))
|
|
d = tmdb.movie_details(603)
|
|
assert d.genres == ["Akční", "Vědeckofantastický"]
|
|
assert d.countries == ["United States of America"]
|
|
assert d.rating == 83 and d.year == "1999"
|
|
assert d.field_values("genres") == ["Akční", "Vědeckofantastický"]
|
|
assert d.field_values("rating") == ["83"]
|
|
|
|
|
|
def test_refresh_tags_from_tmdb(tmp_path, monkeypatch):
|
|
monkeypatch.setattr("src.core.config.GLOBAL_CONFIG_FILE", tmp_path / ".gtag")
|
|
from src.core.tag_manager import TagManager
|
|
from src.core.file_manager import FileManager
|
|
from src.core.tmdb import TmdbDetails
|
|
|
|
fm = FileManager(TagManager())
|
|
fm.set_pool_dir(tmp_path / "pool")
|
|
(tmp_path / "a.mkv").write_bytes(b"x")
|
|
(tmp_path / "b.mkv").write_bytes(b"y")
|
|
matched = fm.import_movie(tmp_path / "a.mkv", "Matrix", year=1999, tmdb_id=603)
|
|
fm.import_movie(tmp_path / "b.mkv", "Bez ID", year=2000) # no tmdbid
|
|
|
|
import src.core.tmdb as tmdb_mod
|
|
monkeypatch.setattr(tmdb_mod, "movie_details", lambda i: TmdbDetails(
|
|
int(i), "Matrix", "1999", ["Akční", "Sci-Fi"], ["USA"], 83))
|
|
|
|
result = fm.refresh_tags_from_tmdb()
|
|
assert result == {"ok": True, "updated": 1, "without_id": 1, "errors": []}
|
|
paths = {t.full_path for t in matched.tags}
|
|
assert {"Žánr/Akční", "Žánr/Sci-Fi", "Rok/1999", "Země původu/USA"} <= paths
|
|
assert matched.meta == {"year": "1999", "rating": 83}
|
|
|
|
|
|
def test_missing_key_raises(monkeypatch):
|
|
monkeypatch.delenv("TMDB_API_KEY", raising=False)
|
|
with pytest.raises(TmdbError):
|
|
tmdb.lookup_movie(603)
|
|
|
|
|
|
def test_bad_status_raises(monkeypatch):
|
|
monkeypatch.setattr(tmdb.requests, "get", lambda *a, **k: _resp(401, {}))
|
|
with pytest.raises(TmdbError):
|
|
tmdb.lookup_movie(603)
|
|
|
|
|
|
def test_network_error_raises(monkeypatch):
|
|
import requests
|
|
|
|
def boom(*a, **k):
|
|
raise requests.ConnectionError("down")
|
|
monkeypatch.setattr(tmdb.requests, "get", boom)
|
|
with pytest.raises(TmdbError):
|
|
tmdb.lookup_movie(603)
|