- 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>
156 lines
4.3 KiB
Python
156 lines
4.3 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 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
|