-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapiTest.py
More file actions
489 lines (438 loc) · 20.7 KB
/
Copy pathapiTest.py
File metadata and controls
489 lines (438 loc) · 20.7 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env python
# encoding: utf-8
import unittest
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from kkbox_developer_sdk.track_fetcher import *
from kkbox_developer_sdk.artist_fetcher import *
from kkbox_developer_sdk.album_fetcher import *
from kkbox_developer_sdk.shared_playlist_fetcher import *
from kkbox_developer_sdk.search_fetcher import *
from kkbox_developer_sdk.chart_fetcher import *
from kkbox_developer_sdk.new_release_category_fetcher import *
from kkbox_developer_sdk.genre_station_fetcher import *
from kkbox_developer_sdk.mood_station_fetcher import *
from kkbox_developer_sdk.feature_playlist_fetcher import *
from kkbox_developer_sdk.feature_playlist_category_fetcher import *
from kkbox_developer_sdk.new_hits_playlist_fetcher import *
from kkbox_developer_sdk.auth_flow import *
from client import ClientInfo
CLIENT_ID = ClientInfo.client_id
CLIENT_SECRET = ClientInfo.client_secret
class TestAPISDK(unittest.TestCase):
def _validate_image(self, image):
keys = ('url', 'width', 'height')
for key in keys:
assert key in image, 'missing key ' + key
def _validate_paging(self, paging):
assert 'offset', 'limit' in paging
assert 'previous', 'next' in paging
def _validate_artist(self, artist):
keys = ('id', 'name', 'url', 'images')
for key in keys:
assert key in artist, 'missing key ' + key
for image in artist['images']:
self._validate_image(image)
def _validate_artist_paging(self, artist_paging):
if artist_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in artist_paging, 'missing key ' + key
for artist in artist_paging['data']:
self._validate_artist(artist)
for paging in artist_paging['paging']:
self._validate_paging(paging)
def _validate_album(self, album):
keys = ('id', 'name', 'url', 'explicitness', 'available_territories', 'images')
for key in keys:
assert key in album, 'missing key ' + key
for image in album['images']:
self._validate_image(image)
if 'artist' in album:
self._validate_artist(album['artist'])
def _validate_album_paging(self, album_paging):
if album_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in album_paging, 'missing key ' + key
for album in album_paging['data']:
self._validate_album(album)
for paging in album_paging['paging']:
self._validate_paging(paging)
def _validate_track(self, track):
keys = ('id', 'name', 'url', 'track_number', 'explicitness', 'available_territories')
for key in keys:
assert key in track, 'missing key ' + key
if 'album' in track:
self._validate_album(track['album'])
if 'paging' in track:
self._validate_paging(track['paging'])
def _validate_track_paging(self, track_paging):
if track_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in track_paging, 'missing key ' + key
for track in track_paging['data']:
self._validate_track(track)
for paging in track_paging['paging']:
self._validate_paging(paging)
def _validate_playlist(self, playlist):
if playlist is None:
pass
else:
keys = ('id', 'title', 'description', 'url', 'images', 'owner')
for key in keys:
assert key in playlist, 'missing key ' + key
for image in playlist['images']:
self._validate_image(image)
for owner in playlist['owner']:
assert 'id', 'name' in playlist['owner']
if 'tracks' in playlist:
track = playlist['tracks']
self._validate_track_paging(track)
if 'paging' in playlist:
self._validate_paging(playlist['paging'])
def _validate_playlist_paging(self, playlist_paging):
if playlist_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in playlist_paging, 'missing key ' + key
for playlist in playlist_paging['data']:
self._validate_playlist(playlist)
for paging in playlist_paging['paging']:
self._validate_paging(playlist_paging['paging'])
def _validate_category(self, category):
if category is None:
pass
else:
keys = ('id', 'title')
for key in keys:
assert key in category, 'missing key ' + key
if 'albums' in category:
album = category['albums']
self._validate_album_paging(album)
if 'images' in category:
for image in category['images']:
self._validate_image(image)
def _validate_category_paging(self, category_paging):
if category_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in category_paging, 'missing key ' + key
for category in category_paging['data']:
self._validate_category(category)
for paging in category_paging['paging']:
self._validate_paging(paging)
def _validate_genre_station_paging(self, station_paging):
if station_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in station_paging, 'missing key ' + key
for station in station_paging['data']:
keys = ('category', 'id', 'name')
for key in keys:
assert key in station
for paging in station_paging['paging']:
self._validate_paging(paging)
def _validate_mood_station_paging(self, station_paging):
if station_paging is None:
pass
else:
keys = ('paging', 'data', 'summary')
for key in keys:
assert key in station_paging, 'missing key ' + key
for station in station_paging['data']:
keys = ('images', 'id', 'name')
for key in keys:
assert key in station
for image in station['images']:
self._validate_image(image)
for paging in station_paging['paging']:
self._validate_paging(paging)
def test_fetch_track(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXTrackFetcher(token)
track_id = '4kxvr3wPWkaL9_y3o_'
track = fetcher.fetch_track(track_id)
self._validate_track(track)
def test_fetch_artist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXArtistFetcher(token)
artist_id = '8q3_xzjl89Yakn_7GB'
artist = fetcher.fetch_artist(artist_id)
self._validate_artist(artist)
def test_fetch_albums_of_artist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXArtistFetcher(token)
artist_id = 'CluDKLYxr1GFQqLSZt'
albums = fetcher.fetch_albums_of_artist(artist_id)
self._validate_album_paging(albums)
next_page_data = fetcher.fetch_next_page(albums)
self._validate_album_paging(next_page_data)
def test_fetch_top_tracks_of_artist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXArtistFetcher(token)
artist_id = 'CluDKLYxr1GFQqLSZt'
tracks = fetcher.fetch_top_tracks_of_artist(artist_id)
self._validate_track_paging(tracks)
next_page_data = fetcher.fetch_next_page(tracks)
self._validate_track_paging(next_page_data)
def test_fetch_related_artists(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXArtistFetcher(token)
artist_id = 'CluDKLYxr1GFQqLSZt'
artists = fetcher.fetch_related_artists(artist_id)
self._validate_artist_paging(artists)
next_page_data = fetcher.fetch_next_page(artists)
self._validate_artist_paging(next_page_data)
def test_fetch_album(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXAlbumFetcher(token)
album_id = 'WpTPGzNLeutVFHcFq6'
album = fetcher.fetch_album(album_id)
self._validate_album(album)
def test_fetch_tracks_in_album(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXAlbumFetcher(token)
album_id = 'WpTPGzNLeutVFHcFq6'
tracks = fetcher.fetch_tracks_in_album(album_id)
self._validate_track_paging(tracks)
next_page_data = fetcher.fetch_next_page(tracks)
self._validate_track_paging(next_page_data)
def test_fetch_shared_playlist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXSharedPlaylistFetcher(token)
playlist_id = '4nUZM-TY2aVxZ2xaA-'
playlist = fetcher.fetch_shared_playlist(playlist_id)
self._validate_playlist(playlist)
next_page_data = fetcher.fetch_next_page(playlist['tracks'])
self._validate_track_paging(next_page_data)
def test_fetch_tracks_of_shared_playlist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXSharedPlaylistFetcher(token)
playlist_id = '4nUZM-TY2aVxZ2xaA-'
tracks = fetcher.fetch_tracks_of_shared_playlists(playlist_id)
self._validate_track_paging(tracks)
next_page_data = fetcher.fetch_next_page(tracks)
self._validate_track_paging(next_page_data)
def test_search(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXSearchFetcher(token)
results = fetcher.search('love',
[KKBOXSearchTypes.ARTIST, KKBOXSearchTypes.ALBUM,
KKBOXSearchTypes.TRACK,
KKBOXSearchTypes.PLAYLIST])
artists = results.get('artists', [])
for artist in artists['data']:
self._validate_artist(artist)
albums = results.get('albums', [])
for album in albums['data']:
self._validate_album(album)
tracks = results.get('tracks', [])
for track in tracks['data']:
self._validate_track(track)
playlists = results.get('playlists', [])
for playlist in playlists['data']:
self._validate_playlist(playlist)
if results['tracks']['paging']['next'] != None:
next_url = results['tracks']['paging']['next']
next_result = fetcher.fetch_data(next_url)
assert(next_result != None)
def test_fetch_charts(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXChartFetcher(token)
charts = fetcher.fetch_charts()
self._validate_playlist_paging(charts)
next_page_data = fetcher.fetch_next_page(charts)
self._validate_playlist_paging(next_page_data)
def test_fetch_charts_playlist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXChartFetcher(token)
playlist_id = '0kTVCy_kzou3AdOsAc'
playlist = fetcher.fetch_charts_playlist(playlist_id)
self._validate_playlist(playlist)
next_page_data = fetcher.fetch_next_page(playlist['tracks'])
self._validate_track_paging(next_page_data)
def test_fetch_charts_playlist_tracks(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXChartFetcher(token)
playlist_id = '0kTVCy_kzou3AdOsAc'
playlist = fetcher.fetch_charts_playlist_tracks(playlist_id)
self._validate_track_paging(playlist)
next_page_data = fetcher.fetch_next_page(playlist)
self._validate_track_paging(next_page_data)
def test_fetch_all_new_release_categories(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXNewReleaseCategoryFetcher(token)
categories = fetcher.fetch_all_new_release_categories()
self._validate_category_paging(categories)
next_page_data = fetcher.fetch_next_page(categories)
self._validate_category_paging(next_page_data)
def test_fetch_new_release_category(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXNewReleaseCategoryFetcher(token)
category_id = '1ZQwmFTaLE4p7BG-Ua'
category = fetcher.fetch_new_release_category(category_id)
self._validate_category(category)
next_page_data = fetcher.fetch_next_page(category['albums'])
self._validate_album_paging(next_page_data)
def test_fetch_albums_of_new_release_category(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXNewReleaseCategoryFetcher(token)
category_id = '1ZQwmFTaLE4p7BG-Ua'
albums = fetcher.fetch_albums_of_new_release_category(category_id)
self._validate_album_paging(albums)
next_page_data = fetcher.fetch_next_page(albums)
self._validate_album_paging(next_page_data)
def test_fetch_all_genre_stations(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXGenreStationFetcher(token)
genre_stations = fetcher.fetch_all_genre_stations()
self._validate_genre_station_paging(genre_stations)
next_page_data = fetcher.fetch_next_page(genre_stations)
self._validate_genre_station_paging(next_page_data)
def test_fetch_genre_station(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXGenreStationFetcher(token)
station_id = 'TYq3EHFTl-1EOvJM5Y'
station = fetcher.fetch_genre_station(station_id)
keys = ('category', 'tracks', 'id', 'name')
for key in keys:
assert key in station
tracks = station['tracks']['data']
for track in tracks:
self._validate_track(track)
def test_fetch_all_mood_stations(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXMoodStationFetcher(token)
mood_stations = fetcher.fetch_all_mood_stations()
self._validate_mood_station_paging(mood_stations)
next_page_data = fetcher.fetch_next_page(mood_stations)
self._validate_mood_station_paging(next_page_data)
def test_fetch_mood_station(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXMoodStationFetcher(token)
station_id = 'StGZp2ToWq92diPHS7'
station = fetcher.fetch_mood_station(station_id)
keys = ('tracks', 'id', 'name')
for key in keys:
assert key in station
for track in station['tracks']['data']:
self._validate_track(track)
def test_fetch_all_feature_playlists(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXFeaturePlaylistFetcher(token)
feature_playlists = fetcher.fetch_all_feature_playlists()
self._validate_playlist_paging(feature_playlists)
next_page_data = fetcher.fetch_next_page(feature_playlists)
self._validate_playlist_paging(next_page_data)
def test_fetch_feature_playlist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXFeaturePlaylistFetcher(token)
playlist_id = 'Wt95My35CqR9hB_FW1'
feature_playlist = fetcher.fetch_feature_playlist(playlist_id)
self._validate_playlist(feature_playlist)
next_page_data = fetcher.fetch_next_page(feature_playlist['tracks'])
self._validate_track_paging(next_page_data)
def test_fetch_feature_playlist_tracks(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXFeaturePlaylistFetcher(token)
playlist_id = 'Wt95My35CqR9hB_FW1'
feature_playlist = fetcher.fetch_feature_playlist_tracks(playlist_id)
self._validate_track_paging(feature_playlist)
next_page_data = fetcher.fetch_next_page(feature_playlist)
self._validate_track_paging(next_page_data)
def test_fetch_categories_of_feature_playlist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXFeaturePlaylistCategoryFetcher(token)
categories = fetcher.fetch_categories_of_feature_playlist()
self._validate_category_paging(categories)
next_page_data = fetcher.fetch_next_page(categories)
self._validate_category_paging(next_page_data)
def test_fetch_feature_playlist_by_category(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXFeaturePlaylistCategoryFetcher(token)
category_id = '9XQKD8BJx595ESs_rb'
category = fetcher.fetch_feature_playlist_by_category(category_id)
keys = ('id', 'title', 'images', 'playlists')
for key in keys:
assert key in category, 'missing key ' + key
for image in category['images']:
self._validate_image(image)
for playlist in category['playlists']['data']:
self._validate_playlist(playlist)
def test_fetch_playlists_of_feature_playlist_category(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXFeaturePlaylistCategoryFetcher(token)
category_id = '9XQKD8BJx595ESs_rb'
category = fetcher.fetch_playlists_of_feature_playlist_category(category_id)
self._validate_playlist_paging(category)
next_page_data = fetcher.fetch_next_page(category)
self._validate_playlist_paging(next_page_data)
def test_fetch_all_new_hits_playlists(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXNewHitsPlaylistFetcher(token)
new_hits = fetcher.fetch_all_new_hits_playlists()
self._validate_playlist_paging(new_hits)
next_page_data = fetcher.fetch_next_page(new_hits)
self._validate_playlist_paging(next_page_data)
def test_fetch_new_hits_playlist(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXNewHitsPlaylistFetcher(token)
playlist_id = 'DZrC8m29ciOFY2JAm3'
new_hits = fetcher.fetch_new_hits_playlist(playlist_id)
self._validate_playlist(new_hits)
next_page_data = fetcher.fetch_next_page(new_hits['tracks'])
self._validate_track_paging(next_page_data)
def test_fetch_new_hits_playlist_track(self):
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
fetcher = KKBOXNewHitsPlaylistFetcher(token)
playlist_id = 'DZrC8m29ciOFY2JAm3'
new_hits = fetcher.fetch_new_hits_playlist_tracks(playlist_id)
self._validate_track_paging(new_hits)
next_page_data = fetcher.fetch_next_page(new_hits)
self._validate_track_paging(next_page_data)
if __name__ == '__main__':
unittest.main()