- log_formatter 纯函数测试(format_debug_log/action_log/error_log 等) - PerformanceTracker 类测试 - logging 子模块存在性验证 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
239 lines
6.7 KiB
Python
239 lines
6.7 KiB
Python
"""douyin/logging 模块单元测试。
|
|
|
|
覆盖:
|
|
- log_formatter: 纯函数格式化测试
|
|
- performance_tracker: PerformanceTracker 类测试
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
class TestFormatDebugLog:
|
|
"""format_debug_log 纯函数测试。"""
|
|
|
|
def test_basic_message(self):
|
|
from douyin.logging.log_formatter import format_debug_log
|
|
|
|
result = format_debug_log("test message")
|
|
assert "[DEBUG]" in result
|
|
assert "test message" in result
|
|
|
|
def test_with_tag(self):
|
|
from douyin.logging.log_formatter import format_debug_log
|
|
|
|
result = format_debug_log("test", tag="MyTag")
|
|
assert "[MyTag]" in result
|
|
|
|
def test_with_level(self):
|
|
from douyin.logging.log_formatter import format_debug_log
|
|
|
|
result = format_debug_log("test", level="INFO")
|
|
assert "[INFO]" in result
|
|
|
|
def test_with_fields(self):
|
|
from douyin.logging.log_formatter import format_debug_log
|
|
|
|
result = format_debug_log("test", count=5, name="foo")
|
|
assert "count=5" in result
|
|
assert "name=foo" in result
|
|
|
|
|
|
class TestFormatActionLog:
|
|
"""format_action_log 纯函数测试。"""
|
|
|
|
def test_basic_action(self):
|
|
from douyin.logging.log_formatter import format_action_log
|
|
|
|
result = format_action_log("click")
|
|
assert "click" in result.lower() or len(result) > 0
|
|
|
|
def test_with_details(self):
|
|
from douyin.logging.log_formatter import format_action_log
|
|
|
|
result = format_action_log("Step", details="swipe_up_to_next_live")
|
|
# 应该包含中文映射或原始动作名
|
|
assert len(result) > 0
|
|
|
|
def test_with_elapsed_seconds(self):
|
|
from douyin.logging.log_formatter import format_action_log
|
|
|
|
result = format_action_log("test", elapsed_seconds=1.5)
|
|
assert "耗时" in result or "ms" in result
|
|
|
|
|
|
class TestFormatErrorLog:
|
|
"""format_error_log 纯函数测试。"""
|
|
|
|
def test_basic_error(self):
|
|
from douyin.logging.log_formatter import format_error_log
|
|
|
|
result = format_error_log("Something went wrong")
|
|
assert "wrong" in result.lower()
|
|
|
|
def test_with_exception(self):
|
|
from douyin.logging.log_formatter import format_error_log
|
|
|
|
result = format_error_log("Error", exception=ValueError("test error"))
|
|
assert len(result) > 0
|
|
|
|
|
|
class TestFormatCallStack:
|
|
"""format_call_stack 纯函数测试。"""
|
|
|
|
def test_empty_stack(self):
|
|
from douyin.logging.log_formatter import format_call_stack
|
|
|
|
result = format_call_stack(None)
|
|
assert isinstance(result, str)
|
|
|
|
def test_with_stack(self):
|
|
from douyin.logging.log_formatter import format_call_stack
|
|
|
|
stack = ["func1", "func2", "func3"]
|
|
result = format_call_stack(stack)
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestFormatParameters:
|
|
"""format_parameters 纯函数测试。"""
|
|
|
|
def test_empty_params(self):
|
|
from douyin.logging.log_formatter import format_parameters
|
|
|
|
result = format_parameters({})
|
|
assert isinstance(result, str)
|
|
|
|
def test_with_params(self):
|
|
from douyin.logging.log_formatter import format_parameters
|
|
|
|
result = format_parameters({"x": 1, "y": 2})
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestFormatVariables:
|
|
"""format_variables 纯函数测试。"""
|
|
|
|
def test_empty_vars(self):
|
|
from douyin.logging.log_formatter import format_variables
|
|
|
|
result = format_variables({})
|
|
assert isinstance(result, str)
|
|
|
|
def test_with_vars(self):
|
|
from douyin.logging.log_formatter import format_variables
|
|
|
|
result = format_variables({"a": "hello", "b": 123})
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestFormatException:
|
|
"""format_exception 纯函数测试。"""
|
|
|
|
def test_value_error(self):
|
|
from douyin.logging.log_formatter import format_exception
|
|
|
|
exc = ValueError("test error")
|
|
result = format_exception(exc)
|
|
assert "ValueError" in result or "test error" in result
|
|
|
|
def test_runtime_error(self):
|
|
from douyin.logging.log_formatter import format_exception
|
|
|
|
exc = RuntimeError("runtime issue")
|
|
result = format_exception(exc)
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestAddSeparators:
|
|
"""add_separators 纯函数测试。"""
|
|
|
|
def test_basic_text(self):
|
|
from douyin.logging.log_formatter import add_separators
|
|
|
|
result = add_separators("test content")
|
|
assert "test content" in result
|
|
|
|
def test_with_label(self):
|
|
from douyin.logging.log_formatter import add_separators
|
|
|
|
result = add_separators("content", label="Section")
|
|
assert "content" in result
|
|
|
|
|
|
class TestAddColors:
|
|
"""add_colors 纯函数测试。"""
|
|
|
|
def test_default_color(self):
|
|
from douyin.logging.log_formatter import add_colors
|
|
|
|
result = add_colors("text")
|
|
assert "text" in result
|
|
|
|
def test_custom_color(self):
|
|
from douyin.logging.log_formatter import add_colors
|
|
|
|
result = add_colors("text", color="red")
|
|
assert "text" in result
|
|
|
|
|
|
class TestBeautifyLogOutput:
|
|
"""beautify_log_output 纯函数测试。"""
|
|
|
|
def test_basic_line(self):
|
|
from douyin.logging.log_formatter import beautify_log_output
|
|
|
|
result = beautify_log_output("[INFO] test message")
|
|
assert isinstance(result, str)
|
|
|
|
def test_empty_line(self):
|
|
from douyin.logging.log_formatter import beautify_log_output
|
|
|
|
result = beautify_log_output("")
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestPerformanceTracker:
|
|
"""PerformanceTracker 类测试。"""
|
|
|
|
def test_class_exists(self):
|
|
from douyin.logging.performance_tracker import PerformanceTracker
|
|
|
|
assert PerformanceTracker is not None
|
|
|
|
def test_can_instantiate(self):
|
|
from douyin.logging.performance_tracker import PerformanceTracker
|
|
|
|
tracker = PerformanceTracker()
|
|
assert tracker is not None
|
|
|
|
|
|
class TestLoggingModules:
|
|
"""logging 模块存在性测试。"""
|
|
|
|
def test_action_logger_importable(self):
|
|
from douyin.logging import action_logger
|
|
|
|
assert action_logger is not None
|
|
|
|
def test_internal_loggers_importable(self):
|
|
from douyin.logging import internal_loggers
|
|
|
|
assert internal_loggers is not None
|
|
|
|
def test_unified_logger_importable(self):
|
|
from douyin.logging import unified_logger
|
|
|
|
assert unified_logger is not None
|
|
|
|
def test_douyin_logging_importable(self):
|
|
from douyin.logging import douyin_logging
|
|
|
|
assert douyin_logging is not None
|
|
|
|
def test_douyin_logging_wrapper_importable(self):
|
|
from douyin.logging import douyin_logging_wrapper
|
|
|
|
assert douyin_logging_wrapper is not None
|