from unittest.mock import patch

import pytest

from just_bin_it.endpoints.sources import HistogramSource
from tests.doubles.consumer import StubConsumer

TEST_MESSAGE = b"this is a byte message"
INVALID_FB = b"this is an invalid fb message"


class TestHistogramSource:
    @pytest.fixture(autouse=True)
    def prepare(self):
        pass

    def test_if_no_consumer_supplied_then_raises(self):
        with pytest.raises(Exception):
            HistogramSource(None)

    def test_if_no_new_messages_then_no_data(self):
        mock_consumer = StubConsumer(["broker1"], ["topic1"])
        mock_consumer.add_messages([])
        hs = HistogramSource(mock_consumer)
        data = hs.get_new_data()
        assert len(data) == 0

    @patch("just_bin_it.endpoints.sources.deserialise_hs00", return_value=TEST_MESSAGE)
    def test_if_five_new_messages_on_one_topic_then_data_has_five_items(
        self, mock_method
    ):
        mock_consumer = StubConsumer(["broker1"], ["topic1"])
        mock_consumer.add_messages([TEST_MESSAGE] * 5)
        hs = HistogramSource(mock_consumer)

        data = hs.get_new_data()
        _, _, message = data[0]

        assert len(data) == 5
        assert message == TEST_MESSAGE

    def test_deserialising_invalid_fb_does_not_throw(self):
        mock_consumer = StubConsumer(["broker1"], ["topic1"])
        mock_consumer.add_messages([INVALID_FB])
        hs = HistogramSource(mock_consumer)

        hs.get_new_data()

[((13, 5, 13, 33), 'pytest.fixture', 'pytest.fixture', (), '', False, 'import pytest\n'), ((28, 5, 28, 87), 'unittest.mock.patch', 'patch', (), '', False, 'from unittest.mock import patch\n'), ((22, 24, 22, 61), 'tests.doubles.consumer.StubConsumer', 'StubConsumer', ({(22, 37, 22, 48): "['broker1']", (22, 50, 22, 60): "['topic1']"}, {}), "(['broker1'], ['topic1'])", False, 'from tests.doubles.consumer import StubConsumer\n'), ((24, 13, 24, 43), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', ({(24, 29, 24, 42): 'mock_consumer'}, {}), '(mock_consumer)', False, 'from just_bin_it.endpoints.sources import HistogramSource\n'), ((32, 24, 32, 61), 'tests.doubles.consumer.StubConsumer', 'StubConsumer', ({(32, 37, 32, 48): "['broker1']", (32, 50, 32, 60): "['topic1']"}, {}), "(['broker1'], ['topic1'])", False, 'from tests.doubles.consumer import StubConsumer\n'), ((34, 13, 34, 43), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', ({(34, 29, 34, 42): 'mock_consumer'}, {}), '(mock_consumer)', False, 'from just_bin_it.endpoints.sources import HistogramSource\n'), ((43, 24, 43, 61), 'tests.doubles.consumer.StubConsumer', 'StubConsumer', ({(43, 37, 43, 48): "['broker1']", (43, 50, 43, 60): "['topic1']"}, {}), "(['broker1'], ['topic1'])", False, 'from tests.doubles.consumer import StubConsumer\n'), ((45, 13, 45, 43), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', ({(45, 29, 45, 42): 'mock_consumer'}, {}), '(mock_consumer)', False, 'from just_bin_it.endpoints.sources import HistogramSource\n'), ((18, 13, 18, 37), 'pytest.raises', 'pytest.raises', ({(18, 27, 18, 36): 'Exception'}, {}), '(Exception)', False, 'import pytest\n'), ((19, 12, 19, 33), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', ({(19, 28, 19, 32): 'None'}, {}), '(None)', False, 'from just_bin_it.endpoints.sources import HistogramSource\n')]