"""Tests for lambda expections module.""" from unittest.mock import patch import pytest from src import lambda_exceptions from src.constants import errors @patch('src.lambda_exceptions.status') @pytest.mark.parametrize(( 'error_code', 'error_params', 'expected_message', ), [ ( 'Some', None, 'Unknown Error', ), ( errors.S3_FILE_NOT_FOUND_CODE, {'key': 'k', 'bucket': 'b'}, 'k not found in bucket b!', ) ]) def test_notify( status_mock, error_code, error_params, expected_message): """Test notifying.""" test_function = 'function' test_status = 'status' test_filename = 'filename' test_bucket = 'bucket' test_input_params = {'foo': 'bar'} result = lambda_exceptions.notify( function=test_function, error_status=test_status, error_code=error_code, filename=test_filename, bucket=test_bucket, error_params=error_params, input_params=test_input_params, ) status_mock.send_general_status.assert_called_with( function=test_function, status=test_status, filename=test_filename, error_code=error_code, description=expected_message, bucket=test_bucket, input_params=test_input_params, ) assert result == expected_message @patch('src.lambda_exceptions.notify', return_value='some error') def test_notify_and_raise(notify_mock): """Test notifying and raising Exception.""" test_function = 'function' test_status = 'status' test_error_code = 'some_error_code' test_filename = 'filename' test_bucket = 'bucket' test_error_params = {'message': 'some error'} test_input_params = {'foo': 'bar'} with pytest.raises(Exception, match='some error'): lambda_exceptions.notify_and_raise( function=test_function, error_status=test_status, error_code=test_error_code, filename=test_filename, bucket=test_bucket, error_params=test_error_params, input_params=test_input_params, ) notify_mock.assert_called_with( function=test_function, error_status=test_status, error_code=test_error_code, filename=test_filename, bucket=test_bucket, error_params=test_error_params, input_params=test_input_params, )