"""Test handler.""" from unittest.mock import call, patch from ddex_ingester_common.helpers.catalog_ingestion import ( CatalogIngestionValidationResult ) from ddex_ingester_common.schemas.state_machine_schema import ( StateMachineSchema ) import index @patch('index.graphql_gateway') def test_handler(mock_graphql, mock_event): """Test handler.""" result = index.handler(mock_event, None) mock_event.pop('errors') assert result == mock_event @patch('index.graphql_gateway') @patch('index.config.catalog_ingestion_session') @patch('index.check_for_asset_validation_errors') def test_handler_with_errors( mock_check_for_asset_validation_errors, mock_catalog_ingestion_session, mock_graphql, mock_event): """Test handler.""" graphql_return = { 'data': { 'product': { 'validation': { 'isValid': False, 'errors': [ { 'code': 'RELEASE_DATE_REQUIRED', 'reason': "'release_date' is a required property" }, { 'code': 'SALE_START_DATE_REQUIRED', 'reason': "'sale_start_date' is a required property" # noqa } ], }, 'tracks': [{ 'isrc': 'ABC123', 'validation': { 'isValid': False, 'errors': [ { 'code': 'TRACK_NAME_VERSION_ALREADY_IN_USE', 'reason': 'Track Name + Version already used in a previous track' # noqa }, { 'code': 'MISSING_OWNERSHIP_RIGHTS', 'reason': 'Field `ownership_rights` is required' # noqa }, { 'code': 'MISSING_WRITERS', 'reason': 'Field `writers` is required' }, ], }, }], } } } expected_calls = [ call([ CatalogIngestionValidationResult( state_machine_name='name', state_machine_execution_name='execution name', validation_rule_id=0, response='Warning', message="'release_date' is a required property", isrc=None, category='RELEASE_DATE_REQUIRED'), CatalogIngestionValidationResult( state_machine_name='name', state_machine_execution_name='execution name', validation_rule_id=0, response='Warning', message="'sale_start_date' is a required property", isrc=None, category='SALE_START_DATE_REQUIRED'), ]), call([ CatalogIngestionValidationResult( state_machine_name='name', state_machine_execution_name='execution name', validation_rule_id=0, response='Warning', message='Track Name + Version already used in a previous track', # noqa isrc='ABC123', category='TRACK_NAME_VERSION_ALREADY_IN_USE'), CatalogIngestionValidationResult( state_machine_name='name', state_machine_execution_name='execution name', validation_rule_id=0, response='Warning', message='Field `ownership_rights` is required', isrc='ABC123', category='MISSING_OWNERSHIP_RIGHTS'), CatalogIngestionValidationResult( state_machine_name='name', state_machine_execution_name='execution name', validation_rule_id=0, response='Warning', message='Field `writers` is required', isrc='ABC123', category='MISSING_WRITERS') ]), ] mock_graphql.execute.return_value = graphql_return index.handler(mock_event, None) calls = mock_catalog_ingestion_session.add.call_args_list assert calls == expected_calls mock_check_for_asset_validation_errors.assert_called() def test_flatten_product_errors(): """Test flatten_errors for product.""" errors = [ [ { 'code': 'ARTWORK', 'reason': 'Artwork has not been uploaded successfully.' } ], ] errors = index.flatten_errors(errors) assert errors == [ 'Artwork has not been uploaded successfully.', ] def test_flatten_track_errors(): """Test flatten_errors for tracks.""" errors = [ [ { 'code': 'MISSING_AUDIO_FILE', 'reason': 'Field `audio_file` is required' }, { 'code': 'MISSING_BOTH_CHINESE_LANGUAGES', 'reason': 'Simplified and Traditional variations are required' } ] ] errors = index.flatten_errors(errors, 'US34523452435') assert errors == [ 'ISRC: US34523452435: Field `audio_file` is required', 'ISRC: US34523452435: Simplified and Traditional variations are required' # noqa ] def test_check_for_asset_validation_errors_true_audio(mock_event): """Test check_for_asset_validation_errors.""" errors = ['A', 'B', 'ISRC: US34523452435: Field `audio_file` is required'] context = StateMachineSchema().load(mock_event) index.check_for_asset_validation_errors(context, errors) assert context.validate_product_retry_counter == 1 def test_check_for_asset_validation_errors_true_artwork(mock_event): """Test check_for_asset_validation_errors.""" errors = ['A', 'B', 'Artwork has not been uploaded successfully.'] context = StateMachineSchema().load(mock_event) index.check_for_asset_validation_errors(context, errors) assert context.validate_product_retry_counter == 1 def test_check_for_asset_validation_errors_increment(mock_event): """Test check_for_asset_validation_errors.""" errors = ['A', 'B', 'Artwork has not been uploaded successfully.'] context = StateMachineSchema().load(mock_event) context.validate_product_retry_counter = 1 index.check_for_asset_validation_errors(context, errors) assert context.validate_product_retry_counter == 2 def test_check_for_asset_validation_errors_false(mock_event): """Test check_for_asset_validation_errors.""" errors = ['A', 'B', 'ISRC: GBKPL1832714: Field `ownership_rights` is required'] # noqa context = StateMachineSchema().load(mock_event) context.validate_product_retry_counter = 0 index.check_for_asset_validation_errors(context, errors) assert context.validate_product_retry_counter == 0