"""Lambda test module.""" from unittest.mock import patch import pytest from constants import general from constants import errors from src import app from src import lambda_exceptions @patch('src.s3.object_exists') @patch('src.status.send_general_status') @patch('src.connectors.ows_assets.get_asset_upload_type') def test_handler( get_asset_upload_type_mock, send_general_status_mock, object_exists_mock): """Test handler function.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key.jpg' event_mock = { 'bucket': test_bucket, 'key': test_key } object_exists_mock.return_value = True get_asset_upload_type_mock.return_value = 'stereo' # test function call result = app.handler(event_mock, None) # checking get_asset_upload_type_mock.assert_called_once_with(test_key) send_general_status_mock.assert_called_once_with( general.LAMBDA_NAME, general.ENCODING_ROUTE_COMPLETE_STATUS, test_key, input_params={**event_mock, 'asset_upload_type': 'stereo'}, bucket=test_bucket) assert result assert result['key'] == test_key assert result['bucket'] == test_bucket assert result['asset_upload_type'] == 'stereo' @patch('src.connectors.ows_assets.get_asset_upload_type') @patch('src.s3.object_exists') @patch('src.lambda_exceptions.notify_and_raise') def test_handler_failure(notify_and_raise_mock, object_exists_mock, get_asset_upload_type_mock): """Test handler function.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key.jpg' event_mock = { 'bucket': test_bucket, 'key': test_key } object_exists_mock.return_value = True get_asset_upload_type_mock.side_effect = Exception('mock exception') notify_and_raise_mock.side_effect = lambda_exceptions.LoggedException('error') # test function call and checking with pytest.raises(Exception): app.handler(event_mock, None) notify_and_raise_mock.assert_called_once_with( general.LAMBDA_NAME, general.ENCODING_ROUTE_ERROR_STATUS, errors.ASSET_ROUTING_ERROR_CODE, test_key, test_bucket, {'message': 'mock exception'}) @patch('src.connectors.ows_assets.get_asset_upload_type') @patch('src.s3.object_exists') @patch('src.lambda_exceptions.notify_and_raise') def test_handler_asset_upload_type_retrieval_failure( notify_and_raise_mock, object_exists_mock, get_asset_upload_type_mock): """Test handler when get_asset_upload_type raises AssetUploadTypeRetrievalException.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key.jpg' event_mock = { 'bucket': test_bucket, 'key': test_key } object_exists_mock.return_value = True get_asset_upload_type_mock.side_effect = lambda_exceptions.AssetUploadTypeRetrievalException( 'Failed to get upload type') notify_and_raise_mock.side_effect = lambda_exceptions.LoggedException('error') # test function call and checking with pytest.raises(Exception): app.handler(event_mock, None) notify_and_raise_mock.assert_called_once_with( general.LAMBDA_NAME, general.ENCODING_ROUTE_ERROR_STATUS, errors.ASSET_ROUTING_ERROR_CODE, test_key, test_bucket, {'message': 'Failed to get upload type'}) @patch('src.s3.object_exists') @patch('src.lambda_exceptions.notify_and_raise') def test_handler_object_not_found(notify_and_raise_mock, object_exists_mock): """Test handler function.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key' event_mock = { 'key': test_key, 'bucket': test_bucket } object_exists_mock.return_value = False notify_and_raise_mock.side_effect = lambda_exceptions.LoggedException('not found') # test function call and checking with pytest.raises(Exception): app.handler(event_mock, None) @patch('src.connectors.ows_assets.get_asset_upload_type') @patch('src.s3.object_exists') def test_handler_asset_not_found(object_exists_mock, get_asset_upload_type_mock): """Test handler returns stop_processing when asset is not found in ows-assets.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key.jpg' event_mock = { 'bucket': test_bucket, 'key': test_key } object_exists_mock.return_value = True get_asset_upload_type_mock.side_effect = lambda_exceptions.AssetNotFoundException('not found') # test function call result = app.handler(event_mock, None) # checking assert result == { 'bucket': test_bucket, 'key': test_key, 'stop_processing': True }