from datetime import timedelta

from django.utils import timezone as tz
import pytest


class TestStreamers:
    @pytest.fixture
    def test___init__(self, streamers):
        assert isinstance(streamers.timeout, timedelta)

    @pytest.fixture
    def test_reset(self, streamers, stations):
        streamers.reset()
        assert all(streamers.streamers[station.pk] == station for station in stations)

    @pytest.fixture
    def test_fetch(self, streamers):
        streamers.next_date = tz.now() - tz.timedelta(seconds=30)
        streamers.streamers = None

        now = tz.now()
        streamers.fetch()

        assert all(streamer.calls.get("fetch") for streamer in streamers)
        assert streamers.next_date > now

    @pytest.fixture
    def test_fetch_timeout_not_reached(self, streamers):
        next_date = tz.now() + tz.timedelta(seconds=30)
        streamers.next_date = next_date

        streamers.fetch()
        assert all(not streamer.calls.get("fetch") for streamer in streamers)
        assert streamers.next_date == next_date
