forked from mpalazzolo/apple-music-python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
359 lines (306 loc) · 16.4 KB
/
tests.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
from applemusicpy import AppleMusic
import unittest, os
class UserTests(unittest.TestCase):
def setUp(self):
# albums
self.born_to_run = '310730204'
self.ready_to_die = '204669326'
# songs
self.xo_tour_life = '1274153124'
self.new_patek = '1436530704'
# artists
self.lil_pump = '1129587661'
self.smokepurpp = '1122104172'
def test_album_retrieve(self):
albums = am.current_user_saved_albums()
self.assertIsNotNone(albums)
# You can add more assertions here based on the expected behavior of the function
def test_playlist_retrieve(self):
playlists = am.current_user_playlists(limit=1)
self.assertIsNotNone(playlists)
# You can add more assertions here based on the expected behavior of the function
def test_song_retrieve(self):
songs = am.current_user_saved_tracks()
self.assertIsNotNone(songs)
# You can add more assertions here based on the expected behavior of the function
def test_artist_retrieve(self):
artists = am.current_user_followed_artists()
self.assertIsNotNone(artists)
# You can add more assertions here based on the expected behavior of the function
def test_album_set(self):
album_id = self.born_to_run
response = am.current_user_saved_albums_add(album_id)
self.assertTrue(response) # Check if the album was added successfully
# You can add more assertions here based on the expected behavior of the function
def test_playlist_create(self):
playlist_name = "Test Playlist"
description = "apple-music-python test playlist!"
tracks = self.xo_tour_life, self.new_patek # Replace with actual track IDs
response = am.user_playlist_create(playlist_name=playlist_name, tracks=tracks, description=description)
self.assertTrue(response) # Check if the playlist was added successfully
# You can add more assertions here based on the expected behavior of the function
def test_song_set(self):
song_id = self.xo_tour_life
response = am.current_user_saved_tracks_add(song_id)
self.assertTrue(response) # Check if the song was added successfully
# You can add more assertions here based on the expected behavior of the function
# It doesn't seem like you can like an artist through the API. (Maybe through MusicKit?)
#def test_artist_set(self):
# artist_id = self.smokepurpp
# response = am.current_user_followed_artists_add(artist_id)
# self.assertTrue(response) # Check if the artist was added successfully
# # You can add more assertions here based on the expected behavior of the function
class BaseTests(unittest.TestCase):
def setUp(self):
# albums
self.born_to_run = '310730204'
self.ready_to_die = '204669326'
# music videos
self.rubber_soul = '401135199'
self.sgt_pepper = '401147268'
# ISRC
self.gods_plan_isrc = 'USCM51800004'
# playlists
self.janet_jackson = 'pl.acc464c750b94302b8806e5fcbe56e17'
self.eighties_pop = 'pl.97c6f95b0b884bedbcce117f9ea5d54b'
# songs
self.xo_tour_life = '1274153124'
self.new_patek = '1436530704'
# artists
self.lil_pump = '1129587661'
self.smokepurpp = '1122104172'
# stations
self.alt = 'ra.985484166'
self.pure_pop = 'ra.686227433'
# curators
self.large_up = '1107687517'
self.grand_ole_opry = '976439448'
# apple curators
self.apple_alt = '976439526'
self.live_nation_tv = '1017168810'
# genres
self.pop = '14'
self.rock = '21'
# storefronts
self.us = 'us'
self.jp = 'jp'
# search
self.search_term = 'nice for what'
def test_album(self):
results = am.album(self.born_to_run)
expected_name = 'Born to Run'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_album_relationship(self):
results = am.album_relationship(self.born_to_run, 'artists')
expected_name = 'Bruce Springsteen'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_albums(self):
results = am.albums([self.born_to_run, self.ready_to_die])
expected_count = 2
expected_type = 'albums'
actual_count = len(results['data'])
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
actual_type = results['data'][0]['type']
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_music_video(self):
results = am.music_video(self.rubber_soul)
expected_name = 'Rubber Soul (Documentary)'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_music_video_relationship(self):
results = am.music_video_relationship(self.rubber_soul, 'artists')
expected_name = 'The Beatles'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_music_videos(self):
results = am.music_videos([self.rubber_soul, self.sgt_pepper])
expected_count = 2
expected_type = 'music-videos'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
# ISRCs don't seem to work for music videos
# def test_music_videos_by_isrc(self):
def test_playlist(self):
results = am.playlist(self.janet_jackson)
expected_name = 'Janet Jackson: No.1 Songs'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_playlist_relationship(self):
results = am.playlist_relationship(self.eighties_pop, 'tracks') # playlist have 'tracks', artists have 'songs'
expected_type = 'songs'
actual_type = results['data'][0]['type']
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_playlists(self):
results = am.playlists([self.janet_jackson, self.eighties_pop])
expected_count = 2
expected_type = 'playlists'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_song(self):
results = am.song(self.xo_tour_life)
expected_name = 'XO TOUR Llif3'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_song_relationship(self):
results = am.song_relationship(self.xo_tour_life, 'artists')
expected_name = 'Lil Uzi Vert'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_songs(self):
results = am.songs([self.xo_tour_life, self.new_patek])
expected_count = 2
expected_type = 'songs'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_songs_by_isrc(self):
results = am.songs_by_isrc([self.gods_plan_isrc])
expected_name = "God's Plan"
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_artist(self):
results = am.artist(self.lil_pump)
expected_name = 'Lil Pump'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_artist_relationship(self):
results = am.artist_relationship(self.lil_pump, 'songs')
expected_type = 'songs'
actual_type = results['data'][0]['type']
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_artists(self):
results = am.artists([self.lil_pump, self.smokepurpp])
expected_count = 2
expected_type = 'artists'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_station(self):
results = am.station(self.alt)
expected_name = 'Alternative Station'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_stations(self):
results = am.stations([self.alt, self.pure_pop])
expected_count = 2
expected_type = 'stations'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_curator(self):
results = am.curator(self.large_up)
expected_name = 'LargeUp'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_curator_relationship(self):
results = am.curator_relationship(self.grand_ole_opry, 'playlists')
expected_type = 'playlists'
actual_type = results['data'][0]['type']
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_curators(self):
results = am.curators([self.large_up, self.grand_ole_opry])
expected_count = 2
expected_type = 'curators'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_apple_curator(self):
results = am.apple_curator(self.apple_alt)
expected_name = 'Apple Music Alternative'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_apple_curator_relationship(self):
results = am.apple_curator_relationship(self.apple_alt, 'playlists')
expected_type = 'playlists'
actual_type = results['data'][0]['type']
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_apple_curators(self):
results = am.apple_curators([self.apple_alt, self.live_nation_tv])
expected_count = 2
expected_type = 'apple-curators'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_genre(self):
results = am.genre(self.pop)
expected_name = 'Pop'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_genres(self):
results = am.genres([self.pop, self.rock])
expected_count = 2
expected_type = 'genres'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_genres_all(self):
results = am.genres_all()
expected_id = '34'
actual_id = results['data'][0]['id']
self.assertTrue(expected_id == actual_id, f"Expected ID: {expected_id}, Actual ID: {actual_id}")
def test_storefront(self):
results = am.storefront(self.us)
expected_name = 'United States'
actual_name = results['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_storefronts(self):
results = am.storefronts([self.us, self.jp])
expected_count = 2
expected_type = 'storefronts'
actual_count = len(results['data'])
actual_type = results['data'][0]['type']
self.assertTrue(expected_count == actual_count, f"Expected Count: {expected_count}, Actual Count: {actual_count}")
self.assertTrue(expected_type == actual_type, f"Expected Type: {expected_type}, Actual Type: {actual_type}")
def test_storefronts_all(self):
results = am.storefronts_all()
expected_id = 'dz'
actual_id = results['data'][0]['id']
self.assertTrue(expected_id == actual_id, f"Expected ID: {expected_id}, Actual ID: {actual_id}")
def test_search(self):
results = am.search(self.search_term, types=['songs'])
expected_name = 'Nice For What'
actual_name = results['results']['songs']['data'][0]['attributes']['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
def test_charts(self):
results = am.charts(types=['songs'], genre=self.pop)
expected_name = 'Top Songs'
actual_name = results['results']['songs'][0]['name']
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")
if __name__ == '__main__':
# These tests require API authorization, so need to read in keys and user token
keys = {}
with open('private_key.p8', 'r') as f:
keys['secret'] = f.read()
with open('keys.txt') as f:
for line in f:
name, val = line.partition('=')[::2]
keys[name.strip()] = val.strip()
test_loader = unittest.TestLoader()
user_token_file = 'music_user_token.txt'
if os.path.exists(user_token_file):
with open(user_token_file, 'r') as f:
music_user_token = f.read().strip()
am = AppleMusic(secret_key=keys['secret'], key_id=keys['keyID'], team_id=keys['teamID'], music_user_token=music_user_token)
print("Running tests with user token...")
user_test_suite = test_loader.loadTestsFromTestCase(UserTests)
else:
print("No music_user_token.txt found. Running normal tests...")
am = AppleMusic(secret_key=keys['secret'], key_id=keys['keyID'], team_id=keys['teamID'])
base_test_suite = test_loader.loadTestsFromTestCase(BaseTests)
all_tests = unittest.TestSuite()
all_tests.addTests(base_test_suite)
if 'user_test_suite' in locals():
all_tests.addTests(user_test_suite)
unittest.TextTestRunner(verbosity=2).run(all_tests)