Files

100 lines
3.5 KiB
Python
Raw Permalink Normal View History

"""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