forked from pylast/pylast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pylast.py
executable file
·1986 lines (1549 loc) · 58.8 KB
/
test_pylast.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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
from flaky import flaky
import os
import pytest
from random import choice
import time
import unittest
import pylast
def load_secrets():
secrets_file = "test_pylast.yaml"
if os.path.isfile(secrets_file):
import yaml # pip install pyyaml
with open(secrets_file, "r") as f: # see example_test_pylast.yaml
doc = yaml.load(f)
else:
doc = {}
try:
doc["username"] = os.environ['PYLAST_USERNAME'].strip()
doc["password_hash"] = os.environ['PYLAST_PASSWORD_HASH'].strip()
doc["api_key"] = os.environ['PYLAST_API_KEY'].strip()
doc["api_secret"] = os.environ['PYLAST_API_SECRET'].strip()
except KeyError:
pytest.skip("Missing environment variables: PYLAST_USERNAME etc.")
return doc
@flaky(max_runs=5, min_passes=1)
class TestPyLast(unittest.TestCase):
secrets = None
def unix_timestamp(self):
return int(time.time())
def setUp(self):
if self.__class__.secrets is None:
self.__class__.secrets = load_secrets()
self.username = self.__class__.secrets["username"]
password_hash = self.__class__.secrets["password_hash"]
API_KEY = self.__class__.secrets["api_key"]
API_SECRET = self.__class__.secrets["api_secret"]
self.network = pylast.LastFMNetwork(
api_key=API_KEY, api_secret=API_SECRET,
username=self.username, password_hash=password_hash)
def test_scrobble(self):
# Arrange
artist = "Test Artist"
title = "Test Title"
timestamp = self.unix_timestamp()
lastfm_user = self.network.get_user(self.username)
# Act
self.network.scrobble(artist=artist, title=title, timestamp=timestamp)
# Assert
# limit=2 to ignore now-playing:
last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0]
self.assertEqual(str(last_scrobble.track.artist), str(artist))
self.assertEqual(str(last_scrobble.track.title), str(title))
self.assertEqual(str(last_scrobble.timestamp), str(timestamp))
def test_unscrobble(self):
# Arrange
artist = "Test Artist 2"
title = "Test Title 2"
timestamp = self.unix_timestamp()
library = pylast.Library(user=self.username, network=self.network)
self.network.scrobble(artist=artist, title=title, timestamp=timestamp)
lastfm_user = self.network.get_user(self.username)
# Act
library.remove_scrobble(
artist=artist, title=title, timestamp=timestamp)
# Assert
# limit=2 to ignore now-playing:
last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0]
self.assertNotEqual(str(last_scrobble.timestamp), str(timestamp))
def test_add_album(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
album = self.network.get_album("Test Artist", "Test Album")
# Act
library.add_album(album)
# Assert
my_albums = library.get_albums()
for my_album in my_albums:
value = (album == my_album[0])
if value:
break
self.assertTrue(value)
def test_remove_album(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Pick an artist with plenty of albums
artist = self.network.get_top_artists(limit=1)[0].item
albums = artist.get_top_albums()
# Pick a random one to avoid problems running concurrent tests
album = choice(albums)[0]
library.add_album(album)
# Act
library.remove_album(album)
# Assert
my_albums = library.get_albums()
for my_album in my_albums:
value = (album == my_album[0])
if value:
break
self.assertFalse(value)
def test_add_artist(self):
# Arrange
artist = "Test Artist 2"
library = pylast.Library(user=self.username, network=self.network)
# Act
library.add_artist(artist)
# Assert
artists = library.get_artists()
for artist in artists:
value = (str(artist[0]) == "Test Artist 2")
if value:
break
self.assertTrue(value)
def test_remove_artist(self):
# Arrange
# Get plenty of artists
artists = self.network.get_top_artists()
# Pick a random one to avoid problems running concurrent tests
my_artist = choice(artists).item
library = pylast.Library(user=self.username, network=self.network)
library.add_artist(my_artist)
# Act
library.remove_artist(my_artist)
# Assert
artists = library.get_artists()
for artist in artists:
value = (artist[0] == my_artist)
if value:
break
self.assertFalse(value)
def test_get_venue(self):
# Arrange
venue_name = "Last.fm Office"
country_name = "United Kingdom"
# Act
venue_search = self.network.search_for_venue(venue_name, country_name)
venue = venue_search.get_next_page()[0]
# Assert
self.assertEqual(str(venue.id), "8778225")
def test_get_user_registration(self):
# Arrange
username = "RJ"
user = self.network.get_user(username)
# Act
registered = user.get_registered()
# Assert
# Just check date because of timezones
self.assertIn(u"2002-11-20 ", registered)
def test_get_user_unixtime_registration(self):
# Arrange
username = "RJ"
user = self.network.get_user(username)
# Act
unixtime_registered = user.get_unixtime_registered()
# Assert
# Just check date because of timezones
self.assertEqual(unixtime_registered, u"1037793040")
def test_get_genderless_user(self):
# Arrange
# Currently test_user has no gender set:
lastfm_user = self.network.get_user("test_user")
# Act
gender = lastfm_user.get_gender()
# Assert
self.assertIsNone(gender)
def test_get_countryless_user(self):
# Arrange
# Currently test_user has no country set:
lastfm_user = self.network.get_user("test_user")
# Act
country = lastfm_user.get_country()
# Assert
self.assertIsNone(country)
def test_love(self):
# Arrange
artist = "Test Artist"
title = "Test Title"
track = self.network.get_track(artist, title)
lastfm_user = self.network.get_user(self.username)
# Act
track.love()
# Assert
loved = lastfm_user.get_loved_tracks(limit=1)
self.assertEqual(str(loved[0].track.artist), "Test Artist")
self.assertEqual(str(loved[0].track.title), "Test Title")
def test_unlove(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
title = "Test Title"
track = pylast.Track(artist, title, self.network)
lastfm_user = self.network.get_user(self.username)
track.love()
# Act
track.unlove()
# Assert
loved = lastfm_user.get_loved_tracks(limit=1)
if len(loved): # OK to be empty but if not:
self.assertNotEqual(str(loved.track.artist), "Test Artist")
self.assertNotEqual(str(loved.track.title), "Test Title")
def test_get_100_albums(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Act
albums = library.get_albums(limit=100)
# Assert
self.assertGreaterEqual(len(albums), 0)
def test_get_limitless_albums(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Act
albums = library.get_albums(limit=None)
# Assert
self.assertGreaterEqual(len(albums), 0)
def test_user_equals_none(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
value = (lastfm_user is None)
# Assert
self.assertFalse(value)
def test_user_not_equal_to_none(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
value = (lastfm_user is not None)
# Assert
self.assertTrue(value)
def test_now_playing_user_with_no_scrobbles(self):
# Arrange
# Currently test-account has no scrobbles:
user = self.network.get_user('test-account')
# Act
current_track = user.get_now_playing()
# Assert
self.assertIsNone(current_track)
def test_love_limits(self):
# Arrange
# Currently test-account has at least 23 loved tracks:
user = self.network.get_user("test-user")
# Act/Assert
self.assertEqual(len(user.get_loved_tracks(limit=20)), 20)
self.assertLessEqual(len(user.get_loved_tracks(limit=100)), 100)
self.assertGreaterEqual(len(user.get_loved_tracks(limit=None)), 23)
self.assertGreaterEqual(len(user.get_loved_tracks(limit=0)), 23)
def test_update_now_playing(self):
# Arrange
artist = "Test Artist"
title = "Test Title"
album = "Test Album"
track_number = 1
lastfm_user = self.network.get_user(self.username)
# Act
self.network.update_now_playing(
artist=artist, title=title, album=album, track_number=track_number)
# Assert
current_track = lastfm_user.get_now_playing()
self.assertIsNotNone(current_track)
self.assertEqual(str(current_track.title), "Test Title")
self.assertEqual(str(current_track.artist), "Test Artist")
def test_libre_fm(self):
# Arrange
username = self.__class__.secrets["username"]
password_hash = self.__class__.secrets["password_hash"]
# Act
network = pylast.LibreFMNetwork(
password_hash=password_hash, username=username)
tags = network.get_top_tags(limit=1)
# Assert
self.assertGreater(len(tags), 0)
self.assertIsInstance(tags[0], pylast.TopItem)
def test_album_tags_are_topitems(self):
# Arrange
albums = self.network.get_user('RJ').get_top_albums()
# Act
tags = albums[0].item.get_top_tags(limit=1)
# Assert
self.assertGreater(len(tags), 0)
self.assertIsInstance(tags[0], pylast.TopItem)
def helper_is_thing_hashable(self, thing):
# Arrange
things = set()
# Act
things.add(thing)
# Assert
self.assertIsNotNone(thing)
self.assertEqual(len(things), 1)
def test_album_is_hashable(self):
# Arrange
album = self.network.get_album("Test Artist", "Test Album")
# Act/Assert
self.helper_is_thing_hashable(album)
def test_artist_is_hashable(self):
# Arrange
test_artist = self.network.get_artist("Test Artist")
artist = test_artist.get_similar(limit=2)[0].item
self.assertIsInstance(artist, pylast.Artist)
# Act/Assert
self.helper_is_thing_hashable(artist)
def test_country_is_hashable(self):
# Arrange
country = self.network.get_country("Italy")
# Act/Assert
self.helper_is_thing_hashable(country)
def test_metro_is_hashable(self):
# Arrange
metro = self.network.get_metro("Helsinki", "Finland")
# Act/Assert
self.helper_is_thing_hashable(metro)
def test_event_is_hashable(self):
# Arrange
user = self.network.get_user("RJ")
event = user.get_past_events(limit=1)[0]
# Act/Assert
self.helper_is_thing_hashable(event)
def test_group_is_hashable(self):
# Arrange
group = self.network.get_group("Audioscrobbler Beta")
# Act/Assert
self.helper_is_thing_hashable(group)
def test_library_is_hashable(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Act/Assert
self.helper_is_thing_hashable(library)
def test_playlist_is_hashable(self):
# Arrange
playlist = pylast.Playlist(
user="RJ", playlist_id="1k1qp_doglist", network=self.network)
# Act/Assert
self.helper_is_thing_hashable(playlist)
def test_tag_is_hashable(self):
# Arrange
tag = self.network.get_top_tags(limit=1)[0]
# Act/Assert
self.helper_is_thing_hashable(tag)
def test_track_is_hashable(self):
# Arrange
artist = self.network.get_artist("Test Artist")
track = artist.get_top_tracks()[0].item
self.assertIsInstance(track, pylast.Track)
# Act/Assert
self.helper_is_thing_hashable(track)
def test_user_is_hashable(self):
# Arrange
artist = self.network.get_artist("Test Artist")
user = artist.get_top_fans(limit=1)[0].item
self.assertIsInstance(user, pylast.User)
# Act/Assert
self.helper_is_thing_hashable(user)
def test_venue_is_hashable(self):
# Arrange
venue_id = "8778225" # Last.fm office
venue = pylast.Venue(venue_id, self.network)
# Act/Assert
self.helper_is_thing_hashable(venue)
def test_xspf_is_hashable(self):
# Arrange
xspf = pylast.XSPF(
uri="lastfm://playlist/1k1qp_doglist", network=self.network)
# Act/Assert
self.helper_is_thing_hashable(xspf)
def test_invalid_xml(self):
# Arrange
# Currently causes PCDATA invalid Char value 25
artist = "Blind Willie Johnson"
title = "It's nobody's fault but mine"
# Act
search = self.network.search_for_track(artist, title)
total = search.get_total_result_count()
# Assert
self.assertGreaterEqual(int(total), 0)
def test_user_play_count_in_track_info(self):
# Arrange
artist = "Test Artist"
title = "Test Title"
track = pylast.Track(
artist=artist, title=title,
network=self.network, username=self.username)
# Act
count = track.get_userplaycount()
# Assert
self.assertGreaterEqual(count, 0)
def test_user_loved_in_track_info(self):
# Arrange
artist = "Test Artist"
title = "Test Title"
track = pylast.Track(
artist=artist, title=title,
network=self.network, username=self.username)
# Act
loved = track.get_userloved()
# Assert
self.assertIsNotNone(loved)
self.assertIsInstance(loved, bool)
self.assertNotIsInstance(loved, str)
def test_album_in_recent_tracks(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
# limit=2 to ignore now-playing:
track = lastfm_user.get_recent_tracks(limit=2)[0]
# Assert
self.assertTrue(hasattr(track, 'album'))
def test_album_in_artist_tracks(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
track = lastfm_user.get_artist_tracks(artist="Test Artist")[0]
# Assert
self.assertTrue(hasattr(track, 'album'))
def test_enable_rate_limiting(self):
# Arrange
self.assertFalse(self.network.is_rate_limited())
# Act
self.network.enable_rate_limit()
then = time.time()
# Make some network call, limit not applied first time
self.network.get_user(self.username)
# Make a second network call, limiting should be applied
self.network.get_top_artists()
now = time.time()
# Assert
self.assertTrue(self.network.is_rate_limited())
self.assertGreaterEqual(now - then, 0.2)
def test_disable_rate_limiting(self):
# Arrange
self.network.enable_rate_limit()
self.assertTrue(self.network.is_rate_limited())
# Act
self.network.disable_rate_limit()
# Make some network call, limit not applied first time
self.network.get_user(self.username)
# Make a second network call, limiting should be applied
self.network.get_top_artists()
# Assert
self.assertFalse(self.network.is_rate_limited())
# Commented out because (a) it'll take a long time and (b) it strangely
# fails due Last.fm's complaining of hitting the rate limit, even when
# limited to one call per second. The ToS allows 5 calls per second.
# def test_get_all_scrobbles(self):
# # Arrange
# lastfm_user = self.network.get_user("RJ")
# self.network.enable_rate_limit() # this is going to be slow...
# # Act
# tracks = lastfm_user.get_recent_tracks(limit=None)
# # Assert
# self.assertGreaterEqual(len(tracks), 0)
def helper_past_events_have_valid_ids(self, thing):
# Act
events = thing.get_past_events()
# Assert
self.helper_assert_events_have_valid_ids(events)
def helper_upcoming_events_have_valid_ids(self, thing):
# Act
events = thing.get_upcoming_events()
# Assert
self.helper_assert_events_have_valid_ids(events)
def helper_assert_events_have_valid_ids(self, events):
# Assert
# If fails, add past/future event for user/Test Artist:
self.assertGreaterEqual(len(events), 1)
for event in events[:2]: # checking first two should be enough
self.assertIsInstance(event.get_headliner(), pylast.Artist)
def test_artist_upcoming_events_returns_valid_ids(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act/Assert
self.helper_upcoming_events_have_valid_ids(artist)
def test_user_past_events_returns_valid_ids(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act/Assert
self.helper_past_events_have_valid_ids(lastfm_user)
def test_user_recommended_events_returns_valid_ids(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
events = lastfm_user.get_upcoming_events()
# Assert
self.helper_assert_events_have_valid_ids(events)
def test_user_upcoming_events_returns_valid_ids(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act/Assert
self.helper_upcoming_events_have_valid_ids(lastfm_user)
def test_venue_past_events_returns_valid_ids(self):
# Arrange
venue_id = "8778225" # Last.fm office
venue = pylast.Venue(venue_id, self.network)
# Act/Assert
self.helper_past_events_have_valid_ids(venue)
def test_venue_upcoming_events_returns_valid_ids(self):
# Arrange
venue_id = "8778225" # Last.fm office
venue = pylast.Venue(venue_id, self.network)
# Act/Assert
self.helper_upcoming_events_have_valid_ids(venue)
def test_pickle(self):
# Arrange
import pickle
lastfm_user = self.network.get_user(self.username)
filename = str(self.unix_timestamp()) + ".pkl"
# Act
with open(filename, "wb") as f:
pickle.dump(lastfm_user, f)
with open(filename, "rb") as f:
loaded_user = pickle.load(f)
os.remove(filename)
# Assert
self.assertEqual(lastfm_user, loaded_user)
def test_bio_published_date(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
bio = artist.get_bio_published_date()
# Assert
self.assertIsNotNone(bio)
self.assertGreaterEqual(len(bio), 1)
def test_bio_content(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
bio = artist.get_bio_content(language="en")
# Assert
self.assertIsNotNone(bio)
self.assertGreaterEqual(len(bio), 1)
def test_bio_summary(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
bio = artist.get_bio_summary(language="en")
# Assert
self.assertIsNotNone(bio)
self.assertGreaterEqual(len(bio), 1)
def test_album_wiki_content(self):
# Arrange
album = pylast.Album("Test Artist", "Test Album", self.network)
# Act
wiki = album.get_wiki_content()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_album_wiki_published_date(self):
# Arrange
album = pylast.Album("Test Artist", "Test Album", self.network)
# Act
wiki = album.get_wiki_published_date()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_album_wiki_summary(self):
# Arrange
album = pylast.Album("Test Artist", "Test Album", self.network)
# Act
wiki = album.get_wiki_summary()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_track_wiki_content(self):
# Arrange
track = pylast.Track("Test Artist", "Test Title", self.network)
# Act
wiki = track.get_wiki_content()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_track_wiki_summary(self):
# Arrange
track = pylast.Track("Test Artist", "Test Title", self.network)
# Act
wiki = track.get_wiki_summary()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_lastfm_network_name(self):
# Act
name = str(self.network)
# Assert
self.assertEqual(name, "Last.fm Network")
def helper_validate_results(self, a, b, c):
# Assert
self.assertIsNotNone(a)
self.assertIsNotNone(b)
self.assertIsNotNone(c)
self.assertGreaterEqual(len(a), 0)
self.assertGreaterEqual(len(b), 0)
self.assertGreaterEqual(len(c), 0)
self.assertEqual(a, b)
self.assertEqual(b, c)
def helper_validate_cacheable(self, thing, function_name):
# Arrange
# get thing.function_name()
func = getattr(thing, function_name, None)
# Act
result1 = func(limit=1, cacheable=False)
result2 = func(limit=1, cacheable=True)
result3 = func(limit=1)
# Assert
self.helper_validate_results(result1, result2, result3)
def test_cacheable_artist_get_shouts(self):
# Arrange
artist = self.network.get_artist("Test Artist")
# Act/Assert
self.helper_validate_cacheable(artist, "get_shouts")
def test_cacheable_event_get_shouts(self):
# Arrange
user = self.network.get_user("RJ")
event = user.get_past_events(limit=1)[0]
# Act/Assert
self.helper_validate_cacheable(event, "get_shouts")
def test_cacheable_track_get_shouts(self):
# Arrange
track = self.network.get_top_tracks()[0].item
# Act/Assert
self.helper_validate_cacheable(track, "get_shouts")
def test_cacheable_group_get_members(self):
# Arrange
group = self.network.get_group("Audioscrobbler Beta")
# Act/Assert
self.helper_validate_cacheable(group, "get_members")
def test_cacheable_library(self):
# Arrange
library = pylast.Library(self.username, self.network)
# Act/Assert
self.helper_validate_cacheable(library, "get_albums")
self.helper_validate_cacheable(library, "get_artists")
self.helper_validate_cacheable(library, "get_tracks")
def test_cacheable_user_artist_tracks(self):
# Arrange
lastfm_user = self.network.get_authenticated_user()
# Act
result1 = lastfm_user.get_artist_tracks("Test Artist", cacheable=False)
result2 = lastfm_user.get_artist_tracks("Test Artist", cacheable=True)
result3 = lastfm_user.get_artist_tracks("Test Artist")
# Assert
self.helper_validate_results(result1, result2, result3)
def test_cacheable_user(self):
# Arrange
lastfm_user = self.network.get_authenticated_user()
# Act/Assert
self.helper_validate_cacheable(lastfm_user, "get_friends")
self.helper_validate_cacheable(lastfm_user, "get_loved_tracks")
self.helper_validate_cacheable(lastfm_user, "get_neighbours")
self.helper_validate_cacheable(lastfm_user, "get_past_events")
self.helper_validate_cacheable(lastfm_user, "get_recent_tracks")
self.helper_validate_cacheable(lastfm_user, "get_recommended_artists")
self.helper_validate_cacheable(lastfm_user, "get_recommended_events")
self.helper_validate_cacheable(lastfm_user, "get_shouts")
def test_geo_get_events_in_location(self):
# Arrange
# Act
events = self.network.get_geo_events(
location="London", tag="blues", limit=1)
# Assert
self.assertEqual(len(events), 1)
event = events[0]
self.assertIsInstance(event, pylast.Event)
self.assertIn(event.get_venue().location['city'],
["London", "Camden"])
def test_geo_get_events_in_latlong(self):
# Arrange
# Act
events = self.network.get_geo_events(
latitude=53.466667, longitude=-2.233333, distance=5, limit=1)
# Assert
self.assertEqual(len(events), 1)
event = events[0]
self.assertIsInstance(event, pylast.Event)
self.assertEqual(event.get_venue().location['city'], "Manchester")
def test_geo_get_events_festival(self):
# Arrange
# Act
events = self.network.get_geo_events(
location="Reading", festivalsonly=True, limit=1)
# Assert
self.assertEqual(len(events), 1)
event = events[0]
self.assertIsInstance(event, pylast.Event)
self.assertEqual(event.get_venue().location['city'], "Reading")
def helper_dates_valid(self, dates):
# Assert
self.assertGreaterEqual(len(dates), 1)
self.assertIsInstance(dates[0], tuple)
(start, end) = dates[0]
self.assertLess(start, end)
def test_get_metro_weekly_chart_dates(self):
# Arrange
# Act
dates = self.network.get_metro_weekly_chart_dates()
# Assert
self.helper_dates_valid(dates)
def helper_geo_chart(self, function_name, expected_type=pylast.Artist):
# Arrange
metro = self.network.get_metro("Madrid", "Spain")
dates = self.network.get_metro_weekly_chart_dates()
(from_date, to_date) = dates[0]
# get metro.function_name()
func = getattr(metro, function_name, None)
# Act
chart = func(from_date=from_date, to_date=to_date, limit=1)
# Assert
self.assertEqual(len(chart), 1)
self.assertIsInstance(chart[0], pylast.TopItem)
self.assertIsInstance(chart[0].item, expected_type)
def test_get_metro_artist_chart(self):
# Arrange/Act/Assert
self.helper_geo_chart("get_artist_chart")
def test_get_metro_hype_artist_chart(self):
# Arrange/Act/Assert
self.helper_geo_chart("get_hype_artist_chart")
def test_get_metro_unique_artist_chart(self):
# Arrange/Act/Assert
self.helper_geo_chart("get_unique_artist_chart")
def test_get_metro_track_chart(self):
# Arrange/Act/Assert
self.helper_geo_chart("get_track_chart", expected_type=pylast.Track)
def test_get_metro_hype_track_chart(self):
# Arrange/Act/Assert
self.helper_geo_chart(
"get_hype_track_chart", expected_type=pylast.Track)
def test_get_metro_unique_track_chart(self):
# Arrange/Act/Assert
self.helper_geo_chart(
"get_unique_track_chart", expected_type=pylast.Track)
def test_geo_get_metros(self):
# Arrange
# Act
metros = self.network.get_metros(country="Poland")
# Assert
self.assertGreaterEqual(len(metros), 1)
self.assertIsInstance(metros[0], pylast.Metro)
self.assertEqual(metros[0].get_country(), "Poland")
def test_geo_get_top_artists(self):
# Arrange
# Act
artists = self.network.get_geo_top_artists(
country="United Kingdom", limit=1)
# Assert
self.assertEqual(len(artists), 1)
self.assertIsInstance(artists[0], pylast.TopItem)
self.assertIsInstance(artists[0].item, pylast.Artist)
def test_geo_get_top_tracks(self):
# Arrange
# Act
tracks = self.network.get_geo_top_tracks(
country="United Kingdom", location="Manchester", limit=1)
# Assert
self.assertEqual(len(tracks), 1)
self.assertIsInstance(tracks[0], pylast.TopItem)
self.assertIsInstance(tracks[0].item, pylast.Track)
def test_metro_class(self):
# Arrange
# Act
metro = self.network.get_metro("Bergen", "Norway")
# Assert
self.assertEqual(metro.get_name(), "Bergen")
self.assertEqual(metro.get_country(), "Norway")
self.assertEqual(str(metro), "Bergen, Norway")
self.assertEqual(metro, pylast.Metro("Bergen", "Norway", self.network))
self.assertNotEqual(
metro,
pylast.Metro("Wellington", "New Zealand", self.network))
def test_get_album_play_links(self):
# Arrange
album1 = self.network.get_album("Portishead", "Dummy")
album2 = self.network.get_album("Radiohead", "OK Computer")
albums = [album1, album2]
# Act
links = self.network.get_album_play_links(albums)
# Assert
self.assertIsInstance(links, list)
self.assertEqual(len(links), 2)
self.assertIn("spotify:album:", links[0])