Add opt-in season cascade to series ingestion
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

Ingesting a series only ever produced season stubs -- getting actual
episodes meant calling POST /admin/ingest/season once per season by
hand, which is exactly what manual end-to-end testing kept requiring
for every multi-season show.

POST /api/v1/admin/ingest/series now takes include_seasons (default
false, since a show can have dozens of seasons and that's dozens of
TMDb calls fired from one request). When true, ingest_series_task
fans out one ingest_season job per season once the series succeeds,
using arq's own ctx['redis'] connection for the job (the same pattern
arq's docs recommend for job chaining) rather than a second unrelated
connection. Each fanned-out season job still goes through
get_or_create_ingestion_job, so re-running a cascaded series ingestion
reuses the same season job rows instead of creating duplicates.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
NixReaper
2026-09-05 02:03:25 -05:00
co-authored by Claude Sonnet 5
parent ec6b5bdd7d
commit b4d1e8f755
6 changed files with 148 additions and 6 deletions
@@ -0,0 +1,99 @@
"""Tests for the series -> season ingestion cascade
(app/workers/tasks.py:ingest_series_task, include_seasons=True).
run_series_ingestion and get_series_season_numbers are monkeypatched so this
doesn't need a real TMDbProvider/network call — the thing actually at risk
here is the fan-out wiring itself (does it enqueue the right task with the
right arguments for every season), not the ingestion pipeline, which
test_series_ingestion.py already covers.
"""
from __future__ import annotations
import uuid
import pytest
from app.workers import tasks
class _FakeArqRedis:
def __init__(self) -> None:
self.enqueued: list[tuple[str, tuple]] = []
async def enqueue_job(self, task_name: str, *args):
self.enqueued.append((task_name, args))
return object() # stand-in for arq's Job — callers don't use it here
@pytest.mark.asyncio
async def test_series_task_does_not_cascade_by_default(monkeypatch):
fake_series_id = uuid.uuid4()
async def fake_run_series_ingestion(job_id, external_id):
return fake_series_id
monkeypatch.setattr(tasks, "run_series_ingestion", fake_run_series_ingestion)
redis = _FakeArqRedis()
result = await tasks.ingest_series_task({"redis": redis}, str(uuid.uuid4()), "1396")
assert result == str(fake_series_id)
assert redis.enqueued == []
@pytest.mark.asyncio
async def test_series_task_cascades_one_season_job_per_season(monkeypatch, db_session):
fake_series_id = uuid.uuid4()
async def fake_run_series_ingestion(job_id, external_id):
return fake_series_id
async def fake_get_series_season_numbers(series_id):
assert series_id == fake_series_id
return [0, 1, 2]
monkeypatch.setattr(tasks, "run_series_ingestion", fake_run_series_ingestion)
monkeypatch.setattr(tasks, "get_series_season_numbers", fake_get_series_season_numbers)
redis = _FakeArqRedis()
result = await tasks.ingest_series_task(
{"redis": redis}, str(uuid.uuid4()), "1396", include_seasons=True
)
assert result == str(fake_series_id)
assert len(redis.enqueued) == 3
for task_name, args in redis.enqueued:
assert task_name == "ingest_season_task"
job_id_arg, external_id_arg, season_number_arg = args
assert external_id_arg == "1396"
uuid.UUID(job_id_arg) # must be a real, parseable job id
seasons_enqueued = sorted(args[2] for _, args in redis.enqueued)
assert seasons_enqueued == [0, 1, 2]
@pytest.mark.asyncio
async def test_series_task_cascade_reuses_job_on_repeat_enqueue(monkeypatch, db_session):
"""Re-cascading the same series (e.g. a retried job) must reuse the same
season IngestionJob rows, not create duplicates — get_or_create_ingestion_job
is idempotency-keyed by (entity_type, external_id)."""
fake_series_id = uuid.uuid4()
async def fake_run_series_ingestion(job_id, external_id):
return fake_series_id
async def fake_get_series_season_numbers(series_id):
return [1]
monkeypatch.setattr(tasks, "run_series_ingestion", fake_run_series_ingestion)
monkeypatch.setattr(tasks, "get_series_season_numbers", fake_get_series_season_numbers)
redis = _FakeArqRedis()
await tasks.ingest_series_task({"redis": redis}, str(uuid.uuid4()), "9999", include_seasons=True)
await tasks.ingest_series_task({"redis": redis}, str(uuid.uuid4()), "9999", include_seasons=True)
first_job_id = redis.enqueued[0][1][0]
second_job_id = redis.enqueued[1][1][0]
assert first_job_id == second_job_id