import pytest


from aircox import models
from aircox.test import Interface
from aircox.views import base
from .conftest import FakeView


@pytest.fixture
def fake_request(station):
    return Interface(station=station)


@pytest.fixture
def base_view(fake_request):
    class View(base.BaseView, FakeView):
        model = models.Page
        request = fake_request

    return View()


@pytest.fixture
def base_api_view(fake_request):
    class View(base.BaseAPIView, FakeView):
        model = models.Program
        queryset = models.Program.objects.all()
        request = fake_request

    return View()


class TestBaseView:
    @pytest.mark.django_db
    def test_station(self, base_view, station):
        assert base_view.station == station

    @pytest.mark.django_db
    def test_get_context_data(self, base_view, station, published_pages):
        context = base_view.get_context_data()
        assert context == {
            "view": base_view,
            "station": station,
            "page": None,  # get_page() returns None
            "audio_streams": station.streams,
            "model": base_view.model,
        }


class TestBaseAPIView:
    @pytest.mark.django_db
    def test_station(self, base_api_view, station):
        assert base_api_view.station == station

    @pytest.mark.django_db
    def test_get_queryset(self, base_api_view, station, programs):
        query = base_api_view.get_queryset()
        assert set(query.values_list("station", flat=True)) == {station.id}
