def test_over_max(self): values = [] for x in xrange(5): exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}} values.append(exc) for y in xrange(5): exc['stacktrace']['frames'].append({ 'filename': 'exc %d frame %d' % (x, y), 'vars': {'foo': 'bar'}, 'context_line': 'b', 'pre_context': ['a'], 'post_context': ['c'], }) interface = Exception.to_python({'values': values}) # slim to 10 frames to make tests easier slim_exception_data(interface, 10) assert len(interface.values) == 5 for e_num, value in enumerate(interface.values): assert value.value == 'exc %d' % e_num assert len(value.stacktrace.frames) == 5 for f_num, frame in enumerate(value.stacktrace.frames): assert frame.filename == 'exc %d frame %d' % (e_num, f_num) print(frame.filename) if e_num in (0, 4): assert frame.vars is not None assert frame.pre_context is not None assert frame.post_context is not None else: assert frame.vars is None assert frame.pre_context is None assert frame.post_context is None
def test_non_string_value_with_no_type(self): inst = Exception.to_python({ 'value': { 'foo': 'bar' }, }) assert inst.values[0].value == '{"foo":"bar"}'
def test_context_with_raw_stacks(self): inst = Exception.to_python( dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foobar', 'raw_stacktrace': { 'frames': [{ 'filename': None, 'lineno': 1, 'function': '<redacted>', 'in_app': True, }] }, 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.c', 'lineno': 1, 'function': 'main', 'in_app': True, }] }, }])) self.create_event(data={ 'exception': inst.to_json(), }) context = inst.get_api_context() assert context['values'][0]['stacktrace']['frames'][0][ 'function'] == 'main' assert context['values'][0]['rawStacktrace']['frames'][0][ 'function'] == '<redacted>'
def test_context_with_only_app_frames(self): values = [{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, }, { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, }] exc = dict(values=values) normalize_in_app({'exception': exc}) inst = Exception.to_python(exc) self.create_event(data={ 'exception': inst.to_json(), }) context = inst.get_api_context() assert not context['hasSystemFrames']
def test_context_with_mixed_frames(self): inst = Exception.to_python(dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': {'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }]}, }, { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': {'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': False, }]}, }])) self.create_event(data={ 'sentry.interfaces.Exception': inst.to_json(), }) context = inst.get_api_context() assert context['hasSystemFrames']
def test_context_with_only_app_frames(self): inst = Exception.to_python(dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': {'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }]}, }, { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': {'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }]}, }])) event = self.create_event(data={ 'sentry.interfaces.Exception': inst.to_json(), }) context = inst.get_context(event) assert context['system_frames'] == 0 assert context['exceptions'][0]['stacktrace']['system_frames'] == 0 assert context['exceptions'][1]['stacktrace']['system_frames'] == 0
def test_get_composite_hash_uses_exception_value_if_no_type_or_stack(self): interface = Stacktrace(frames=[]) interface_exc = Exception.to_python(dict(value='bar')) result = interface.get_composite_hash({ 'sentry.interfaces.Exception': interface_exc, }) self.assertEquals(result[-1], 'bar')
def interface(self): return Exception.to_python( dict( values=[ { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, }, { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, } ] ) )
def test_non_string_value_with_no_type(self): inst = Exception.to_python( { 'value': {'foo': 'bar'}, } ) assert inst.values[0].value == '{"foo":"bar"}'
def test_context_with_raw_stacks(self): inst = Exception.to_python(dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foobar', 'raw_stacktrace': {'frames': [{ 'filename': None, 'lineno': 1, 'function': '<redacted>', 'in_app': True, }]}, 'stacktrace': {'frames': [{ 'filename': 'foo/baz.c', 'lineno': 1, 'function': 'main', 'in_app': True, }]}, }])) self.create_event(data={ 'sentry.interfaces.Exception': inst.to_json(), }) context = inst.get_api_context() assert context['values'][0]['stacktrace']['frames'][0]['function'] == 'main' assert context['values'][0]['rawStacktrace']['frames'][0]['function'] == '<redacted>'
def test_over_max(self): values = [] for x in range(5): exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}} values.append(exc) for y in range(5): exc['stacktrace']['frames'].append({ 'filename': 'exc %d frame %d' % (x, y), 'vars': {'foo': 'bar'}, 'context_line': 'b', 'pre_context': ['a'], 'post_context': ['c'], }) interface = Exception.to_python({'values': values}) # slim to 10 frames to make tests easier slim_exception_data(interface, 10) assert len(interface.values) == 5 for e_num, value in enumerate(interface.values): assert value.value == 'exc %d' % e_num assert len(value.stacktrace.frames) == 5 for f_num, frame in enumerate(value.stacktrace.frames): assert frame.filename == 'exc %d frame %d' % (e_num, f_num) if e_num in (0, 4): assert frame.vars is not None assert frame.pre_context is not None assert frame.post_context is not None else: assert frame.vars is None assert frame.pre_context is None assert frame.post_context is None
def interface(self): return Exception.to_python( dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, }, { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, }]))
def test_context_with_symbols(self): inst = Exception.to_python( dict( values=[ { 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [ { 'filename': 'foo/baz.py', 'function': 'myfunc', 'symbol': 'Class.myfunc', 'lineno': 1, 'in_app': True, } ] }, } ] ) ) self.create_event(data={ 'sentry.interfaces.Exception': inst.to_json(), }) context = inst.get_api_context() assert context['values'][0]['stacktrace']['frames'][0]['symbol'] == 'Class.myfunc'
def test_does_not_wrap_if_exception_omitted_present(self): input = { "exc_omitted": None, "mechanism": { "handled": True, "type": "generic" } } assert Exception.to_python(input).to_json() == {}
def test_get_composite_hash_uses_exception_value_if_no_type_or_stack(self): interface = Stacktrace(frames=[]) interface_exc = Exception.to_python(dict(type='bar')) result = interface.get_composite_hash({ 'sentry.interfaces.Exception': interface_exc, }) self.assertEquals(result[-1], 'bar')
def test_under_max(self): interface = Exception.to_python({'values': [ {'value': 'foo', 'stacktrace': {'frames': [{'filename': 'foo'}]}, } ]}) slim_exception_data(interface) assert len(interface.values[0].stacktrace.frames) == 1
def test_iteration(self): inst = Exception.to_python({ 'values': [None, {'type': 'ValueError'}, None] }) assert len(inst) == 1 assert inst[0].type == 'ValueError' for exc in inst: assert exc.type == 'ValueError'
def test_iteration(): inst = Exception.to_python({ 'values': [None, {'type': 'ValueError'}, None] }) assert len(inst) == 1 assert inst[0].type == 'ValueError' for exc in inst: assert exc.type == 'ValueError'
def test_args_as_old_style(self): inst = Exception.to_python({ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', }) assert isinstance(inst.values[0], SingleException) assert inst.values[0].type == 'ValueError' assert inst.values[0].value == 'hello world' assert inst.values[0].module == 'foo.bar'
def test_args_as_keyword_args(self): inst = Exception.to_python(dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', }])) assert type(inst.values[0]) is SingleException assert inst.values[0].type == 'ValueError' assert inst.values[0].value == 'hello world' assert inst.values[0].module == 'foo.bar'
def test_args_as_old_style(self): inst = Exception.to_python({ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', }) assert type(inst.values[0]) is SingleException assert inst.values[0].type == 'ValueError' assert inst.values[0].value == 'hello world' assert inst.values[0].module == 'foo.bar'
def test_iteration(): inst = Exception.to_python( {"values": [None, { "type": "ValueError" }, None]}) assert len(inst) == 1 assert inst[0].type == "ValueError" for exc in inst: assert exc.type == "ValueError"
def test_get_composite_hash_uses_exception_if_present(self): interface = Stacktrace.to_python(dict(frames=[{ 'context_line': 'foo bar', 'lineno': 1, 'filename': 'foo.py', 'function': 'bar' }])) interface_exc = Exception.to_python(dict(type='exception', value='bar')) result = interface.get_composite_hash({ 'sentry.interfaces.Exception': interface_exc, }) self.assertEquals(result[-1], 'exception')
def test_slim_exception_data_under_max(insta_snapshot): interface = Exception.to_python({ "values": [{ "value": "foo", "stacktrace": { "frames": [{ "filename": "foo" }] } }] }) slim_exception_data(interface) insta_snapshot(interface.to_json())
def test_slim_exception_data_under_max(insta_snapshot): interface = Exception.to_python({ 'values': [{ 'value': 'foo', 'stacktrace': { 'frames': [{ 'filename': 'foo' }] }, }] }) slim_exception_data(interface) insta_snapshot(interface.to_json())
def test_slim_exception_data_under_max(insta_snapshot): interface = Exception.to_python( { 'values': [{ 'value': 'foo', 'stacktrace': { 'frames': [{ 'filename': 'foo' }] }, }] } ) slim_exception_data(interface) insta_snapshot(interface.to_json())
def interface(self): return Exception.to_python( dict( values=[ { "type": "ValueError", "value": "hello world", "module": "foo.bar", "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": True}]}, }, { "type": "ValueError", "value": "hello world", "module": "foo.bar", "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": True}]}, }, ] ) )
def test_context_with_symbols(self): inst = Exception.to_python(dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': {'frames': [{ 'filename': 'foo/baz.py', 'function': 'myfunc', 'symbol': 'Class.myfunc', 'lineno': 1, 'in_app': True, }]}, }])) self.create_event(data={ 'sentry.interfaces.Exception': inst.to_json(), }) context = inst.get_api_context() assert context['values'][0]['stacktrace']['frames'][ 0]['symbol'] == 'Class.myfunc'
def test_slim_exception_data_over_max(insta_snapshot): values = [] for x in range(5): exc = {"value": "exc %d" % x, "stacktrace": {"frames": []}} values.append(exc) for y in range(5): exc["stacktrace"]["frames"].append({ "filename": "exc %d frame %d" % (x, y), "vars": { "foo": "bar" }, "context_line": "b", "pre_context": ["a"], "post_context": ["c"], }) interface = Exception.to_python({"values": values}) # slim to 10 frames to make tests easier slim_exception_data(interface, 10) insta_snapshot(interface.to_json())
def test_slim_exception_data_over_max(insta_snapshot): values = [] for x in range(5): exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}} values.append(exc) for y in range(5): exc['stacktrace']['frames'].append( { 'filename': 'exc %d frame %d' % (x, y), 'vars': { 'foo': 'bar' }, 'context_line': 'b', 'pre_context': ['a'], 'post_context': ['c'], } ) interface = Exception.to_python({'values': values}) # slim to 10 frames to make tests easier slim_exception_data(interface, 10) insta_snapshot(interface.to_json())
def test_slim_exception_data_over_max(insta_snapshot): values = [] for x in range(5): exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}} values.append(exc) for y in range(5): exc['stacktrace']['frames'].append({ 'filename': 'exc %d frame %d' % (x, y), 'vars': { 'foo': 'bar' }, 'context_line': 'b', 'pre_context': ['a'], 'post_context': ['c'], }) interface = Exception.to_python({'values': values}) # slim to 10 frames to make tests easier slim_exception_data(interface, 10) insta_snapshot(interface.to_json())
def test_context_with_only_system_frames(self): inst = Exception.to_python( dict( values=[ { "type": "ValueError", "value": "hello world", "module": "foo.bar", "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": False}]}, }, { "type": "ValueError", "value": "hello world", "module": "foo.bar", "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": False}]}, }, ] ) ) self.create_event(data={"sentry.interfaces.Exception": inst.to_json()}) context = inst.get_api_context() assert not context["hasSystemFrames"]
def test_context_with_mechanism(self): inst = Exception.to_python( dict(values=[{ 'type': 'ValueError', 'value': 'hello world', 'module': 'foo.bar', 'stacktrace': { 'frames': [{ 'filename': 'foo/baz.py', 'lineno': 1, 'in_app': True, }] }, 'mechanism': { 'type': 'generic', } }])) self.create_event(data={ 'exception': inst.to_json(), }) context = inst.get_api_context() assert context['values'][0]['mechanism']['type'] == 'generic'
def test_over_max(self): values = [] for x in xrange(5): exc = {"value": "exc %d" % x, "stacktrace": {"frames": []}} values.append(exc) for y in xrange(5): exc["stacktrace"]["frames"].append( { "filename": "exc %d frame %d" % (x, y), "vars": {"foo": "bar"}, "context_line": "b", "pre_context": ["a"], "post_context": ["c"], } ) interface = Exception.to_python({"values": values}) # slim to 10 frames to make tests easier slim_exception_data(interface, 10) assert len(interface.values) == 5 for e_num, value in enumerate(interface.values): assert value.value == "exc %d" % e_num assert len(value.stacktrace.frames) == 5 for f_num, frame in enumerate(value.stacktrace.frames): assert frame.filename == "exc %d frame %d" % (e_num, f_num) print (frame.filename) if e_num in (0, 4): assert frame.vars is not None assert frame.pre_context is not None assert frame.post_context is not None else: assert frame.vars is None assert frame.pre_context is None assert frame.post_context is None
def test_args_as_old_style(self): inst = Exception.to_python({"type": "ValueError", "value": "hello world", "module": "foo.bar"}) assert type(inst.values[0]) is SingleException assert inst.values[0].type == "ValueError" assert inst.values[0].value == "hello world" assert inst.values[0].module == "foo.bar"
def test_under_max(self): interface = Exception.to_python({"values": [{"value": "foo", "stacktrace": {"frames": [{"filename": "foo"}]}}]}) slim_exception_data(interface) assert len(interface.values[0].stacktrace.frames) == 1
def test_args_as_keyword_args(self): inst = Exception.to_python(dict(values=[{"type": "ValueError", "value": "hello world", "module": "foo.bar"}])) assert type(inst.values[0]) is SingleException assert inst.values[0].type == "ValueError" assert inst.values[0].value == "hello world" assert inst.values[0].module == "foo.bar"