Пример #1
0
class TestClicksAPI(unittest.TestCase):

    def setUp(self):
        self.api = API()

    @patch.object(API, '_invoke_api', mocked_clicks_API_response(200))
    def test_expand_api_response_200(self):
        api_response = self.api.clicks(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 200)
        self.assertTrue(api_response.has_key('clicks'))
        self.assertTrue(len(api_response['clicks']) == 2)

    @patch.object(API, '_invoke_api', mocked_clicks_API_response(403))
    def test_expand_api_response_403(self):
        api_response = self.api.clicks(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 403)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Rate limit exceeded' in api_response['error_message'])

    @patch.object(API, '_invoke_api', mocked_clicks_API_response(503))
    def test_expand_api_response_503(self):
        api_response = self.api.clicks(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 503)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Unknow error or temporary unavailability' in api_response['error_message'])

    @patch.object(API, '_invoke_api', mocked_clicks_API_response(500))
    def test_expand_api_response_500(self):
        api_response = self.api.clicks(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 500)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Invalid request format' in api_response['error_message'])