from .....src.logic.producer import handler from common.src.enums import AWSPayload import pytest from unittest.mock import AsyncMock, Mock sample_labels_periods = { 1: [100, 101], 2: [200], } @pytest.fixture def patcher_earliest_period_to_process(monkeypatch): """Patches the EARLIEST_PERIOD_TO_PROCESS constant to a fixed value for testing.""" monkeypatch.setattr( handler, "EARLIEST_PERIOD_TO_PROCESS", 0, ) # Set to a fixed value for testing def test_get_existing_reports_in_s3(mocker): mock_iter = mocker.patch.object( handler.aws.utils, "iter_s3_bucket_contents", autospec=True ) mock_iter.return_value = [ {AWSPayload.KEY: f"{label}/{period}/file.xlsx"} for label, period in [ ("1", "100"), ("1", "101"), ("2", "200"), ("invalid", "123"), ] ] result = handler._get_existing_reports_in_s3(Mock(), "mock-bucket") assert result == sample_labels_periods class TestProducerLoop: @pytest.mark.asyncio async def test_producer_loop(self): mock_queue = AsyncMock() skip_periods = { 1: [100], 2: [], } count = await handler._producer_loop( mock_queue, sample_labels_periods, skip_periods, earliest_period_to_process=None, ) assert count == (2, 1), "Expected 2 items added and 1 skipped" calls = [call.args[0] for call in mock_queue.put.call_args_list] assert calls == [ handler.QueueItem(label_id=1, period_id=101), handler.QueueItem(label_id=2, period_id=200), ] @pytest.mark.asyncio async def test_producer_loop_earliest_period_to_process(self): mock_queue = AsyncMock() skip_periods = {} count = await handler._producer_loop( mock_queue, sample_labels_periods, skip_periods, earliest_period_to_process=999, ) assert count == (0, 3), "Expected 0 items added and 3 skipped"