mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
Further clean up of tests
This commit is contained in:
parent
e9edf2a255
commit
6639a98e24
@ -1,3 +1,4 @@
|
|||||||
mock==2.0.0
|
mock==2.0.0
|
||||||
requests==2.3.0
|
requests==2.3.0
|
||||||
|
simplejson==3.13.2
|
||||||
six==1.10.0
|
six==1.10.0
|
||||||
@ -1,24 +1,47 @@
|
|||||||
# -*- coding: latin-1 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import spotipy
|
"""
|
||||||
from spotipy import util
|
These tests require user authentication - provide client credentials using the
|
||||||
import unittest
|
following environment variables
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
'SPOTIPY_CLIENT_USERNAME'
|
||||||
|
'SPOTIPY_CLIENT_ID'
|
||||||
|
'SPOTIPY_CLIENT_SECRET'
|
||||||
|
'SPOTIPY_REDIRECT_URI'
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import os
|
||||||
import pprint
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
'''
|
sys.path.insert(0, os.path.abspath(os.pardir))
|
||||||
Since these tests require authentication they are maintained
|
|
||||||
separately from the other tests.
|
from spotipy import (
|
||||||
|
prompt_for_user_token,
|
||||||
|
Spotify,
|
||||||
|
SpotifyException,
|
||||||
|
)
|
||||||
|
|
||||||
These tests try to be benign and leave your collection and
|
|
||||||
playlists in a relatively stable state.
|
|
||||||
'''
|
|
||||||
|
|
||||||
class AuthTestSpotipy(unittest.TestCase):
|
class AuthTestSpotipy(unittest.TestCase):
|
||||||
'''
|
"""
|
||||||
These tests require user authentication
|
These tests require user authentication - provide client credentials using the
|
||||||
'''
|
following environment variables
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
'SPOTIPY_CLIENT_USERNAME'
|
||||||
|
'SPOTIPY_CLIENT_ID'
|
||||||
|
'SPOTIPY_CLIENT_SECRET'
|
||||||
|
'SPOTIPY_REDIRECT_URI'
|
||||||
|
"""
|
||||||
|
|
||||||
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
|
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
|
||||||
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
|
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
|
||||||
@ -35,38 +58,60 @@ class AuthTestSpotipy(unittest.TestCase):
|
|||||||
|
|
||||||
bad_id = 'BAD_ID'
|
bad_id = 'BAD_ID'
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(self):
|
||||||
|
client_cred_env_vars = ['SPOTIPY_CLIENT_USERNAME', 'SPOTIPY_CLIENT_ID', 'SPOTIPY_CLIENT_SECRET', 'SPOTIPY_REDIRECT_URI']
|
||||||
|
missing = filter(lambda var: not os.getenv(var), client_cred_env_vars)
|
||||||
|
|
||||||
|
if missing:
|
||||||
|
raise Exception('Please set the client credetials for the test application using the following environment variables: {}'.format(client_cred_env_vars))
|
||||||
|
|
||||||
|
self.username = os.getenv('SPOTIPY_CLIENT_USERNAME')
|
||||||
|
|
||||||
|
self.scope = (
|
||||||
|
'playlist-modify-public '
|
||||||
|
'user-library-read '
|
||||||
|
'user-follow-read '
|
||||||
|
'user-library-modify '
|
||||||
|
'user-read-private '
|
||||||
|
'user-top-read'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.token = prompt_for_user_token(self.username, scope=self.scope)
|
||||||
|
|
||||||
|
self.spotify = Spotify(auth=self.token)
|
||||||
|
|
||||||
def test_track_bad_id(self):
|
def test_track_bad_id(self):
|
||||||
try:
|
try:
|
||||||
track = spotify.track(self.bad_id)
|
track = self.spotify.track(self.bad_id)
|
||||||
self.assertTrue(False)
|
self.assertTrue(False)
|
||||||
except spotipy.SpotifyException:
|
except SpotifyException:
|
||||||
self.assertTrue(True)
|
self.assertTrue(True)
|
||||||
|
|
||||||
|
|
||||||
def test_basic_user_profile(self):
|
def test_basic_user_profile(self):
|
||||||
user = spotify.user(username)
|
user = self.spotify.user(self.username)
|
||||||
self.assertTrue(user['id'] == username)
|
self.assertTrue(user['id'] == self.username.lower())
|
||||||
|
|
||||||
def test_current_user(self):
|
def test_current_user(self):
|
||||||
user = spotify.current_user()
|
user = self.spotify.current_user()
|
||||||
self.assertTrue(user['id'] == username)
|
self.assertTrue(user['id'] == self.username.lower())
|
||||||
|
|
||||||
def test_me(self):
|
def test_me(self):
|
||||||
user = spotify.me()
|
user = self.spotify.me()
|
||||||
self.assertTrue(user['id'] == username)
|
self.assertTrue(user['id'] == self.username.lower())
|
||||||
|
|
||||||
def test_user_playlists(self):
|
def test_user_playlists(self):
|
||||||
playlists = spotify.user_playlists(username, limit=5)
|
playlists = self.spotify.user_playlists(self.username, limit=5)
|
||||||
self.assertTrue('items' in playlists)
|
self.assertTrue('items' in playlists)
|
||||||
self.assertTrue(len(playlists['items']) == 5)
|
self.assertTrue(len(playlists['items']) == 5)
|
||||||
|
|
||||||
def test_user_playlist_tracks(self):
|
def test_user_playlist_tracks(self):
|
||||||
playlists = spotify.user_playlists(username, limit=5)
|
playlists = self.spotify.user_playlists(self.username, limit=5)
|
||||||
self.assertTrue('items' in playlists)
|
self.assertTrue('items' in playlists)
|
||||||
for playlist in playlists['items']:
|
for playlist in playlists['items']:
|
||||||
user = playlist['owner']['id']
|
user = playlist['owner']['id']
|
||||||
pid = playlist['id']
|
pid = playlist['id']
|
||||||
results = spotify.user_playlist_tracks(user, pid)
|
results = self.spotify.user_playlist_tracks(user, pid)
|
||||||
self.assertTrue(len(results['items']) >= 0)
|
self.assertTrue(len(results['items']) >= 0)
|
||||||
|
|
||||||
def user_playlist_tracks(self, user, playlist_id = None, fields=None,
|
def user_playlist_tracks(self, user, playlist_id = None, fields=None,
|
||||||
@ -79,144 +124,128 @@ class AuthTestSpotipy(unittest.TestCase):
|
|||||||
self.assertTrue(len(playlists['items']) == 5)
|
self.assertTrue(len(playlists['items']) == 5)
|
||||||
|
|
||||||
def test_current_user_saved_tracks(self):
|
def test_current_user_saved_tracks(self):
|
||||||
tracks = spotify.current_user_saved_tracks()
|
tracks = self.spotify.current_user_saved_tracks()
|
||||||
self.assertTrue(len(tracks['items']) > 0)
|
self.assertTrue(len(tracks['items']) > 0)
|
||||||
|
|
||||||
def test_current_user_saved_albums(self):
|
def test_current_user_saved_albums(self):
|
||||||
albums = spotify.current_user_saved_albums()
|
albums = self.spotify.current_user_saved_albums()
|
||||||
self.assertTrue(len(albums['items']) > 0)
|
self.assertTrue(len(albums['items']) > 0)
|
||||||
|
|
||||||
def test_current_user_playlists(self):
|
def test_current_user_playlists(self):
|
||||||
playlists = spotify.current_user_playlists(limit=10)
|
playlists = self.spotify.current_user_playlists(limit=10)
|
||||||
self.assertTrue('items' in playlists)
|
self.assertTrue('items' in playlists)
|
||||||
self.assertTrue(len(playlists['items']) == 10)
|
self.assertTrue(len(playlists['items']) == 10)
|
||||||
|
|
||||||
def test_user_playlist_follow(self):
|
def test_user_playlist_follow(self):
|
||||||
spotify.user_playlist_follow_playlist('plamere', '4erXB04MxwRAVqcUEpu30O')
|
self.spotify.user_playlist_follow_playlist('plamere', '4erXB04MxwRAVqcUEpu30O')
|
||||||
follows = spotify.user_playlist_is_following('plamere', '4erXB04MxwRAVqcUEpu30O', ['plamere'])
|
follows = self.spotify.user_playlist_is_following('plamere', '4erXB04MxwRAVqcUEpu30O', [self.spotify.current_user()['id']])
|
||||||
|
|
||||||
self.assertTrue(len(follows) == 1, 'proper follows length')
|
self.assertTrue(len(follows) == 1, 'proper follows length')
|
||||||
self.assertTrue(follows[0], 'is following')
|
self.assertTrue(follows[0], 'is following')
|
||||||
spotify.user_playlist_unfollow('plamere', '4erXB04MxwRAVqcUEpu30O')
|
self.spotify.user_playlist_unfollow('plamere', '4erXB04MxwRAVqcUEpu30O')
|
||||||
|
|
||||||
follows = spotify.user_playlist_is_following('plamere', '4erXB04MxwRAVqcUEpu30O', ['plamere'])
|
follows = self.spotify.user_playlist_is_following('plamere', '4erXB04MxwRAVqcUEpu30O', [self.spotify.current_user()['id']])
|
||||||
self.assertTrue(len(follows) == 1, 'proper follows length')
|
self.assertTrue(len(follows) == 1, 'proper follows length')
|
||||||
self.assertFalse(follows[0], 'is no longer following')
|
self.assertFalse(follows[0], 'is no longer following')
|
||||||
|
|
||||||
|
|
||||||
def test_current_user_save_and_unsave_tracks(self):
|
def test_current_user_save_and_unsave_tracks(self):
|
||||||
tracks = spotify.current_user_saved_tracks()
|
tracks = self.spotify.current_user_saved_tracks()
|
||||||
total = tracks['total']
|
total = tracks['total']
|
||||||
|
|
||||||
spotify.current_user_saved_tracks_add(self.four_tracks)
|
self.spotify.current_user_saved_tracks_add(self.four_tracks)
|
||||||
|
|
||||||
tracks = spotify.current_user_saved_tracks()
|
tracks = self.spotify.current_user_saved_tracks()
|
||||||
new_total = tracks['total']
|
new_total = tracks['total']
|
||||||
self.assertTrue(new_total - total == len(self.four_tracks))
|
self.assertTrue(new_total - total == len(self.four_tracks))
|
||||||
|
|
||||||
tracks = spotify.current_user_saved_tracks_delete(self.four_tracks)
|
tracks = self.spotify.current_user_saved_tracks_delete(self.four_tracks)
|
||||||
tracks = spotify.current_user_saved_tracks()
|
tracks = self.spotify.current_user_saved_tracks()
|
||||||
new_total = tracks['total']
|
new_total = tracks['total']
|
||||||
self.assertTrue(new_total == total)
|
self.assertTrue(new_total == total)
|
||||||
|
|
||||||
|
|
||||||
def test_categories(self):
|
def test_categories(self):
|
||||||
response = spotify.categories()
|
response = self.spotify.categories()
|
||||||
self.assertTrue(len(response['categories']) > 0)
|
self.assertTrue(len(response['categories']) > 0)
|
||||||
|
|
||||||
def test_category_playlists(self):
|
def test_category_playlists(self):
|
||||||
response = spotify.categories()
|
response = self.spotify.categories()
|
||||||
for cat in response['categories']['items']:
|
for cat in response['categories']['items']:
|
||||||
cat_id = cat['id']
|
cat_id = cat['id']
|
||||||
response = spotify.category_playlists(category_id=cat_id)
|
response = self.spotify.category_playlists(category_id=cat_id)
|
||||||
self.assertTrue(len(response['playlists']["items"]) > 0)
|
self.assertTrue(len(response['playlists']["items"]) > 0)
|
||||||
|
|
||||||
def test_new_releases(self):
|
def test_new_releases(self):
|
||||||
response = spotify.new_releases()
|
response = self.spotify.new_releases()
|
||||||
self.assertTrue(len(response['albums']) > 0)
|
self.assertTrue(len(response['albums']) > 0)
|
||||||
|
|
||||||
def test_featured_releases(self):
|
def test_featured_releases(self):
|
||||||
response = spotify.featured_playlists()
|
response = self.spotify.featured_playlists()
|
||||||
self.assertTrue(len(response['playlists']) > 0)
|
self.assertTrue(len(response['playlists']) > 0)
|
||||||
|
|
||||||
def test_current_user_follows(self):
|
def test_current_user_follows(self):
|
||||||
response = spotify.current_user_followed_artists()
|
response = self.spotify.current_user_followed_artists()
|
||||||
artists = response['artists']
|
artists = response['artists']
|
||||||
self.assertTrue(len(artists['items']) > 0)
|
self.assertTrue(len(artists['items']) > 0)
|
||||||
|
|
||||||
def test_current_user_top_tracks(self):
|
def test_current_user_top_tracks(self):
|
||||||
response = spotify.current_user_top_tracks()
|
response = self.spotify.current_user_top_tracks()
|
||||||
items = response['items']
|
items = response['items']
|
||||||
self.assertTrue(len(items) > 0)
|
self.assertTrue(len(items) > 0)
|
||||||
|
|
||||||
def test_current_user_top_artists(self):
|
def test_current_user_top_artists(self):
|
||||||
response = spotify.current_user_top_artists()
|
response = self.spotify.current_user_top_artists()
|
||||||
items = response['items']
|
items = response['items']
|
||||||
self.assertTrue(len(items) > 0)
|
self.assertTrue(len(items) > 0)
|
||||||
|
|
||||||
def get_or_create_spotify_playlist(self, username, playlist_name):
|
def get_or_create_spotify_playlist(self, playlist_name):
|
||||||
playlists = spotify.user_playlists(username)
|
playlists = self.spotify.user_playlists(self.username)
|
||||||
while playlists:
|
while playlists:
|
||||||
for item in playlists['items']:
|
for item in playlists['items']:
|
||||||
if item['name'] == playlist_name:
|
if item['name'] == playlist_name:
|
||||||
return item['id']
|
return item['id']
|
||||||
playlists = spotify.next(playlists)
|
playlists = self.spotify.next(playlists)
|
||||||
playlist = spotify.user_playlist_create(username, playlist_name)
|
playlist = self.spotify.user_playlist_create(self.username, playlist_name)
|
||||||
playlist_id = playlist['uri']
|
playlist_id = playlist['uri']
|
||||||
return playlist_id
|
return playlist_id
|
||||||
|
|
||||||
def test_user_playlist_ops(self):
|
def test_user_playlist_ops(self):
|
||||||
# create empty playlist
|
# create empty playlist
|
||||||
playlist_id = self.get_or_create_spotify_playlist(username,
|
playlist_id = self.get_or_create_spotify_playlist('spotipy-testing-playlist-1')
|
||||||
'spotipy-testing-playlist-1')
|
|
||||||
|
|
||||||
# remove all tracks from it
|
# remove all tracks from it
|
||||||
|
|
||||||
spotify.user_playlist_replace_tracks(username, playlist_id,[])
|
self.spotify.user_playlist_replace_tracks(self.username, playlist_id,[])
|
||||||
|
|
||||||
playlist = spotify.user_playlist(username, playlist_id)
|
playlist = self.spotify.user_playlist(self.username, playlist_id)
|
||||||
self.assertTrue(playlist['tracks']['total'] == 0)
|
self.assertTrue(playlist['tracks']['total'] == 0)
|
||||||
self.assertTrue(len(playlist['tracks']['items']) == 0)
|
self.assertTrue(len(playlist['tracks']['items']) == 0)
|
||||||
|
|
||||||
# add tracks to it
|
# add tracks to it
|
||||||
|
|
||||||
spotify.user_playlist_add_tracks(username, playlist_id, self.four_tracks)
|
self.spotify.user_playlist_add_tracks(self.username, playlist_id, self.four_tracks)
|
||||||
playlist = spotify.user_playlist(username, playlist_id)
|
playlist = self.spotify.user_playlist(self.username, playlist_id)
|
||||||
self.assertTrue(playlist['tracks']['total'] == 4)
|
self.assertTrue(playlist['tracks']['total'] == 4)
|
||||||
self.assertTrue(len(playlist['tracks']['items']) == 4)
|
self.assertTrue(len(playlist['tracks']['items']) == 4)
|
||||||
|
|
||||||
# remove two tracks from it
|
# remove two tracks from it
|
||||||
|
|
||||||
spotify.user_playlist_remove_all_occurrences_of_tracks (username,
|
self.spotify.user_playlist_remove_all_occurrences_of_tracks (self.username,
|
||||||
playlist_id, self.two_tracks)
|
playlist_id, self.two_tracks)
|
||||||
|
|
||||||
playlist = spotify.user_playlist(username, playlist_id)
|
playlist = self.spotify.user_playlist(self.username, playlist_id)
|
||||||
self.assertTrue(playlist['tracks']['total'] == 2)
|
self.assertTrue(playlist['tracks']['total'] == 2)
|
||||||
self.assertTrue(len(playlist['tracks']['items']) == 2)
|
self.assertTrue(len(playlist['tracks']['items']) == 2)
|
||||||
|
|
||||||
# replace with 3 other tracks
|
# replace with 3 other tracks
|
||||||
spotify.user_playlist_replace_tracks(username,
|
self.spotify.user_playlist_replace_tracks(self.username,
|
||||||
playlist_id, self.other_tracks)
|
playlist_id, self.other_tracks)
|
||||||
|
|
||||||
playlist = spotify.user_playlist(username, playlist_id)
|
playlist = self.spotify.user_playlist(self.username, playlist_id)
|
||||||
self.assertTrue(playlist['tracks']['total'] == 3)
|
self.assertTrue(playlist['tracks']['total'] == 3)
|
||||||
self.assertTrue(len(playlist['tracks']['items']) == 3)
|
self.assertTrue(len(playlist['tracks']['items']) == 3)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) > 1:
|
|
||||||
username = sys.argv[1]
|
|
||||||
del sys.argv[1]
|
|
||||||
|
|
||||||
scope = 'playlist-modify-public '
|
|
||||||
scope += 'user-library-read '
|
|
||||||
scope += 'user-follow-read '
|
|
||||||
scope += 'user-library-modify '
|
|
||||||
scope += 'user-read-private '
|
|
||||||
scope += 'user-top-read'
|
|
||||||
|
|
||||||
token = util.prompt_for_user_token(username, scope)
|
|
||||||
spotify = spotipy.Spotify(auth=token)
|
|
||||||
spotify.trace = False
|
|
||||||
unittest.main()
|
unittest.main()
|
||||||
else:
|
|
||||||
print("Usage: %s username" % (sys.argv[0],))
|
|
||||||
|
|||||||
@ -1,25 +1,44 @@
|
|||||||
# -*- coding: latin-1 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import spotipy
|
"""
|
||||||
from spotipy import util
|
These tests require user authentication - provide client credentials using the
|
||||||
import unittest
|
following environment variables
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
'SPOTIPY_CLIENT_USERNAME'
|
||||||
|
'SPOTIPY_CLIENT_ID'
|
||||||
|
'SPOTIPY_CLIENT_SECRET'
|
||||||
|
'SPOTIPY_REDIRECT_URI'
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import pprint
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
from spotipy.oauth2 import SpotifyClientCredentials
|
|
||||||
|
|
||||||
'''
|
sys.path.insert(0, os.path.abspath(os.pardir))
|
||||||
Since these tests require authentication they are maintained
|
|
||||||
separately from the other tests.
|
from spotipy import (
|
||||||
|
Spotify,
|
||||||
|
SpotifyClientCredentials,
|
||||||
|
)
|
||||||
|
|
||||||
These tests try to be benign and leave your collection and
|
|
||||||
playlists in a relatively stable state.
|
|
||||||
'''
|
|
||||||
|
|
||||||
class AuthTestSpotipy(unittest.TestCase):
|
class AuthTestSpotipy(unittest.TestCase):
|
||||||
'''
|
"""
|
||||||
These tests require user authentication
|
These tests require user authentication - provide client credentials using the
|
||||||
'''
|
following environment variables
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
'SPOTIPY_CLIENT_USERNAME'
|
||||||
|
'SPOTIPY_CLIENT_ID'
|
||||||
|
'SPOTIPY_CLIENT_SECRET'
|
||||||
|
'SPOTIPY_REDIRECT_URI'
|
||||||
|
"""
|
||||||
|
|
||||||
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
|
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
|
||||||
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
|
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
|
||||||
@ -36,13 +55,17 @@ class AuthTestSpotipy(unittest.TestCase):
|
|||||||
|
|
||||||
bad_id = 'BAD_ID'
|
bad_id = 'BAD_ID'
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(self):
|
||||||
|
self.spotify = Spotify(client_credentials_manager=SpotifyClientCredentials())
|
||||||
|
self.spotify.trace = False
|
||||||
|
|
||||||
def test_audio_analysis(self):
|
def test_audio_analysis(self):
|
||||||
result = spotify.audio_analysis(self.four_tracks[0])
|
result = self.spotify.audio_analysis(self.four_tracks[0])
|
||||||
assert('beats' in result)
|
assert('beats' in result)
|
||||||
|
|
||||||
def test_audio_features(self):
|
def test_audio_features(self):
|
||||||
results = spotify.audio_features(self.four_tracks)
|
results = self.spotify.audio_features(self.four_tracks)
|
||||||
self.assertTrue(len(results) == len(self.four_tracks))
|
self.assertTrue(len(results) == len(self.four_tracks))
|
||||||
for track in results:
|
for track in results:
|
||||||
assert('speechiness' in track)
|
assert('speechiness' in track)
|
||||||
@ -51,7 +74,7 @@ class AuthTestSpotipy(unittest.TestCase):
|
|||||||
bad_tracks = []
|
bad_tracks = []
|
||||||
bad_tracks = ['spotify:track:bad']
|
bad_tracks = ['spotify:track:bad']
|
||||||
input = self.four_tracks + bad_tracks
|
input = self.four_tracks + bad_tracks
|
||||||
results = spotify.audio_features(input)
|
results = self.spotify.audio_features(input)
|
||||||
self.assertTrue(len(results) == len(input))
|
self.assertTrue(len(results) == len(input))
|
||||||
for track in results[:-1]:
|
for track in results[:-1]:
|
||||||
if track != None:
|
if track != None:
|
||||||
@ -59,12 +82,10 @@ class AuthTestSpotipy(unittest.TestCase):
|
|||||||
self.assertTrue(results[-1] == None)
|
self.assertTrue(results[-1] == None)
|
||||||
|
|
||||||
def test_recommendations(self):
|
def test_recommendations(self):
|
||||||
results = spotify.recommendations(seed_tracks=self.four_tracks, min_danceability=0, max_loudness=0, target_popularity=50)
|
results = self.spotify.recommendations(seed_tracks=self.four_tracks, min_danceability=0, max_loudness=0, target_popularity=50)
|
||||||
self.assertTrue(len(results['tracks']) == 20)
|
self.assertTrue(len(results['tracks']) == 20)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
client_credentials_manager = SpotifyClientCredentials()
|
|
||||||
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
|
||||||
spotify.trace = False
|
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
@ -15,9 +15,17 @@ from spotipy import (
|
|||||||
|
|
||||||
|
|
||||||
class ClientCredentialsTestSpotipy(unittest.TestCase):
|
class ClientCredentialsTestSpotipy(unittest.TestCase):
|
||||||
'''
|
"""
|
||||||
These tests require user authentication
|
These tests require user authentication - provide client credentials using the
|
||||||
'''
|
following environment variables
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
'SPOTIPY_CLIENT_USERNAME'
|
||||||
|
'SPOTIPY_CLIENT_ID'
|
||||||
|
'SPOTIPY_CLIENT_SECRET'
|
||||||
|
'SPOTIPY_REDIRECT_URI'
|
||||||
|
"""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(self):
|
def setUpClass(self):
|
||||||
|
|||||||
@ -18,6 +18,18 @@ from spotipy import (
|
|||||||
|
|
||||||
class TestSpotipy(unittest.TestCase):
|
class TestSpotipy(unittest.TestCase):
|
||||||
|
|
||||||
|
"""
|
||||||
|
These tests require user authentication - provide client credentials using the
|
||||||
|
following environment variables
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
'SPOTIPY_CLIENT_USERNAME'
|
||||||
|
'SPOTIPY_CLIENT_ID'
|
||||||
|
'SPOTIPY_CLIENT_SECRET'
|
||||||
|
'SPOTIPY_REDIRECT_URI'
|
||||||
|
"""
|
||||||
|
|
||||||
creep_urn = 'spotify:track:3HfB5hBU0dmBt8T0iCmH42'
|
creep_urn = 'spotify:track:3HfB5hBU0dmBt8T0iCmH42'
|
||||||
creep_id = '3HfB5hBU0dmBt8T0iCmH42'
|
creep_id = '3HfB5hBU0dmBt8T0iCmH42'
|
||||||
creep_url = 'http://open.spotify.com/track/3HfB5hBU0dmBt8T0iCmH42'
|
creep_url = 'http://open.spotify.com/track/3HfB5hBU0dmBt8T0iCmH42'
|
||||||
@ -34,13 +46,20 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(self):
|
def setUpClass(self):
|
||||||
self.token = os.getenv('SPOTIPY_CLIENT_TOKEN')
|
client_cred_env_vars = ['SPOTIPY_CLIENT_USERNAME', 'SPOTIPY_CLIENT_ID', 'SPOTIPY_CLIENT_SECRET', 'SPOTIPY_REDIRECT_URI']
|
||||||
if not self.token:
|
missing = filter(lambda var: not os.getenv(var), client_cred_env_vars)
|
||||||
raise Exception('Set your Spotify client app token via the environment variable `SPOTIPY_CLIENT_TOKEN`')
|
|
||||||
|
if missing:
|
||||||
|
raise Exception('Please set the client credetials for the test application using the following environment variables: {}'.format(client_cred_env_vars))
|
||||||
|
|
||||||
|
self.username = os.getenv('SPOTIPY_CLIENT_USERNAME')
|
||||||
|
|
||||||
|
self.scope = 'user-library-read'
|
||||||
|
|
||||||
|
self.token = prompt_for_user_token(self.username, scope=self.scope)
|
||||||
|
|
||||||
self.spotify = Spotify(auth=self.token)
|
self.spotify = Spotify(auth=self.token)
|
||||||
|
|
||||||
|
|
||||||
def test_artist_urn(self):
|
def test_artist_urn(self):
|
||||||
artist = self.spotify.artist(self.radiohead_urn)
|
artist = self.spotify.artist(self.radiohead_urn)
|
||||||
self.assertTrue(artist['name'] == 'Radiohead')
|
self.assertTrue(artist['name'] == 'Radiohead')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user