"""Tests for asset_copy logic module.""" from typing import Any import pytest from flexmock import flexmock from owsresponse import response from sqlalchemy.exc import SQLAlchemyError from assets.constants import asset_upload_types from assets.logic import asset_copy from assets.models import ( asset_final as asset_final_model, asset_status as asset_status_model, asset_upload, ows_product, ) @pytest.fixture def fixture_records_to_copy() -> dict[str, Any]: """Fixture with correct mocks for update_asset_upload call.""" return { "asset_upload": { "asset_type": "asset type", "filename": "file name", "is_correction": False, "original_filename": "original filename", }, "asset_finals": [ { "asset_type": "JPG", "asset_subtype": "cover", "bucket": "bucket", "filename": "images/v2/product/cover/123", "duration": 0, }, { "asset_type": "JPG", "asset_subtype": "large_cover", "bucket": "bucket", "filename": "images/v2/product/large_cover/123", "duration": 0, }, ], "asset_status": [ { "status": "upload_complete", "description": "test", "status_time": "2020-03-17 06:12:59.072000", "message": '{"function": "acknowledge", "status": "upload_complete"}', }, { "status": "acknowledge_complete", "description": "test", "status_time": "2020-03-17 06:12:59.072000", "message": '{"function": "acknowledge", "status": "acknowledge_complete"}', }, ], } @pytest.fixture def fixture_audio_records_to_copy() -> dict[str, Any]: """Fixture with correct mocks for update_asset_upload call.""" return { "asset_upload": { "asset_type": "audio", "bucket_name": "some_bucket", "filename": "filename.wav", "is_correction": False, "original_filename": "original filename", }, "asset_finals": [ { "asset_type": "wav", "asset_subtype": "none", "bucket": "bucket", "filename": "filename.wav", "duration": 2543, }, { "asset_type": "mp3", "asset_subtype": "none", "bucket": "bucket", "filename": "filename.mp3", "duration": 221, }, ], "asset_status": [ { "status": "upload_complete", "description": "test", "status_time": "2020-03-17 06:12:59.072000", "message": '{"function": "acknowledge", "status": "upload_complete"}', }, { "status": "acknowledge_complete", "description": "test", "status_time": "2020-03-17 06:12:59.072000", "message": '{"function": "acknowledge", "status": "acknowledge_complete"}', }, ], } def test_copy_artwork_assets_success(fixture_records_to_copy: dict[str, Any]) -> None: """Test for success of update_asset_upload logic with v2 FF enabled.""" from_product_id = 123 to_product_id = 456 full_user_id = "alw:123" ( flexmock(ows_product) .should_receive("get_upcs_by_product_ids") .with_args([123, 456]) .and_return( { "items": [ {"upc": 11, "product_id": 123, "status": "label_processing"}, {"upc": 21, "product_id": 456, "status": "label_processing"}, ] } ) ) ( flexmock(asset_upload) .should_receive("get_asset_records_to_copy") .with_args(from_product_id, 0, asset_upload_types.STATIC_ARTWORK) .and_return(fixture_records_to_copy) ) ( flexmock(asset_upload) .should_receive("create_asset_upload_record_for_copy") .with_args( { "asset_type": "asset type", "filename": "file name", "is_correction": False, "original_filename": "original filename", "user_id": "alw:123", "token": None, "product_id": 456, "track_unique_id": 0, "upc": "21", "api_version": 2, } ) .and_return( { "id": 789, "user_id": "alw:123", "asset_type": "asset type", "token": None, "filename": "file name", "api_version": 2, "product_id": 456, "track_unique_id": 0, "upc": "UPC 456", "original_filename": "original filename", "deleted": 0, "is_correction": False, } ) ) ( flexmock(asset_final_model) .should_receive("create_asset_final") .and_return(None, None) ) ( flexmock(asset_status_model) .should_receive("create_asset_status") .and_return( response.Response( message={ "id": 11, "asset_upload_id": 789, "status": "upload_complete", "description": "test", "status_time": "2020-03-17 06:12:59.072000", "message": '{"function": "acknowledge", "status": "upload_complete"}', } ), response.Response( message={ "id": 12, "asset_upload_id": 789, "status": "acknowledge_complete", "description": "test", "status_time": "2020-03-17 06:12:59.072000", "message": '{"function": "acknowledge", "status": "acknowledge_complete"}', } ), ) ) asset_copy.copy_artwork_assets( from_product_id, to_product_id, full_user_id, ) def test_copy_artwork_assets_failure_on_asset_upload( fixture_records_to_copy: dict[str, Any], ) -> None: """Test for failure on asset upload creation with v2 FF enabled.""" from_product_id = 123 to_product_id = 456 full_user_id = "alw:123" error_message = "Query error" ( flexmock(ows_product) .should_receive("get_upcs_by_product_ids") .with_args([123, 456]) .and_return( { "items": [ {"upc": 11, "product_id": 123, "status": "label_processing"}, {"upc": 21, "product_id": 456, "status": "label_processing"}, ] } ) ) ( flexmock(asset_upload) .should_receive("get_asset_records_to_copy") .with_args(from_product_id, 0, asset_upload_types.STATIC_ARTWORK) .and_return(fixture_records_to_copy) ) ( flexmock(asset_upload) .should_receive("create_asset_upload_record_for_copy") .with_args( { "asset_type": "asset type", "filename": "file name", "is_correction": False, "original_filename": "original filename", "user_id": "alw:123", "token": None, "product_id": 456, "track_unique_id": 0, "upc": "21", "api_version": 2, } ) .and_raise(SQLAlchemyError(error_message)) ) with pytest.raises(SQLAlchemyError) as exc: asset_copy.copy_artwork_assets( from_product_id, to_product_id, full_user_id, ) assert str(exc.value) == error_message def test_copy_artwork_assets_failure_on_asset_final( fixture_records_to_copy: dict[str, Any], ) -> None: """Test for failure on asset final creation with v2 FF enabled.""" from_product_id = 123 to_product_id = 456 full_user_id = "alw:123" ( flexmock(ows_product) .should_receive("get_upcs_by_product_ids") .with_args([123, 456]) .and_return( { "items": [ {"upc": 11, "product_id": 123, "status": "label_processing"}, {"upc": 21, "product_id": 456, "status": "label_processing"}, ] } ) ) ( flexmock(asset_upload) .should_receive("get_asset_records_to_copy") .with_args(from_product_id, 0, asset_upload_types.STATIC_ARTWORK) .and_return(fixture_records_to_copy) ) ( flexmock(asset_upload) .should_receive("create_asset_upload_record_for_copy") .with_args( { "asset_type": "asset type", "filename": "file name", "is_correction": False, "original_filename": "original filename", "user_id": "alw:123", "token": None, "product_id": 456, "track_unique_id": 0, "upc": "21", "api_version": 2, } ) .and_return( { "id": 789, "user_id": "alw:123", "asset_type": "asset type", "token": None, "filename": "file name", "api_version": 2, "product_id": 456, "track_unique_id": 0, "upc": "UPC 456", "original_filename": "original filename", "deleted": 0, "is_correction": False, } ) ) ( flexmock(asset_status_model) .should_receive("create_asset_status") .and_return(fixture_records_to_copy["asset_status"]) ) ( flexmock(asset_final_model) .should_receive("create_asset_final") .and_raise(SQLAlchemyError("fatal error")) ) with pytest.raises(SQLAlchemyError) as exc: asset_copy.copy_artwork_assets( from_product_id, to_product_id, full_user_id, ) assert str(exc.value) == "fatal error" def test_copy_v2_assets_success(fixture_audio_records_to_copy: dict[str, Any]) -> None: """Test for success of copying v2 audio assets.""" from_product_id = 123 to_product_id = 456 to_upc = 21 full_user_id = "alw:123" from_tuid = 1001 to_tuid = 1002 ( flexmock(asset_upload) .should_receive("get_asset_records_to_copy") .with_args(from_product_id, from_tuid, asset_upload_types.STEREO, False) .and_return(fixture_audio_records_to_copy) ) ( flexmock(asset_upload) .should_receive("create_asset_upload_record_for_copy") .with_args( { "asset_type": "audio", "bucket_name": "some_bucket", "filename": "filename.wav", "is_correction": False, "original_filename": "original filename", "user_id": "alw:123", "token": None, "product_id": 456, "track_unique_id": 1002, "upc": "21", "api_version": 2, } ) .and_return( { "id": 789, "user_id": "alw:123", "asset_type": "audio", "token": None, "bucket_name": "some_bucket", "filename": "filename.wav", "api_version": 2, "product_id": 456, "track_unique_id": 1002, "upc": 21, "original_filename": "original filename", "deleted": 0, "is_correction": False, } ) ) ( flexmock(asset_status_model) .should_receive("create_asset_status") .and_return(None) ) ( flexmock(asset_final_model) .should_receive("create_asset_final") .and_return(None, None) ) asset_copy.copy_v2_assets( from_product_id, to_product_id, to_upc, full_user_id, from_tuid, to_tuid, asset_upload_types.STEREO, ) def test_copy_v2_assets_when_ignore_if_not_exist_true() -> None: """Test copy_v2_assets returns early when optional asset type is not found.""" from_product_id = 123 to_product_id = 456 to_upc = 21 full_user_id = "alw:123" from_tuid = 1001 to_tuid = 1002 ( flexmock(asset_upload) .should_receive("get_asset_records_to_copy") .with_args(from_product_id, from_tuid, asset_upload_types.ATMOS, True) .and_return(None) .once() ) ( flexmock(asset_upload) .should_receive("create_asset_upload_record_for_copy") .never() ) (flexmock(asset_status_model).should_receive("create_asset_status").never()) (flexmock(asset_final_model).should_receive("create_asset_final").never()) asset_copy.copy_v2_assets( from_product_id, to_product_id, to_upc, full_user_id, from_tuid, to_tuid, asset_upload_types.ATMOS, True, )