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()

[((276, 304), 'pytest.fixture', 'pytest.fixture', ([], {'autouse': '(True)'}), '(autouse=True)\n', (290, 304), False, 'import pytest\n'), ((738, 825), 'unittest.mock.patch', 'patch', (['"""just_bin_it.endpoints.sources.deserialise_hs00"""'], {'return_value': 'TEST_MESSAGE'}), "('just_bin_it.endpoints.sources.deserialise_hs00', return_value=\n    TEST_MESSAGE)\n", (743, 825), False, 'from unittest.mock import patch\n'), ((548, 585), 'tests.doubles.consumer.StubConsumer', 'StubConsumer', (["['broker1']", "['topic1']"], {}), "(['broker1'], ['topic1'])\n", (560, 585), False, 'from tests.doubles.consumer import StubConsumer\n'), ((638, 668), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', (['mock_consumer'], {}), '(mock_consumer)\n', (653, 668), False, 'from just_bin_it.endpoints.sources import HistogramSource\n'), ((951, 988), 'tests.doubles.consumer.StubConsumer', 'StubConsumer', (["['broker1']", "['topic1']"], {}), "(['broker1'], ['topic1'])\n", (963, 988), False, 'from tests.doubles.consumer import StubConsumer\n'), ((1057, 1087), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', (['mock_consumer'], {}), '(mock_consumer)\n', (1072, 1087), False, 'from just_bin_it.endpoints.sources import HistogramSource\n'), ((1309, 1346), 'tests.doubles.consumer.StubConsumer', 'StubConsumer', (["['broker1']", "['topic1']"], {}), "(['broker1'], ['topic1'])\n", (1321, 1346), False, 'from tests.doubles.consumer import StubConsumer\n'), ((1409, 1439), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', (['mock_consumer'], {}), '(mock_consumer)\n', (1424, 1439), False, 'from just_bin_it.endpoints.sources import HistogramSource\n'), ((411, 435), 'pytest.raises', 'pytest.raises', (['Exception'], {}), '(Exception)\n', (424, 435), False, 'import pytest\n'), ((449, 470), 'just_bin_it.endpoints.sources.HistogramSource', 'HistogramSource', (['None'], {}), '(None)\n', (464, 470), False, 'from just_bin_it.endpoints.sources import HistogramSource\n')]