"""Lambda test module.""" import json from pathlib import Path from unittest.mock import patch import pytest from src import app THIS_DIR = Path(__file__).parent @patch.object(app, 'lambda_metric') @patch.object(app, 'logic') def test_handler_happy_path(logic_mock, lambda_metric_mock): context = {} event = json.loads((THIS_DIR / 'sample-sqs-event.json').read_text()) app.handler(event=event, context=context) expected_body = json.loads(event['Records'][0]['body']) logic_mock.process_message.assert_called_once_with( body=expected_body, message_id='059f36b4-87a3-44ab-83d2-661975830a7d', ) lambda_metric_mock.assert_called_once_with('amazon_datapulse_trigger.success', 1) @patch.object(app, 'lambda_metric') @patch.object(app, 'logic') def test_handler_error_path(logic_mock, lambda_metric_mock): logic_mock.process_message.side_effect = ValueError( 'Unsupported report_type: unknown' ) context = {} event = json.loads((THIS_DIR / 'sample-sqs-event.json').read_text()) with pytest.raises(ValueError): app.handler(event=event, context=context) lambda_metric_mock.assert_called_once_with('amazon_datapulse_trigger.error', 1)