Beispiel #1
0
    def test_get_active_users_to_process(self):
        db_user.create(2, 'newspotifyuser')
        db_spotify.create_spotify(
            user_id=2,
            user_token='token',
            refresh_token='refresh_token',
            token_expires_ts=int(time.time()),
        )
        users = db_spotify.get_active_users_to_process()
        self.assertEqual(len(users), 2)
        self.assertEqual(users[0]['user_id'], 1)
        self.assertEqual(users[1]['user_id'], 2)

        # check order, the users should be sorted by latest_listened_at timestamp
        db_user.create(3, 'newnewspotifyuser')
        db_spotify.create_spotify(
            user_id=3,
            user_token='tokentoken',
            refresh_token='newrefresh_token',
            token_expires_ts=int(time.time()),
        )
        t = int(time.time())
        db_spotify.update_latest_listened_at(2, t + 20)
        db_spotify.update_latest_listened_at(1, t + 10)
        users = db_spotify.get_active_users_to_process()
        self.assertEqual(len(users), 3)
        self.assertEqual(users[0]['user_id'], 2)
        self.assertEqual(users[1]['user_id'], 1)
        self.assertEqual(users[2]['user_id'], 3)

        db_spotify.add_update_error(2, 'something broke')
        db_spotify.add_update_error(3, 'oops.')
        users = db_spotify.get_active_users_to_process()
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0]['user_id'], 1)
    def test_get_active_users_to_process(self):
        db_user.create(2, 'newspotifyuser')
        db_spotify.create_spotify(
            user_id=2,
            user_token='token',
            refresh_token='refresh_token',
            token_expires_ts=int(time.time()),
        )
        users = db_spotify.get_active_users_to_process()
        self.assertEqual(len(users), 2)
        self.assertEqual(users[0]['user_id'], 1)
        self.assertEqual(users[1]['user_id'], 2)

        # check order, the users should be sorted by latest_listened_at timestamp
        db_user.create(3, 'newnewspotifyuser')
        db_spotify.create_spotify(
            user_id=3,
            user_token='tokentoken',
            refresh_token='newrefresh_token',
            token_expires_ts=int(time.time()),
        )
        t = int(time.time())
        db_spotify.update_latest_listened_at(2, t + 20)
        db_spotify.update_latest_listened_at(1, t + 10)
        users = db_spotify.get_active_users_to_process()
        self.assertEqual(len(users), 3)
        self.assertEqual(users[0]['user_id'], 2)
        self.assertEqual(users[1]['user_id'], 1)
        self.assertEqual(users[2]['user_id'], 3)

        db_spotify.add_update_error(2, 'something broke')
        db_spotify.add_update_error(3, 'oops.')
        users = db_spotify.get_active_users_to_process()
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0]['user_id'], 1)
Beispiel #3
0
 def test_add_update_error(self):
     old_spotify_user = db_spotify.get_user(self.user['id'])
     self.assertTrue(old_spotify_user['active'])
     db_spotify.add_update_error(self.user['id'], 'test error message')
     spotify_user = db_spotify.get_user(self.user['id'])
     self.assertFalse(spotify_user['active'])
     self.assertEqual(spotify_user['error_message'], 'test error message')
     self.assertIsNotNone(spotify_user['last_updated'])
 def test_add_update_error(self):
     old_spotify_user = db_spotify.get_user(self.user['id'])
     self.assertTrue(old_spotify_user['active'])
     db_spotify.add_update_error(self.user['id'], 'test error message')
     spotify_user = db_spotify.get_user(self.user['id'])
     self.assertFalse(spotify_user['active'])
     self.assertEqual(spotify_user['error_message'], 'test error message')
     self.assertIsNotNone(spotify_user['last_updated'])
Beispiel #5
0
def update_last_updated(user_id, success=True, error_message=None):
    """ Update the last_update field for user with specified user ID.
    Also, set the user as active or inactive depending on whether their listens
    were imported without error.

    If there was an error, add the error to the db.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        success (bool): flag representing whether the last import was successful or not.
        error_message (str): the user-friendly error message to be displayed.
    """
    if error_message:
        db_spotify.add_update_error(user_id, error_message)
    else:
        db_spotify.update_last_updated(user_id, success)
def update_last_updated(user_id, success=True, error_message=None):
    """ Update the last_update field for user with specified user ID.
    Also, set the user as active or inactive depending on whether their listens
    were imported without error.

    If there was an error, add the error to the db.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        success (bool): flag representing whether the last import was successful or not.
        error_message (str): the user-friendly error message to be displayed.
    """
    if error_message:
        db_spotify.add_update_error(user_id, error_message)
    else:
        db_spotify.update_last_updated(user_id, success)