|
1 | 1 | from collections import OrderedDict |
2 | 2 |
|
3 | 3 | import pytest |
4 | | - |
5 | 4 | from google_cloud_logger import GoogleCloudFormatter |
6 | 5 |
|
| 6 | +# from https://stackoverflow.com/a/19258720 |
| 7 | +class FakeCode(object): |
| 8 | + def __init__(self, co_filename, co_name): |
| 9 | + self.co_filename = co_filename |
| 10 | + self.co_name = co_name |
| 11 | + |
| 12 | + |
| 13 | +class FakeFrame(object): |
| 14 | + def __init__(self, f_code, f_globals): |
| 15 | + self.f_code = f_code |
| 16 | + self.f_globals = f_globals |
| 17 | + |
| 18 | + |
| 19 | +class FakeTraceback(object): |
| 20 | + def __init__(self, frames, line_nums): |
| 21 | + if len(frames) != len(line_nums): |
| 22 | + raise ValueError("Ya messed up!") |
| 23 | + self._frames = frames |
| 24 | + self._line_nums = line_nums |
| 25 | + self.tb_frame = frames[0] |
| 26 | + self.tb_lineno = line_nums[0] |
| 27 | + |
| 28 | + @property |
| 29 | + def tb_next(self): |
| 30 | + if len(self._frames) > 1: |
| 31 | + return FakeTraceback(self._frames[1:], self._line_nums[1:]) |
| 32 | + |
7 | 33 |
|
8 | 34 | @pytest.fixture |
9 | 35 | def formatter(): |
@@ -45,6 +71,25 @@ def record_with_extra_attribute(log_record_factory, mocker): |
45 | 71 | return record |
46 | 72 |
|
47 | 73 |
|
| 74 | +@pytest.fixture |
| 75 | +def record_with_exception(log_record_factory, mocker): |
| 76 | + code = FakeCode("module.py", "function") |
| 77 | + frame = FakeFrame(code, {}) |
| 78 | + traceback = FakeTraceback([frame], [1]) |
| 79 | + data = { |
| 80 | + "asctime": "2018-08-30 20:40:57,245", |
| 81 | + "filename": "_internal.py", |
| 82 | + "funcName": "_log", |
| 83 | + "lineno": "88", |
| 84 | + "levelname": "WARNING", |
| 85 | + "message": "farofa", |
| 86 | + "exc_info": (Exception, "ERROR", traceback), |
| 87 | + } |
| 88 | + record = log_record_factory(**data) |
| 89 | + record.getMessage = mocker.Mock(return_value=data["message"]) |
| 90 | + return record |
| 91 | + |
| 92 | + |
48 | 93 | def test_add_fields(formatter, record, mocker): |
49 | 94 | log_record = OrderedDict({}) |
50 | 95 | mocker.patch.object( |
@@ -96,14 +141,22 @@ def test_make_metadata(formatter, record): |
96 | 141 | assert metadata["userLabels"]["extra_field"] == "extra" |
97 | 142 |
|
98 | 143 |
|
99 | | -def test_make_metadata_with_extra_attribute( |
100 | | - formatter, |
101 | | - record_with_extra_attribute): |
| 144 | +def test_make_metadata_with_extra_attribute(formatter, record_with_extra_attribute): |
102 | 145 | metadata = formatter.make_metadata(record_with_extra_attribute) |
103 | 146 |
|
104 | 147 | assert metadata["userLabels"]["extra_field"] == "extra" |
105 | 148 |
|
106 | 149 |
|
| 150 | +def test_make_metadata_with_exception(formatter, record_with_exception): |
| 151 | + metadata = formatter.make_metadata(record_with_exception) |
| 152 | + |
| 153 | + assert metadata["exception"] == { |
| 154 | + "class": Exception().__class__, |
| 155 | + "message": "ERROR", |
| 156 | + "traceback": ' File "module.py", line 1, in function\n', |
| 157 | + } |
| 158 | + |
| 159 | + |
107 | 160 | def test_make_source_location(formatter, record): |
108 | 161 | assert formatter.make_source_location(record) == { |
109 | 162 | "file": "_internal.py", |
|
0 commit comments