|
1 | 1 | # -*- coding: iso-8859-15 -*-
|
| 2 | + |
2 | 3 | import pytest
|
| 4 | +import sys |
3 | 5 |
|
4 | 6 | from aws_xray_sdk.core.models.segment import Segment
|
5 | 7 | from aws_xray_sdk.core.models.subsegment import Subsegment
|
@@ -194,3 +196,70 @@ def test_missing_parent_segment():
|
194 | 196 |
|
195 | 197 | with pytest.raises(SegmentNotFoundException):
|
196 | 198 | Subsegment('name', 'local', None)
|
| 199 | + |
| 200 | + |
| 201 | +def test_add_exception(): |
| 202 | + segment = Segment('seg') |
| 203 | + exception = Exception("testException") |
| 204 | + stack = [['path', 'line', 'label']] |
| 205 | + segment.add_exception(exception=exception, stack=stack) |
| 206 | + segment.close() |
| 207 | + |
| 208 | + cause = segment.cause |
| 209 | + assert 'exceptions' in cause |
| 210 | + exceptions = cause['exceptions'] |
| 211 | + assert len(exceptions) == 1 |
| 212 | + assert 'working_directory' in cause |
| 213 | + exception = exceptions[0] |
| 214 | + assert 'testException' == exception.message |
| 215 | + expected_stack = [{'path': 'path', 'line': 'line', 'label': 'label'}] |
| 216 | + assert expected_stack == exception.stack |
| 217 | + |
| 218 | + |
| 219 | +def test_add_exception_referencing(): |
| 220 | + segment = Segment('seg') |
| 221 | + subseg = Subsegment('subseg', 'remote', segment) |
| 222 | + exception = Exception("testException") |
| 223 | + stack = [['path', 'line', 'label']] |
| 224 | + subseg.add_exception(exception=exception, stack=stack) |
| 225 | + segment.add_exception(exception=exception, stack=stack) |
| 226 | + subseg.close() |
| 227 | + segment.close() |
| 228 | + |
| 229 | + seg_cause = segment.cause |
| 230 | + subseg_cause = subseg.cause |
| 231 | + |
| 232 | + assert isinstance(subseg_cause, dict) |
| 233 | + if sys.version_info.major == 2: |
| 234 | + assert isinstance(seg_cause, basestring) |
| 235 | + else: |
| 236 | + assert isinstance(seg_cause, str) |
| 237 | + assert seg_cause == subseg_cause['exceptions'][0].id |
| 238 | + |
| 239 | + |
| 240 | +def test_add_exception_cause_resetting(): |
| 241 | + segment = Segment('seg') |
| 242 | + subseg = Subsegment('subseg', 'remote', segment) |
| 243 | + exception = Exception("testException") |
| 244 | + stack = [['path', 'line', 'label']] |
| 245 | + subseg.add_exception(exception=exception, stack=stack) |
| 246 | + segment.add_exception(exception=exception, stack=stack) |
| 247 | + |
| 248 | + segment.add_exception(exception=Exception("newException"), stack=stack) |
| 249 | + subseg.close() |
| 250 | + segment.close() |
| 251 | + |
| 252 | + seg_cause = segment.cause |
| 253 | + assert isinstance(seg_cause, dict) |
| 254 | + assert 'newException' == seg_cause['exceptions'][0].message |
| 255 | + |
| 256 | + |
| 257 | +def test_add_exception_appending_exceptions(): |
| 258 | + segment = Segment('seg') |
| 259 | + stack = [['path', 'line', 'label']] |
| 260 | + segment.add_exception(exception=Exception("testException"), stack=stack) |
| 261 | + segment.add_exception(exception=Exception("newException"), stack=stack) |
| 262 | + segment.close() |
| 263 | + |
| 264 | + assert isinstance(segment.cause, dict) |
| 265 | + assert len(segment.cause['exceptions']) == 2 |
0 commit comments