"""Tests for remove asset logic layer.""" import flexmock from oto import response import pytest from assets.constants import error from assets.constants import asset_types from assets.logic.legacy import remove_asset from assets.models import ows_product from assets.models import ows_track from assets.models.legacy import manage_asset from assets.models.legacy import asset_type @pytest.fixture def correct_remove_image_asset_data(): """Correct data for remove_image_asset_by_product_id call.""" return { 'product_id': 123345, 'asset_filenames': ['54321.tif'], 'asset_type_id': 3 } @pytest.fixture def correct_remove_track_asset_data(): """Correct data for remove_track_asset call.""" return { 'track_id': 999, 'product_id': 123345, 'asset_filenames': ['54321_999.wav'], 'asset_type_id': 3 } @pytest.fixture def remove_asset_mocks(): """Success mocks for assets removal calls.""" return { 'ows_track': response.Response( {'tuid': 999, 'product_id': 123345} ), 'ows_product': response.Response( {'upc': 54321, 'status': 'in_progress'} ), 'asset_type': response.Response({'id': 3, 'extension': '.wav'}), 'delete_assets_response': response.Response(message=error.SUCCESS_CODE) } def test_remove_image_asset_by_product_id_success( correct_remove_image_asset_data, remove_asset_mocks): """Test for successful remove_image_asset_by_product_id call.""" (flexmock(ows_product) .should_receive('get_product_by_id') .with_args(correct_remove_image_asset_data['product_id']) .and_return(remove_asset_mocks['ows_product'])) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_TIF) .and_return(remove_asset_mocks['asset_type'])) (flexmock(manage_asset) .should_receive('delete_assets') .with_args( correct_remove_image_asset_data['asset_filenames'], correct_remove_image_asset_data['asset_type_id'], ) .and_return(remove_asset_mocks['delete_assets_response'])) result = remove_asset.remove_image_asset_by_product_id( correct_remove_image_asset_data['product_id']) assert result @pytest.mark.parametrize('failing_mock', [ 'ows_product', 'asset_type', 'delete_assets_response']) def test_remove_image_asset_by_product_id_failure( correct_remove_image_asset_data, remove_asset_mocks, failing_mock): """Test for failure remove_image_asset_by_product_id call.""" remove_asset_mocks[failing_mock] = response.create_fatal_response( message='%s_failure' % failing_mock ) (flexmock(ows_product) .should_receive('get_product_by_id') .with_args(correct_remove_image_asset_data['product_id']) .and_return(remove_asset_mocks['ows_product'])) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_TIF) .and_return(remove_asset_mocks['asset_type'])) (flexmock(manage_asset) .should_receive('delete_assets') .with_args( correct_remove_image_asset_data['asset_filenames'], correct_remove_image_asset_data['asset_type_id'], ) .and_return(remove_asset_mocks['delete_assets_response'])) result = remove_asset.remove_image_asset_by_product_id( correct_remove_image_asset_data['product_id']) assert not result assert result.errors['message'] == '%s_failure' % failing_mock def test_remove_image_asset_by_product_id_with_in_content_product( correct_remove_image_asset_data, remove_asset_mocks): """Test for failure remove_image_asset_by_product_id call.""" ows_product_mock = response.Response( {'upc': 54321, 'status': 'in_content'} ) (flexmock(ows_product) .should_receive('get_product_by_id') .with_args(correct_remove_image_asset_data['product_id']) .and_return(ows_product_mock)) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_TIF) .and_return(remove_asset_mocks['asset_type'])) (flexmock(manage_asset) .should_receive('delete_assets') .with_args( correct_remove_image_asset_data['asset_filenames'], correct_remove_image_asset_data['asset_type_id'], ) .and_return(remove_asset_mocks['delete_assets_response'])) result = remove_asset.remove_image_asset_by_product_id( correct_remove_image_asset_data['product_id']) assert result.status == 400 assert result.errors['message'] == error.ERROR_MESSAGE_PRODUCT_IN_CONTENT assert result.errors['code'] == error.ERROR_CODE_WRONG_VALUE def test_remove_track_asset_success( correct_remove_track_asset_data, remove_asset_mocks): """Test for successful remove_track_asset call.""" (flexmock(ows_track) .should_receive('get_track_by_id') .with_args(correct_remove_track_asset_data['track_id']) .and_return(remove_asset_mocks['ows_track'])) (flexmock(ows_product) .should_receive('get_product_by_id') .with_args(correct_remove_track_asset_data['product_id']) .and_return(remove_asset_mocks['ows_product'])) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_WAV) .and_return(remove_asset_mocks['asset_type']) .ordered()) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_MP3_192) .and_return(remove_asset_mocks['asset_type']) .ordered()) (flexmock(manage_asset) .should_receive('delete_assets') .with_args( correct_remove_track_asset_data['asset_filenames'], correct_remove_track_asset_data['asset_type_id'], ) .and_return(remove_asset_mocks['delete_assets_response'])) result = remove_asset.remove_track_asset( correct_remove_track_asset_data['track_id']) assert result @pytest.mark.parametrize('failing_mock', [ 'ows_track', 'ows_product', 'asset_type', 'delete_assets_response']) def test_remove_track_asset_failure( correct_remove_track_asset_data, remove_asset_mocks, failing_mock): """Test for failure remove_track_asset call.""" remove_asset_mocks[failing_mock] = response.create_fatal_response( message='%s_failure' % failing_mock ) (flexmock(ows_track) .should_receive('get_track_by_id') .with_args(correct_remove_track_asset_data['track_id']) .and_return(remove_asset_mocks['ows_track'])) (flexmock(ows_product) .should_receive('get_product_by_id') .with_args(correct_remove_track_asset_data['product_id']) .and_return(remove_asset_mocks['ows_product'])) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_WAV) .and_return(remove_asset_mocks['asset_type']) .ordered()) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_MP3_192) .and_return(remove_asset_mocks['asset_type']) .ordered()) (flexmock(manage_asset) .should_receive('delete_assets') .with_args( correct_remove_track_asset_data['asset_filenames'], correct_remove_track_asset_data['asset_type_id'], ) .and_return(remove_asset_mocks['delete_assets_response'])) result = remove_asset.remove_track_asset( correct_remove_track_asset_data['track_id']) assert not result assert result.errors['message'] == '%s_failure' % failing_mock def test_remove_track_asset_with_in_content_product( correct_remove_track_asset_data, remove_asset_mocks): """Test for failure remove_track_asset call.""" ows_product_mock = response.Response( {'upc': 54321, 'status': 'in_content'} ) (flexmock(ows_track) .should_receive('get_track_by_id') .with_args(correct_remove_track_asset_data['track_id']) .and_return(remove_asset_mocks['ows_track'])) (flexmock(ows_product) .should_receive('get_product_by_id') .with_args(correct_remove_track_asset_data['product_id']) .and_return(ows_product_mock)) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_WAV) .and_return(remove_asset_mocks['asset_type']) .ordered()) (flexmock(asset_type) .should_receive('get_asset_type_by_name') .with_args(asset_types.TYPE_FILE_MP3_192) .and_return(remove_asset_mocks['asset_type']) .ordered()) (flexmock(manage_asset) .should_receive('delete_assets') .with_args( correct_remove_track_asset_data['asset_filenames'], correct_remove_track_asset_data['asset_type_id'], ) .and_return(remove_asset_mocks['delete_assets_response'])) result = remove_asset.remove_track_asset( correct_remove_track_asset_data['track_id']) assert result.status == 400 assert result.errors['message'] == error.ERROR_MESSAGE_PRODUCT_IN_CONTENT assert result.errors['code'] == error.ERROR_CODE_WRONG_VALUE