"""Test parse_ddex handler.""" from unittest.mock import patch import pytest from ddex_ingester_common.lambda_exceptions import AudioException from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema from index import (construct_error_correction_items, construct_s3_metadata, format_correction_detail_data, format_correction_detail_response, handle_track_asset, handler) def test_construct_s3_metadata(product_object, context_track_object): """Test s3 metadata formatting.""" output = construct_s3_metadata(product_object, context_track_object, '0') assert output['asset_type'] == 'FLAC' assert output['product_id'] == str(product_object.product_id) assert output['upc'] == product_object.upc assert output['track_unique_id'] == str(context_track_object.tuid) assert output['original_filename'] == context_track_object.asset.filename assert output['is_correction'] == '0' def test_construct_s3_metadata_error_correction(product_object, context_track_object, ddex_track_object): """Test s3 metadata formatting for error correction.""" output = construct_s3_metadata(product_object, context_track_object, '1') assert output['asset_type'] == 'FLAC' assert output['product_id'] == str(product_object.product_id) assert output['upc'] == product_object.upc assert output['track_unique_id'] == str(context_track_object.tuid) assert output['original_filename'] == ddex_track_object.asset.filename assert output['is_correction'] == '1' def test_format_correction_detail_data(error_correction_object): """Test correction detail payload formatting.""" product_id = error_correction_object['release_id'] release_correction_id = error_correction_object['release_correction_id'] items = error_correction_object['items'] output = format_correction_detail_data( product_id, release_correction_id, items ) assert output['productId'] == product_id assert output['releaseCorrectionId'] == release_correction_id assert output['corrections'] == [ { 'fieldName': 'track', 'keyValue': '"true"', 'keyId': 2851194, 'tableName': 'track', } ] def test_format_correction_detail_response(correction_detail_object): """Test correction detail creation response formatting.""" output = format_correction_detail_response(correction_detail_object) assert output['release_correction_detail_id'] == \ correction_detail_object['correctionDetailId'] assert output['key_id'] == correction_detail_object['keyId'] assert output['key_value'] == correction_detail_object['keyValue'] assert output['table_name'] == correction_detail_object['tableName'] assert output['field_name'] == correction_detail_object['fieldName'] @patch('index.StateMachineSchema.load', wrap=StateMachineSchema.load) @patch('index.StateMachineSchema.dump', wrap=StateMachineSchema.dump) @patch('index.logger') @patch('index.create_asset_token') @patch('index.copy_asset') @patch('index.graphql_gateway') @patch('index.load_ddex_json') def test_handler(mock_load_ddex_json, mock_graphql_gateway, mock_copy_asset, mock_create_asset_token, mock_logger, mock_context_schema_dump, mock_context_schema_load, ddex_json, lambda_input): """Test handler.""" mock_load_ddex_json.return_value = ddex_json handler(lambda_input, None) mock_create_asset_token.assert_called_once_with( mock_graphql_gateway, 'audio') mock_copy_asset.assert_called_once() mock_context_schema_load.assert_called_once() mock_context_schema_dump.assert_called_once() @patch('index.logger') @patch('index.create_asset_token') @patch('index.copy_asset') @patch('index.format_correction_detail_response') @patch('index.format_correction_detail_data') @patch('index.graphql_gateway.execute') @patch('index.graphql_gateway') @patch('index.load_ddex_json') def test_handler_create_error_correction( mock_load_ddex_json, mock_graphql_gateway, mock_graphql_execute, mock_format_correction_detail_data, mock_format_correction_detail_response, mock_copy_asset, mock_create_asset_token, mock_logger, error_correction_object, ddex_json, lambda_input): """Test error correction creation in handler.""" product_id = 2851194 release_correction_id = 287202 release_correction_detail_id = 4203813 items = [{ 'key_id': product_id, 'key_value': 'true', 'table_name': 'track', 'field_name': 'track', }] mock_format_correction_detail_data.return_value = { 'productId': product_id, 'releaseCorrectionId': release_correction_id, 'corrections': items, } mock_format_correction_detail_response.return_value = { 'release_correction_detail_id': release_correction_detail_id, **items[0] } mock_graphql_execute.return_value = { 'data': { 'createProductCorrectionDetail': [ { 'correctionDetailId': release_correction_detail_id, 'fieldName': 'track', 'keyValue': True, 'keyId': product_id, 'tableName': 'track', }, ] } } mock_load_ddex_json.return_value = ddex_json output_context = handler(lambda_input, None)['context'] mock_create_asset_token.assert_called_once_with( mock_graphql_gateway, 'audio') mock_copy_asset.assert_called_once() assert output_context['error_correction'] == error_correction_object @patch('index.StateMachineSchema.load', wrap=StateMachineSchema.load) @patch('index.load_ddex_json') def test_exception_raised_when_tuid_missing(mock_load_ddex_json, mock_context_schema_load, ddex_json, lambda_input): """Test handler raises AudioException when no TUID.""" lambda_input['track']['tuid'] = None mock_load_ddex_json.return_value = ddex_json with pytest.raises(AudioException): handle_track_asset(lambda_input) mock_context_schema_load.assert_called_once() def test_construct_error_correction_items( product_object, context_track_object, ddex_track_object): """Test constructing error correction.""" output = construct_error_correction_items( product_object.product_id, context_track_object ) assert type(output) is list assert len(output) == 2 assert output[0]['field_name'] == 'track' assert output[0]['key_value'] assert output[0]['key_id'] == context_track_object.tuid assert output[0]['table_name'] == 'track' assert output[1]['field_name'] == 'originalFileName' assert output[1]['key_value'] == ddex_track_object.asset.filename assert output[1]['key_id'] == context_track_object.tuid assert output[1]['table_name'] == 'track'