import pytest

from aircox_streamer.controllers import Metadata


class TestBaseMetaData:
    @pytest.mark.django_db
    def test_is_playing(self, metadata):
        metadata.status = "playing"
        assert metadata.is_playing

    @pytest.mark.django_db
    def test_is_playing_false(self, metadata):
        metadata.status = "other"
        assert not metadata.is_playing

    @pytest.mark.django_db
    def test_fetch(self, controller, metadata, metadata_data, metadata_string):
        controller.connector.socket.recv_data = metadata_string
        metadata.fetch()
        assert metadata.uri == metadata_data["initial_uri"]

    @pytest.mark.django_db
    def test_validate_status_playing(self, controller, metadata):
        controller.source = metadata
        assert metadata.validate_status("playing") == "playing"

    @pytest.mark.django_db
    def test_validate_status_paused(self, controller, metadata):
        controller.source = Metadata(controller, metadata.rid + 1)
        assert metadata.validate_status("playing") == "paused"

    @pytest.mark.django_db
    def test_validate_status_stopped(self, controller, metadata):
        controller.source = Metadata(controller, 2)
        assert metadata.validate_status("") == "stopped"
        assert metadata.validate_status("any") == "stopped"

    @pytest.mark.django_db
    def test_validate_air_time(self, metadata, metadata_data, metadata_data_air_time):
        air_time = metadata_data["on_air"]
        result = metadata.validate_air_time(air_time)
        assert result == metadata_data_air_time

    @pytest.mark.django_db
    def test_validate_air_time_none(self, metadata):
        assert metadata.validate_air_time("") is None

    @pytest.mark.django_db
    def test_validate(self, metadata, metadata_data, metadata_data_air_time):
        metadata.validate(metadata_data)
        assert metadata.uri == metadata_data["initial_uri"]
        assert metadata.air_time == metadata_data_air_time
        # controller.source != metadata + status = "playing"
        #       => status == "paused"
        assert metadata.status == "paused"
        assert metadata.request_status == "playing"
