"""Tests for track formatter module.""" from unittest.mock import Mock import pytest from switchboard_consumer.formatters import track from switchboard_consumer.formatters.track import \ _format_publishers, \ format_artists, \ format_rearrange_tracks, \ format_track_unmapped_publishers, \ format_tracks_unmapped_publishers_batch, \ format_writers def test_format_switchboard_tracks(switchboard_product_with_tracks): """Test for successful parse of switchboard_product_track_item.""" expected = [ { 'volumeNumber': 1, 'trackNumber': 1, 'trackName': 'TrackFormalTitle1', 'explicit': 'Y', 'pInfo': '2013 Columbia Records, ' 'a Division of Sony Music Entertainment', 'metaLanguageCode': 'ENG', 'isrc': 'USSM11307800', 'recordingCountryId': 128, 'originalRightsHolderCountryId': 31, 'ownershipRights': 'no_rights', 'trackType': 'music', 'artists': [ { 'artistInfoId': 123, 'artistName': 'Test Artist', 'artistType': 'performer' } ], 'writers': [ { 'artistInfoId': 234, 'writerName': 'Test Writer', 'writerType': 'writer' } ], 'performers': [ { 'name': 'DisplayPerformer 1', 'roleId': 27, 'type': 'primary' }, { 'name': 'Performer1', 'roleId': 27, 'type': 'non-featured' } ], 'samples': [], 'lyrics': 'Lyrics track 1', 'publishers': [ {'name': 'B-Day Publishing'}, {'name': 'Music World, LLC'}, {'name': 'Solange MVP Publishing'}, {'name': 'Songs Of Universal Inc.'}, {'name': 'Universal Tunes'}, ], 'version': 'Used for Track version' }, { 'volumeNumber': 1, 'trackNumber': 2, 'trackName': 'TrackFormalTitle2', 'explicit': 'Y', 'pInfo': '2013 Columbia Records, ' 'a Division of Sony Music Entertainment', 'metaLanguageCode': 'ENG', 'isrc': 'USSM11307807', 'recordingCountryId': 31, 'originalRightsHolderCountryId': 6, 'ownershipRights': 'original_owner', 'trackType': 'music', 'artists': [ { 'artistInfoId': 456, 'artistName': 'Test Artist 2', 'artistType': 'performer' } ], 'writers': [], 'performers': [{ 'name': 'Participant1', 'roleId': 27, 'type': 'non-featured' }], 'samples': [], 'publishers': [], 'version': None } ] mock_participant_results = [ ([], [{ 'artistInfoId': 123, 'artistName': 'Test Artist', 'artistType': 'performer' }], [{ 'artistInfoId': 234, 'artistName': 'Test Writer', 'artistType': 'arranger' }]), ([], [{ 'artistInfoId': 456, 'artistName': 'Test Artist 2', 'artistType': 'performer' }], []) ] mock_logger = Mock() mock_track_participant_handler = Mock() mock_track_participant_handler.process = \ Mock(side_effect=mock_participant_results) mock_track_performer_handler = Mock() mock_track_performer_handler.process = \ Mock(return_value=[]) mock_studio_personnel_handler = Mock() mock_studio_personnel_handler.create_label_participants = \ Mock(return_value=[]) processing_result, tracks = \ track.format_switchboard_product_tracks_to_orchard_tracks_batch( switchboard_product_with_tracks, mock_logger, mock_track_participant_handler) track_1_artists = tracks[0].get('artists') track_1_writers = tracks[0].get('writers') assert len(track_1_artists) == 1 expected_track_1_artist_1 = { 'artistInfoId': 123, 'artistName': 'Test Artist', 'artistType': 'performer' } assert expected_track_1_artist_1 in track_1_artists expected_track_1_writer = { 'artistInfoId': 234, 'writerName': 'Test Writer', 'writerType': 'writer' } assert expected_track_1_writer in track_1_writers assert tracks == expected def test_format_switchboard_tracks_for_rc( switchboard_product_with_mapped_tracks): """Test for successful parse of switchboard_product_track_item for rc.""" expected = [ { 'tuids': [1234], 'volumeNumber': 1, 'trackNumber': 1, 'trackName': 'TrackFormalTitle1', 'explicit': 'Y', 'pInfo': '2013 Columbia Records, ' 'a Division of Sony Music Entertainment', 'metaLanguageCode': 'ENG', 'isrc': 'USSM11307800', 'recordingCountryId': 128, 'originalRightsHolderCountryId': 31, 'ownershipRights': 'no_rights', 'trackType': 'music', 'artists': [ { 'artistInfoId': 123, 'artistName': 'Test Artist', 'artistType': 'performer' } ], 'writers': [ { 'artistInfoId': 234, 'writerName': 'Test Writer', 'writerType': 'writer' } ], 'performers': [ { 'name': 'DisplayPerformer 1', 'roleId': 27, 'type': 'primary' }, { 'name': 'Performer1', 'roleId': 27, 'type': 'non-featured' } ], 'samples': [], 'lyrics': 'Lyrics track 1', 'publishers': [ {'name': 'B-Day Publishing'}, {'name': 'Music World, LLC'}, {'name': 'Solange MVP Publishing'}, {'name': 'Songs Of Universal Inc.'}, {'name': 'Universal Tunes'}, ], 'version': 'Used for Track version' }, { 'tuids': [1234, 1235], 'volumeNumber': 1, 'trackNumber': 2, 'trackName': 'TrackFormalTitle2', 'explicit': 'Y', 'pInfo': '2013 Columbia Records, ' 'a Division of Sony Music Entertainment', 'metaLanguageCode': 'ENG', 'isrc': 'USSM11307807', 'recordingCountryId': 31, 'originalRightsHolderCountryId': 6, 'ownershipRights': 'original_owner', 'trackType': 'music', 'artists': [ { 'artistInfoId': 456, 'artistName': 'Test Artist 2', 'artistType': 'performer' } ], 'writers': [], 'performers': [{ 'name': 'Participant1', 'roleId': 27, 'type': 'non-featured' }], 'samples': [], 'lyrics': 'Lyrics track 2', 'publishers': [], 'version': None } ] mock_participant_results = [ ([], [{ 'artistInfoId': 123, 'artistName': 'Test Artist', 'artistType': 'performer' }], [{ 'artistInfoId': 234, 'artistName': 'Test Writer', 'artistType': 'arranger' }]), ([], [{ 'artistInfoId': 456, 'artistName': 'Test Artist 2', 'artistType': 'performer' }], []) ] mock_logger = Mock() mock_track_participant_handler = Mock() mock_track_participant_handler.process = \ Mock(side_effect=mock_participant_results) mock_track_performer_handler = Mock() mock_track_performer_handler.process = \ Mock(return_value=[]) processing_result, tracks = \ track.format_switchboard_product_tracks_for_release_corrections( switchboard_product_with_mapped_tracks, mock_logger, mock_track_participant_handler) track_1_artists = tracks[0].get('artists') track_1_writers = tracks[0].get('writers') assert len(track_1_artists) == 1 expected_track_1_artist_1 = { 'artistInfoId': 123, 'artistName': 'Test Artist', 'artistType': 'performer' } assert expected_track_1_artist_1 in track_1_artists expected_track_1_writer = { 'artistInfoId': 234, 'writerName': 'Test Writer', 'writerType': 'writer' } assert expected_track_1_writer in track_1_writers assert tracks == expected def test_samples_included_with_track_json(switchboard_track_item_2): """Test for include_samples flag.""" mock_logger = Mock() samples = [ { 'sampleType': 'ORIGINAL_RECORDING', 'displayTitle': 'Testing', 'durationInSeconds': 3600, 'notes': 'Notes', 'displayArtists': [], 'contributors': [ { 'sequenceNumber': 1, 'participant': { 'name': 'Chuck Middleton' }, 'role': 'Composer' } ] } ] switchboard_track_item_2['track']['samples'] = samples formatted_track = track.format_switchboard_to_orchard_track( switchboard_track_item_2['track'], [], [], [], mock_logger) assert formatted_track['samples'] def test_format_track_sample(track_samples_body): """Test for correct formatting of track samples.""" logger = Mock() result = track.format_track_samples(track_samples_body, logger) artists = result[0].pop('artists') assert result == [ { 'sampleType': 'ORIGINAL_RECORDING', 'trackName': 'Baby Blue', 'isrc': None, 'pLine': '', 'trackLength': 123, 'notes': 'sample notes', } ] expected_artists = [ {'artistName': 'Gene Vincent', 'artistType': 'ARTIST'}, {'artistName': 'Robert Jones', 'artistType': 'SONGWRITER'}, {'artistName': 'Gene Vincent', 'artistType': 'SONGWRITER'}, {'artistName': 'Robert Producer', 'artistType': 'PRODUCER'} ] for artist in artists: assert artist in expected_artists assert len(artists) == len(expected_artists) def test_format_track_sample_empty_type(track_samples_body): """Test for correct formatting of track samples.""" logger = Mock() track_samples_body[0]['sampleType'] = '' result = track.format_track_samples(track_samples_body, logger) artists = result[0].pop('artists') assert result == [ { 'sampleType': '', 'trackName': 'Baby Blue', 'isrc': None, 'pLine': '', 'trackLength': 123, 'notes': 'sample notes', } ] expected_artists = [ {'artistName': 'Gene Vincent', 'artistType': 'ARTIST'}, {'artistName': 'Robert Jones', 'artistType': 'SONGWRITER'}, {'artistName': 'Gene Vincent', 'artistType': 'SONGWRITER'}, {'artistName': 'Robert Producer', 'artistType': 'PRODUCER'} ] for artist in artists: assert artist in expected_artists assert len(artists) == len(expected_artists) def test_format_track_sample_with_valid_compound_artists( track_samples_body_with_valid_compound_artists): """Test for formatting of track samples with valid compound artist.""" logger = Mock() result = track.format_track_samples( track_samples_body_with_valid_compound_artists, logger) artists = result[0].pop('artists') assert result == [ { 'sampleType': 'ORIGINAL_RECORDING', 'trackName': 'He Cant Love U', 'isrc': None, 'pLine': '', 'trackLength': 0, 'notes': '' }] expected_artists = [ {'artistName': 'Brian D. Casey', 'artistType': 'ARTIST'}, {'artistName': 'Richard G. Wingo', 'artistType': 'ARTIST'} ] for artist in artists: assert artist in expected_artists assert len(artists) == len(expected_artists) def test_format_track_sample_with_not_valid_compound_artists( track_samples_body_with_not_valid_compound_artists): """Test for formatting of track samples with not valid compound artist.""" logger = Mock() result = track.format_track_samples( track_samples_body_with_not_valid_compound_artists, logger) artists = result[0].pop('artists') assert result == [ { 'sampleType': 'ORIGINAL_RECORDING', 'trackName': 'He Cant Love U', 'isrc': None, 'pLine': '', 'trackLength': 0, 'notes': '' }] expected_artists = [ {'artistName': 'Jagged Edge', 'artistType': 'ARTIST'}, {'artistName': 'Brian Casey', 'artistType': 'SONGWRITER'}, {'artistName': 'Bryan Cox', 'artistType': 'SONGWRITER'}, {'artistName': 'Brandon Casey', 'artistType': 'SONGWRITER'} ] for artist in artists: assert artist in expected_artists assert len(artists) == len(expected_artists) def test_format_publishers(): """Test for publishers filter for unique.""" body = { 'publishers': [ {'name': 'Duplicate Name 1'}, {'name': 'Unique Name 1'}, {'name': 'Unique Name 2'}, {'name': 'Duplicate Name 1'} ] } result = _format_publishers(body) assert result == [ {'name': 'Duplicate Name 1'}, {'name': 'Unique Name 1'}, {'name': 'Unique Name 2'} ] @pytest.mark.parametrize('test_input,expected', [ ([], {}), ({}, {}), ({'components': []}, {}), ({'components': None}, {}), ({'components': [{}]}, {}), ({'components': [{'sides': None}]}, {}), ({'components': [{'sides': []}]}, {}), ({'components': [{'sides': [{}]}]}, {}), ({'components': [{'sides': [{'productTracks': None}]}]}, {}), ({'components': [{'sides': [{'productTracks': []}]}]}, {}), ]) def test_format_tracks_unmapped_publishers_missing_data(test_input, expected): """Test format tracks unmapped publishers batch with missing data.""" assert format_tracks_unmapped_publishers_batch(test_input) == expected def test_format_tracks_unmapped_publishers_batch(): """Test for correct extraction of unmapped publishers.""" body = { 'components': [{ 'sides': [{ 'productTracks': [ { 'track': { 'trackName': 'Track 1', 'isrc': 'ISRC001', 'publishers': [ { 'ids': [ { 'localId': '1007797', 'system': 'SONY' } ], 'name': 'Universal - Songs of Polygram Int' } ] } }, { 'track': { 'trackName': 'Track 1', 'isrc': 'ISRC002', 'publishers': [{ 'ids': [ { 'localId': '416192', 'system': 'SONY' }, { 'localId': 'UNID:CQS+WelcQj/VYnHKy=', 'system': 'ORCHARD' } ], 'name': 'EMI Music Publishing, Ltd.' }] } } ] }] }] } result = format_tracks_unmapped_publishers_batch(body) assert result == { 'ISRC001': [ [ 'Universal - Songs of Polygram Int', {'localId': '1007797', 'system': 'SONY'} ] ], 'ISRC002': [] } def test_format_track_unmapped_publishers(): """Test of format_track_unmapped_publishers.""" track_body = { 'trackName': 'Track 1', 'isrc': 'ISRC001', 'publishers': [ { 'ids': [ { 'localId': '1007797', 'system': 'SONY' } ], 'name': 'Universal.' }, { 'ids': [ { 'localId': '1113180', 'system': 'SONY' } ], 'name': 'EMI Blackwood Music.' }, { 'ids': [ { 'localId': '416192', 'system': 'SONY' }, { 'localId': 'UNID:CQS+WelcOZTQj/VYnHKy=', 'system': 'ORCHARD' } ], 'name': 'EMI Music Publishing, Ltd.' } ] } result = format_track_unmapped_publishers(track_body) assert result == [ ['Universal.', {'localId': '1007797', 'system': 'SONY'}], ['EMI Blackwood Music.', {'localId': '1113180', 'system': 'SONY'}], ] def test_format_artists(): """Test of format_artists.""" artists = [ { 'artistInfoId': 1, 'artistName': 'Test Artist 1', 'artistType': 'Artist Type 1' }, { 'artistInfoId': 2, 'artistName': 'Test Artist 2', 'artistType': 'Artist Type 2' }, { 'artistInfoId': 3, 'artistName': 'Test Artist 3', 'artistType': 'Artist Type 3' }, { 'artistInfoId': 4, 'artistName': 'Test Artist 4', 'artistType': 'Artist Type 4' }, { 'artistInfoId': 4, 'artistName': 'Test Artist 4', 'artistType': 'Artist Type 4' } ] result = format_artists(artists) assert result == [ { 'artistInfoId': 1, 'artistName': 'Test Artist 1', 'artistType': 'Artist Type 1' }, { 'artistInfoId': 2, 'artistName': 'Test Artist 2', 'artistType': 'Artist Type 2' }, { 'artistInfoId': 3, 'artistName': 'Test Artist 3', 'artistType': 'Artist Type 3' }, { 'artistInfoId': 4, 'artistName': 'Test Artist 4', 'artistType': 'Artist Type 4' } ] def test_format_writers(): """Test of format_writers.""" writers = [ { 'artistInfoId': 1, 'artistName': 'Test Writer 1', 'artistType': 'Writer Type 1' }, { 'artistInfoId': 2, 'artistName': 'Test Writer 2', 'artistType': 'Writer Type 2' }, { 'artistInfoId': 3, 'artistName': 'Test Writer 3', 'artistType': 'Writer Type 3' }, { 'artistInfoId': 4, 'artistName': 'Test Writer 4', 'artistType': 'Writer Type 4' }, { 'artistInfoId': 4, 'artistName': 'Test Writer 4', 'artistType': 'Writer Type 4' } ] result = format_writers(writers) assert result == [ { 'artistInfoId': 1, 'writerName': 'Test Writer 1', 'writerType': 'writer' }, { 'artistInfoId': 2, 'writerName': 'Test Writer 2', 'writerType': 'writer' }, { 'artistInfoId': 3, 'writerName': 'Test Writer 3', 'writerType': 'writer' }, { 'artistInfoId': 4, 'writerName': 'Test Writer 4', 'writerType': 'writer' } ] @pytest.mark.parametrize('test_input,mapping,expected', [ ([], {}, []), ({}, {}, []), ({'components': []}, {}, []), ({'components': None}, {}, []), ({'components': [{}]}, {}, []), ({'components': [{'sides': None}]}, {}, []), ({'components': [{'sides': []}]}, {}, []), ({'components': [{'sides': [{}]}]}, {}, []), ({'components': [{'sides': [{'productTracks': None}]}]}, {}, []), ({'components': [{'sides': [{'productTracks': []}]}]}, {}, []), ({'components': [{ 'sides': [{ 'productTracks': [{ 'track': {'isrc': 12345}, 'componentNumber': 1, 'sequenceNumber': 2, }] }] }] }, { 12345: '56789' }, [{ 'trackNumber': 2, 'tuid': 56789, 'volumeNumber': 1 }]), ]) def test_format_rearrange_tracks(test_input, mapping, expected): """Test rearrange tracks formatter.""" assert format_rearrange_tracks( test_input, mapping ) == expected