"""Test for upload_job_results module.""" from botocore.exceptions import ConnectionError from flexmock import flexmock import pytest from transcoding.constants import exceptions as transcoding_exceptions from transcoding.logic import storage from transcoding.models import s3_file @pytest.fixture def fixture_upload_job_results_args(): """Return args for upload_job_results call.""" return { 'source_path': 'some/file/path', 'transcoding_job_details': { 'transcoding_job_id': 20, 'transcoding_order_id': 10, 'output_bucket': 'some_bucket', 'output_key': 'some_unique_filename.key' } } @pytest.fixture def fixture_upload_result(): """Return s3 upload result.""" return { 'status': 'ok' } @pytest.fixture def fixture_generate_url_for_uploaded_asset_args(): """Fixture with args for generate_url_for_uploaded_asset call.""" return { 'bucket': 'test_bucket', 'key': 'audio_file.wav', 'client_method': 'put_object', 'expires_in': 3600 } @pytest.fixture def fixture_copy_object_args(): """Return args for copy_object call.""" return { 'source_bucket': 'source_bucket', 'source_key': 'source_key.wav', 'destination_bucket': 'destination_bucket', 'destination_key': 'destination_key.wav', } def test_upload_job_results_success( fixture_upload_job_results_args, fixture_upload_result): """Test for success of upload_job_results.""" (flexmock(s3_file) .should_receive('upload_s3_file') .with_args( fixture_upload_job_results_args['source_path'], fixture_upload_job_results_args['transcoding_job_details'][ 'output_bucket'], fixture_upload_job_results_args['transcoding_job_details'][ 'output_key']) .and_return(fixture_upload_result) .once()) result = storage.upload_job_results( **fixture_upload_job_results_args) assert result == fixture_upload_result def test_upload_job_results_fatal_error(fixture_upload_job_results_args): """Test for failure of upload_job_results.""" exception_message = 'fatal_error' (flexmock(s3_file) .should_receive('upload_s3_file') .with_args( fixture_upload_job_results_args['source_path'], fixture_upload_job_results_args['transcoding_job_details'][ 'output_bucket'], fixture_upload_job_results_args['transcoding_job_details'][ 'output_key']) .and_raise(Exception(exception_message)) .once()) with pytest.raises(transcoding_exceptions.TranscodingFatalError): storage.upload_job_results(**fixture_upload_job_results_args) def test_upload_job_results_retryable_error(fixture_upload_job_results_args): """Test for failure of upload_job_results.""" exception_message = 'fatal_error' (flexmock(s3_file) .should_receive('upload_s3_file') .with_args( fixture_upload_job_results_args['source_path'], fixture_upload_job_results_args['transcoding_job_details'][ 'output_bucket'], fixture_upload_job_results_args['transcoding_job_details'][ 'output_key']) .and_raise(ConnectionError(error=exception_message)) .once()) with pytest.raises(transcoding_exceptions.TranscodingRetryableError): storage.upload_job_results(**fixture_upload_job_results_args) def test_generate_url_for_uploaded_asset_success( fixture_generate_url_for_uploaded_asset_args): """Test for success of generate_url_for_uploaded_asset call.""" # data preparation expected_url = 'url' # mocking (flexmock(s3_file) .should_receive('create_presigned_url') .with_args( fixture_generate_url_for_uploaded_asset_args['bucket'], fixture_generate_url_for_uploaded_asset_args['key'], fixture_generate_url_for_uploaded_asset_args['client_method'], fixture_generate_url_for_uploaded_asset_args['expires_in']) .and_return(expected_url) .once()) # test functional call result = storage.generate_url_for_uploaded_asset( **fixture_generate_url_for_uploaded_asset_args) # checking assert result == expected_url def test_copy_object_success(fixture_copy_object_args): """Test for success of copy_object.""" (flexmock(s3_file) .should_receive('copy_s3_file') .with_args( fixture_copy_object_args['source_bucket'], fixture_copy_object_args['source_key'], fixture_copy_object_args['destination_bucket'], fixture_copy_object_args['destination_key']) .and_return(None) .once()) storage.copy_object(**fixture_copy_object_args) def test_copy_object_fatal_error(fixture_copy_object_args): """Test for fatal error during copy_object.""" (flexmock(s3_file) .should_receive('copy_s3_file') .with_args( fixture_copy_object_args['source_bucket'], fixture_copy_object_args['source_key'], fixture_copy_object_args['destination_bucket'], fixture_copy_object_args['destination_key']) .and_raise(Exception('fatal_error')) .once()) with pytest.raises(transcoding_exceptions.TranscodingFatalError): storage.copy_object(**fixture_copy_object_args) def test_copy_object_retryable_error(fixture_copy_object_args): """Test for retryable error during copy_object.""" (flexmock(s3_file) .should_receive('copy_s3_file') .with_args( fixture_copy_object_args['source_bucket'], fixture_copy_object_args['source_key'], fixture_copy_object_args['destination_bucket'], fixture_copy_object_args['destination_key']) .and_raise(ConnectionError(error='retryable_error')) .once()) with pytest.raises(transcoding_exceptions.TranscodingRetryableError): storage.copy_object(**fixture_copy_object_args) def test_generate_url_for_uploaded_asset_failure( fixture_generate_url_for_uploaded_asset_args): """Test for failure of generate_url_for_uploaded_asset call.""" # data preparation exception_message = 'fatal_error' # mocking (flexmock(s3_file) .should_receive('create_presigned_url') .with_args( fixture_generate_url_for_uploaded_asset_args['bucket'], fixture_generate_url_for_uploaded_asset_args['key'], fixture_generate_url_for_uploaded_asset_args['client_method'], fixture_generate_url_for_uploaded_asset_args['expires_in']) .and_raise(Exception(exception_message)) .once()) # test functional call and checking with pytest.raises(Exception) as exc_info: storage.generate_url_for_uploaded_asset( **fixture_generate_url_for_uploaded_asset_args) assert exc_info.value.args[0] == exception_message