"""Test handler.""" from unittest.mock import patch from constants import queries from ddex_ingester_common.lambda_exceptions import ProcessParticipantsException from ddex_ingester_common.schemas.s3_schema import ParticipantSchema, S3Schema import index import pytest @patch('index.logging_utils') @patch('index.load_ddex_json') @patch('index.get_participants') @patch('index.get_artist') @patch('index.create_artist') @patch('index.create_label_participant') @patch('index.save_s3_context') def test_handler( mock_save_s3_context, mock_create_label_participant, mock_create_artist, mock_get_artist, mock_get_participants, mock_load_ddex_json, mock_logging_utils, serialized_participant, mock_event): """Test the main handler.""" mock_load_ddex_json.return_value = mock_event mock_get_participants.return_value = [serialized_participant] mock_get_artist.return_value = None mock_create_artist.return_value = { 'artistId': '1', 'artistName': 'Giveon' } mock_create_label_participant.return_value = { 'id': '3', 'uuid': 'AAAAAA', } expected_output = [ { 'name': 'Giveon', 'artist_id': '1', 'label_participant_id': '3', 'label_participant_uuid': 'AAAAAA', } ] index.handler(mock_event, None) mock_get_participants.assert_called() mock_get_artist.assert_called_with(serialized_participant, None) mock_create_artist.assert_called_with(serialized_participant, None, None) mock_create_label_participant.assert_called_with( serialized_participant, None, None) assert mock_save_s3_context.call_args[0][1].label_participants \ == expected_output @patch('index.check_participant_ids') @patch('index.get_all_participants') def test_get_participants( mock_get_all_participants, mock_check_participant_ids, s3_parsed_ddex): """Test get_participants.""" all_participants_dict = [ { 'sequence_number': 1, 'apple_id': '4A', 'name': 'Extra Artist', 'roles': ['ExtraArtist'], 'spotify_uri': '4S' }, { 'sequence_number': 1, 'name': 'Giveon', 'roles': ['MainArtist'], 'spotify_uri': '1S' }, { 'sequence_number': 2, 'apple_id': '2A', 'name': 'Artist', 'roles': ['SecondaryArtist'], 'spotify_uri': '2S' }, { 'sequence_number': 1, 'apple_id': '1A', 'name': 'Giveon', 'roles': [], }, { 'sequence_number': 2, 'name': 'Artist', 'roles': [], }, { 'sequence_number': 1, 'apple_id': '3A', 'name': 'Giveon Evans', 'roles': ['Arranger', 'Composer', 'Lyricist'], 'spotify_uri': '3S' } ] all_participants = [] for participant in all_participants_dict: all_participants.append(ParticipantSchema().load(participant)) mock_get_all_participants.return_value = all_participants expected_reponse = [ { 'name': 'Extra Artist', 'roles': ['ExtraArtist'], 'apple_id': '4A', 'spotify_uri': '4S' }, { 'name': 'Giveon', 'roles': ['MainArtist'], 'apple_id': '1A', 'spotify_uri': '1S' }, { 'name': 'Artist', 'roles': ['SecondaryArtist'], 'apple_id': '2A', 'spotify_uri': '2S' }, { 'name': 'Giveon Evans', 'roles': ['Arranger', 'Composer', 'Lyricist'], 'apple_id': '3A', 'spotify_uri': '3S' } ] parsed_ddex = S3Schema().load(s3_parsed_ddex) response = index.get_participants(parsed_ddex) assert response == expected_reponse def test_get_all_participants(s3_parsed_ddex): """Test get_all_participants.""" s3_parsed_ddex['project'] = { 'artist': { 'name': 'Extra Artist', 'sequence_number': 1, 'apple_id': '4A', 'spotify_uri': '4S', 'roles': [ 'ExtraArtist' ] } } expected_output = [ { 'localized_names': None, 'sequence_number': 1, 'apple_id': '4A', 'name': 'Extra Artist', 'roles': ['ExtraArtist'], 'spotify_uri': '4S' }, { 'localized_names': None, 'sequence_number': 1, 'apple_id': '1A', 'name': 'Giveon', 'roles': ['MainArtist'], 'spotify_uri': '1S' }, { 'localized_names': None, 'sequence_number': 2, 'apple_id': '2A', 'name': 'Artist', 'roles': ['SecondaryArtist'], 'spotify_uri': '2S' }, { 'localized_names': None, 'sequence_number': 1, 'apple_id': '1A', 'name': 'Giveon', 'roles': ['MainArtist'], 'spotify_uri': '1S' }, { 'localized_names': None, 'sequence_number': 2, 'apple_id': '2A', 'name': 'Artist', 'roles': ['SecondaryArtist'], 'spotify_uri': '2S' }, { 'localized_names': None, 'sequence_number': 1, 'apple_id': '3A', 'name': 'Giveon Evans', 'roles': ['Arranger', 'Composer', 'Lyricist'], 'spotify_uri': '3S' } ] parsed_ddex = S3Schema().load(s3_parsed_ddex) response = index.get_all_participants(parsed_ddex) assert len(response) == 6 serialized_response = [] for participant in response: serialized_response.append(ParticipantSchema().dump(participant)) assert serialized_response == expected_output def test_check_participant_ids(s3_parsed_ddex): """Test check_participant_ids.""" parsed_ddex = S3Schema().load(s3_parsed_ddex) participant = { 'apple_id': '1A', 'spotify_uri': '1S', 'roles': ['PrimaryMusician'], } # Assure no exceptions are raised when they match index.check_participant_ids( parsed_ddex.product.display_artists[0], participant) participant = { 'apple_id': '2A', 'spotify_uri': '1S', 'roles': ['12StringGuitar', 'AcousticGuitar'], } expected_message = 'Multiple Apple IDs for participant "Giveon": "1A" with roles "MainArtist" and "2A" with roles "12StringGuitar", "AcousticGuitar"' # noqa: E501 with pytest.raises(ProcessParticipantsException, match=expected_message): index.check_participant_ids( parsed_ddex.product.display_artists[0], participant) participant = { 'apple_id': '1A', 'spotify_uri': '2S', 'roles': ['12StringGuitar', 'AcousticGuitar'], } expected_message = 'Multiple Spotify URIs for participant "Giveon": "1S" with roles "MainArtist" and "2S" with roles "12StringGuitar", "AcousticGuitar"' # noqa: E501 with pytest.raises(ProcessParticipantsException, match=expected_message): index.check_participant_ids( parsed_ddex.product.display_artists[0], participant) @patch('index.graphql_gateway.execute') def test_get_artist( mock_graphql_execute, serialized_participant, s3_parsed_ddex): """Test get_artist.""" parsed_ddex = S3Schema().load(s3_parsed_ddex) artist = { 'artistId': '1', 'artistName': 'Artist' } mock_graphql_execute.return_value = { 'data': { 'filterArtists': [artist] } } payload = { 'artistName': parsed_ddex.product.display_artists[0].name, 'vendorId': None } result = index.get_artist(serialized_participant, None) mock_graphql_execute.assert_called_with(queries.get_artist, payload) assert result == artist @patch('index.graphql_gateway.execute') def test_create_artist(mock_graphql_execute, serialized_participant): """Test create_artist.""" artist = { 'artistId': '1', 'artistName': 'Artist' } mock_graphql_execute.return_value = { 'data': { 'saveArtists': [artist] } } payload = { 'create': [ { 'artistName': 'Giveon', 'vendorId': None, 'subaccountId': None } ] } result = index.create_artist(serialized_participant, None, None) mock_graphql_execute.assert_called_with( queries.save_artist, {'data': payload}) assert result == artist @patch('index.graphql_gateway.execute') def test_create_label_participant( mock_graphql_execute, serialized_participant): """Test create_label_participant.""" artist = { 'id': '1', 'uuid': 'AAAA-BBBB-CCCC-DDDD', } mock_graphql_execute.return_value = { 'data': { 'createLabelParticipant': artist } } payload = { 'name': 'Giveon', 'spotifyId': '1S', 'appleMusicId': '1A' } result = index.create_label_participant( serialized_participant, 34588, 60194) mock_graphql_execute.assert_called_with( queries.get_or_create_label_participant, { 'data': payload, 'vendorId': 34588, 'subaccountId': 60194, } ) assert result == artist @patch('index.logging_utils') @patch('index.load_ddex_json') @patch('index.get_participants') @patch('index.get_artist') @patch('index.create_artist') @patch('index.create_label_participant') @patch('index.save_s3_context') def test_label_participant_id_duplicate( mock_save_s3_context, mock_create_label_participant, mock_create_artist, mock_get_artist, mock_get_participants, mock_load_ddex_json, mock_logging_utils, serialized_participant, mock_event): """Test there is no participant_id duplicates.""" mock_load_ddex_json.return_value = mock_event mock_get_participants.return_value = [ { 'name': 'Giveon 1 1', 'sequence_number': 1, 'apple_id': '1A', 'spotify_uri': '1S', 'roles': [ 'MainArtist' ] }, { 'name': 'Giveon1 1', 'sequence_number': 2, 'apple_id': '1A', 'spotify_uri': '1S', 'roles': [ 'MainArtist' ] } ] mock_get_artist.return_value = None mock_create_artist.return_value = { 'artistId': '1', 'artistName': 'Giveon' } mock_create_label_participant.return_value = { 'id': '3', 'uuid': 'AAAAAA', } expected_output = [ { 'name': 'Giveon 1 1', 'artist_id': '1', 'label_participant_id': '3', 'label_participant_uuid': 'AAAAAA', } ] index.handler(mock_event, None) mock_get_participants.assert_called() assert mock_save_s3_context.call_args[0][1].label_participants \ == expected_output def test_rename_participants(s3_parsed_ddex): """Test rename participants.""" s3_parsed_ddex['product']['display_artists'][0]['name'] = 'Give on' s3_parsed_ddex['tracks'][0]['display_artists'][0]['name'] = 'Giveo n' participants_renaming = {'Give on': 'Giveo n'} parsed_ddex = S3Schema().load(s3_parsed_ddex) index.rename_participants(parsed_ddex, participants_renaming) assert parsed_ddex.tracks[0].display_artists[0].name == 'Giveo n' assert parsed_ddex.product.display_artists[0].name == 'Giveo n' def test_participants_product_deduplication(s3_parsed_ddex): """Test rename test_participants_product_deduplication.""" s3_parsed_ddex['product']['display_artists'][0]['name'] = 'Give on' s3_parsed_ddex['product']['display_artists'][1]['name'] = 'Giveo n' participants_renaming = {'Give on': 'Giveo n'} parsed_ddex = S3Schema().load(s3_parsed_ddex) index.rename_participants(parsed_ddex, participants_renaming) assert len(parsed_ddex.product.display_artists) == 2 index.remove_duplicates(parsed_ddex) assert parsed_ddex.product.display_artists[0].name == 'Giveo n' assert len(parsed_ddex.product.display_artists) == 1