import pytest from django.test import Client from core.models import AppUser, Library, Channel @pytest.fixture def client(): return Client() @pytest.fixture def user(db): return AppUser.objects.create_user(username="apiuser2", email="api2@example.com", password="password") @pytest.fixture def library(db, user): return Library.objects.create(owner_user=user, name="Network Lib") @pytest.fixture def channel(db, user, library): return Channel.objects.create( owner_user=user, library=library, name="Test Channel", slug="test-ch", channel_number=1, scheduling_mode="template_driven" ) @pytest.mark.django_db def test_list_channels(client, channel): response = client.get("/api/channel/") assert response.status_code == 200 data = response.json() assert len(data) == 1 assert data[0]["name"] == "Test Channel" assert data[0]["slug"] == "test-ch" @pytest.mark.django_db def test_get_channel(client, channel): response = client.get(f"/api/channel/{channel.id}") assert response.status_code == 200 data = response.json() assert data["name"] == "Test Channel" @pytest.mark.django_db def test_get_channel_not_found(client): response = client.get("/api/channel/999") assert response.status_code == 404 @pytest.mark.django_db def test_create_channel(client, user, library): payload = { "name": "New API Channel", "slug": "new-api-ch", "channel_number": 5, "description": "Created via API", "library_id": library.id, "owner_user_id": user.id } response = client.post("/api/channel/", data=payload, content_type="application/json") assert response.status_code == 201 data = response.json() assert data["name"] == "New API Channel" assert data["slug"] == "new-api-ch" # Verify it hit the DB assert Channel.objects.count() == 1 assert Channel.objects.get(id=data["id"]).name == "New API Channel"