def setUp(self): bob = AccountFactory(username='******') terry = AccountFactory(username='******') self.artist1 = ArtistFactory() self.track1 = TrackFactory(artist=self.artist1) self.album1 = AlbumFactory(artist=self.artist1) self.artist2 = ArtistFactory() self.track2 = TrackFactory(artist=self.artist2) bobs1 = ScrobbleFactory.create_batch(2, account=bob, track=self.track1, artist=self.artist1, album=self.album1) bobs2 = ScrobbleFactory.create_batch(5, account=bob, track=self.track2, artist=self.artist2) terrys1 = ScrobbleFactory.create_batch(3, account=terry, track=self.track1, artist=self.artist1, album=self.album1) terrys2 = ScrobbleFactory.create_batch(7, account=terry, track=self.track2, artist=self.artist2)
def test_get_most_recent_scrobble(self): artist = ArtistFactory() scrobble1 = ScrobbleFactory(artist=artist, post_time=datetime_from_str('2015-08-11 12:00:00')) scrobble2 = ScrobbleFactory(artist=artist, post_time=datetime_from_str('2015-08-12 12:00:00')) self.assertEqual(artist.get_most_recent_scrobble(), scrobble2)
def setUp(self): self.artist1 = ArtistFactory() self.tracks = TrackFactory.create_batch(11, artist=self.artist1) self.albums = AlbumFactory.create_batch(11, artist=self.artist1) # Scrobble each album/track once: for idx, track in enumerate(self.tracks): ScrobbleFactory(artist=self.artist1, track=track, album=self.albums[idx]) # Extra scrobbles. # For artist1, tracks[1] will be 1st, tracks[2] will be 2nd: ScrobbleFactory.create_batch(2, artist=self.artist1, track=self.tracks[1], album=self.albums[1]) ScrobbleFactory.create_batch(1, artist=self.artist1, track=self.tracks[2], album=self.albums[2]) # But artist2 has a more scrobbled album: self.artist2 = ArtistFactory() self.track2 = TrackFactory(artist=self.artist2) self.album2 = AlbumFactory(artist=self.artist2) ScrobbleFactory.create_batch(4, artist=self.artist2, track=self.track2, album=self.album2)
def test_get_top_tracks_limit(self): "Only returns the number of top tracks requested" artist = ArtistFactory() tracks = TrackFactory.create_batch(4, artist=artist) for t in tracks: ScrobbleFactory(artist=artist, track=t) top_tracks = artist.get_top_tracks(limit=3) self.assertEqual(len(top_tracks), 3)
def test_get_top_albums_limit(self): "Only returns the number of top albums requested" artist = ArtistFactory() albums = AlbumFactory.create_batch(4, artist=artist) tracks = TrackFactory.create_batch(4, artist=artist) for idx, t in enumerate(tracks): ScrobbleFactory(artist=artist, track=t, album=albums[idx]) top_albums = artist.get_top_albums(limit=3) self.assertEqual(len(top_albums), 3)
def test_get_top_tracks(self): "Returns all tracks, ordered by most-scrobbled first" artist = ArtistFactory() tracks = TrackFactory.create_batch(3, artist=artist) # Scrobble all tracks once... for t in tracks: ScrobbleFactory(artist=artist, track=t) # ... but scrobble one of them an additional time: ScrobbleFactory(artist=artist, track=tracks[1]) top_tracks = artist.get_top_tracks() self.assertEqual(len(top_tracks), 3) self.assertEqual(top_tracks[0], tracks[1])
def test_get_top_albums(self): "Returns all albums, ordered by most-scrobbled first" artist = ArtistFactory() albums = AlbumFactory.create_batch(3, artist=artist) # NB, Tracks & Albums are only associated via Scrobbles, not directly: tracks = TrackFactory.create_batch(3, artist=artist) # Scrobble all tracks+albums once... for idx, t in enumerate(tracks): ScrobbleFactory(artist=artist, track=t, album=albums[idx]) # ... but scrobble one of them an additional time: ScrobbleFactory(artist=artist, track=tracks[1], album=albums[1]) top_albums = artist.get_top_albums() self.assertEqual(len(top_albums), 3) self.assertEqual(top_albums[0], albums[1])
def test_ignores_case_when_finding_artist(self): "Should match an Artist with slug 'Lou+Reed' with 'lou+reed'." ArtistFactory(slug='lou+reed', name='Lou Reed', mbid='') self.add_recent_tracks_response() results = self.fetcher.fetch() artists = Artist.objects.filter(name='Lou Reed') self.assertEqual(len(artists), 1)
def test_long_titles(self): "If artist + track is longer than 255 characters, it gets truncated." a = ArtistFactory(name='Drew Danburry') t = TrackFactory(artist=a, name="Jerry Spinelli and Patricia Polacco or Every Moment of Every Day We Are Faced With the Decision as to Whether We Will Continue Doing What We Are Doing or Choose a Different Way to Do Things. This, Essentially, Means That It Is Also Our Fault When...") scrobble = ScrobbleFactory(artist=a, track=t) self.maxDiff = None # To show the full diff. self.assertEqual(scrobble.title, "Drew Danburry – Jerry Spinelli and Patricia Polacco or Every Moment of Every Day We Are Faced With the Decision as to Whether We Will Continue Doing What We Are Doing or Choose a Different Way to Do Things. This, Essentially, Means That It Is Also Our…")
def test_absolute_url(self): artist = ArtistFactory(slug='life+without+buildings', original_slug='Life+Without+Buildings') album = AlbumFactory(artist=artist, slug='any+other+city', original_slug='Any+Other+City') self.assertEqual(album.get_absolute_url(), '/lastfm/music/life+without+buildings/any+other+city/')
def test_permalink(self): artist = ArtistFactory(slug='life+without+buildings', original_slug='Life+Without+Buildings') album = AlbumFactory(artist=artist, slug='any+other+city', original_slug='Any+Other+City') self.assertEqual(album.permalink, 'http://www.last.fm/music/Life+Without+Buildings/Any+Other+City')
def setUp(self): artist = ArtistFactory() track = TrackFactory(artist=artist) album = AlbumFactory(artist=artist) # Scrobbled 4 times on different days, by 2 different Accounts: scrobble1 = ScrobbleFactory( artist=artist, track=track, album=album, post_time=datetime_from_str('2015-08-11 12:00:00')) scrobble2 = ScrobbleFactory( artist=artist, track=track, album=album, post_time=datetime_from_str('2015-08-12 12:00:00')) scrobble2 = ScrobbleFactory( artist=artist, track=track, album=album, post_time=datetime_from_str('2015-08-13 12:00:00')) scrobble4 = ScrobbleFactory( artist=artist, track=track, album=album, post_time=datetime_from_str('2015-08-14 12:00:00'))
def test_permalink(self): artist = ArtistFactory(slug='the+mountain+goats', original_slug='The+Mountain+Goats') track = TrackFactory(slug='dance+music',original_slug='Dance+Music', artist=artist) self.assertEqual(track.permalink, 'http://www.last.fm/music/The+Mountain+Goats/_/Dance+Music')
def test_absolute_url(self): artist = ArtistFactory(slug='the+mountain+goats', original_slug='The+Mountain+Goats') track = TrackFactory(slug='dance+music', original_slug='Dance+Music', artist=artist) self.assertEqual(track.get_absolute_url(), '/lastfm/music/the+mountain+goats/_/dance+music/')
def setUp(self): self.album1 = AlbumFactory() self.artist1 = ArtistFactory() self.tracks = TrackFactory.create_batch(11, artist=self.artist1) # Extra scrobbles. # For artist1, tracks[1] will be 1st, tracks[2] will be 2nd: ScrobbleFactory.create_batch(2, album=self.album1, artist=self.artist1, track=self.tracks[1]) ScrobbleFactory.create_batch(1, album=self.album1, artist=self.artist1, track=self.tracks[2]) # But artist2 has a more-scrobbled Track: self.album2 = AlbumFactory() self.artist2 = ArtistFactory() self.track2 = TrackFactory(artist=self.artist2) ScrobbleFactory.create_batch(4, album=self.album2, artist=self.artist2, track=self.track2)
def test_get_scrobble_count(self): artist = ArtistFactory() track = TrackFactory(artist=artist) # Scrobbled 2 times: ScrobbleFactory.create_batch(2, artist=artist, track=track) # And another Scrobble with different artist/track: ScrobbleFactory() self.assertEqual(track.get_scrobble_count(), 2)
def test_get_most_recent_scrobble(self): artist = ArtistFactory() track = TrackFactory(artist=artist) album = AlbumFactory(artist=artist) scrobble1 = ScrobbleFactory(artist=artist, track=track, album=album, post_time=datetime_from_str('2015-08-11 12:00:00')) scrobble2 = ScrobbleFactory(artist=artist, track=track, album=album, post_time=datetime_from_str('2015-08-12 12:00:00')) self.assertEqual(album.get_most_recent_scrobble(), scrobble2)
def test_args_artist(self): "Should only return Scrobbles by supplied Artist" artist2 = ArtistFactory() track2 = TrackFactory(artist=artist2) scrobble2 = ScrobbleFactory(artist=artist2, track=track2) tracks = Track.objects.with_scrobble_counts(artist=artist2) self.assertEqual(len(tracks), 1) self.assertEqual(tracks[0].artist, artist2) self.assertEqual(tracks[0].scrobble_count, 1)
def test_scrobble_counts(self): "Should add a `scrobble_count` aggregate Count to each Track." # Add another track, with 1 scrobble, as well as the one in setUp(): artist2 = ArtistFactory() track2 = TrackFactory(artist=artist2) scrobble2 = ScrobbleFactory(artist=artist2, track=track2) tracks = Track.objects.with_scrobble_counts() self.assertEqual(len(tracks), 2) self.assertEqual(tracks[0].scrobble_count, 4) self.assertEqual(tracks[1].scrobble_count, 1)
def test_scrobble_counts(self): "Should add a `scrobble_count` aggregate Count to each Album." # Add another album, with 1 scrobble, as well as the one in setUp(): artist2 = ArtistFactory() track2 = TrackFactory(artist=artist2) album2 = AlbumFactory(artist=artist2) scrobble2 = ScrobbleFactory(artist=artist2, track=track2, album=album2) albums = Album.objects.with_scrobble_counts() self.assertEqual(len(albums), 2) self.assertEqual(albums[0].scrobble_count, 4) self.assertEqual(albums[1].scrobble_count, 1)
def test_updates_existing_artist(self): "Doesn't create a new Artist if it already exists" ArtistFactory(slug='lou+reed', mbid='') self.add_recent_tracks_response() results = self.fetcher.fetch() artists = Artist.objects.filter(slug='lou+reed') self.assertEqual(len(artists), 1) self.assertEqual(artists[0].slug, 'lou+reed') self.assertEqual(artists[0].original_slug, 'Lou+Reed') self.assertEqual(artists[0].name, 'Lou Reed') self.assertEqual(artists[0].mbid, '9d1ebcfe-4c15-4d18-95d3-d919898638a1')
def test_updates_existing_album(self): "Doesn't create a new Album if it already exists" loureed = ArtistFactory(slug='lou+reed') AlbumFactory(artist=loureed, slug='transformer') self.add_recent_tracks_response() results = self.fetcher.fetch() albums = Album.objects.filter(slug='transformer') self.assertEqual(len(albums), 1) self.assertEqual(albums[0].name,'Transformer') self.assertEqual(albums[0].slug,'transformer') self.assertEqual(albums[0].original_slug,'Transformer') self.assertEqual(albums[0].mbid,'4ee40d97-630c-3f0d-9ea2-d49fa253c354') self.assertEqual(albums[0].artist, loureed)
def test_updates_existing_track(self): "Doesn't create a new Track if it already exists" loureed = ArtistFactory(slug='lou+reed') TrackFactory(artist=loureed, slug='make+up') self.add_recent_tracks_response() results = self.fetcher.fetch() tracks = Track.objects.filter(slug='make+up') self.assertEqual(len(tracks), 1) self.assertEqual(tracks[0].name,'Make Up') self.assertEqual(tracks[0].slug,'make+up') self.assertEqual(tracks[0].original_slug,'Make+Up') self.assertEqual(tracks[0].mbid,'8e73b23a-6a01-4743-b414-047974f66e22') self.assertEqual(tracks[0].artist, loureed)
def test_default_days(self): "Has correct scrobble count context when all days are viewed, the default." artist = ArtistFactory() album = AlbumFactory(artist=artist) scrobble1 = ScrobbleFactory( artist=artist, album=album, post_time=datetime_from_str('2012-10-01 12:00:00')) scrobble2 = ScrobbleFactory( artist=artist, album=album, post_time=datetime_from_str('2016-10-01 12:00:00')) response = self.client.get(reverse('lastfm:album_list')) self.assertEqual(response.context['album_list'][0].scrobble_count, 2)
def test_7_days(self): "Has correct scrobble count context when a restricted number of days are viewed." artist = ArtistFactory() track = TrackFactory(artist=artist) scrobble1 = ScrobbleFactory( artist=artist, track=track, post_time=datetime_from_str('2012-10-01 12:00:00')) scrobble2 = ScrobbleFactory( artist=artist, track=track, post_time=datetime_from_str('2016-10-01 12:00:00')) response = self.client.get("%s?days=7" % reverse('lastfm:track_list')) self.assertEqual(response.context['track_list'][0].scrobble_count, 1)
def test_context(self): "Sends the correct data to the templates" accounts = AccountFactory.create_batch(2) artists = ArtistFactory.create_batch(3) response = self.client.get(reverse('lastfm:artist_list')) self.assertIn('account_list', response.context) self.assertEqual(len(response.context['account_list']), 2) self.assertIn('artist_list', response.context) self.assertEqual(len(response.context['artist_list']), 3) self.assertIn('valid_days', response.context) self.assertEqual(response.context['valid_days'], ['7', '30', '90', '180', '365', 'all',]) self.assertIn('current_days', response.context) self.assertEqual(response.context['current_days'], 'all')
def test_args_album(self): "Only aggregates scrobble_counts for a particular album." artist2 = ArtistFactory() album1 = AlbumFactory() album2 = AlbumFactory() track = TrackFactory() ScrobbleFactory(album=album1, artist=self.artist, track=track) ScrobbleFactory(album=album1, artist=self.artist, track=track) ScrobbleFactory(album=album1, artist=artist2, track=track) ScrobbleFactory(album=album2, artist=self.artist, track=track) artists = Artist.objects.with_scrobble_counts(album=album1) self.assertEqual(len(artists), 2) self.assertEqual(artists[0].scrobble_count, 2) self.assertEqual(artists[1].scrobble_count, 1)
def setUp(self): self.artists = ArtistFactory.create_batch(11) self.tracks = [] # Each artist has one track, scrobbled once: for artist in self.artists: track = TrackFactory(artist=artist) self.tracks.append(track) ScrobbleFactory(artist=artist, track=track) # Extra scrobbles. # For tracks[1] will be 1st, tracks[2] will be 2nd: ScrobbleFactory.create_batch(2, artist=self.artists[1], track=self.tracks[1]) ScrobbleFactory.create_batch(1, artist=self.artists[2], track=self.tracks[2])
def test_tracks(self): artist = ArtistFactory() track1 = TrackFactory(artist=artist) track2 = TrackFactory(artist=artist) album = AlbumFactory(artist=artist) # Scrobble track1 once, track2 twice: ScrobbleFactory(artist=artist, track=track1, album=album) ScrobbleFactory(artist=artist, track=track2, album=album) ScrobbleFactory(artist=artist, track=track2, album=album) tracks = album.tracks self.assertEqual(len(tracks), 2) self.assertEqual(tracks[0], track2) self.assertEqual(tracks[1], track1) self.assertEqual(tracks[0].scrobble_count, 2) self.assertEqual(tracks[1].scrobble_count, 1)
def test_albums(self): artist = ArtistFactory() track = TrackFactory(artist=artist) album1 = AlbumFactory(artist=artist) album2 = AlbumFactory(artist=artist) # Scrobble album1 once, album2 twice: ScrobbleFactory(artist=artist, track=track, album=album1) ScrobbleFactory(artist=artist, track=track, album=album2) ScrobbleFactory(artist=artist, track=track, album=album2) albums = track.albums self.assertEqual(len(albums), 2) self.assertEqual(albums[0], album2) self.assertEqual(albums[1], album1) self.assertEqual(albums[0].scrobble_count, 2) self.assertEqual(albums[1].scrobble_count, 1)
def test_updates_existing_scrobbles(self): "Updates existing scrobble objects" # Make our existing scrobble: artist = ArtistFactory(slug='lou+reed') track = TrackFactory(artist=artist, slug='make+up') post_time = datetime.datetime.strptime( '2016-09-22 09:23:33', '%Y-%m-%d %H:%M:%S' ).replace(tzinfo=pytz.utc) scrobble = ScrobbleFactory(account=self.account, track=track, post_time=post_time) self.add_recent_tracks_response() results = self.fetcher.fetch(fetch_type='all') scrobbles = Scrobble.objects.all() # We have this many finished scrobbles in our JSON fixture: self.assertEqual(len(scrobbles), 3) scrobble_reloaded = Scrobble.objects.get(artist=artist) self.assertEqual(scrobble.pk, scrobble_reloaded.pk)
def test_absolute_url(self): mbid = '80fe34d0-6b4f-4ccd-81c3-281ab37f0451' artist = ArtistFactory(slug='tom+waits', original_slug='Tom+Waits') self.assertEqual(artist.get_absolute_url(), '/lastfm/music/tom+waits/')
def test_get_scrobble_count(self): artist = ArtistFactory() scrobbles = ScrobbleFactory.create_batch(3, artist=artist) self.assertEqual(artist.get_scrobble_count(), 3)