import pytest from django.test import Client from core.models import AppUser, Library, Channel, ScheduleTemplate @pytest.fixture def client(): return Client() @pytest.fixture def user(db): return AppUser.objects.create_user(username="apiuser3", email="api3@example.com", password="password") @pytest.fixture def library(db, user): return Library.objects.create(owner_user=user, name="Schedule Lib") @pytest.fixture def channel(db, user, library): return Channel.objects.create( owner_user=user, library=library, name="Schedule Channel", slug="sch-ch", channel_number=2 ) @pytest.fixture def schedule_template(db, channel): return ScheduleTemplate.objects.create( channel=channel, name="Morning Block", timezone_name="UTC", priority=10 ) @pytest.mark.django_db def test_list_schedule_templates(client, schedule_template): response = client.get("/api/schedule/template/") assert response.status_code == 200 data = response.json() assert len(data) == 1 assert data[0]["name"] == "Morning Block" assert data[0]["priority"] == 10 @pytest.mark.django_db def test_get_schedule_template(client, schedule_template): response = client.get(f"/api/schedule/template/{schedule_template.id}") assert response.status_code == 200 data = response.json() assert data["name"] == "Morning Block" @pytest.mark.django_db def test_get_schedule_template_not_found(client): response = client.get("/api/schedule/template/999") assert response.status_code == 404 @pytest.mark.django_db def test_create_schedule_template(client, channel): payload = { "name": "Evening Block", "description": "Late night programming", "timezone_name": "America/New_York", "priority": 5, "is_active": True, "channel_id": channel.id } response = client.post("/api/schedule/template/", data=payload, content_type="application/json") assert response.status_code == 201 data = response.json() assert data["name"] == "Evening Block" assert data["timezone_name"] == "America/New_York" assert ScheduleTemplate.objects.count() == 1 assert ScheduleTemplate.objects.get(id=data["id"]).name == "Evening Block" @pytest.mark.django_db def test_generate_schedule(client, channel, schedule_template): # Setup some media for it to find from core.models import MediaSource, MediaItem source = MediaSource.objects.create(library=channel.library, name="G", uri="/tmp", source_type="local_directory") MediaItem.objects.create(media_source=source, title="M1", item_kind="movie", runtime_seconds=3600, file_path="1") MediaItem.objects.create(media_source=source, title="M2", item_kind="movie", runtime_seconds=3600, file_path="2") # Add a schedule block to the template (created in fixture) from core.models import ScheduleBlock from datetime import time t = schedule_template ScheduleBlock.objects.create( schedule_template=t, name="TestBlock", block_type="programming", start_local_time=time(0, 0), end_local_time=time(23, 59), day_of_week_mask=127 # All days ) payload = {"target_date": "2026-03-08"} response = client.post(f"/api/schedule/generate/{channel.id}", data=payload, content_type="application/json") assert response.status_code == 200 data = response.json() assert data["status"] == "success" # Should have scheduled both movies back to back until the block ends assert data["airings_created"] > 0 from core.models import Airing assert Airing.objects.filter(channel=channel).count() == data["airings_created"]