def test_for_account(self): "Should only count Scrobbles by the supplied Account." account = AccountFactory() # Our one scrobble by this account: ScrobbleFactory(account=account, artist=self.artists[1], track=self.tracks[1]) artists = ditto_lastfm.top_artists(account=account) self.assertEqual(len(artists), 1) self.assertEqual(artists[0].scrobble_count, 1)
def test_for_year(self): "Should return only tracks from the requested year." # 2 scrobbles in this year: ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2015-01-01 12:00:00')) ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2015-12-31 12:00:00')) # 1 scrobble in this year: ScrobbleFactory(track=self.tracks[2], artist=self.artists[2], post_time=datetime_from_str('2016-03-01 12:00:00')) artists = ditto_lastfm.top_artists(period='year', date=datetime_from_str('2015-09-01 00:00:00')) self.assertEqual(len(artists), 1) self.assertEqual(artists[0].scrobble_count, 2)
def test_for_month(self): "Should return only tracks from the requested month." # 2 scrobbles in this month: ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2016-09-01 12:00:00')) ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2016-09-30 12:00:00')) # 1 scrobble in this month: ScrobbleFactory(track=self.tracks[2], artist=self.artists[2], post_time=datetime_from_str('2016-10-01 12:00:00')) artists = ditto_lastfm.top_artists(period='month', date=datetime_from_str('2016-09-15 00:00:00')) self.assertEqual(len(artists), 1) self.assertEqual(artists[0].scrobble_count, 2)
def test_for_day_with_date(self): "Should return only artists from the requested day, using date." # 2 scrobbles on this day: ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2016-10-01 00:00:00')) ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2016-10-01 23:59:59')) # 1 scrobble on this day: ScrobbleFactory(track=self.tracks[2], artist=self.artists[2], post_time=datetime_from_str('2016-10-02 12:00:00')) artists = ditto_lastfm.top_artists(period='day', date=datetime_from_str('2016-10-01 00:00:00').date()) self.assertEqual(len(artists), 1) self.assertEqual(artists[0].scrobble_count, 2)
def test_for_week_monday_start(self): "Should return only artists from the requested week (Monday start)." # 2 scrobbles on this day: # 2017-09-04 is a Monday ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2017-09-04 00:00:00')) ScrobbleFactory(track=self.tracks[1], artist=self.artists[1], post_time=datetime_from_str('2017-09-10 23:59:59')) # 1 scrobble on this day: ScrobbleFactory(track=self.tracks[2], artist=self.artists[2], post_time=datetime_from_str('2017-09-11 12:00:00')) artists = ditto_lastfm.top_artists(period='week', date=datetime_from_str('2017-09-07 00:00:00')) self.assertEqual(len(artists), 1) self.assertEqual(artists[0].scrobble_count, 2)
def lastfm_top_artists_card(limit=10, date=None, period='day'): """ Displays the most listened-to Artists for all accounts. """ card_title = 'Most listened-to artists' more = None if date is None: more = {'text': 'More at Last.fm', 'url': 'https://www.last.fm/user/gyford',} else: if period == 'day': pass # card_title += ' on this day' elif period == 'month': card_title += ' this month' elif period == 'year': card_title += ' this year' return { 'card_title': card_title, 'artist_list': top_artists(limit=limit, date=date, period=period), 'more': more, }
def test_period_error(self): "Should raise TypeError if invalid period is supplied." d = datetime_from_str('2016-10-24 12:00:00') with self.assertRaises(TypeError): ditto_lastfm.top_artists(date=d, period='bob')
def test_date_error(self): "Should raise TypeError if invalid date is supplied." with self.assertRaises(TypeError): ditto_lastfm.top_artists(date='bob', period='month')
def test_limit_error(self): "Should raise ValueError if `limit` is invalid." with self.assertRaises(ValueError): ditto_lastfm.top_artists(limit='bob')
def test_account_error(self): "Should raise TypeError if invalid Account is supplied." with self.assertRaises(TypeError): ditto_lastfm.top_artists(account='bob', limit=3)
def test_limit_all(self): "Should return all artists if `limit` is 'all'." artists = ditto_lastfm.top_artists(limit='all') self.assertEqual(len(artists), 11)
def test_limit(self): "Should return `limit` artists." artists = ditto_lastfm.top_artists(limit=3) self.assertEqual(len(artists), 3)
def test_default_limit(self): "By default should return 10 artists." artists = ditto_lastfm.top_artists() self.assertEqual(len(artists), 10)
def test_order(self): "The most scrobbled artists should be first" artists = ditto_lastfm.top_artists() self.assertEqual(artists[0], self.artists[1]) self.assertEqual(artists[1], self.artists[2])