def test_connect_retries_can_succeed(self): # Should return True total_retries = 5 class SucceedsAtLast(BaseHTTPSConnMock): tries = 0 def connect(self, *args, **kwargs): # The retry mechanism in POST_data will recreate # the connection object completely, so we need # to store the retries in the class, _not_ the # instance if self.__class__.tries < (total_retries - 1): self.__class__.tries += 1 raise socket.timeout else: return True def getresponse(self): return BaseHTTPSRespMock( json.dumps({ "success": True, "failed_events": [] })) result = send_signifai.POST_data(auth_key="", data=self.events, attempts=total_retries, httpsconn=SucceedsAtLast) self.assertTrue(result)
def test_post_request_generation(self): # Should return True AND no test case in TestEventGeneration # may fail test_case = self API_KEY = "TEST_API_KEY" def request_gen_test(method, uri, body, headers): test_case.assertEqual(uri, send_signifai.DEFAULT_POST_URI) test_case.assertEqual(body, json.dumps(test_case.events)) test_case.assertEqual(headers['Authorization'], "Bearer {KEY}".format(KEY=API_KEY)) test_case.assertEqual(headers['Content-Type'], "application/json") test_case.assertEqual(headers['Accept'], "application/json") test_case.assertEqual(method, "POST") with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: m['request'].side_effect = request_gen_test mockresp = unittest_mock.Mock() mockresp.status = 200 mockresp.read.return_value = json.dumps({ "success": True, "failed_events": [] }) m['getresponse'].return_value = mockresp result = send_signifai.POST_data(auth_key=API_KEY, data=self.events) self.assertTrue(result)
def test_post_request_generation(self): # Should return True AND no test case in TestEventGeneration # may fail test_case = self API_KEY = "TEST_API_KEY" class TestEventGeneration(BaseHTTPSConnMock): def request(self, method, uri, body, headers): # This sort of blows encapsulation, but whatever test_case.assertEqual(uri, send_signifai.DEFAULT_POST_URI) # XXX: json.dumps (or some underlying process) determinism # (specifically, the string may not be generated the # same in both cases due to key/value traversal order, # etc.) test_case.assertEqual(body, json.dumps(test_case.events)) test_case.assertEqual(headers['Authorization'], "Bearer {KEY}".format(KEY=API_KEY)) test_case.assertEqual(headers['Content-Type'], "application/json") test_case.assertEqual(headers['Accept'], "application/json") test_case.assertEqual(method, "POST") def getresponse(self): return BaseHTTPSRespMock( json.dumps({ "success": True, "failed_events": [] })) result = send_signifai.POST_data(auth_key=API_KEY, data=self.events, httpsconn=TestEventGeneration) self.assertTrue(result)
def test_post_somebad_somegood(self): # Should return False, NOT throw events = {"events": [self.corpus, self.corpus]} def random_event_failure(*args, **kwargs): body = kwargs['body'] mockresponse = unittest_mock.Mock() mockresponse.status = 200 mockresponse.read.return_value = json.dumps({ "success": True, "failed_events": [{ "event": json.loads(body)['events'][1], "error": "some error, doesn't matter" }] }) m['getresponse'].return_value = mockresponse with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: m['request'].side_effect = random_event_failure result = send_signifai.POST_data(auth_key="", data=events) self.assertIsNone(result)
def test_connect_retries_can_succeed(self): # Should return True total_retries = 5 def conn_mock(): if m['connect'].call_count < (total_retries - 1): raise socket.timeout else: return True def getresponse_mock(): ret = unittest_mock.Mock() ret.status = 200 ret.read.return_value = json.dumps({ "success": True, "failed_events": [] }) return ret with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: m['connect'].side_effect = conn_mock m['getresponse'].side_effect = getresponse_mock result = send_signifai.POST_data(auth_key="", data=self.events, attempts=total_retries) self.assertTrue(result) self.assertEqual(m['connect'].call_count, total_retries - 1)
def test_post_bad_host(self): # Should return False, NOT throw # (don't use a mock for this, it should never touch the collector) result = send_signifai.POST_data(auth_key="", data=self.events, signifai_host="noresolve.signifai.io") self.assertFalse(result)
def test_create_exception(self): with unittest_mock.patch.object( http_client.HTTPSConnection, '__init__', side_effect=http_client.HTTPException) as conn_call: # noqa result = send_signifai.POST_data(auth_key="", data=self.events) self.assertFalse(result) # Ensure we don't attempt a retry self.assertEqual(conn_call.call_count, 1)
def test_getresponse_httpexception(self): # Should return False, NOT throw with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: m['getresponse'].side_effect = http_client.HTTPException result = send_signifai.POST_data(auth_key="", data=self.events) self.assertFalse(result)
def test_request_timeout(self): # Should return False, NOT throw with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: m['request'].side_effect = socket.timeout result = send_signifai.POST_data(auth_key="", data=self.events) self.assertFalse(result) self.assertEqual(m['request'].call_count, 1)
def test_getresponse_timeout(self): # Should return False, NOT throw class GetResponseTimesOut(BaseHTTPSConnMock): def getresponse(self): raise socket.timeout result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=GetResponseTimesOut) self.assertFalse(result)
def test_getresponse_httpexception(self): # Should return False, NOT throw class GetResponseThrows(BaseHTTPSConnMock): def getresponse(self): raise http_client.HTTPException() result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=GetResponseThrows) self.assertFalse(result)
def test_connect_exception(self): # Should return False with unittest_mock.patch.object( http_client.HTTPSConnection, 'connect', side_effect=http_client.HTTPException) as conn_call: # noqa result = send_signifai.POST_data(auth_key="", data=self.events) self.assertFalse(result) # Ensure we attempted retries self.assertEqual(conn_call.call_count, 5)
def test_connect_retries_fail(self): # Should return False total_retries = 5 with unittest_mock.patch.object( http_client.HTTPSConnection, 'connect', side_effect=socket.timeout) as conn_call: # noqa result = send_signifai.POST_data(auth_key="", data=self.events, attempts=total_retries) self.assertFalse(result) self.assertEqual(conn_call.call_count, total_retries)
def test_post_bad_status(self): # Should return False, NOT throw class BadStatus(BaseHTTPSConnMock): def getresponse(self): return BaseHTTPSRespMock("500 Internal Server Error", status=500) result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=BadStatus) self.assertFalse(result)
def test_post_bad_response(self): # Should return False, NOT throw class BadResponse(BaseHTTPSConnMock): def getresponse(self): return BaseHTTPSRespMock("this is a bad response text", status=200) result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=BadResponse) self.assertFalse(result)
def test_post_bad_response(self): # Should return False, NOT throw with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: resp = unittest_mock.Mock() resp.read.return_value = "this is a bad response text" resp.status = 200 m['getresponse'].return_value = resp result = send_signifai.POST_data(auth_key="", data=self.events) self.assertFalse(result)
def test_good_post(self): # Should return True class SucceedsToPOST(BaseHTTPSConnMock): def getresponse(self): return BaseHTTPSRespMock( json.dumps({ "success": True, "failed_events": [] })) result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=SucceedsToPOST) self.assertTrue(result)
def test_good_post(self): # Should return True with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: mockresp = unittest_mock.Mock() mockresp.status = 200 mockresp.read.return_value = json.dumps({ "success": True, "failed_events": [] }) m['getresponse'].return_value = mockresp result = send_signifai.POST_data(auth_key="", data=self.events) self.assertTrue(result)
def test_request_httpexception(self): # Should return False, NOT throw class RequestThrows(BaseHTTPSConnMock): retries = 0 def request(self, *args, **kwargs): self.__class__.retries += 1 raise http_client.HTTPException() result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=RequestThrows) self.assertFalse(result) self.assertEqual(RequestThrows.retries, 1)
def test_request_timeout(self): # Should return False, NOT throw class RequestTimesOut(BaseHTTPSConnMock): retries = 0 def request(self, *args, **kwargs): self.__class__.retries += 1 raise socket.timeout result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=RequestTimesOut) self.assertFalse(result) self.assertEqual(RequestTimesOut.retries, 1)
def test_connect_exception(self): # Should return False class AlwaysThrowOnConnect(BaseHTTPSConnMock): retries = 0 def connect(self, *args, **kwargs): self.__class__.retries += 1 raise http_client.HTTPException() result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=AlwaysThrowOnConnect) self.assertFalse(result) # Ensure we don't attempt a retry self.assertEqual(AlwaysThrowOnConnect.retries, 1)
def test_post_bad_corpus(self): # Should return False, NOT throw class BadContent(BaseHTTPSConnMock): def request(self, *args, **kwargs): body = kwargs['body'] self.failed_events = json.loads(body)['events'] def getresponse(self): return BaseHTTPSRespMock( json.dumps({ "success": False, "failed_events": self.failed_events })) result = send_signifai.POST_data(auth_key="", data=self.events, httpsconn=BadContent) self.assertIsNone(result)
def test_post_bad_corpus(self): # Should return False, NOT throw def everything_failed_mock(*args, **kwargs): body = kwargs['body'] mockresponse = unittest_mock.Mock() mockresponse.read.return_value = json.dumps({ "success": False, "failed_events": json.loads(body)['events'] }) mockresponse.status = 200 m['getresponse'].return_value = mockresponse return True with unittest_mock.patch.multiple(http_client.HTTPSConnection, connect=unittest_mock.DEFAULT, getresponse=unittest_mock.DEFAULT, request=unittest_mock.DEFAULT) as m: m['request'].side_effect = everything_failed_mock result = send_signifai.POST_data(auth_key="", data=self.events) self.assertIsNone(result)
def test_post_somebad_somegood(self): # Should return False, NOT throw events = {"events": [self.corpus, self.corpus]} class ReturnsPartialBad(BaseHTTPSConnMock): def request(self, *args, **kwargs): body = kwargs['body'] self.failed_events = [{ "event": json.loads(body)['events'][1], "error": "some error, doesn't matter" }] def getresponse(self): return BaseHTTPSRespMock( json.dumps({ "success": True, "failed_events": self.failed_events })) result = send_signifai.POST_data(auth_key="", data=events, httpsconn=ReturnsPartialBad) self.assertIsNone(result)
def test_connect_retries_fail(self): # Should return False total_retries = 5 class AlwaysTimeout(BaseHTTPSConnMock): retry_count = 0 def __init__(self, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) def connect(self): # The retry mechanism in POST_data will recreate # the connection object completely, so we need # to store the retries in the class, _not_ the # instance self.__class__.retry_count += 1 raise socket.timeout result = send_signifai.POST_data(auth_key="", data=self.events, attempts=total_retries, httpsconn=AlwaysTimeout) self.assertFalse(result) self.assertEqual(AlwaysTimeout.retry_count, total_retries)