def test_process(self): post = get_fixture('post_tag_one_friend.json') self.action.process(post) self.assertEqual(self.client.update.call_count, 1) post = get_fixture('post_tag_friend.json') self.action.process(post) self.assertEqual(self.client.update.call_count, 2)
def test_api_call_no_check_ratelimits(self, m): response = get_fixture('blocks_ids.json', True) m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response) self.client.ratelimiter.check_limit = MagicMock() self.client._api_call('blocks/ids', check_ratelimit=False) self.assertFalse(self.client.ratelimiter.check_limit.called)
def test_tag_needed(self): post = get_fixture('post_tag_one_friend.json') self.assertTrue(self.action.tag_needed(post)) post['full_text'] = " Testestset asdasda testesadst astagaring!" self.assertFalse(self.action.tag_needed(post)) post['full_text'] = " Testestset tag testesadst astagaring!" self.assertFalse(self.action.tag_needed(post))
def test_multiple_follows(self): TwitterConfig.get()['actions']['follow']['multiple'] = True post = get_fixture('post_multiple_mentions.json') self.action.process(post) self.assertEqual(self.client.follow.call_count, 2) for user in post['entities']['user_mentions']: self.client.follow.assert_any_call(user['screen_name'])
def test_api_call_decrease_remaining_calls(self, m): response = get_fixture('blocks_ids.json', True) m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response) self.client.ratelimiter.check_limit = MagicMock() before_remaining = self.client.ratelimiter['/blocks/ids']['remaining'] self.client._api_call('blocks/ids') self.assertEqual(before_remaining - 1, self.client.ratelimiter['/blocks/ids']['remaining'])
def test_update_ratelimits(self, m): # revert original function response = get_fixture('application_rate_limit_status.json', True) m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response) self.client.update_ratelimits(False) self.assertEqual(len(self.client.ratelimiter), 80) # check if percent is computed for x in self.client.ratelimiter.values(): self.assertIn('percent', x) self.assertEqual(x['limit'] / 100 * x['percent'], x['remaining'])
def test_update(self, m): response = get_fixture('statuses_update_reply.json', True) m.post('https://api.twitter.com/1.1/statuses/update.json', text=response) self.client.update('test', 2) self.assertTrue(m.called) self.assertEqual(m.call_count, 1) history = m.request_history[0] self.assertEqual(history.method, 'POST') self.assertIn('test', history.text) self.assertIn('2', history.text)
def test_check_limit_with_no_more_remaining(self): response = get_fixture('application_rate_limit_status.json') ratelimiter = RateLimiter() ratelimiter.refresh_limits(response['resources']) for limit in ratelimiter.values(): limit['percent'] = 0 limit['remaining'] = 0 limit['reset'] = 100 with freeze_time(datetime.datetime.fromtimestamp(0)): ratelimiter['/geo/search']['remaining'] = 0 ratelimiter['/geo/search']['percent'] = 0 with patch('time.sleep') as p: with self.assertRaises(RateLimiterExpired): ratelimiter.check_limit('geo/search') p.assert_called_with(100)
def test_get_tweet(self, m): response = get_fixture('statuses_show.json', True) m.get('https://api.twitter.com/1.1/statuses/show/210462857140252672.json', text=response) r = self.client.get_tweet("210462857140252672") self.assertEqual(r['id'], 210462857140252672)
def test_retweet_already_retweeted(self, m): response = get_fixture('error_already_retweeted.json', True) m.post('https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json', text=response) with self.assertRaises(TwitterClientRetweetedException): self.client.retweet("241259202004267009")
def test_retweet(self, m): response = get_fixture('statuses_retweet.json', True) m.post('https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json', text=response) r = self.client.retweet("241259202004267009") self.assertEqual(r['retweeted_status']['id'], 241259202004267009)
def test_update_ratelimits(self): response = get_fixture('application_rate_limit_status.json') ratelimiter = RateLimiter() ratelimiter.refresh_limits(response['resources']) self.assertEqual(len(ratelimiter), 80)
def test_search_tweets_with_language(self, m): response = get_fixture('search_tweets.json', True) m.get('https://api.twitter.com/1.1/search/tweets.json?&lang=en&q=210462857140252672&result_type=mixed&count=50', text=response) r = self.client.search_tweets("210462857140252672", 50, language="en") self.assertEqual(len(r), 4)
def test_favorite(self, m): response = get_fixture('favorites_create.json', True) m.post('https://api.twitter.com/1.1/favorites/create.json', text=response) r = self.client.favorite(243138128959913986) self.assertEqual(r['id'], 243138128959913986)
def test_get_friends_ids(self, m): response = get_fixture('friends_ids.json', True) m.get('https://api.twitter.com/1.1/friends/ids.json', text=response) r = self.client.get_friends_ids() self.assertEqual(len(r), 31)
def test_get_mentions_timeline_since_id(self, m): response = get_fixture('statuses_mentions_timeline_since_id.json', True) m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json?since_id=653965849364180992', text=response) r = self.client.get_mentions_timeline(since_id=653965849364180992) self.assertEqual(len(r), 1)
def test_api_call_error(self, m): response = get_fixture('error.json', True) m.get(requests_mock.ANY, text=response) with self.assertRaises(TwitterClientException): self.client._api_call('blocks/ids')
def test_get_mentions_timeline_count_1(self, m): response = get_fixture('statuses_mentions_timeline_count_1.json', True) m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=1', text=response) r = self.client.get_mentions_timeline(count=1) self.assertEqual(len(r), 1)
def test_unfollow(self, m): response = get_fixture('friendship_create.json', True) m.post('https://api.twitter.com/1.1/friendships/destroy.json', text=response) r = self.client.unfollow(1401881) self.assertEqual(r['id'], 1401881)
def setUp(self, m): load_fixture_config() response = get_fixture('application_rate_limit_status.json', True) m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response) self.client = TwitterClient('Consumer Key', "Consumer Secret", "Access Key", "Access Secret")