- test_check_contain.py:fudai_filter.check_contain 黑名单关键词检查 * 空黑名单 / 命中 / 未命中 / None / 空字符串 * 命中后短路(不继续遍历) * 黑名单空字符串关键词不误匹配 * 缺 contains_not_want 属性 / broken _log_kv 兜底 - test_log_formatter.py:9 个格式化纯函数 * format_action_log / format_debug_log / format_error_log(前缀 + tag + 字段) * format_parameters / format_variables / format_call_stack * format_exception(含 traceback) * add_colors(默认绿 / red / 未知颜色兜底) * add_separators(带/不带 label) * beautify_log_output / colorize_log_line(不抛 + 一致性) 回归:tests/ 308 passed / 1 skipped。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
185 lines
5.6 KiB
Python
185 lines
5.6 KiB
Python
"""douyin/logging/log_formatter 的 pytest 单元测试。
|
|
|
|
覆盖 9 个格式化纯函数:
|
|
- format_action_log / format_debug_log / format_error_log
|
|
- format_parameters / format_variables / format_call_stack
|
|
- format_exception / add_colors / add_separators
|
|
- 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
|
|
|
|
|
|
# ============================================================
|
|
# format_parameters / format_variables
|
|
# ============================================================
|
|
|
|
class TestFormatParametersVariables:
|
|
def test_format_parameters_with_data(self):
|
|
result = f.format_parameters({"a": 1, "b": "x"})
|
|
assert "a=1" in result
|
|
assert "b=x" in result
|
|
|
|
def test_format_parameters_empty(self):
|
|
assert f.format_parameters({}) == ""
|
|
|
|
def test_format_variables_with_data(self):
|
|
result = f.format_variables({"k": "v"})
|
|
assert "k=v" in result
|
|
|
|
def test_format_variables_empty(self):
|
|
assert f.format_variables({}) == ""
|
|
|
|
|
|
# ============================================================
|
|
# format_call_stack
|
|
# ============================================================
|
|
|
|
class TestFormatCallStack:
|
|
def test_empty(self):
|
|
assert f.format_call_stack() == ""
|
|
|
|
def test_with_frames(self):
|
|
result = f.format_call_stack(["frame1", "frame2"])
|
|
assert "frame1" in result
|
|
assert "frame2" in result
|
|
|
|
|
|
# ============================================================
|
|
# format_exception
|
|
# ============================================================
|
|
|
|
class TestFormatException:
|
|
def test_includes_traceback(self):
|
|
try:
|
|
raise RuntimeError("boom")
|
|
except RuntimeError as e:
|
|
result = f.format_exception(e)
|
|
assert "RuntimeError" in result
|
|
assert "boom" in result
|
|
|
|
def test_returns_string(self):
|
|
try:
|
|
raise ValueError("bad")
|
|
except ValueError as e:
|
|
result = f.format_exception(e)
|
|
assert isinstance(result, str)
|
|
assert len(result) > 0
|
|
|
|
|
|
# ============================================================
|
|
# add_colors / add_separators
|
|
# ============================================================
|
|
|
|
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
|
|
|
|
|
|
class TestAddSeparators:
|
|
def test_with_label(self):
|
|
result = f.add_separators("content", label="SECTION")
|
|
assert "content" in result
|
|
assert "SECTION" in result
|
|
# 包含分隔线
|
|
assert "=" * 80 in result
|
|
|
|
def test_without_label(self):
|
|
result = f.add_separators("content")
|
|
assert "content" in result
|
|
assert "=" * 80 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)
|