/
test_formatter.py
164 lines (132 loc) · 5.56 KB
/
test_formatter.py
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
from picologging import Formatter, LogRecord
import logging
import pytest
import datetime
def test_formatter_default_fmt():
f = Formatter()
assert f.datefmt == None
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
s = f.format(record)
assert s == 'bork bork bork'
def test_formatter_custom_fmt():
f = Formatter("%(name)s %(levelname)s %(message)s")
assert f.datefmt == None
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
s = f.format(record)
assert s == 'hello WARNING bork bork bork'
assert f.usesTime() == False
assert f.formatMessage(record) == s
def test_formatter_default_fmt_against_builtin():
pico_f = Formatter()
logging_f = logging.Formatter()
assert pico_f.datefmt == logging_f.datefmt
pico_record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
logging_record = logging.LogRecord('hello', logging.WARNING, '/serv/', 123, 'bork bork bork', (), None)
assert pico_f.format(pico_record) == logging_f.format(logging_record)
assert pico_f.format(pico_record) == logging_f.format(pico_record)
assert pico_f.formatMessage(pico_record) == logging_f.formatMessage(pico_record)
def test_formatter_custom_datefmt():
f = Formatter("%(name)s %(levelname)s %(message)s", datefmt="%Y-%m-%d")
assert f.datefmt == "%Y-%m-%d"
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
s = f.format(record)
assert s == 'hello WARNING bork bork bork'
def test_formatter_explicit_none_datefmt_style():
f = Formatter("%(name)s %(levelname)s %(message)s", None, '%')
assert f.datefmt == None
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
s = f.format(record)
assert s == 'hello WARNING bork bork bork'
possible_format_strings = [
"%(message)s",
"%(message)s is a potato",
"%(name)s",
"%(msg)s",
"%(args)s",
"%(levelname)s",
"%(levelno)s",
"%(pathname)s",
"%(filename)s",
"%(module)s",
"%(funcName)s",
"%(lineno)d",
"%(threadName)s",
"%(process)d",
"%(processName)s",
"%(stack_info)s",
"%(exc_info)s",
"%(exc_text)s",
]
@pytest.mark.parametrize("field", possible_format_strings)
def test_format_field(field):
pico_f = Formatter(field)
log_f = logging.Formatter(field)
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
assert pico_f.format(record) == log_f.format(record)
def test_format_time():
pico_f = Formatter("%(msecs)d %(relativeCreated)d")
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
assert pico_f.format(record)
def test_asctime_field():
pico_f = Formatter("%(asctime)s")
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
assert pico_f.format(record)
assert pico_f.usesTime()
def test_record_with_stack_info():
pico_f = Formatter("%(message)s")
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None, None, "hello")
assert pico_f.format(record) == "bork bork bork\nhello"
def test_record_with_non_str_sstack_info():
pico_f = Formatter("%(message)s")
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None, None, ["hello", "world"])
assert pico_f.format(record) == "bork bork bork\n['hello', 'world']"
def test_format_stack():
pico_f = Formatter("%(message)s")
assert pico_f.formatStack([1,2,3]) == [1,2,3]
def test_delete_formatter():
pico_f = Formatter("%(message)s")
del pico_f
def test_formatter_bad_init_args():
with pytest.raises(TypeError):
Formatter(dog="good boy")
def test_formatter_bad_style():
with pytest.raises(ValueError):
Formatter(style='!')
def test_formatter_bad_style_type():
with pytest.raises(TypeError):
Formatter(style=123)
def test_formatter_bad_fmt_type():
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
with pytest.raises((TypeError, ValueError)):
f = Formatter(fmt=123)
assert f.format(record) == "bork bork bork"
def test_formatter_with_validate_flag_and_invalid_fmt():
f = Formatter(fmt="%(message ", validate=True)
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
assert f.format(record) == "%(message "
def test_datefmt_bad_type():
with pytest.raises(TypeError):
Formatter(datefmt=123)
def test_format_with_custom_datefmt():
actual_date = datetime.datetime.now().strftime("%Y-%m-%d")
f = Formatter("%(name)s %(levelname)s %(message)s %(asctime)s", datefmt="%Y-%m-%d")
assert f.datefmt == "%Y-%m-%d"
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), None)
s = f.format(record)
assert s == f'hello WARNING bork bork bork {actual_date}'
assert f.usesTime() == True
assert f.formatMessage(record) == s
def test_formatter_repr():
f = Formatter("%(message)s")
assert repr(f) == "<Formatter: fmt='%(message)s'>"
def test_exc_info_invalid_type():
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), (1,2,3))
f = Formatter()
with pytest.raises(AttributeError):
f.format(record)
def test_exc_info_invalid_value_types():
record = LogRecord('hello', logging.WARNING, __file__, 123, 'bork bork bork', (), [1,2,3])
f = Formatter()
with pytest.raises(TypeError):
f.format(record)
# TODO : test defaults