class TestExpandAPI(unittest.TestCase): def setUp(self): self.api = API() @patch.object(API, '_invoke_api', mocked_expand_API_request_failure(200)) def test_expand_api_response_200(self): api_response = self.api.expand(['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('expand')) self.assertTrue(len(api_response['expand']) == 2) @patch.object(API, '_invoke_api', mocked_expand_API_request_failure(403)) def test_expand_api_response_403(self): api_response = self.api.expand(['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_expand_API_request_failure(503)) def test_expand_api_response_503(self): api_response = self.api.expand(['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_expand_API_request_failure(500)) def test_expand_api_response_500(self): api_response = self.api.expand(['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'])