"""Tests for Spotify search logic.""" import pytest from owsresponse import response from participant.logic.service import spotify @pytest.mark.parametrize( ('return_value', 'expected_value'), [ ( { 'artists': { 'items': [ { 'id': 'spotify-id', 'name': 'Danger Mouse', 'followers': {'total': 100}, 'genres': ['Electronica', 'Alternative rock'], 'external_urls': {'spotify': 'https://spotify.com/artist/'}, 'images': [ {'url': 'https://i.scdn.co/image/1'}, {'url': 'https://i.scdn.co/image/-1'}, ], 'other': {'random': 'data'}, } ] } }, [ { 'identifier': 'spotify-id', 'name': 'Danger Mouse', 'followers': 100, 'genres': ['Electronica', 'Alternative rock'], 'url': 'https://spotify.com/artist/', 'image': 'https://i.scdn.co/image/-1', } ], ), ( { 'artists': { 'items': [ { 'id': 'spotify-id', 'name': 'Danger Mouse', 'followers': {'total': 100}, 'genres': ['Electronica', 'Alternative rock'], 'external_urls': {'spotify': 'https://spotify.com/artist/'}, 'images': [], 'other': {'random': 'data'}, } ] } }, [ { 'identifier': 'spotify-id', 'name': 'Danger Mouse', 'followers': 100, 'genres': ['Electronica', 'Alternative rock'], 'url': 'https://spotify.com/artist/', 'image': None, } ], ), ( {}, [], ), ], ) def test_artist_search(mocker, return_value, expected_value): """Test artist search.""" search_artist_mock = mocker.patch( 'participant.models.service.spotify.search_artist', return_value=return_value ) query, limit, offset, localization = 'Danger Mouse', 10, 0, 'en' response = spotify.artist_search(query, limit, offset, localization) search_artist_mock.assert_called_once_with(query, limit, offset, localization) assert response.message == expected_value @pytest.mark.parametrize( ('return_value', 'expected_value'), [ ( { 'id': 'spotify-id', 'name': 'Danger Mouse', 'followers': {'total': 100}, 'genres': ['Electronica', 'Alternative rock'], 'external_urls': {'spotify': 'https://spotify.com/artist/'}, 'images': [ {'url': 'https://i.scdn.co/image/1'}, {'url': 'https://i.scdn.co/image/-1'}, ], 'other': {'random': 'data'}, }, { 'identifier': 'spotify-id', 'name': 'Danger Mouse', 'followers': 100, 'genres': ['Electronica', 'Alternative rock'], 'url': 'https://spotify.com/artist/', 'image': 'https://i.scdn.co/image/-1', }, ), ], ) def test_get_artist_by_id(mocker, return_value, expected_value): """Test get artist by ID.""" get_artist_by_id_mock = mocker.patch( 'participant.models.service.spotify.get_artist_by_id', return_value=return_value ) artist_id = 'ARTIST_ID' response = spotify.get_artist_by_id(artist_id) get_artist_by_id_mock.assert_called_once_with(artist_id) assert response.message == expected_value @pytest.mark.parametrize( ('return_value', 'expected_value'), [ ( response.Response( { '0WCo84qtCKfbyIf1lqQWB4': { 'id': '0WCo84qtCKfbyIf1lqQWB4', 'name': 'Geese', 'followers': {'total': 157141}, 'genres': ['indie rock'], 'external_urls': { 'spotify': 'https://open.spotify.com/artist/0WCo84qtCKfbyIf1lqQWB4' # noqa: B950 }, 'images': [ { 'url': 'https://i.scdn.co/image/ab6761610000f178bc9546a945c7563a9eb21f3d' # noqa: B950 } ], }, '7DjwIxbe8kpw4pqnzAMoin': { 'id': '7DjwIxbe8kpw4pqnzAMoin', 'name': 'The Beths', 'followers': {'total': 176241}, 'genres': ['indie rock', 'power pop'], 'external_urls': { 'spotify': 'https://open.spotify.com/artist/7DjwIxbe8kpw4pqnzAMoin' # noqa: B950 }, 'images': [ { 'url': 'https://i.scdn.co/image/ab6761610000f178eb26b0d0de46b77e23675281' # noqa: B950 } ], }, 'invalid_id': None, } ), [ { 'identifier': '0WCo84qtCKfbyIf1lqQWB4', 'name': 'Geese', 'followers': 157141, 'genres': ['indie rock'], 'url': 'https://open.spotify.com/artist/0WCo84qtCKfbyIf1lqQWB4', 'image': 'https://i.scdn.co/image/ab6761610000f178bc9546a945c7563a9eb21f3d', # noqa: B950 }, { 'identifier': '7DjwIxbe8kpw4pqnzAMoin', 'name': 'The Beths', 'followers': 176241, 'genres': ['indie rock', 'power pop'], 'url': 'https://open.spotify.com/artist/7DjwIxbe8kpw4pqnzAMoin', 'image': 'https://i.scdn.co/image/ab6761610000f178eb26b0d0de46b77e23675281', # noqa: B950 }, ], ), ], ) def test_get_artists_by_ids(mocker, return_value, expected_value): """Test get artists by IDs.""" get_artists_by_ids_mock = mocker.patch( 'participant.models.service.spotify.get_artists_by_ids', return_value=return_value, ) artist_ids = ['ARTIST_ID_1', 'ARTIST_ID_2'] response = spotify.get_artists_by_ids(artist_ids) get_artists_by_ids_mock.assert_called_once_with(artist_ids=artist_ids) assert response.message == expected_value def test_get_artist_by_id_error(mocker): """Test get artist by ID error.""" get_artist_by_id_mock = mocker.patch( 'participant.models.service.spotify.get_artist_by_id', return_value=response.create_error_response(code=400, message='error'), ) _format_artist_response_mock = mocker.patch( 'participant.logic.service.spotify._format_artist_response' ) artist_id = 'ARTIST_ID' artist_response = spotify.get_artist_by_id(artist_id) get_artist_by_id_mock.assert_called_once_with(artist_id) assert not _format_artist_response_mock.called assert not artist_response def test_artist_search_error(mocker): """Test artist search error response is gracefully handled by the formatter.""" mocker.patch( 'participant.models.service.spotify.search_artist', return_value=response.create_error_response(code=400, message='error'), ) query, limit, offset, localization = 'Danger Mouse', 10, 0, 'en' artist_response = spotify.artist_search(query, limit, offset, localization) assert artist_response.message == []