CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
Movies have separate /credits, /images, /videos sub-endpoints; series only had /seasons. Adds the matching three for series, reusing the same cast/crew/creators three-way split series detail already does (extracted into _split_series_credits so both share it). Co-Authored-By: Claude Sonnet 5 <[email protected]>
217 lines
7.7 KiB
Python
217 lines
7.7 KiB
Python
"""Integration tests for the series/season ingestion pipeline (section 15).
|
|
A fake provider stands in for TMDb so these run without network access,
|
|
while still exercising the real normalize -> match -> upsert -> provenance
|
|
path against PostgreSQL."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.ids import aurora_id
|
|
from app.models.tv import Episode, Season, Series
|
|
from app.providers.base import MetadataProvider, ProviderNotFoundError
|
|
from app.services import tv_service
|
|
from app.services.ingestion.series_ingestion import ingest_season, ingest_series
|
|
from tests.unit.test_normalizer import FAKE_SEASON, FAKE_SERIES
|
|
|
|
|
|
class FakeTMDbSeriesProvider(MetadataProvider):
|
|
slug = "tmdb"
|
|
name = "The Movie Database (TMDb)"
|
|
|
|
def __init__(self, series_payload: dict[str, Any], season_payload: dict[str, Any]) -> None:
|
|
self._series_payload = series_payload
|
|
self._season_payload = season_payload
|
|
|
|
async def get_movie(self, external_id: str) -> dict[str, Any]:
|
|
raise NotImplementedError
|
|
|
|
async def get_series(self, external_id: str) -> dict[str, Any]:
|
|
return self._series_payload
|
|
|
|
async def get_season(self, series_external_id: str, season_number: int) -> dict[str, Any]:
|
|
return self._season_payload
|
|
|
|
async def get_person(self, external_id: str) -> dict[str, Any]:
|
|
raise NotImplementedError
|
|
|
|
async def get_collection(self, external_id: str) -> dict[str, Any]:
|
|
raise NotImplementedError
|
|
|
|
async def search_movie(self, query: str) -> list[dict[str, Any]]:
|
|
raise NotImplementedError
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ingest_series_creates_series_and_season_stubs(db_session: AsyncSession):
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
|
|
series_id = await ingest_series(db_session, provider, "1396")
|
|
await db_session.commit()
|
|
|
|
series = await db_session.get(Series, series_id)
|
|
assert series is not None
|
|
assert series.title == "Ozymandias"
|
|
assert series.status.value == "ended"
|
|
|
|
seasons = (
|
|
(await db_session.execute(select(Season).where(Season.series_id == series_id)))
|
|
.scalars()
|
|
.all()
|
|
)
|
|
season_numbers = sorted(s.season_number for s in seasons)
|
|
assert season_numbers == [0, 1]
|
|
# Season stubs have no episodes yet — that's ingest_season's job.
|
|
for season in seasons:
|
|
result = await db_session.execute(select(Episode).where(Episode.season_id == season.id))
|
|
assert result.scalars().first() is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ingest_season_requires_series_ingested_first(db_session: AsyncSession):
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
|
|
with pytest.raises(ProviderNotFoundError):
|
|
await ingest_season(db_session, provider, "1396", 1)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ingest_season_creates_episodes_with_credits_and_stills(db_session: AsyncSession):
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
|
|
await ingest_series(db_session, provider, "1396")
|
|
await db_session.flush()
|
|
|
|
season_id = await ingest_season(db_session, provider, "1396", 1)
|
|
await db_session.commit()
|
|
|
|
episodes = (
|
|
(await db_session.execute(select(Episode).where(Episode.season_id == season_id)))
|
|
.scalars()
|
|
.all()
|
|
)
|
|
assert len(episodes) == 2
|
|
pilot = next(e for e in episodes if e.episode_number == 1)
|
|
assert pilot.title == "Pilot"
|
|
assert pilot.runtime_minutes == 58
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ingest_season_is_idempotent(db_session: AsyncSession):
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
|
|
await ingest_series(db_session, provider, "1396")
|
|
await db_session.flush()
|
|
|
|
first_season_id = await ingest_season(db_session, provider, "1396", 1)
|
|
await db_session.flush()
|
|
second_season_id = await ingest_season(db_session, provider, "1396", 1)
|
|
await db_session.commit()
|
|
|
|
assert first_season_id == second_season_id
|
|
episodes = (
|
|
(await db_session.execute(select(Episode).where(Episode.season_id == first_season_id)))
|
|
.scalars()
|
|
.all()
|
|
)
|
|
# Re-running must not duplicate episode rows.
|
|
assert len(episodes) == 2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_series_detail_surfaces_network_logo_path(db_session: AsyncSession):
|
|
"""Same class of bug as movie companies: tv_service used to build
|
|
networks[]/companies[] without ever looking up their logo artwork, so
|
|
logo_path was always None regardless of what ingestion had stored."""
|
|
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
series_id = await ingest_series(db_session, provider, "1396")
|
|
await db_session.commit()
|
|
|
|
detail = await tv_service.get_series_detail(db_session, aurora_id("series", str(series_id)))
|
|
|
|
amc = next(n for n in detail.networks if n.name == "AMC")
|
|
assert amc.logo_path is not None
|
|
assert amc.logo_path.endswith("/amc-logo.png")
|
|
|
|
gran_via = next(c for c in detail.companies if c.name == "Gran Via Productions")
|
|
assert gran_via.logo_path is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_series_credits_separates_cast_crew_and_creators(db_session: AsyncSession):
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
series_id = await ingest_series(db_session, provider, "1396")
|
|
await db_session.commit()
|
|
|
|
credits_out = await tv_service.get_series_credits(
|
|
db_session, aurora_id("series", str(series_id))
|
|
)
|
|
|
|
assert [c.person.name for c in credits_out.cast] == ["Bryan Cranston"]
|
|
assert credits_out.cast[0].character == "Walter White"
|
|
assert credits_out.crew == []
|
|
assert [c.person.name for c in credits_out.creators] == ["Vince Gilligan"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_series_images_returns_poster_and_backdrop(db_session: AsyncSession):
|
|
provider = FakeTMDbSeriesProvider(FAKE_SERIES, FAKE_SEASON)
|
|
series_id = await ingest_series(db_session, provider, "1396")
|
|
await db_session.commit()
|
|
|
|
images_out = await tv_service.get_series_images(
|
|
db_session, aurora_id("series", str(series_id))
|
|
)
|
|
|
|
assert len(images_out.images.posters) == 1
|
|
assert images_out.images.posters[0].url.endswith("/poster.jpg")
|
|
assert len(images_out.images.backdrops) == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_series_videos_returns_normalized_videos(db_session: AsyncSession):
|
|
payload = {
|
|
**FAKE_SERIES,
|
|
"videos": {
|
|
"results": [
|
|
{
|
|
"id": "abc123",
|
|
"site": "YouTube",
|
|
"key": "dQw4w9WgXcQ",
|
|
"type": "Trailer",
|
|
"official": True,
|
|
"iso_639_1": "en",
|
|
"iso_3166_1": "US",
|
|
}
|
|
]
|
|
},
|
|
}
|
|
provider = FakeTMDbSeriesProvider(payload, FAKE_SEASON)
|
|
series_id = await ingest_series(db_session, provider, "1396")
|
|
await db_session.commit()
|
|
|
|
videos_out = await tv_service.get_series_videos(
|
|
db_session, aurora_id("series", str(series_id))
|
|
)
|
|
|
|
assert len(videos_out.videos) == 1
|
|
assert videos_out.videos[0].site == "YouTube"
|
|
assert videos_out.videos[0].is_official is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_series_sub_endpoints_raise_not_found_for_unknown_series(db_session: AsyncSession):
|
|
unknown_id = aurora_id("series", str(uuid.uuid4()))
|
|
with pytest.raises(tv_service.SeriesNotFoundError):
|
|
await tv_service.get_series_credits(db_session, unknown_id)
|
|
with pytest.raises(tv_service.SeriesNotFoundError):
|
|
await tv_service.get_series_images(db_session, unknown_id)
|
|
with pytest.raises(tv_service.SeriesNotFoundError):
|
|
await tv_service.get_series_videos(db_session, unknown_id)
|