def test_ignores_account_with_no_creds(self, download): # Quietly prevents avatar files being fetched: download.side_effect = DownloadException('Oops') user_3 = UserFactory() account_3 = AccountFactory(user=user_3) self.add_response(body=self.make_response_body()) result = RecentTweetsFetcher().fetch() self.assertEqual(2, len(responses.calls))
def make_user_object(self, user_data, download): """"Creates/updates a User from API data, then fetches that User from the DB and returns it. """ # Quietly prevents avatar files being fetched: download.side_effect = DownloadException('Oops') saved_user = UserSaver().save_user(user_data, datetime_now()) return User.objects.get(twitter_id=12552)
def test_ignores_account_with_no_creds(self, download): # This will just stop us requesting avatars from Twitter: download.side_effect = DownloadException('Ooops') # Add a third Account that has no API credentials: account_3 = AccountFactory(user=UserFactory()) self.add_response(body=self.make_response_body()) result = FavoriteTweetsFetcher().fetch() # Should only have fetched faves for the two accounts with API creds: self.assertEqual(2, len(responses.calls))
def test_calls_fetch_and_save_avatar(self, fetch_avatar, download): "_fetch_and_save_avatar should be called with the User object." # Quietly prevents avatar files being fetched: download.side_effect = DownloadException('Oops') # Just make the mocked method return the User that's passed in: fetch_avatar.side_effect = lambda value: value user_data = self.make_user_data() saved_user = UserSaver().save_user(user_data, datetime_now()) fetch_avatar.assert_called_once_with(saved_user)
def test_fetches_multiple_pages_for_new(self, download): """Fetches subsequent pages until no results are returned.""" # This will just stop us requesting avatars from Twitter: download.side_effect = DownloadException('Ooops') self.account_1.last_favorite_id = 10 self.account_1.save() qs = { 'user_id': self.account_1.user.twitter_id, 'count': 200, 'since_id': 10 } self.add_response(body=self.make_response_body(), querystring=qs, match_querystring=True) qs['max_id'] = 99 self.add_response(body='[]', querystring=qs, match_querystring=True) with patch('time.sleep'): result = FavoriteTweetsFetcher(screen_name='jill').fetch() self.assertEqual(2, len(responses.calls))
def test_fetches_multiple_pages_for_new(self, download): "Fetches subsequent pages until no more recent results are returned." # Quietly prevents avatar files being fetched: download.side_effect = DownloadException('Oops') self.account_1.last_recent_id = 10 self.account_1.save() qs = { 'user_id': self.account_1.user.twitter_id, 'include_rts': 'true', 'count': 200, 'since_id': 10 } self.add_response(body=self.make_response_body(), querystring=qs, match_querystring=True) qs['max_id'] = 99 self.add_response(body='[]', querystring=qs, match_querystring=True) with patch('time.sleep'): result = RecentTweetsFetcher(screen_name='jill').fetch() self.assertEqual(2, len(responses.calls))
def test_raises_error_if_download_fails(self, download): "If download() raises an error, so does _fetch_and_save_file()" download.side_effect = DownloadException("Ooops") with self.assertRaises(FetchError): self.fetcher._fetch_and_save_file(self.photo_2, 'photo')
def test_raises_error_if_download_fails(self, download): "If download() raises an error, so does _fetch_and_save_file()" download.side_effect = DownloadException("Ooops") with self.assertRaises(FetchError): FetchFiles()._fetch_and_save_file(self.image, 'image')
def test_makes_one_api_call(self, download): # This will just stop us requesting avatars from Twitter: download.side_effect = DownloadException('Ooops') self.add_response(body=self.make_response_body(), method='POST') result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100]) self.assertEqual(1, len(responses.calls))
def test_api_requests_for_all_accounts(self, download): # This will just stop us requesting avatars from Twitter: download.side_effect = DownloadException('Ooops') self.add_response(body=self.make_response_body()) result = FavoriteTweetsFetcher().fetch() self.assertEqual(2, len(responses.calls))
def test_api_requests_for_all_accounts(self, download): # Quietly prevents avatar files being fetched: download.side_effect = DownloadException('Oops') self.add_response(body=self.make_response_body()) result = RecentTweetsFetcher().fetch() self.assertEqual(2, len(responses.calls))