- unified_logger.py: 删除 5 个从未使用的装饰器/函数 - internal_loggers.py: 删除 3 个未使用方法和工厂函数 - action_logger.py: 删除 3 个未使用方法 - log_formatter.py: 删除 6 个未使用格式化函数(保留 GUI 依赖的 4 个) - 同步更新 __init__.py 和测试文件 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
113 lines
3.4 KiB
Python
113 lines
3.4 KiB
Python
"""douyin/logging/log_formatter 的 pytest 单元测试。
|
|
|
|
覆盖格式化纯函数:
|
|
- format_action_log / format_debug_log / format_error_log
|
|
- add_colors
|
|
- beautify_log_output / colorize_log_line(一致性 + 不抛断言)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from douyin.logging import log_formatter as f
|
|
|
|
|
|
# ============================================================
|
|
# format_action_log
|
|
# ============================================================
|
|
|
|
class TestFormatActionLog:
|
|
def test_action_only(self):
|
|
assert f.format_action_log("click") == "[ACTION] click"
|
|
|
|
def test_action_with_details(self):
|
|
assert f.format_action_log("click", "button") == "[ACTION] click"
|
|
|
|
def test_action_with_kv_fields(self):
|
|
result = f.format_action_log("click", x=100, y=200)
|
|
# 字段顺序按 dict 插入序
|
|
assert "[ACTION] click" in result
|
|
assert "x=100" in result
|
|
assert "y=200" in result
|
|
|
|
|
|
# ============================================================
|
|
# format_debug_log
|
|
# ============================================================
|
|
|
|
class TestFormatDebugLog:
|
|
def test_default_level(self):
|
|
assert f.format_debug_log("msg") == "[DEBUG] msg"
|
|
|
|
def test_custom_level(self):
|
|
assert f.format_debug_log("msg", level="INFO") == "[INFO] msg"
|
|
|
|
def test_with_tag(self):
|
|
result = f.format_debug_log("msg", tag="TEST", x=1)
|
|
assert "[TEST]" in result
|
|
assert "[DEBUG]" in result
|
|
assert "msg" in result
|
|
assert "x=1" in result
|
|
|
|
|
|
# ============================================================
|
|
# format_error_log
|
|
# ============================================================
|
|
|
|
class TestFormatErrorLog:
|
|
def test_default_level(self):
|
|
assert f.format_error_log("failure") == "[ERROR] failure"
|
|
|
|
def test_with_tag_and_level(self):
|
|
result = f.format_error_log("failure", level="WARNING", tag="IO")
|
|
assert "[IO]" in result
|
|
assert "[WARNING]" in result
|
|
assert "failure" in result
|
|
|
|
|
|
# ============================================================
|
|
# add_colors
|
|
# ============================================================
|
|
|
|
class TestAddColors:
|
|
def test_default_color_green(self):
|
|
# 默认绿色 ANSI 码 \x1b[32m
|
|
result = f.add_colors("hi")
|
|
assert "\x1b[32m" in result
|
|
assert "hi" in result
|
|
assert "\x1b[0m" in result
|
|
|
|
def test_red_color(self):
|
|
result = f.add_colors("hi", color="red")
|
|
assert "\x1b[31m" in result
|
|
assert "hi" in result
|
|
|
|
def test_unknown_color_does_not_crash(self):
|
|
# 未知颜色应不抛
|
|
result = f.add_colors("hi", color="不存在的颜色")
|
|
assert "hi" in result
|
|
|
|
|
|
# ============================================================
|
|
# beautify_log_output / colorize_log_line(一致性 / 不抛)
|
|
# ============================================================
|
|
|
|
class TestBeautify:
|
|
@pytest.mark.parametrize(
|
|
"line",
|
|
["plain line", "", "[INFO] message", "中文行", "x" * 500],
|
|
)
|
|
def test_does_not_crash(self, line):
|
|
result = f.beautify_log_output(line)
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestColorize:
|
|
@pytest.mark.parametrize(
|
|
"line",
|
|
["test", "", "[ERROR] something failed", "中文行"],
|
|
)
|
|
def test_does_not_crash(self, line):
|
|
result = f.colorize_log_line(line)
|
|
assert isinstance(result, str)
|