"""Tests for Spotify model.""" from unittest.mock import call from owsresponse import response from participant.models.service import spotify def test_artist_search(mocker): """Test artist search.""" spotify_search_mock = mocker.patch( 'participant.models.service.spotify._spotipy_client.search' ) spotify_search_response_mock = spotify_search_mock.return_value redis_mock = mocker.patch('participant.models.service.spotify.redis') redis_mock.get_value.return_value = None query, limit, offset, localization = 'Danger Mouse', 10, 0, 'en' result = spotify.search_artist(query, limit, offset, localization) assert result == spotify_search_response_mock spotify_search_mock.assert_called_once_with( q=f'{query}*', limit=limit, offset=offset, search_type='artist', locale='en' ) redis_mock.get_value.assert_called_once_with( f'{query},{limit},{offset},{localization}' ) redis_mock.set_value.assert_called_once_with( f'{query},{limit},{offset},{localization}', spotify_search_response_mock ) def test_get_artist_by_id(mocker): """Test get artist by ID.""" spotify_artist_mock = mocker.patch( 'participant.models.service.spotify._spotipy_client.artist' ) spotify_search_response_mock = spotify_artist_mock.return_value redis_mock = mocker.patch('participant.models.service.spotify.redis') redis_mock.get_value.return_value = None artist_id = 'ARTIST_ID' result = spotify.get_artist_by_id(artist_id) assert result == spotify_search_response_mock spotify_artist_mock.assert_called_once_with(artist_id) redis_mock.get_value.assert_called_once_with(artist_id) redis_mock.set_value.assert_called_once_with( artist_id, spotify_search_response_mock ) def test_get_artist_by_id_error(mocker): """Test get artist by ID error.""" spotify_artist_mock = mocker.patch( 'participant.models.service.spotify._spotipy_client.artist', return_value=response.create_error_response(code=400, message='error'), ) redis_mock = mocker.patch('participant.models.service.spotify.redis') redis_mock.get_value.return_value = None artist_id = 'ARTIST_ID' result = spotify.get_artist_by_id(artist_id) assert not result assert result.status == 400 spotify_artist_mock.assert_called_once_with(artist_id) redis_mock.get_value.assert_called_once_with(artist_id) def test_get_artists_by_ids(mocker): """Test get artists by IDs.""" spotify_artists_mock = mocker.patch( 'participant.models.service.spotify._spotipy_client.get_artists_by_ids' ) redis_mock = mocker.patch('participant.models.service.spotify.redis') redis_mock.get_value.return_value = None redis_mock.set_value.return_value = None response_value = { 'id1': {'id': 'id1', 'name': 'Artist 1'}, 'id2': {'id': 'id2', 'name': 'Artist 2'}, } spotify_artists_mock.return_value = response_value artist_ids = ['id1', 'id2'] result = spotify.get_artists_by_ids(artist_ids) assert result.status == 200 assert result.message == response_value redis_mock.get_value.assert_has_calls([call('id1'), call('id2')], any_order=True) spotify_artists_mock.assert_called_once_with(artist_ids=['id1', 'id2']) redis_mock.set_value.assert_has_calls( [call('id1', response_value['id1']), call('id2', response_value['id2'])], any_order=True, ) def test_get_artists_by_ids_partial_cache(mocker): """Test get artists by IDs with partial cache.""" spotify_artists_mock = mocker.patch( 'participant.models.service.spotify._spotipy_client.get_artists_by_ids' ) redis_mock = mocker.patch('participant.models.service.spotify.redis') redis_mock.set_value.return_value = None def redis_get_value_side_effect(key): if key == 'id1': return {'id': 'id1', 'name': 'Artist 1'} return None redis_mock.get_value.side_effect = redis_get_value_side_effect response_value = { 'id2': {'id': 'id2', 'name': 'Artist 2'}, 'id3': {'id': 'id3', 'name': 'Artist 3'}, } spotify_artists_mock.return_value = response_value artist_ids = ['id1', 'id2', 'id3'] result = spotify.get_artists_by_ids(artist_ids) assert result.status == 200 assert result.message == { 'id1': {'id': 'id1', 'name': 'Artist 1'}, 'id2': {'id': 'id2', 'name': 'Artist 2'}, 'id3': {'id': 'id3', 'name': 'Artist 3'}, } redis_mock.get_value.assert_has_calls( [call('id1'), call('id2'), call('id3')], any_order=True ) spotify_artists_mock.assert_called_once_with(artist_ids=['id2', 'id3']) redis_mock.set_value.assert_has_calls( [call('id2', response_value['id2']), call('id3', response_value['id3'])], any_order=True, )