"""Tests for StudioPersonnelHandler.""" from mock import Mock, mock, patch import pytest from switchboard_consumer.constants.exceptions import ( ParticipantHandlerGraphQLError) from switchboard_consumer.logic.label_participant.label_participant_handler \ import LabelParticipantHandler @patch(('switchboard_consumer.logic.label_participant.' 'label_participant_handler.ROLE_MAPPING_OVERRIDE'), return_value={}) def test_get_supported_roles( mock_role_mapping_override, get_participation_role_categories_response_fixture): """Roles should be filtered and returned as key values pairs.""" orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) assert handler.get_supported_roles() == { 'AdditionalEngineer': 'Additional Engineer', 'AmazingEngineer': 'Amazing Engineer', 'AAndRAdministrator': 'A & R Administrator' } def test_get_supported_roles_on_error( get_participation_role_categories_response_fixture, graphql_error_response_fixture): """Should throw exception when error is encountered on from graphql""" orchard_client = Mock() orchard_client.get_participation_role_categories.side_effect = \ [ get_participation_role_categories_response_fixture, graphql_error_response_fixture ] handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) with pytest.raises(ParticipantHandlerGraphQLError): handler.get_supported_roles() def test_get_supported_roles_with_override( get_participation_role_categories_response_fixture): """Roles should be filtered and returned as key values pairs.""" overrides = { 'Cool SWB Role Non DDEX': 'Some Orchard Role Name', 'AmazingEngineer': 'Non Amazing Engineer' } with patch(('switchboard_consumer.logic.label_participant.' 'label_participant_handler.ROLE_MAPPING_OVERRIDE'), new=overrides): orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) assert handler.get_supported_roles() == { 'AdditionalEngineer': 'Additional Engineer', 'AAndRAdministrator': 'A & R Administrator', **overrides } @patch(('switchboard_consumer.logic.label_participant.' 'label_participant_handler.ROLE_MAPPING_OVERRIDE'), return_value={}) def test_get_roles_on_initialisation( mock_role_mapping_override, get_participation_role_categories_response_fixture): """Should fetch roles on initialisation""" orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) assert handler.supported_roles == { 'AdditionalEngineer': 'Additional Engineer', 'AmazingEngineer': 'Amazing Engineer', 'AAndRAdministrator': 'A & R Administrator' } def test_get_roles_on_initialisation_with_error( graphql_error_response_fixture): """Should throw exception when error is encountered on from graphql""" orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ graphql_error_response_fixture with pytest.raises(ParticipantHandlerGraphQLError): LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) def test_create_label_participant( get_participation_role_categories_response_fixture, create_label_participant_response_fixture): """Should create a label participant using valid contributor data.""" participation = { 'sequenceNumber': 1, 'participant': { 'name': "An Additional Engineer", 'spotifyUri': None, 'appleId': None, 'members': [] }, 'role': "AdditionalEngineer" } orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture orchard_client.create_label_participant.return_value = \ create_label_participant_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) assert handler.create_label_participant(participation) == \ { 'labelParticipantId': create_label_participant_response_fixture['id'], 'participationRoleName': 'Additional Engineer' } orchard_client.create_label_participant.assert_called_with( participation['participant'], handler.vendor_id, handler.subaccount_id, handler.correlation_id ) def test_create_label_participant_with_identifiers( get_participation_role_categories_response_fixture, create_label_participant_response_fixture): """Should create a label participant using valid participation data.""" participation = { 'sequenceNumber': 1, 'participant': { 'name': "An Additional Engineer", 'spotifyUri': 'spotify:artist:12345', 'appleId': 'abcde', 'members': [] }, 'role': "AdditionalEngineer" } orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture orchard_client.create_label_participant.return_value = \ create_label_participant_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) assert handler.create_label_participant(participation) == \ { 'labelParticipantId': create_label_participant_response_fixture['id'], 'participationRoleName': 'Additional Engineer' } orchard_client.create_label_participant.assert_called_with( participation['participant'], handler.vendor_id, handler.subaccount_id, handler.correlation_id ) def test_create_label_participant_on_error( get_participation_role_categories_response_fixture, graphql_error_response_fixture): """Should throw an exception if error encountered from graphql.""" contributor = { 'sequenceNumber': 1, 'participant': { 'name': "An Additional Engineer", 'spotifyUri': None, 'appleId': None, 'members': [] }, 'role': "AdditionalEngineer" } orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture orchard_client.create_label_participant.return_value = \ graphql_error_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) with pytest.raises(ParticipantHandlerGraphQLError): handler.create_label_participant(contributor) orchard_client.create_label_participant.assert_called_with( contributor['participant']['name'], handler.vendor_id, handler.subaccount_id, contributor['participant']['spotifyUri'], contributor['participant']['appleId'], handler.correlation_id ) def test_create_label_participant_on_invalid_role( get_participation_role_categories_response_fixture, graphql_error_response_fixture): """Should not create label participant if no valid role mapping""" contributor = { 'sequenceNumber': 1, 'participant': { 'name': "An Additional Engineer", 'spotifyUri': None, 'appleId': None, 'members': [] }, 'role': "SomeInvalidRole" } orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) handler.create_label_participant(contributor) orchard_client.create_label_participant.assert_not_called() def test_create_label_participants( get_participation_role_categories_response_fixture, contributors_fixture, create_label_participant_response_fixture): """Should be able to handle the creation of multiple participants.""" orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture orchard_client.create_label_participant.return_value = \ create_label_participant_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) expected_calls = [ mock.call( contributors_fixture[0] ), mock.call( contributors_fixture[1] ), ] handler.create_label_participant = Mock( wraps=handler.create_label_participant) participants = handler.create_label_participants(contributors_fixture) handler.create_label_participant.assert_has_calls(expected_calls) assert orchard_client.create_label_participant.call_count == 2 assert len(participants) == 2 def test_set_studio_personnel_for_track( get_participation_role_categories_response_fixture, track_fixture): """Should set label participants (studio personnel) for track.""" orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture orchard_client.set_label_sound_recording_participations.return_value = \ Mock() handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) sequenced_label_participants = [ { 'sequenceNumber': 1, 'labelParticipantId': '200858473', 'participationRoleName': 'Additional Engineer' }, { 'sequenceNumber': 2, 'labelParticipantId': '200858474', 'participationRoleName': 'Amazing Engineer' } ] expected_calls = [ mock.call( track_fixture['isrc'], handler.vendor_id, handler.subaccount_id, sequenced_label_participants, handler.correlation_id ) ] handler.set_label_participants_for_track( track_fixture['isrc'], track_fixture['studio_personnel'] ) orchard_client.set_label_sound_recording_participations.\ assert_has_calls(expected_calls) def test_set_studio_personnel_for_track_on_error( get_participation_role_categories_response_fixture, track_fixture, graphql_error_response_fixture): """Should throw exception when graphql error encountered.""" orchard_client = Mock() orchard_client.get_participation_role_categories.return_value = \ get_participation_role_categories_response_fixture orchard_client.set_label_sound_recording_participations.return_value = \ graphql_error_response_fixture handler = LabelParticipantHandler( orchard_client, 123, 456, Mock(), Mock() ) with pytest.raises(ParticipantHandlerGraphQLError): handler.set_label_participants_for_track( track_fixture['isrc'], track_fixture['studio_personnel'] )