Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
class ScrobblesMultiAccountFetcherTestCase(TestCase):
    """
    Testing the MultiAccount version by completely patching the standard
    Fetcher that it calls.
    """

    def setUp(self):
        self.account_1 = AccountFactory(api_key='1234', username='******')
        self.inactive_account = AccountFactory(api_key='2345', username='******',
                                                            is_active=False)
        self.account_2 = AccountFactory(api_key='3456', username='******')

        self.ScrobblesFetcher_patch = patch(
                                        'ditto.lastfm.fetch.ScrobblesFetcher')
        self.ScrobblesFetcher = self.ScrobblesFetcher_patch.start()

    def tearDown(self):
        self.ScrobblesFetcher_patch.stop()

    def test_uses_all_active_accounts_by_default(self):
        ScrobblesMultiAccountFetcher().fetch()
        self.ScrobblesFetcher.assert_has_calls([
            call(self.account_1),
            call().fetch(),
            call(self.account_2),
            call().fetch(),
        ])

    def test_uses_one_account(self):
        "If an account is supplied, only uses that."
        ScrobblesMultiAccountFetcher(username='******').fetch()
        self.ScrobblesFetcher.assert_has_calls([
            call(self.account_2),
            call().fetch(),
        ])

    def test_passes_kwargs_to_fetch(self):
        "Passes kwargs to the ScrobbleFetcher.fetch() method"
        ScrobblesMultiAccountFetcher(username='******').fetch(
                                                    fetch_type='days', days=3)
        self.ScrobblesFetcher.assert_has_calls([
            call(self.account_1),
            call().fetch(fetch_type='days', days=3),
        ])

    def test_raises_error_if_no_accounts_are_active(self):
        self.account_1.is_active = False
        self.account_1.save()
        self.account_2.is_active = False
        self.account_2.save()
        with self.assertRaises(FetchError):
            ScrobblesMultiAccountFetcher().fetch()

    def test_raises_error_if_supplied_account_does_not_exist(self):
        with self.assertRaises(FetchError):
            ScrobblesMultiAccountFetcher(username='******').fetch()

    def test_raises_error_if_supplied_account_is_inactive(self):
        with self.assertRaises(FetchError):
            ScrobblesMultiAccountFetcher(username='******').fetch()
Exemplo n.º 3
0
 def test_recent_scrobbles_order(self):
     "It returns the most recent first"
     account = AccountFactory()
     scrobble1 = ScrobbleFactory(account=account,
                         post_time=datetime_from_str('2015-08-11 12:00:00'))
     scrobble2 = ScrobbleFactory(account=account,
                         post_time=datetime_from_str('2015-08-12 12:00:00'))
     self.assertEqual(account.get_recent_scrobbles()[0], scrobble2)
Exemplo n.º 4
0
    def setUp(self):
        self.account_1 = AccountFactory(api_key='1234', username='******')
        self.inactive_account = AccountFactory(api_key='2345', username='******',
                                                            is_active=False)
        self.account_2 = AccountFactory(api_key='3456', username='******')

        self.ScrobblesFetcher_patch = patch(
                                        'ditto.lastfm.fetch.ScrobblesFetcher')
        self.ScrobblesFetcher = self.ScrobblesFetcher_patch.start()
Exemplo n.º 5
0
 def test_recent_scrobbles_correct_account(self):
     "It only returns scrobbles for itself, not another account"
     account1 = AccountFactory()
     account2 = AccountFactory()
     scrobble1 = ScrobbleFactory(account=account1)
     scrobble2 = ScrobbleFactory(account=account2)
     scrobbles = account1.get_recent_scrobbles()
     self.assertEqual(len(scrobbles), 1)
     self.assertEqual(scrobbles[0], scrobble1)
Exemplo n.º 6
0
 def setUp(self):
     self.account1 = AccountFactory()
     self.account2 = AccountFactory()
     # Some for account1 in 2015 and 2016:
     scrobbles1 = ScrobbleFactory.create_batch(3,
                         post_time=datetime_from_str('2015-01-01 12:00:00'),
                         account=self.account1)
     scrobbles2 = ScrobbleFactory.create_batch(2,
                         post_time=datetime_from_str('2016-01-01 12:00:00'),
                         account=self.account1)
     # And one for account2 in 2015:
     scrobble3 = ScrobbleFactory(account=self.account2,
                         post_time=datetime_from_str('2015-01-01 12:00:00'))
Exemplo n.º 7
0
 def test_context(self):
     "Sends the correct data to the templates"
     accounts = AccountFactory.create_batch(3)
     response = self.client.get(reverse('lastfm:scrobble_list'))
     self.assertIn('account_list', response.context)
     self.assertEqual(len(response.context['account_list']), 3)
     self.assertIn('scrobble_list', response.context)
Exemplo n.º 8
0
 def test_args_account(self):
     "Should only return Scrobbles by supplied Account"
     account = AccountFactory()
     scrobble2 = ScrobbleFactory(artist=self.artist, account=account)
     artists = Artist.objects.with_scrobble_counts(account=account)
     self.assertEqual(len(artists), 1)
     self.assertEqual(artists[0].scrobble_count, 1)
Exemplo n.º 9
0
 def test_no_credentials(self):
     "__init__ should return error if Account has no API creds."
     account = AccountFactory(api_key='')
     result = ScrobblesFetcher(account=account).fetch()
     self.assertFalse(result['success'])
     self.assertEqual(result['messages'],
                     ['Account has no API credentials'])
Exemplo n.º 10
0
 def test_context(self):
     "Sends the correct data to the templates"
     accounts = AccountFactory.create_batch(3)
     response = self.client.get(reverse('lastfm:scrobble_list'))
     self.assertIn('account_list', response.context)
     self.assertEqual(len(response.context['account_list']), 3)
     self.assertIn('scrobble_list', response.context)
Exemplo n.º 11
0
 def test_args_account(self):
     "Should only return Scrobbles by supplied Account"
     account = AccountFactory()
     album2 = AlbumFactory()
     scrobble2 = ScrobbleFactory(account=account, album=album2)
     albums = Album.objects.with_scrobble_counts(account=account)
     self.assertEqual(len(albums), 1)
     self.assertEqual(albums[0], album2)
Exemplo n.º 12
0
 def setUp(self):
     self.account1 = AccountFactory()
     self.account2 = AccountFactory()
     # Some for account1 on 1st Oct:
     self.scrobble1 = ScrobbleFactory(
                         post_time=datetime_from_str('2016-10-01 12:00:00'),
                         account=self.account1)
     self.scrobble2 = ScrobbleFactory(
                         post_time=datetime_from_str('2016-10-01 12:05:00'),
                         account=self.account1)
     # One for account1 on 2nd Oct:
     self.scrobble3 = ScrobbleFactory(
                         post_time=datetime_from_str('2016-10-02 00:00:01'),
                         account=self.account1)
     # And one for account2 on 1st Oct:
     self.scrobble4 = ScrobbleFactory(account=self.account2,
                         post_time=datetime_from_str('2016-10-01 13:00:00'))
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
 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.artist1, track=self.tracks[1])
     tracks = ditto_lastfm.top_tracks(account=account, limit='all')
     self.assertEqual(len(tracks), 1)
     self.assertEqual(tracks[0], self.tracks[1])
     self.assertEqual(tracks[0].scrobble_count, 1)
Exemplo n.º 15
0
 def test_args_account(self):
     "Should only return Scrobbles by supplied Account"
     account = AccountFactory()
     scrobble2 = ScrobbleFactory(account=account,
                                 track=self.track,
                                 artist=self.artist)
     tracks = Track.objects.with_scrobble_counts(account=account)
     self.assertEqual(len(tracks), 1)
     self.assertEqual(tracks[0], self.track)
     self.assertEqual(tracks[0].scrobble_count, 1)
Exemplo n.º 16
0
 def test_context(self):
     "Sends the correct data to the templates"
     accounts = AccountFactory.create_batch(3)
     scrobble1 = ScrobbleFactory(account=accounts[0])
     scrobble2 = ScrobbleFactory(account=accounts[1])
     response = self.client.get(reverse('lastfm:home'))
     self.assertIn('account_list', response.context)
     self.assertEqual(len(response.context['account_list']), 3)
     self.assertIn('counts', response.context)
     self.assertIn('scrobbles', response.context['counts'])
     self.assertEqual(response.context['counts']['scrobbles'], 2)
Exemplo n.º 17
0
    def setUp(self):
        self.fetcher = ScrobblesFetcher(account=AccountFactory(
                                        username='******', realname='Bob Ferris'))

        # Patch the _send_request() method:
        self.send_request_patch = patch.object(
                                            ScrobblesFetcher, '_send_request')
        self.send_request = self.send_request_patch.start()
        self.send_request.return_value = []
        self.sleep_patch = patch('time.sleep')
        self.sleep = self.sleep_patch.start()
Exemplo n.º 18
0
 def test_context(self):
     "Sends the correct data to the templates"
     accounts = AccountFactory.create_batch(3)
     scrobble1 = ScrobbleFactory(account=accounts[0])
     scrobble2 = ScrobbleFactory(account=accounts[1])
     response = self.client.get(reverse('lastfm:home'))
     self.assertIn('account_list', response.context)
     self.assertEqual(len(response.context['account_list']), 3)
     self.assertIn('counts', response.context)
     self.assertIn('scrobbles', response.context['counts'])
     self.assertEqual(response.context['counts']['scrobbles'], 2)
Exemplo n.º 19
0
 def test_context(self):
     "Sends the correct data to the templates"
     accounts = AccountFactory.create_batch(2)
     albums = AlbumFactory.create_batch(3)
     response = self.client.get(reverse('lastfm:album_list'))
     self.assertIn('account_list', response.context)
     self.assertEqual(len(response.context['account_list']), 2)
     self.assertIn('album_list', response.context)
     self.assertEqual(len(response.context['album_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')
Exemplo n.º 20
0
 def test_context(self):
     "Sends the correct data to the templates"
     accounts = AccountFactory.create_batch(2)
     albums = AlbumFactory.create_batch(3)
     response = self.client.get(reverse('lastfm:album_list'))
     self.assertIn('account_list', response.context)
     self.assertEqual(len(response.context['account_list']), 2)
     self.assertIn('album_list', response.context)
     self.assertEqual(len(response.context['album_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')
Exemplo n.º 21
0
 def setUp(self):
     self.account1 = AccountFactory()
     self.account2 = AccountFactory()
     scrobbles1 = ScrobbleFactory.create_batch(11, account=self.account1)
     self.scrobble2 = ScrobbleFactory(account=self.account2)
Exemplo n.º 22
0
 def test_permalink(self):
     account = AccountFactory(username='******')
     self.assertEqual(account.permalink, 'http://www.last.fm/user/gyford')
Exemplo n.º 23
0
 def test_has_credentials(self):
     account = AccountFactory(api_key='1234')
     self.assertTrue(account.has_credentials())
     account = AccountFactory(api_key='')
     self.assertFalse(account.has_credentials())
Exemplo n.º 24
0
 def test_recent_scrobbles(self):
     "By default it returns 10 scrobbles"
     account = AccountFactory()
     scrobbles = ScrobbleFactory.create_batch(11, account=account)
     self.assertEqual(len(account.get_recent_scrobbles()), 10)
Exemplo n.º 25
0
 def test_absolute_url(self):
     account = AccountFactory(username='******')
     self.assertEqual(account.get_absolute_url(), '/lastfm/user/gyford/')
Exemplo n.º 26
0
 def test_recent_scrobbles_limit(self):
     "It returns `limit` scrobbles"
     account = AccountFactory()
     scrobbles = ScrobbleFactory.create_batch(5, account=account)
     self.assertEqual(len(account.get_recent_scrobbles(limit=3)), 3)
Exemplo n.º 27
0
 def setUp(self):
     self.out = StringIO()
     self.out_err = StringIO()
     self.account = AccountFactory(username='******', api_key='1234')
Exemplo n.º 28
0
 def test_will_not_fetch_inactive_accounts(self):
     account = AccountFactory(is_active=False, username='******')
     result = ScrobblesFetcher(account=account).fetch()
     self.assertFalse(result['success'])
     self.assertEqual(result['messages'],
                     ['The Account terry is currently marked as inactive.'])
Exemplo n.º 29
0
 def setUp(self):
     self.account_1 = AccountFactory(api_key='1234', username='******')
     self.account_2 = AccountFactory(api_key='3456', username='******')
Exemplo n.º 30
0
 def test_ordering(self):
     account_1 = AccountFactory(username='******', realname='Alan')
     account_2 = AccountFactory(username='******', realname='Zak')
     accounts = Account.objects.all()
     self.assertEqual(accounts[0], account_2)
     self.assertEqual(accounts[1], account_1)
Exemplo n.º 31
0
 def setUp(self):
     self.account = AccountFactory(username='******', api_key='1234')
     self.fetcher = ScrobblesFetcher(account=self.account)
     self.sleep_patch = patch('time.sleep')
     self.sleep = self.sleep_patch.start()
Exemplo n.º 32
0
 def test_str(self):
     account = AccountFactory(realname='Phil Gyford')
     self.assertEqual(str(account), 'Phil Gyford')