"""Product utils tests.""" # from bulk_metadata_ingester_common.models.bulk_release import BulkRelease from unittest.mock import MagicMock, patch from constants import queries from constants.exceptions import ( DuplicateParticipantException, ParticipantRoleGenreException) # from constants.product import ( # BULK_NOT_FOR_DISTRIBUTION, # BULK_PRODUCT_HIGHLIGHTS) import pytest from utils.product_utils import ( add_release_artist, check_for_product, get_all_roles, get_context_participant, get_participations, # create_product, # get_participations, map_release_type) @patch('utils.product_utils.graphql_gateway.execute') def test_check_for_product( mock_execute, test_model, test_check_for_product_exists_result): """Test the check_for_product() function.""" # Set up mock data for graphql_gateway.execute mock_data = test_check_for_product_exists_result mock_execute.return_value = { 'data': { 'productByUpc': mock_data } } # Call the function with a mocked logger and check the result result = check_for_product(test_model, logger=MagicMock()) assert result == mock_data # Check that graphql_gateway.execute was called with the correct payload mock_execute.assert_called_once_with( queries.GET_PRODUCT_BY_UPC, {'upc': str(test_model.upc)} ) def test_get_all_roles(test_model, test_all_roles_result): """Test get_all_roles() function.""" # Call function with test data result = get_all_roles(test_model, MagicMock()) # Check that expected keys are present in result assert result == test_all_roles_result @pytest.mark.parametrize( 'test_field, test_genre_name', [ pytest.param( 'genre', 'Horible Drek', id='Genre Fails'), pytest.param( 'subgenre', 'Subb-Horible Drek', id='SubGenre Fails') ]) def test_get_all_roles_fails( test_field, test_genre_name, test_model): """Test that get_all_roles() function fails.""" # Default genre to one that requires sub-genres test_model._product = test_model._product._replace(genre='World Music') # Change genre, and then subgenre test_model._product = \ test_model._product._replace(**{test_field: test_genre_name}) with pytest.raises(ParticipantRoleGenreException): # Call function with test data get_all_roles(test_model, MagicMock()) def test_map_release_type_with_valid_input(test_model): """Test the map_release_type() function with valid input.""" mapped_release_type = map_release_type(test_model) assert mapped_release_type == 'Full Length' def test_map_release_type_with_unknown_format_raises_error(test_model): """Test the map_release_type function with unknown format.""" release = test_model._product._replace(release_type='Broken') with pytest.raises(ValueError): map_release_type(release) def test_add_release_artist( test_label_participant_uuid, test_performer, test_release_performers_result): """Test add_release_artist() function.""" release_performers = [] add_release_artist( test_label_participant_uuid, test_performer, release_performers ) assert release_performers == test_release_performers_result[:1] @patch('utils.product_utils.get_all_roles') @patch('utils.product_utils.get_context_participant') def test_get_participations( mock_get_context_participant, mock_get_all_roles, test_get_all_roles_result, test_release_participants, test_get_participations_result, test_model): """Test get_participations() function.""" # Mock the list of artists mock_get_all_roles.return_value = test_get_all_roles_result # Mock multiple calls to get_context_participant() within mock_get_context_participant.side_effect = [ {'label_participant_uuid': '1234'}, {'label_participant_uuid': '5678'} ] # Attach mocked list of participants test_model.participants = test_release_participants # Run the logic result = get_participations(test_model, logger=MagicMock()) # Check the logic assert result == test_get_participations_result def test_get_context_participant_with_matching_name( test_release_participants): """Test get_context_participant returns the correct participant.""" # Valid name name_to_find = 'Pedro Libe' expected_result = test_release_participants[0] result = get_context_participant(test_release_participants, name_to_find) assert result == expected_result def test_get_context_participant_with_no_matching_name( test_release_participants): """Test get_context_participant returns None when name doesn't match.""" # Invalid name name_to_find = 'Bob' result = get_context_participant(test_release_participants, name_to_find) assert result is None def test_get_context_participant_with_duplicate_names( test_release_participants): """Test get_context_participant raises an exception on duplicate name.""" # Valid name name_to_find = 'Pedro Libe' # Add an ad-hoc duplicate participant to the list test_release_participants.append({ 'name': 'Pedro Libe', 'artist_id': '3004909', 'label_participant_id': '5679', 'label_participant_uuid': '124' }) # Pass duped set with pytest.raises(DuplicateParticipantException): get_context_participant(test_release_participants, name_to_find)