-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
test_logging.py
53 lines (44 loc) · 2.16 KB
/
test_logging.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
from __future__ import annotations
import logging
import pytest
from charset_normalizer.api import explain_handler, from_bytes
from charset_normalizer.constant import TRACE
from charset_normalizer.utils import set_logging_handler
class TestLogBehaviorClass:
def setup_method(self):
self.logger = logging.getLogger("charset_normalizer")
self.logger.handlers.clear()
self.logger.addHandler(logging.NullHandler())
self.logger.level = logging.WARNING
def test_explain_true_behavior(self, caplog):
test_sequence = b"This is a test sequence of bytes that should be sufficient"
from_bytes(test_sequence, steps=1, chunk_size=50, explain=True)
assert explain_handler not in self.logger.handlers
for record in caplog.records:
assert record.levelname in ["Level 5", "DEBUG"]
def test_explain_false_handler_set_behavior(self, caplog):
test_sequence = b"This is a test sequence of bytes that should be sufficient"
set_logging_handler(level=TRACE, format_string="%(message)s")
from_bytes(test_sequence, steps=1, chunk_size=50, explain=False)
assert any(
isinstance(hdl, logging.StreamHandler) for hdl in self.logger.handlers
)
for record in caplog.records:
assert record.levelname in ["Level 5", "DEBUG"]
assert "Encoding detection: ascii is most likely the one." in caplog.text
def test_set_stream_handler(self, caplog):
set_logging_handler("charset_normalizer", level=logging.DEBUG)
self.logger.debug("log content should log with default format")
for record in caplog.records:
assert record.levelname in ["Level 5", "DEBUG"]
assert "log content should log with default format" in caplog.text
def test_set_stream_handler_format(self, caplog):
set_logging_handler("charset_normalizer", format_string="%(message)s")
self.logger.info("log content should only be this message")
assert caplog.record_tuples == [
(
"charset_normalizer",
logging.INFO,
"log content should only be this message",
)
]