-
Notifications
You must be signed in to change notification settings - Fork 956
/
test.py
374 lines (310 loc) · 15 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# -*- coding: utf-8 -*-
from spotipy import (
Spotify,
SpotifyClientCredentials,
SpotifyException
)
import spotipy
import unittest
import requests
class AuthTestSpotipy(unittest.TestCase):
"""
These tests require client authentication - provide client credentials
using the following environment variables
::
'SPOTIPY_CLIENT_ID'
'SPOTIPY_CLIENT_SECRET'
"""
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB"]
other_tracks = ["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
bad_id = 'BAD_ID'
creep_urn = 'spotify:track:6b2oQwSGFkzsMtQruIWm2p'
creep_id = '6b2oQwSGFkzsMtQruIWm2p'
creep_url = 'http://open.spotify.com/track/6b2oQwSGFkzsMtQruIWm2p'
el_scorcho_urn = 'spotify:track:0Svkvt5I79wficMFgaqEQJ'
el_scorcho_bad_urn = 'spotify:track:0Svkvt5I79wficMFgaqEQK'
pinkerton_urn = 'spotify:album:04xe676vyiTeYNXw15o9jT'
weezer_urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu'
pablo_honey_urn = 'spotify:album:6AZv3m27uyRxi8KyJSfUxL'
radiohead_urn = 'spotify:artist:4Z8W4fKeB5YxbusRsdQVPb'
angeles_haydn_urn = 'spotify:album:1vAbqAeuJVWNAe7UR00bdM'
heavyweight_urn = 'spotify:show:5c26B28vZMN8PG0Nppmn5G'
heavyweight_id = '5c26B28vZMN8PG0Nppmn5G'
heavyweight_url = 'https://open.spotify.com/show/5c26B28vZMN8PG0Nppmn5G'
reply_all_urn = 'spotify:show:7gozmLqbcbr6PScMjc0Zl4'
heavyweight_ep1_urn = 'spotify:episode:68kq3bNz6hEuq8NtdfwERG'
heavyweight_ep1_id = '68kq3bNz6hEuq8NtdfwERG'
heavyweight_ep1_url = 'https://open.spotify.com/episode/68kq3bNz6hEuq8NtdfwERG'
reply_all_ep1_urn = 'spotify:episode:1KHjbpnmNpFmNTczQmTZlR'
@classmethod
def setUpClass(self):
self.spotify = Spotify(
client_credentials_manager=SpotifyClientCredentials())
self.spotify.trace = False
def test_audio_analysis(self):
result = self.spotify.audio_analysis(self.four_tracks[0])
assert ('beats' in result)
def test_audio_features(self):
results = self.spotify.audio_features(self.four_tracks)
self.assertTrue(len(results) == len(self.four_tracks))
for track in results:
assert ('speechiness' in track)
def test_audio_features_with_bad_track(self):
bad_tracks = ['spotify:track:bad']
input = self.four_tracks + bad_tracks
results = self.spotify.audio_features(input)
self.assertTrue(len(results) == len(input))
for track in results[:-1]:
if track is not None:
assert ('speechiness' in track)
self.assertTrue(results[-1] is None)
def test_recommendations(self):
results = self.spotify.recommendations(
seed_tracks=self.four_tracks,
min_danceability=0,
max_loudness=0,
target_popularity=50)
self.assertTrue(len(results['tracks']) == 20)
def test_artist_urn(self):
artist = self.spotify.artist(self.radiohead_urn)
self.assertTrue(artist['name'] == 'Radiohead')
def test_artists(self):
results = self.spotify.artists([self.weezer_urn, self.radiohead_urn])
self.assertTrue('artists' in results)
self.assertTrue(len(results['artists']) == 2)
def test_album_urn(self):
album = self.spotify.album(self.pinkerton_urn)
self.assertTrue(album['name'] == 'Pinkerton')
def test_album_tracks(self):
results = self.spotify.album_tracks(self.pinkerton_urn)
self.assertTrue(len(results['items']) == 10)
def test_album_tracks_many(self):
results = self.spotify.album_tracks(self.angeles_haydn_urn)
tracks = results['items']
total, received = results['total'], len(tracks)
while received < total:
results = self.spotify.album_tracks(
self.angeles_haydn_urn, offset=received)
tracks.extend(results['items'])
received = len(tracks)
self.assertEqual(received, total)
def test_albums(self):
results = self.spotify.albums(
[self.pinkerton_urn, self.pablo_honey_urn])
self.assertTrue('albums' in results)
self.assertTrue(len(results['albums']) == 2)
def test_track_urn(self):
track = self.spotify.track(self.creep_urn)
self.assertTrue(track['name'] == 'Creep')
def test_track_id(self):
track = self.spotify.track(self.creep_id)
self.assertTrue(track['name'] == 'Creep')
self.assertTrue(track['popularity'] > 0)
def test_track_url(self):
track = self.spotify.track(self.creep_url)
self.assertTrue(track['name'] == 'Creep')
def test_track_bad_urn(self):
try:
self.spotify.track(self.el_scorcho_bad_urn)
self.assertTrue(False)
except SpotifyException:
self.assertTrue(True)
def test_tracks(self):
results = self.spotify.tracks([self.creep_url, self.el_scorcho_urn])
self.assertTrue('tracks' in results)
self.assertTrue(len(results['tracks']) == 2)
def test_artist_top_tracks(self):
results = self.spotify.artist_top_tracks(self.weezer_urn)
self.assertTrue('tracks' in results)
self.assertTrue(len(results['tracks']) == 10)
def test_artist_related_artists(self):
results = self.spotify.artist_related_artists(self.weezer_urn)
self.assertTrue('artists' in results)
self.assertTrue(len(results['artists']) == 20)
for artist in results['artists']:
if artist['name'] == 'Jimmy Eat World':
found = True
self.assertTrue(found)
def test_artist_search(self):
results = self.spotify.search(q='weezer', type='artist')
self.assertTrue('artists' in results)
self.assertTrue(len(results['artists']['items']) > 0)
self.assertTrue(results['artists']['items'][0]['name'] == 'Weezer')
def test_artist_search_with_market(self):
results = self.spotify.search(q='weezer', type='artist', market='GB')
self.assertTrue('artists' in results)
self.assertTrue(len(results['artists']['items']) > 0)
self.assertTrue(results['artists']['items'][0]['name'] == 'Weezer')
def test_artist_search_with_multiple_markets(self):
total = 5
countries_list = ['GB', 'US', 'AU']
countries_tuple = ('GB', 'US', 'AU')
results_multiple = self.spotify.search_markets(q='weezer', type='artist',
markets=countries_list)
results_all = self.spotify.search_markets(q='weezer', type='artist')
results_tuple = self.spotify.search_markets(q='weezer', type='artist',
markets=countries_tuple)
results_limited = self.spotify.search_markets(q='weezer', limit=3, type='artist',
markets=countries_list, total=total)
self.assertTrue(
all('artists' in results_multiple[country] for country in results_multiple))
self.assertTrue(all('artists' in results_all[country] for country in results_all))
self.assertTrue(all('artists' in results_tuple[country] for country in results_tuple))
self.assertTrue(all('artists' in results_limited[country] for country in results_limited))
self.assertTrue(
all(len(results_multiple[country]['artists']['items']) > 0 for country in
results_multiple))
self.assertTrue(all(len(results_all[country]['artists']
['items']) > 0 for country in results_all))
self.assertTrue(
all(len(results_tuple[country]['artists']['items']) > 0 for country in results_tuple))
self.assertTrue(
all(len(results_limited[country]['artists']['items']) > 0 for country in
results_limited))
self.assertTrue(all(results_multiple[country]['artists']['items']
[0]['name'] == 'Weezer' for country in results_multiple))
self.assertTrue(all(results_all[country]['artists']['items']
[0]['name'] == 'Weezer' for country in results_all))
self.assertTrue(all(results_tuple[country]['artists']['items']
[0]['name'] == 'Weezer' for country in results_tuple))
self.assertTrue(all(results_limited[country]['artists']['items']
[0]['name'] == 'Weezer' for country in results_limited))
total_limited_results = 0
for country in results_limited:
total_limited_results += len(results_limited[country]['artists']['items'])
self.assertTrue(total_limited_results <= total)
def test_artist_albums(self):
results = self.spotify.artist_albums(self.weezer_urn)
self.assertTrue('items' in results)
self.assertTrue(len(results['items']) > 0)
def find_album():
for album in results['items']:
if album['name'] == 'Death to False Metal':
return True
return False
self.assertTrue(find_album())
def test_search_timeout(self):
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(requests_timeout=0.01,
client_credentials_manager=client_credentials_manager)
# depending on the timing or bandwidth, this raises a timeout or connection error
self.assertRaises((requests.exceptions.Timeout, requests.exceptions.ConnectionError),
lambda: sp.search(q='my*', type='track'))
@unittest.skip("flaky test, need a better method to test retries")
def test_max_retries_reached_get(self):
spotify_no_retry = Spotify(
client_credentials_manager=SpotifyClientCredentials(),
retries=0)
i = 0
while i < 100:
try:
spotify_no_retry.search(q='foo')
except SpotifyException as e:
self.assertIsInstance(e, SpotifyException)
self.assertEqual(e.http_status, 429)
return
i += 1
self.fail()
def test_album_search(self):
results = self.spotify.search(q='weezer pinkerton', type='album')
self.assertTrue('albums' in results)
self.assertTrue(len(results['albums']['items']) > 0)
self.assertTrue(results['albums']['items'][0]
['name'].find('Pinkerton') >= 0)
def test_track_search(self):
results = self.spotify.search(q='el scorcho weezer', type='track')
self.assertTrue('tracks' in results)
self.assertTrue(len(results['tracks']['items']) > 0)
self.assertTrue(results['tracks']['items'][0]['name'] == 'El Scorcho')
def test_user(self):
user = self.spotify.user(user='plamere')
self.assertTrue(user['uri'] == 'spotify:user:plamere')
def test_track_bad_id(self):
try:
self.spotify.track(self.bad_id)
self.assertTrue(False)
except SpotifyException:
self.assertTrue(True)
def test_show_urn(self):
show = self.spotify.show(self.heavyweight_urn, market="US")
self.assertTrue(show['name'] == 'Heavyweight')
def test_show_id(self):
show = self.spotify.show(self.heavyweight_id, market="US")
self.assertTrue(show['name'] == 'Heavyweight')
def test_show_url(self):
show = self.spotify.show(self.heavyweight_url, market="US")
self.assertTrue(show['name'] == 'Heavyweight')
def test_show_bad_urn(self):
with self.assertRaises(SpotifyException):
self.spotify.show("bogus_urn", market="US")
def test_shows(self):
results = self.spotify.shows([self.heavyweight_urn, self.reply_all_urn], market="US")
self.assertTrue('shows' in results)
self.assertTrue(len(results['shows']) == 2)
def test_show_episodes(self):
results = self.spotify.show_episodes(self.heavyweight_urn, market="US")
self.assertTrue(len(results['items']) > 1)
def test_show_episodes_many(self):
results = self.spotify.show_episodes(self.reply_all_urn, market="US")
episodes = results['items']
total, received = results['total'], len(episodes)
while received < total:
results = self.spotify.show_episodes(
self.reply_all_urn, offset=received, market="US")
episodes.extend(results['items'])
received = len(episodes)
self.assertEqual(received, total)
def test_episode_urn(self):
episode = self.spotify.episode(self.heavyweight_ep1_urn, market="US")
self.assertTrue(episode['name'] == '#1 Buzz')
def test_episode_id(self):
episode = self.spotify.episode(self.heavyweight_ep1_id, market="US")
self.assertTrue(episode['name'] == '#1 Buzz')
def test_episode_url(self):
episode = self.spotify.episode(self.heavyweight_ep1_url, market="US")
self.assertTrue(episode['name'] == '#1 Buzz')
def test_episode_bad_urn(self):
with self.assertRaises(SpotifyException):
self.spotify.episode("bogus_urn", market="US")
def test_episodes(self):
results = self.spotify.episodes(
[self.heavyweight_ep1_urn, self.reply_all_ep1_urn],
market="US"
)
self.assertTrue('episodes' in results)
self.assertTrue(len(results['episodes']) == 2)
def test_unauthenticated_post_fails(self):
with self.assertRaises(SpotifyException) as cm:
self.spotify.user_playlist_create(
"spotify", "Best hits of the 90s")
self.assertTrue(cm.exception.http_status == 401 or cm.exception.http_status == 403)
def test_custom_requests_session(self):
sess = requests.Session()
sess.headers["user-agent"] = "spotipy-test"
with_custom_session = spotipy.Spotify(
client_credentials_manager=SpotifyClientCredentials(),
requests_session=sess)
self.assertTrue(
with_custom_session.user(
user="akx")["uri"] == "spotify:user:akx")
sess.close()
def test_force_no_requests_session(self):
with_no_session = spotipy.Spotify(
client_credentials_manager=SpotifyClientCredentials(),
requests_session=False)
self.assertNotIsInstance(with_no_session._session, requests.Session)
user = with_no_session.user(user="akx")
self.assertEqual(user["uri"], "spotify:user:akx")
def test_available_markets(self):
markets = self.spotify.available_markets()["markets"]
self.assertTrue(isinstance(markets, list))
self.assertIn("US", markets)
self.assertIn("GB", markets)