"""Unit tests for metadata-consistency-checks logic.""" from unittest.mock import MagicMock from unittest.mock import Mock import pytest from src.logic import start_sfn_execution @pytest.fixture def product_id(): """Fixture for product_id.""" return 123 class TestStartSfnExecution: """Tests for start_sfn_execution.""" @pytest.fixture def event(self, product_id): """Fixture for start_sfn_execution.""" return {'product_id': product_id} @pytest.fixture def get_product_id_from_event(self, mocker, product_id): """Mock for _get_product_id_from_event.""" return mocker.patch.object( start_sfn_execution, '_get_product_id_from_event', return_value=product_id) @pytest.fixture def execution_info(self): """Fixture for execution_info.""" return MagicMock() @pytest.fixture def start_execution(self, mocker, execution_info): """Mock for _start_execution.""" return mocker.patch.object( start_sfn_execution, '_start_execution', return_value=execution_info) @pytest.fixture def start_sfn_execution(self, event, get_product_id_from_event, start_execution): """Fixture for start_sfn_execution.""" return start_sfn_execution.start_sfn_execution(event) def test_start_sfn_execution(self, start_sfn_execution, execution_info): """Test test_start_sfn_execution result.""" result = start_sfn_execution assert result == execution_info def test_start_sfn_execution_product_id( self, event, start_sfn_execution, get_product_id_from_event): """Test _get_product_id_from_event is called with proper param.""" get_product_id_from_event.assert_called_once_with(event) def test_start_sfn_execution_start_execution( self, product_id, start_sfn_execution, start_execution): """Test _start_execution is called with proper param.""" start_execution.assert_called_once_with(product_id) class TestGetProductIdFromEvent: """Tests for _get_product_id_from_event.""" @pytest.fixture def event(self): """Fixture for event.""" return {'records': [{'my': 'event'}]} @pytest.fixture def msk_message_value(self): """Fixture for msk_message.value.""" return 'value' @pytest.fixture def msk_message_topic(self): """Fixture for msk_message.topic.""" return 'my-topic' @pytest.fixture def msk_message(self, msk_message_value, msk_message_topic): """Fixture for msk_message.""" return Mock(value=msk_message_value, topic=msk_message_topic) @pytest.fixture def event_source_message(self, mocker, msk_message): """Mock EventSourceMessage object.""" return_value = [(Mock(), msk_message)] mocker.patch.object(start_sfn_execution, 'EventSourceMessage', return_value=return_value) @pytest.fixture def event_value(self, product_id): """Fixture for event_value.""" return {'payload': {'product_id': product_id}} @pytest.fixture def deserializer(self, event_value): """Fixture for deserializer.""" deserializer_mock = Mock() deserializer_mock.deserialize.return_value = event_value return deserializer_mock @pytest.fixture def avro_deserializer(self, mocker, deserializer): """Mock AvroDeserializer object.""" return mocker.patch.object( start_sfn_execution, 'AvroDeserializer', return_value=deserializer) @pytest.fixture def get_product_id_from_event(self, event, event_source_message, avro_deserializer): """Fixture for _get_product_id_from_event.""" return start_sfn_execution._get_product_id_from_event(event) def test_get_product_id_from_event(self, get_product_id_from_event, product_id): """Test _get_product_id_from_event result.""" assert get_product_id_from_event == product_id def test_deserializer( self, get_product_id_from_event, deserializer, msk_message_value, msk_message_topic): """Test deserializer.""" deserializer.deserialize.assert_called_once_with( msk_message_value, msk_message_topic, 'value')