コード例 #1
0
    def test_get_raise_for_status(self, mock_get, mock_rate_limit):
        max_retries = 7
        expected_result = "test_text"
        test_url = "example.com/route"

        api = PushshiftAPIMinimal(max_retries=max_retries,
                                  rate_limit_per_minute=self._rate_limit)

        # Test a subset of codes that should cause an outright failure
        for idx, status_code in enumerate(
            [400, 401, 403, 404, 405, 500, 502, 503, 504]):
            mock_get.return_value = MockResponse(
                status_code=status_code, text=json.dumps(expected_result))

            try:
                api._get(test_url)
                self.fail("call failed to trigger expected exception")
            except HTTPError as exc:
                self.assertIn(
                    "{} {} Error".format(
                        status_code,
                        "Server" if status_code >= 500 else "Client"),
                    str(exc),
                )

            expected_calls = max_retries * (idx + 1)

            # Ensure the correct count of retries triggered
            mock_get.assert_called_with(test_url, params={})
            self.assertEqual(expected_calls, mock_get.call_count)

            # Ensure the rate limit was applied
            self.assertEqual(expected_calls, mock_rate_limit.call_count)
            mock_rate_limit.assert_has_calls(
                [mock.call(idx) for idx in range(0, max_retries)])

            mock_get.return_value.raise_for_status.assert_called_once()
コード例 #2
0
    def test_get(self, mock_get, mock_rate_limit):
        max_retries = 7
        expected_result = "test_text"
        test_url = "example.com/route"

        api = PushshiftAPIMinimal(max_retries=max_retries,
                                  rate_limit_per_minute=self._rate_limit)

        mock_get.return_value = MockResponse(status_code=200,
                                             text=json.dumps(expected_result))

        self.assertEqual(expected_result, api._get(test_url))

        # Ensure the correct count of retries triggered
        mock_get.assert_called_with(test_url, params={})
        self.assertEqual(1, mock_get.call_count)

        # Ensure the rate limit was applied
        self.assertEqual(1, mock_rate_limit.call_count)
        mock_rate_limit.assert_has_calls([mock.call(0)])

        mock_get.return_value.raise_for_status.assert_called_once()
コード例 #3
0
    def test_get_429(self, mock_get, mock_rate_limit):
        max_retries = 7
        expected_result = "test_text"
        test_url = "example.com/route"

        api = PushshiftAPIMinimal(max_retries=max_retries,
                                  rate_limit_per_minute=self._rate_limit)

        mock_get.return_value = MockResponse(status_code=429,
                                             text=json.dumps(expected_result))

        self.assertEqual(expected_result, api._get(test_url))

        # Ensure the correct count of retries triggered
        mock_get.assert_called_with(test_url, params={})
        self.assertEqual(max_retries, mock_get.call_count)

        # Ensure the rate limit was applied
        self.assertEqual(max_retries, mock_rate_limit.call_count)
        mock_rate_limit.assert_has_calls(
            [mock.call(idx) for idx in range(0, max_retries)])

        # This is the key difference with code 429
        mock_get.return_value.raise_for_status.assert_not_called()