Пример #1
0
    def test_tweets_params(self):
        """Test tweets API call with params"""

        setup_http_server()

        client = TwitterClient("aaa", max_items=2)
        group_tweets = client.tweets("query", since_id=1, max_id=1005163131042193407,
                                     geocode="37.781157 -122.398720 1km", lang="eu",
                                     include_entities=False, result_type=TWEET_TYPE_POPULAR)
        group_tweets = [tweets for tweets in group_tweets]

        self.assertEqual(len(group_tweets), 2)

        expected = {
            'q': ['query'],
            'since_id': ['1'],
            'max_id': ['1005163131042193407'],
            'count': ['2'],
            'include_entities': ['False'],
            'result_type': ['popular'],
            'geocode': ['37.781157 -122.398720 1km'],
            'lang': ['eu']
        }

        self.assertDictEqual(httpretty.last_request().querystring, expected)
        self.assertEqual(httpretty.last_request().headers["Authorization"], "Bearer aaa")
Пример #2
0
    def test_init(self):
        """Test initialization"""

        client = TwitterClient('aaaa', max_items=10)
        self.assertEqual(client.api_key, 'aaaa')
        self.assertEqual(client.max_items, 10)
        self.assertFalse(client.sleep_for_rate)
        self.assertEqual(client.min_rate_to_sleep, MIN_RATE_LIMIT)
        self.assertTrue(client.ssl_verify)

        client = TwitterClient('aaaa',
                               max_items=10,
                               sleep_for_rate=True,
                               min_rate_to_sleep=4)
        self.assertEqual(client.api_key, 'aaaa')
        self.assertEqual(client.max_items, 10)
        self.assertTrue(client.sleep_for_rate)
        self.assertEqual(client.min_rate_to_sleep, 4)

        # Max rate limit is never overtaken
        client = TwitterClient('aaaa',
                               max_items=10,
                               sleep_for_rate=True,
                               min_rate_to_sleep=100000000,
                               ssl_verify=False)
        self.assertEqual(client.min_rate_to_sleep, client.MAX_RATE_LIMIT)
        self.assertFalse(client.ssl_verify)
Пример #3
0
    def test_calculate_time_to_reset(self, mock_utcnow):
        """Test whether the time to reset is zero if the sleep time is negative"""

        mock_utcnow.return_value = datetime.datetime(2017, 1, 1,
                                                     tzinfo=dateutil.tz.tzutc())

        client = TwitterClient("aaa", max_items=2)
        client.rate_limit_reset_ts = 0

        time_to_reset = client.calculate_time_to_reset()
        self.assertEqual(time_to_reset, 0)
Пример #4
0
    def test_too_many_requests(self):
        """Test if a Retry error is raised"""

        setup_http_server(status=429)

        client = TwitterClient("aaa", max_items=2, sleep_time=0.1)
        start = float(time.time())
        expected = start + (sum([i * client.sleep_time for i in range(client.MAX_RETRIES)]))

        events = client.tweets('query')
        with self.assertRaises(requests.exceptions.RetryError):
            _ = [event for event in events]

        end = float(time.time())
        self.assertGreater(end, expected)
Пример #5
0
    def test_tweets_no_params(self):
        """Test tweets API call with no params"""

        setup_http_server()

        client = TwitterClient("aaa", max_items=2)
        group_tweets = client.tweets("query")
        group_tweets = [tweets for tweets in group_tweets]

        self.assertEqual(len(group_tweets), 2)

        # Check requests
        expected = {
            'q': ['query'],
            'count': ['2'],
            'max_id': ['1005163131042193407'],
            'include_entities': ['True'],
            'result_type': ['mixed']
        }

        self.assertDictEqual(httpretty.last_request().querystring, expected)
        self.assertEqual(httpretty.last_request().headers["Authorization"], "Bearer aaa")
Пример #6
0
    def test_sanitize_for_archive(self):
        """Test whether the sanitize method works properly"""

        url = "http://example.com"
        headers = {'Authorization': 'Bearer my-token'}
        payload = {'q': 'query',
                   'count': 100,
                   'include_entities': True,
                   'result_type': 'mixed'}

        s_url, s_headers, s_payload = TwitterClient.sanitize_for_archive(url, headers, payload)

        self.assertEqual(url, s_url)
        self.assertEqual({}, s_headers)
        self.assertEqual(payload, s_payload)