def test_max_tweets_zero(self): api = twitter.TwitterAPI(self.credentials) corpus = api.search_content('hello', max_tweets=0) self.assertEqual(len(corpus), 20) # 20 is the #tweets in cache corpus = api.search_authors('hello', max_tweets=0) self.assertEqual(len(corpus), 20) # 20 is the #tweets in cache
def test_error_reporting(self): with unittest.mock.patch('tweepy.Cursor.items') as mock: mock.side_effect = tweepy.TweepError('', Response(500)) error_callback = unittest.mock.Mock() api = twitter.TwitterAPI(self.credentials, on_error=error_callback) api.search_authors('hello', max_tweets=5) self.assertEqual(error_callback.call_count, 1)
def test_search_author_collecting(self): api = twitter.TwitterAPI(self.credentials) with unittest.mock.patch('tweepy.Cursor', MyCursor) as mock: corpus = api.search_authors('hello', max_tweets=5) self.assertEqual(len(corpus), 5) # MyCursor2 so we get different tweets with unittest.mock.patch('tweepy.Cursor', MyCursor2) as mock: corpus = api.search_authors('world', max_tweets=5, collecting=True) self.assertEqual(len(corpus), 10)
def test_search_callbacks(self): self.checker = 0 def on_progress(total, current): self.assertTrue(total % 20 == 0) self.checker += 1 api = twitter.TwitterAPI(self.credentials, on_progress=on_progress) api.search_content('hello', max_tweets=20, lang='en') self.assertEqual(self.checker, 1)
def update_api(self, key): if key: self.Error.key_missing.clear() self.api = twitter.TwitterAPI( key, on_error=self.Error.api, on_rate_limit=self.Error.rate_limit, should_break=self.search.should_break, on_progress=self.update_tweets_num) else: self.api = None
def test_search_callbacks(self): self.checker = 0 def on_progress(_): self.checker += 1 api = twitter.TwitterAPI(self.credentials) api.search_content( "hello", max_tweets=20, lang="en", callback=on_progress ) self.assertEqual(self.checker, 1)
def test_breaking(self): count = 0 def should_break(): nonlocal count if count == 1: return True count += 1 return False api = twitter.TwitterAPI(self.credentials, should_break=should_break) corpus = api.search_content('hello', max_tweets=10) self.assertEqual(len(corpus), 1)
def update_api(self, key): if key: self.Error.key_missing.clear() self.api = twitter.TwitterAPI(key) else: self.api = None
def setUp(self): self.credentials = get_credentials() self.api = twitter.TwitterAPI(self.credentials)
def test_search_author(self): api = twitter.TwitterAPI(self.credentials) corpus = api.search_authors('hello', max_tweets=5) self.assertEqual(len(corpus), 5)
def test_empty_content(self): api = twitter.TwitterAPI(self.credentials) corpus = api.search_content('', max_tweets=10, allow_retweets=False) self.assertEqual(len(corpus), 10)
def test_report(self): api = twitter.TwitterAPI(self.credentials) api.search_content('hello', max_tweets=5, collecting=True) self.assertEqual(len(api.report()), 1) api.search_content('world', collecting=True) self.assertEqual(len(api.report()), 2)
def setUp(self): self.client = twitter.TwitterAPI("key")
def test_rate_limit_reporting(self): with unittest.mock.patch("tweepy.Cursor.items") as mock: mock.side_effect = tweepy.TweepError("", Response(429)) api = twitter.TwitterAPI(self.credentials) with self.assertRaises(TweepError): api.search_authors("hello", max_tweets=5)