-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest_utils_cli.py
56 lines (40 loc) · 1.58 KB
/
test_utils_cli.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
import logging
from pytest import LogCaptureFixture
from fastapi_cli.utils.cli import CustomFormatter, get_uvicorn_log_config
def test_get_uvicorn_config_uses_custom_formatter() -> None:
config = get_uvicorn_log_config()
assert config["formatters"]["default"]["()"] is CustomFormatter
assert config["formatters"]["access"]["()"] is CustomFormatter
def test_custom_formatter() -> None:
formatter = CustomFormatter()
record = logging.LogRecord(
name="uvicorn.access",
level=logging.INFO,
pathname="",
lineno=0,
msg="%(client_addr)s - '%(request_line)s' %(status_code)s",
args={
"client_addr": "127.0.0.1",
"request_line": "GET / HTTP/1.1",
"status_code": 200,
},
exc_info=None,
)
formatted = formatter.formatMessage(record)
assert "INFO" in formatted
assert "127.0.0.1" in formatted
assert "GET / HTTP/1.1" in formatted
assert "200" in formatted
def test_log_config_does_not_disable_existing_loggers(
caplog: LogCaptureFixture,
) -> None:
logger1 = logging.getLogger(__name__)
logger1.setLevel(logging.INFO)
logger1.info("Message before configuration")
logging.config.dictConfig(get_uvicorn_log_config())
logger2 = logging.getLogger(__name__)
logger1.info("Message after configuration from logger1") # Should not appear
logger2.info("Message from logger2")
assert "Message before configuration" in caplog.text
assert "Message after configuration from logger1" in caplog.text
assert "Message from logger2" in caplog.text