def test_notify_error(self): ab = Airbrake(project_id=1234, api_key='fake') try: raise TypeError except Exception as e: exc_info = sys.exc_info() error = Error(exc_info=exc_info) user = {"id": "12345", "name": "root", "email": "*****@*****.**"} notice = ab.build_notice(error, user=user) exception_str = type(e).__name__ exception_type = type(e).__name__ expected_payload = self.get_expected_payload( exception_str, exception_type) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': pytb_lastline(exc_info) } expected_payload['errors'] = [data] expected_payload['context']['user'] = user expected_payload['context']['severity'] = ErrorLevels.DEFAULT_LEVEL self.assertEqual(expected_payload, notice.payload) with mock.patch('requests.post') as requests_post: ab.notify(error) data = json.loads(requests_post.call_args[1]['data']) self.assertEqual(expected_payload, data)
def test_notice_severity(self): ab = Airbrake(project_id=1234, api_key='fake') notice = ab.build_notice(ValueError("This is a test"), severity=ErrorLevels.CRITICAL) self.assertEqual(ErrorLevels.CRITICAL, notice.payload['context']['severity']) with mock.patch('requests.post') as requests_post: ab.notify(notice) data = json.loads(requests_post.call_args[1]['data']) error_level = data['context']['severity'] self.assertEqual(ErrorLevels.CRITICAL, error_level)
def test_notify_str(self): ab = Airbrake(project_id=1234, api_key='fake') exception_str = "This is a test" exception_type = 'Error' notice = ab.build_notice(exception_str) expected_payload = self.get_expected_payload(exception_str, exception_type) self.assertEqual(expected_payload, notice.payload) with mock.patch('requests.post') as requests_post: ab.notify(exception_str) data = json.loads(requests_post.call_args[1]['data']) self.assertEqual(expected_payload, data)
def check_timeout(self, timeout=None, expected_timeout=None): ab = Airbrake(project_id=1234, api_key='fake', environment='test', timeout=timeout) if not timeout: ab = Airbrake(project_id=1234, api_key='fake', environment='test') with mock.patch('requests.post') as requests_post: ab.deploy('test', 'user1') timeout = requests_post.call_args[1]['timeout'] self.assertEqual(expected_timeout, timeout) with mock.patch('requests.post') as requests_post: notice = ab.build_notice("This is a test") ab.notify(notice) timeout = requests_post.call_args[1]['timeout'] self.assertEqual(expected_timeout, timeout)
def test_capture(self): ab = Airbrake(project_id=1234, api_key='fake') try: raise ValueError("oh nos") except Exception: with mock.patch('requests.post') as requests_post: ab.capture() exc_info = sys.exc_info() raw_frames = traceback.extract_tb(exc_info[2]) exc_frame = raw_frames[0] exception_str = exc_frame[3] exception_type = "ERROR:%s" % exc_frame[0] expected_payload = self.get_expected_payload( exception_str, exception_type) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': str(exc_frame[3]) } expected_payload['errors'] = [data] expected_payload['context']['severity'] =\ ErrorLevels.DEFAULT_LEVEL err = Error(exc_info=exc_info, filename=str(exc_frame[0]), line=str(exc_frame[1]), function=str(exc_frame[2]), message=str(exc_frame[3]), errtype="ERROR:%s" % str(exc_frame[0])) notice = ab.build_notice(err) self.assertEqual(expected_payload, notice.payload) data = json.loads(requests_post.call_args[1]['data']) self.assertEqual(expected_payload, data)