"""Lambda test module.""" import importlib from unittest import mock import pytest from src import app @pytest.mark.parametrize( 'resource_type', [ ('Vendor'), ('SubAccount') ] ) @mock.patch('src.connectors.ows_sound_recordings.touch_sound_recordings') def test_handler(mock_ows, resource_type): """Test handler function.""" test_event = { 'label': resource_type, 'id': 123, 'timestamp': '2019-06-15T16:00:00+00:00', 'limit': 100 } context = {} mock_ows.side_effect = [ 10000, 25 ] result = app.handler(test_event, context) assert mock_ows.call_args_list == [ mock.call(123, resource_type, '2019-06-15T16:00:00+00:00', 10000), mock.call(123, resource_type, '2019-06-15T16:00:00+00:00', 10000) ] assert result == {'total_updates': 10025} @pytest.mark.parametrize( 'data, exc_type, exc_value', [ ( { 'label': 'Vendor', 'id': 123, 'timestamp': '2019-06-15T16:00', 'limit': 100 }, Exception, 'Timestamp tzinfo "None" is not UTC' ), ( { 'label': 'Vendor', 'id': 123, 'timestamp': '06/15/2019', 'limit': 100 }, ValueError, "Invalid isoformat string: '06/15/2019'" ), ( { 'label': 'Product', 'id': 123, 'timestamp': '2019-06-15T16:00:00+00:00', 'limit': 100 }, Exception, 'Unexpected data type of "Product"' ) ] ) @mock.patch('src.app.logger') @mock.patch('src.connectors.ows_sound_recordings.touch_sound_recordings') def test_invalid_inputs(mock_ows, mock_logger, data, exc_type, exc_value): """Test invalid inputs.""" context = {} mock_ows.return_value = 0 with pytest.raises(exc_type) as e: app.handler(data, context) assert str(e.value) == exc_value assert not mock_ows.called assert mock_logger.exception.called @mock.patch('sentry_sdk.integrations.aws_lambda.AwsLambdaIntegration') @mock.patch('config.secrets_manager_client') @mock.patch('sentry_sdk.init') def test_handler_init(mock_sentry, mock_secrets, mock_aws_integration): """Test expected init.""" mock_secret_values = { 'SENTRY_DSN': 'fake-dsn' } mock_secrets.get_cred.side_effect = lambda x: mock_secret_values[x] assert not mock_sentry.called importlib.reload(app) mock_sentry.assert_called_once_with('fake-dsn', integrations=[mock_aws_integration.return_value]) # noqa:E501 mock_secrets.get_cred.assert_called_once_with('SENTRY_DSN') @mock.patch('config.secrets_manager_client') @mock.patch('sentry_sdk.init') def test_handler_init_empty_sentry(mock_sentry, mock_secrets): """Test sentry init with empty dsn.""" mock_secret_values = { 'SENTRY_DSN': '' } mock_secrets.get_cred.side_effect = lambda x: mock_secret_values[x] assert not mock_sentry.called importlib.reload(app) assert not mock_sentry.called @mock.patch('config.secrets_manager_client') @mock.patch('sentry_sdk.init') def test_handler_init_none_sentry(mock_sentry, mock_secrets): """Test sentry init with dsn.""" mock_secret_values = { 'SENTRY_DSN': None } mock_secrets.get_cred.side_effect = lambda x: mock_secret_values[x] assert not mock_sentry.called importlib.reload(app) assert not mock_sentry.called