"""Unit tests for the proces_participants Lambda function.""" from unittest.mock import patch from common.constants.artist_role import FEATURED_ARTIST, PRIMARY_ARTIST from common.schemas.state_machine_schema import ParticipantSchema, \ StateMachineSchema from src import index from src.constants import queries from src.index import handler @patch('src.index.get_participants') @patch('src.index.get_artist') @patch('src.index.create_artist') @patch('src.index.create_label_participant') def test_handler(mock_create_label_participant, mock_create_artist, mock_get_artist, mock_get_participants, context_event, serialized_participant): """Test handler function.""" 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', } ] result = handler(context_event, None) mock_get_participants.assert_called() mock_get_artist.assert_called_with(serialized_participant, 34563) mock_create_artist.assert_called_with(serialized_participant, 34563, 57803) mock_create_label_participant.assert_called_with( serialized_participant, 34563, 57803) assert result['label_participants'] == expected_output @patch('src.index.get_all_participants') def test_get_participants( mock_get_all_participants, context_event): """Test get_participants.""" all_participants_dict = [ { 'sequence_number': 1, 'apple_id': '4A', 'name': 'Extra Artist', 'roles': [FEATURED_ARTIST], 'spotify_uri': '4S' }, { 'sequence_number': 1, 'name': 'Giveon', 'roles': [PRIMARY_ARTIST], 'spotify_uri': '1S' }, { 'sequence_number': 2, 'apple_id': '2A', 'name': 'Artist', 'roles': [FEATURED_ARTIST], 'spotify_uri': '2S' } ] all_participants = [] for participant in all_participants_dict: all_participants.append(ParticipantSchema().load(participant)) mock_get_all_participants.return_value = all_participants expected_response = [{'apple_id': '4A', 'name': 'Extra Artist', 'roles': ['FEATURED_ARTIST'], 'spotify_uri': '4S'}, {'name': 'Giveon', 'roles': ['PRIMARY_ARTIST'], 'spotify_uri': '1S'}, {'apple_id': '2A', 'name': 'Artist', 'roles': ['FEATURED_ARTIST'], 'spotify_uri': '2S'}] sm_context = StateMachineSchema().load(context_event) response = index.get_participants(sm_context) assert response == expected_response @patch('src.index.graphql_gateway.execute') def test_get_artist( mock_graphql_execute, product_display_artist_participant, context_event): """Test get_artist.""" sm_context = StateMachineSchema().load(context_event) artist = { 'artistId': '1', 'artistName': 'Artist' } mock_graphql_execute.return_value = { 'data': { 'filterArtists': [artist] } } payload = { 'artistName': sm_context.product.display_artists[0].name, 'vendorId': None } result = index.get_artist(product_display_artist_participant, None) mock_graphql_execute.assert_called_with(queries.GET_ARTIST, payload) assert result == artist @patch('src.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('src.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 def test_participants_product_deduplication(context_event): """Test rename test_participants_product_deduplication.""" context_event['product']['display_artists'][0]['name'] = 'Give on' context_event['product']['display_artists'].append({ 'name': 'Giveo n'}) participants_renaming = {'Give on': 'Giveo n'} sm_context = StateMachineSchema().load(context_event) index.rename_participants(sm_context, participants_renaming) assert len(sm_context.product.display_artists) == 2 index.remove_duplicates(sm_context) assert sm_context.product.display_artists[0].name == 'Giveo n' assert len(sm_context.product.display_artists) == 1