-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_picologging.py
More file actions
165 lines (107 loc) · 4.07 KB
/
test_picologging.py
File metadata and controls
165 lines (107 loc) · 4.07 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
import sys
import pytest
import picologging
from picologging.handlers import BufferingHandler
levels = [
(picologging.DEBUG, "DEBUG"),
(picologging.INFO, "INFO"),
(picologging.WARNING, "WARNING"),
(picologging.ERROR, "ERROR"),
(picologging.CRITICAL, "CRITICAL"),
(picologging.NOTSET, "NOTSET"),
]
@pytest.mark.parametrize("level, level_name", levels)
def test_getlevelname(level, level_name):
assert picologging.getLevelName(level) == level_name
assert picologging.getLevelName(level_name) == level
def test_value_error_invalid_string_names():
with pytest.raises(ValueError):
assert picologging.getLevelName("EXample") == "Level EXample"
junk_level_names = [None, 3.2, (), [], {}, 100]
@pytest.mark.parametrize("level", junk_level_names)
def test_getlevelname_invalid_level(level):
with pytest.raises((TypeError, ValueError)):
picologging.getLevelName(level)
def test_root_logger_critical(capsys):
picologging.root.handlers = []
picologging.critical("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == "CRITICAL:root:test\n"
def test_root_logger_fatal(capsys):
picologging.root.handlers = []
picologging.fatal("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == "CRITICAL:root:test\n"
def test_root_logger_error(capsys):
picologging.root.handlers = []
picologging.error("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == "ERROR:root:test\n"
def test_root_logger_exception(capsys):
picologging.root.handlers = []
picologging.exception("test", exc_info=Exception("bork bork bork"))
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == "ERROR:root:test\nException: bork bork bork\n"
def test_root_logger_warning(capsys):
picologging.root.handlers = []
picologging.warning("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == "WARNING:root:test\n"
def test_root_logger_warn(capsys):
picologging.root.handlers = []
picologging.warn("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == "WARNING:root:test\n"
def test_root_logger_info(capsys):
picologging.root.handlers = []
picologging.info("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == ""
def test_root_logger_debug(capsys):
picologging.root.handlers = []
picologging.debug("test")
cap = capsys.readouterr()
assert cap.out == ""
assert cap.err == ""
def test_root_logger_log():
picologging.root.handlers = []
picologging.log(picologging.DEBUG, "test")
def test_basic_config_with_stream_and_filename_without_handlers():
picologging.root.handlers = []
with pytest.raises(ValueError):
picologging.basicConfig(stream=sys.stderr, filename="log.txt")
def test_basic_config_with_stream_or_filename_with_handlers():
handler = picologging.StreamHandler(sys.stderr)
with pytest.raises(ValueError):
picologging.basicConfig(handlers=[handler], stream=sys.stdout)
def test_basic_config_invalid_style():
with pytest.raises(ValueError):
picologging.basicConfig(style="!")
def test_basic_config_with_level():
picologging.basicConfig(level=picologging.INFO)
assert picologging.root.level == picologging.INFO
def test_basic_config_invalid_arguments():
picologging.root.handlers = []
with pytest.raises(ValueError):
picologging.basicConfig(invalid_argument="value")
def test_make_log_record():
log_record = picologging.makeLogRecord({"levelno": picologging.WARNING})
assert log_record.levelno == picologging.WARNING
@pytest.mark.parametrize("encoding", ["utf-8", None])
def test_basic_config_encoding(encoding):
picologging.basicConfig(filename="test.txt", encoding=encoding)
def test_shutdown():
handler = BufferingHandler(capacity=1)
logger = picologging.getLogger("test")
logger.setLevel(picologging.DEBUG)
logger.addHandler(handler)
logger.debug("test")
picologging.shutdown()
assert handler.buffer == []