"""Lambda test module.""" from unittest.mock import patch import pytest from src import app from src import config from src.constants import general from src.lambda_exceptions import LoggedException from src.metadata_validator import InvalidAudioError @patch('src.app.create_transcoding_by_preset') @patch('src.audio_validation_logic.validate_audio_asset') @patch('src.s3.object_exists') @patch('src.status.ows_assets') @patch('src.status.send_general_status') def test_handler_success( send_general_status_mock, mock_ows_assets, object_exists_mock, validate_audio_asset_mock, create_transcoding_by_preset_mock): """Test handler function.""" # mock data test_bucket = 'test_bucket' test_key = 'test_key.flac' event_mock = { 'key': test_key, 'bucket': test_bucket, 'asset_upload_type': 'stereo', } # mocking object_exists_mock.return_value = True validate_audio_asset_mock.return_value = {'abc': '123'} # test function call result = app.handler(event_mock, None) # checking assert result assert result['is_valid'] is True validate_audio_asset_mock.assert_called_with(test_bucket, test_key) create_transcoding_by_preset_mock.assert_called_with( bucket=test_bucket, key=test_key, preset_alias='{}_mezzanine'.format(config.ENVIRONMENT), metadata={'abc': '123'}, status_topic_alias=general.SNS_STATUS_TOPIC_ALIAS) send_general_status_mock.assert_called_once_with( general.LAMBDA_NAME, general.AUDIO_VALIDATION_COMPLETE_STATUS, event_mock['key'], bucket=event_mock['bucket']) @patch('src.app.create_transcoding_by_preset') @patch('src.audio_validation_logic.validate_audio_asset') @patch('src.s3.object_exists') @patch('src.status.ows_assets') def test_handler_failure_unexpected_error( mock_ows_assets, object_exists_mock, validate_audio_asset_mock, create_transcoding_by_preset_mock): """Test handler function.""" # mock data test_bucket = 'test_bucket' test_key = 'test_key.flac' event_mock = { 'key': test_key, 'bucket': test_bucket, 'asset_upload_type': 'stereo', } error_message = 'Unexpected error' # mocking validate_audio_asset_mock.side_effect = Exception(error_message) with pytest.raises(LoggedException, match=error_message): app.handler(event_mock, None) @patch('src.app.create_transcoding_by_preset') @patch('src.audio_validation_logic.validate_audio_asset') @patch('src.s3.object_exists') @patch('src.status.ows_assets') def test_handler_object_not_found( ows_assets_mock, object_exists_mock, validate_audio_asset_mock, create_transcoding_by_preset_mock): """Test handler function.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key' event_mock = { 'key': test_key, 'bucket': test_bucket, 'asset_upload_type': 'stereo', } object_exists_mock.return_value = False with pytest.raises(Exception): # test function call app.handler(event_mock, None) @patch('src.app.create_transcoding_by_preset') @patch('src.audio_validation_logic.validate_audio_asset') @patch('src.s3.object_exists') @patch('src.status.ows_assets') def test_handler_with_failed_validation( ows_assets_mock, object_exists_mock, validate_audio_asset_mock, create_transcoding_by_preset_mock, ): """Test handler function with failed validation.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key' event_mock = { 'key': test_key, 'bucket': test_bucket, 'asset_upload_type': 'stereo', } validation_error = InvalidAudioError( errors='some errors', metadata='some metadata', ) object_exists_mock.return_value = True validate_audio_asset_mock.side_effect = validation_error result = app.handler(event_mock, None) create_transcoding_by_preset_mock.assert_not_called() ows_assets_mock.post_asset_status.assert_called_once() assert result == {'is_valid': False} @patch('src.app.create_transcoding_by_preset') @patch('src.audio_validation_logic.validate_audio_asset') @patch('src.s3.object_exists') @patch('src.status.ows_assets') def test_handler_with_wrong_asset_upload_type( ows_assets_mock, object_exists_mock, validate_audio_asset_mock, create_transcoding_by_preset_mock, ): """Test handler raises LoggedException for non-stereo asset upload type.""" # mock data test_bucket = 'test_bucket' test_key = 'test_key.flac' event_mock = { 'key': test_key, 'bucket': test_bucket, 'asset_upload_type': 'unknown', } # mocking object_exists_mock.return_value = True validate_audio_asset_mock.return_value = {'abc': '123'} with pytest.raises(LoggedException): app.handler(event_mock, None) create_transcoding_by_preset_mock.assert_not_called() ows_assets_mock.post_asset_status.assert_called_once_with( test_key, 'validation_error', f'{test_key} file (bucket {test_bucket}) has wrong asset upload type (unknown)', { 'function': 'audio-validation', 'status': 'validation_error', 'error_code': 'audio_wrong_asset_upload_type_error', 'description': f'{test_key} file (bucket {test_bucket}) has wrong asset upload type (unknown)', 'input': {'key': test_key, 'bucket': test_bucket}, }, ) @patch('src.app.create_transcoding_by_preset') @patch('src.audio_validation_logic.validate_audio_asset') @patch('src.s3.object_exists') @patch('src.status.ows_assets') def test_handler_with_already_logged_exception( ows_assets_mock, object_exists_mock, validate_audio_asset_mock, create_transcoding_by_preset_mock, ): """Test handler function with already logged exception.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key' event_mock = { 'key': test_key, 'bucket': test_bucket, 'asset_upload_type': 'stereo', } object_exists_mock.return_value = False with pytest.raises( LoggedException, match='test_key not found in bucket test_bucket!' ): # test function call app.handler(event_mock, None) validate_audio_asset_mock.assert_not_called() create_transcoding_by_preset_mock.assert_not_called() ows_assets_mock.post_asset_status.assert_called_once()