def test_notification_on_api_error(self, mock_get_active_users, mock_process_one_user, mock_notify_error, mock_update): mock_process_one_user.side_effect = SpotifyAPIError('api borked') mock_get_active_users.return_value = [self.spotify_user] app = listenbrainz.webserver.create_app() app.config['TESTING'] = False with app.app_context(): spotify_read_listens.process_all_spotify_users() mock_notify_error.assert_called_once_with(312, 'api borked') mock_update.assert_called_once()
def test_spotipy_methods_are_called_with_correct_params_with_no_latest_listened_at( self, mock_current_user_playing_track, mock_current_user_recently_played): mock_current_user_playing_track.return_value = None mock_current_user_recently_played.return_value = None with listenbrainz.webserver.create_app().app_context(): spotify_read_listens.process_all_spotify_users() mock_current_user_playing_track.assert_called_once() mock_current_user_recently_played.assert_called_once_with(limit=50, after=0)
def test_notification_on_api_error(self, mock_make_api_request, mock_notify_error, mock_update): mock_make_api_request.side_effect = ExternalServiceAPIError( 'api borked') app = listenbrainz.webserver.create_app() app.config['TESTING'] = False with app.app_context(): spotify_read_listens.process_all_spotify_users() mock_notify_error.assert_called_once_with(self.user['id'], 'api borked') mock_update.assert_called_once()
def test_spotipy_methods_are_called_with_correct_params( self, mock_get_active_users): self.spotify_user.get_spotipy_client = MagicMock() mock_get_active_users.return_value = [self.spotify_user] with listenbrainz.webserver.create_app().app_context(): spotify_read_listens.process_all_spotify_users() self.spotify_user.get_spotipy_client( ).current_user_playing_track.assert_called_once() self.spotify_user.get_spotipy_client( ).current_user_recently_played.assert_called_once_with( limit=50, after=1400000000000)
def test_spotipy_methods_are_called_with_correct_params( self, mock_spotipy): mock_spotipy.return_value.current_user_playing_track.return_value = None with listenbrainz.webserver.create_app().app_context(): SpotifyService().update_latest_listen_ts( self.user['id'], int(datetime(2014, 5, 13, 16, 53, 20).timestamp())) spotify_read_listens.process_all_spotify_users() mock_spotipy.return_value.current_user_playing_track.assert_called_once( ) mock_spotipy.return_value.current_user_recently_played.assert_called_once_with( limit=50, after=1400000000000)
def test_spotify_recently_played_submitted(self, mock_recently_played, mock_currently_playing): with open(self.path_to_data_file('spotify_recently_played_submitted.json')) as f: mock_recently_played.return_value = json.load(f) mock_currently_playing.return_value = None result = spotify_read_listens.process_all_spotify_users() self.assertEqual(result, (1, 0)) with open(self.path_to_data_file('spotify_recently_played_expected.json')) as f: expected_data = json.load(f) url = url_for('api_v1.get_listens', user_name=self.user['musicbrainz_id']) r = self.wait_for_query_to_have_items(url, 1) self.assert200(r) payload = r.json['payload'] self.assertEqual(payload['count'], 1) self.assertEqual(payload['latest_listen_ts'], 1635138793) actual_listen = payload['listens'][0] expected_listen = expected_data['payload']['listens'][0] # some fields vary from run to run, set those to our expected values before testing equality actual_listen['inserted_at'] = expected_listen['inserted_at'] actual_listen['recording_msid'] = expected_listen['recording_msid'] actual_listen['track_metadata']['additional_info']['recording_msid'] = \ expected_listen['track_metadata']['additional_info']['recording_msid'] actual_listen['track_metadata']['additional_info']['release_msid'] = \ expected_listen['track_metadata']['additional_info']['release_msid'] actual_listen['track_metadata']['additional_info']['artist_msid'] = \ expected_listen['track_metadata']['additional_info']['artist_msid'] self.assertEqual(expected_listen, actual_listen)
def test_notification_on_api_error(self, mock_get_active_users, mock_process_one_user, mock_notify_error, mock_update): mock_process_one_user.side_effect = SpotifyAPIError('api borked') mock_get_active_users.return_value = [ Spotify( user_id=1, musicbrainz_id='jude', musicbrainz_row_id=312, user_token='token', token_expires=int(time.time()), refresh_token='refresh', last_updated=None, record_listens=True, error_message=None, latest_listened_at=None, permission='user-read-recently-played', ), ] app = listenbrainz.webserver.create_app() app.config['TESTING'] = False with app.app_context(): spotify_read_listens.process_all_spotify_users() mock_notify_error.assert_called_once_with(312, 'api borked') mock_update.assert_called_once()