Exemplo n.º 1
0
    def test_iter(self):
        # missing union key parameter error:
        client = HttpClient(self.API_KEY)
        paginator = Paginator(client, self.URI)
        with self.assertRaises(ValueError):
            for i in paginator:
                pass

        # invalid/missing union key in response:
        responses = [
            self._generate_response(1, 2, page_num=2, page_index=0),
            self._generate_response(3,
                                    6,
                                    page_num=2,
                                    page_index=1,
                                    union_key=self.UNION_KEY + 'other'),
        ]
        expected_result = self._generate_response(1, 2).get(self.UNION_KEY)
        result, mock_request = self._get_iter_result(responses)
        self.assertEqual(result, expected_result)

        # no pages in result:
        responses = [
            self._generate_response(1, 1, page_num=1),
        ]
        result, mock_request = self._get_iter_result(responses)
        self.assertEqual(result, [])

        # page_num is not returned:
        responses = [
            self._generate_response(1, 1),
        ]
        result, mock_request = self._get_iter_result(responses)
        self.assertEqual(result, [])

        # single page response:
        page_response = self._generate_response(1, 2, page_num=1)
        expected_result = page_response.get(self.UNION_KEY)
        result, mock_request = self._get_iter_result([page_response])
        self.assertEqual(result, expected_result)

        # general case:
        page_num = 3
        responses = []
        for page_index in range(0, page_num):
            # making param names consecutive we can generate the expected
            # result easier
            response = self._generate_response(0,
                                               3,
                                               page_num=page_num,
                                               page_index=page_index)
            responses.append(response)

        expected_result = self._generate_response(0, 3).get(self.UNION_KEY)
        expected_result = expected_result * page_num

        result, mock_request = self._get_iter_result(responses)
        self.assertEqual(result, expected_result)
        self.assertEqual(mock_request.call_count, page_num)
Exemplo n.º 2
0
class TestHttp(unittest.TestCase):
    API_KEY = 'dummy'
    client = HttpClient(API_KEY)

    def test_default_headers(self):
        headers = self.client._get_default_headers()
        self.assertIn('Authorization', headers)
        self.assertTrue(self.API_KEY in headers['Authorization'])

    def test_get_url_default_version(self):
        uri = '/some/uri'
        data = {
            'param1': 'val1',
            'param2': 'val2',
        }

        url = self.client.get_url(uri)
        self.assertTrue(url.startswith(HttpClient.ENDPOINT_PREFIX))
        self.assertTrue(url.endswith('/some/uri'))
        self.assertTrue("/v1.2/" in url)
        self.assertFalse('?' in url)

        url = self.client.get_url(uri, data)
        self.assertTrue(url.startswith(HttpClient.ENDPOINT_PREFIX))
        self.assertTrue(url.endswith('/some/uri?param1=val1&param2=val2'))
        self.assertTrue('?' in url)

    def test_get_url_specified_version(self):
        uri = '/v1.4/some/uri'
        data = {
            'param1': 'val1',
            'param2': 'val2',
        }

        url = self.client.get_url(uri)
        self.assertTrue(url.startswith(HttpClient.ENDPOINT_PREFIX))
        self.assertTrue(url.endswith('/some/uri'))
        self.assertTrue("/v1.4/" in url)
        self.assertTrue("/v1.2/" not in url)
        self.assertFalse('?' in url)

    def test_is_error(self):
        response = {'error': 'some error'}
        result = self.client.is_error(response)
        self.assertEqual(result, True)

        response = {'param': 'val'}
        result = self.client.is_error(response)
        self.assertEqual(result, False)

    def test_get_exception_from_response(self):
        msg = 'some error'

        # general case for unknown code:
        code = 999
        exception = self.client.get_exception_from_response({
            'error': msg,
            'code': code
        })
        self.assertTrue(issubclass(exception.__class__, AppAnnieException))
        self.assertEqual(str(exception), msg)
        self.assertEqual(exception.ERROR_CODE, AppAnnieException.ERROR_CODE)

        # general case for known code:
        code = AppAnnieRateLimitException.ERROR_CODE
        exception = self.client.get_exception_from_response({
            'error': msg,
            'code': code
        })
        self.assertTrue(isinstance(exception, AppAnnieRateLimitException))
        self.assertEqual(str(exception), msg)
        self.assertEqual(exception.ERROR_CODE, code)

        # some 'not found' exceptions:
        code = 405
        exception = self.client.get_exception_from_response({
            'error': msg,
            'code': code
        })
        self.assertTrue(isinstance(exception, AppAnnieNotFoundException))

        code = 403
        exception = self.client.get_exception_from_response({
            'error': msg,
            'code': code
        })
        self.assertTrue(isinstance(exception, AppAnnieNotFoundException))

    @mock.patch('appannie.http.requests.get')
    def test_request(self, mock_get):
        uri = '/some/uri'
        data = {'breakdowns': 'b1+b2'}
        expected_result = {
            'code': 200,
            'list': [{
                'param:': 'vals'
            }, {
                'param2': 'vals2'
            }]
        }
        expected_url = self.client.get_url(uri, data)
        expected_headers = self.client._get_default_headers()

        response = mock.Mock()
        response.json.return_value = expected_result
        mock_get.return_value = response
        result = self.client.request(uri, data)
        self.assertEqual(expected_result, result)
        mock_get.assert_called_once_with(expected_url,
                                         headers=expected_headers)

    @mock.patch('appannie.http.requests.get')
    def test_request_error(self, mock_get):
        uri = '/some/uri'

        response = mock.Mock()
        response.json.side_effect = ValueError("")
        mock_get.return_value = response
        with self.assertRaises(AppAnnieException):
            self.client.request(uri)

        mock_get.reset_mock()
        mock_get.side_effect = requests.exceptions.RequestException
        with self.assertRaises(AppAnnieException):
            self.client.request(uri)

    @mock.patch('appannie.http.requests.get')
    def test_request_response_error(self, mock_get):
        uri = '/some/uri'
        expected_message = 'error message'
        expected_result = {'code': 500, 'error': expected_message}

        response = mock.Mock()
        response.json.return_value = expected_result
        mock_get.return_value = response
        with self.assertRaises(AppAnnieException) as context:
            self.client.request(uri)
            self.assertEqual(str(context.exception.message), expected_message)
Exemplo n.º 3
0
 def _get_iter_result(self, responses, mock_request):
     mock_request.side_effect = responses
     client = HttpClient(self.API_KEY)
     paginator = Paginator(client, self.URI, union_key=self.UNION_KEY)
     return [i for i in paginator], mock_request