"""Lambda handler unit tests.""" import pytest from src import app from src.models.preference_change import InvalidPreferenceMessage class TestHandler: """Unit tests for the Lambda handler function.""" def test_returns_ok_on_success(self, handler_event, handler_success_mocks, sample_invalid_messages): """Handler should run the whole flow and return OK.""" result = app.handler(handler_event, None) assert result == {'status': 'OK'} handler_success_mocks.parse.assert_called_once_with(handler_event) handler_success_mocks.flatten.assert_called_once_with(handler_success_mocks.parse.return_value[0]) handler_success_mocks.prepare.assert_called_once_with(handler_success_mocks.flatten.return_value) handler_success_mocks.send.assert_called_once_with(handler_success_mocks.prepare.return_value) handler_success_mocks.dlq_send.assert_called_once_with(sample_invalid_messages) def test_logs_and_reraises_on_failure(self, handler_event, handler_error_mocks): """Handler should log and re-raise unexpected errors.""" with pytest.raises(RuntimeError): app.handler(handler_event, None) handler_error_mocks.parse.assert_called_once_with(handler_event) handler_error_mocks.flatten.assert_not_called() handler_error_mocks.prepare.assert_not_called() handler_error_mocks.send.assert_not_called() handler_error_mocks.dlq_send.assert_called_once() sent_messages = handler_error_mocks.dlq_send.call_args.args[0] assert len(sent_messages) == 1 dlq_message = sent_messages[0] assert isinstance(dlq_message, InvalidPreferenceMessage) assert dlq_message.message_key is None assert dlq_message.message_value == str(handler_event) assert dlq_message.error_type == 'RuntimeError' assert dlq_message.error_message == 'source parsing failed'