Repository navigation
Expand file tree
/
Copy pathtest_logged.py
More file actions
224 lines (156 loc) · 6.39 KB
/
Copy pathtest_logged.py
File metadata and controls
224 lines (156 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Tests for the logging mixins in ``python_utils.logger``."""
import logging
import pytest
from python_utils import logger
def records_of(
caplog: pytest.LogCaptureFixture, name: str
) -> list[logging.LogRecord]:
"""Return the captured records of the logger called ``name``."""
return [record for record in caplog.records if record.name == name]
def test_exception_logs_traceback(caplog: pytest.LogCaptureFixture) -> None:
"""Attach the active exception to the record, like ``logging`` does."""
class Spam(logger.Logged):
pass
spam: Spam = Spam()
name: str = Spam.logger.name
with caplog.at_level(logging.DEBUG, logger=name):
try:
int('eggs')
except ValueError:
spam.exception('bacon')
record: logging.LogRecord = records_of(caplog, name)[-1]
assert record.exc_info is not None
assert record.exc_info[0] is ValueError
def test_exception_without_traceback(caplog: pytest.LogCaptureFixture) -> None:
"""Leave the traceback out when ``exc_info`` is switched off."""
class Spam(logger.Logged):
pass
spam: Spam = Spam()
name: str = Spam.logger.name
with caplog.at_level(logging.DEBUG, logger=name):
try:
int('eggs')
except ValueError:
spam.exception('bacon', exc_info=False)
record: logging.LogRecord = records_of(caplog, name)[-1]
assert not record.exc_info
def test_records_name_the_caller(caplog: pytest.LogCaptureFixture) -> None:
"""Report the calling function in every record, not the mixin."""
class Spam(logger.Logged):
pass
spam: Spam = Spam()
name: str = Spam.logger.name
with caplog.at_level(logging.DEBUG, logger=name):
spam.debug('debug')
spam.info('info')
spam.warning('warning')
spam.error('error')
spam.critical('critical')
spam.exception('exception')
spam.log(logging.INFO, 'log')
records: list[logging.LogRecord] = records_of(caplog, name)
assert len(records) == 7
assert {record.funcName for record in records} == {
'test_records_name_the_caller'
}
assert {record.filename for record in records} == {'test_logged.py'}
def log_one_frame_up(spam: logger.Logged) -> None:
"""Log a record that points at the caller of this helper."""
spam.info('eggs', stacklevel=2)
def test_stacklevel_counts_from_the_caller(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Count an explicit ``stacklevel`` from the calling function."""
class Spam(logger.Logged):
pass
spam: Spam = Spam()
name: str = Spam.logger.name
with caplog.at_level(logging.DEBUG, logger=name):
log_one_frame_up(spam)
record: logging.LogRecord = records_of(caplog, name)[-1]
assert record.funcName == 'test_stacklevel_counts_from_the_caller'
class Bacon:
"""A base class whose ``__new__`` needs the constructor arguments."""
value: int
def __new__(cls, value: int) -> 'Bacon':
"""Store ``value`` on the new instance."""
self: Bacon = super().__new__(cls)
self.value = value
return self
def test_new_forwards_arguments_to_builtin() -> None:
"""Keep the value of a built-in type that is created in ``__new__``."""
class LoggedInt(logger.Logged, int):
pass
class LoggedStr(logger.Logged, str):
pass
number: LoggedInt = LoggedInt(5)
octal: LoggedInt = LoggedInt('7', base=8)
text: LoggedStr = LoggedStr('spam')
assert number == 5
assert octal == 7
assert text == 'spam'
def test_new_forwards_arguments_to_base() -> None:
"""Pass the constructor arguments on to the next ``__new__``."""
class Eggs(logger.Logged, Bacon):
pass
eggs: Eggs = Eggs(5)
assert eggs.value == 5
assert Eggs.logger.name.endswith('.Eggs')
def test_new_accepts_arguments_for_init() -> None:
"""Keep accepting arguments that only ``__init__`` uses."""
class Spam(logger.Logged):
def __init__(self, value: int, name: str = 'spam') -> None:
"""Store both arguments on the instance."""
self.value: int = value
self.name: str = name
spam: Spam = Spam(1, name='eggs')
assert (spam.value, spam.name) == (1, 'eggs')
class Singleton:
"""A base class whose ``__new__`` takes no constructor arguments."""
instance: 'Singleton | None' = None
def __new__(cls) -> 'Singleton':
"""Create the one instance on the first call and reuse it after."""
if cls.instance is None:
cls.instance = super().__new__(cls)
return cls.instance
def test_new_falls_back_without_arguments() -> None:
"""Keep working with a base ``__new__`` that takes no arguments."""
class Config(logger.Logged, Singleton):
def __init__(self, path: str = 'defaults.ini') -> None:
"""Store the path on the instance."""
self.path: str = path
config: Config = Config('settings.ini')
assert config.path == 'settings.ini'
assert Config('other.ini') is config
def test_new_reports_the_first_error() -> None:
"""Raise the error of the forwarded call when no call works."""
class Eggs(logger.Logged, Bacon):
def __init__(self, *args: int) -> None:
"""Accept any number of arguments."""
# The message is the interpreter's own and differs on PyPy.
with pytest.raises(TypeError):
Eggs(1, 2)
def test_logger_is_created_at_first_instance() -> None:
"""Create the logger of a class when it is first instantiated.
``logging.config.dictConfig`` disables every logger that exists when it
runs. A logger that is created when the class is defined would be gone
for an application that configures logging after its imports.
"""
class LateSpam(logger.Logged):
pass
name: str = f'{LateSpam.__module__}.LateSpam'
assert name not in logging.Logger.manager.loggerDict
assert 'logger' not in vars(LateSpam)
LateSpam()
assert name in logging.Logger.manager.loggerDict
assert LateSpam.logger.name == name
def test_subclass_inherits_class_body_logger() -> None:
"""Let a subclass use the logger from the class body of its parent."""
custom: logging.Logger = logging.getLogger('python_utils.tests.custom')
class Parent(logger.Logged):
logger = custom
class Child(Parent):
pass
assert Child.logger is custom
Child()
assert Child.logger.name.endswith('.Child')