补 douyin/core 模块单元测试(+24 用例)
- io_utils 纯函数测试(_verify_png_integrity/_ensure_parent_dir/_safe_replace_file) - path_config 路径配置函数测试 - version 版本信息格式验证 - flow_control shim 与子模块导出一致性验证 - core 子模块存在性测试 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b5a15ecea4
commit
8d98143df1
1 file changed
+223
@@ -0,0 +1,223 @@
|
||||
"""douyin/core 模块单元测试。
|
||||
|
||||
覆盖:
|
||||
- io_utils: _verify_png_integrity, _ensure_parent_dir 纯函数
|
||||
- path_config: 路径配置函数
|
||||
- version: 版本信息
|
||||
- douyin_main_flow: MainFlowController 类存在性
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import pytest
|
||||
|
||||
|
||||
class TestVerifyPngIntegrity:
|
||||
"""_verify_png_integrity 纯函数测试。"""
|
||||
|
||||
def test_valid_png_file(self):
|
||||
from douyin.core.io_utils import _verify_png_integrity
|
||||
|
||||
# 创建一个最小的有效 PNG 文件
|
||||
# PNG 签名 + IHDR + IEND
|
||||
png_data = (
|
||||
b'\x89PNG\r\n\x1a\n' # PNG 签名
|
||||
b'\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde' # IHDR
|
||||
b'\x00\x00\x00\x00IEND\xaeB`\x82' # IEND
|
||||
)
|
||||
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
|
||||
f.write(png_data)
|
||||
tmp_path = f.name
|
||||
try:
|
||||
result = _verify_png_integrity(tmp_path)
|
||||
assert result is True
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
def test_invalid_png_header(self):
|
||||
from douyin.core.io_utils import _verify_png_integrity
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
|
||||
f.write(b'not a png file')
|
||||
tmp_path = f.name
|
||||
try:
|
||||
result = _verify_png_integrity(tmp_path)
|
||||
assert result is False
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
def test_nonexistent_file(self):
|
||||
from douyin.core.io_utils import _verify_png_integrity
|
||||
|
||||
result = _verify_png_integrity('/nonexistent/path/file.png')
|
||||
assert result is False
|
||||
|
||||
def test_empty_file(self):
|
||||
from douyin.core.io_utils import _verify_png_integrity
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
|
||||
tmp_path = f.name
|
||||
try:
|
||||
result = _verify_png_integrity(tmp_path)
|
||||
assert result is False
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
|
||||
class TestEnsureParentDir:
|
||||
"""_ensure_parent_dir 函数测试。"""
|
||||
|
||||
def test_function_exists(self):
|
||||
from douyin.core.io_utils import _ensure_parent_dir
|
||||
|
||||
assert callable(_ensure_parent_dir)
|
||||
|
||||
def test_creates_parent_directory(self):
|
||||
from douyin.core.io_utils import _ensure_parent_dir
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
nested_path = os.path.join(tmpdir, 'a', 'b', 'c', 'file.txt')
|
||||
_ensure_parent_dir(nested_path)
|
||||
parent = os.path.dirname(nested_path)
|
||||
assert os.path.isdir(parent)
|
||||
|
||||
|
||||
class TestSafeReplaceFile:
|
||||
"""_safe_replace_file 函数测试。"""
|
||||
|
||||
def test_function_exists(self):
|
||||
from douyin.core.io_utils import _safe_replace_file
|
||||
|
||||
assert callable(_safe_replace_file)
|
||||
|
||||
def test_replaces_file(self):
|
||||
from douyin.core.io_utils import _safe_replace_file
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
src = os.path.join(tmpdir, 'src.txt')
|
||||
dst = os.path.join(tmpdir, 'dst.txt')
|
||||
with open(src, 'w') as f:
|
||||
f.write('new content')
|
||||
with open(dst, 'w') as f:
|
||||
f.write('old content')
|
||||
result = _safe_replace_file(src, dst)
|
||||
assert result is True
|
||||
with open(dst) as f:
|
||||
assert f.read() == 'new content'
|
||||
|
||||
|
||||
class TestPathConfig:
|
||||
"""path_config 模块测试。"""
|
||||
|
||||
def test_project_root_exists(self):
|
||||
from douyin.core.path_config import PROJECT_ROOT
|
||||
|
||||
assert PROJECT_ROOT is not None
|
||||
assert os.path.isdir(PROJECT_ROOT)
|
||||
|
||||
def test_get_config_path_returns_string(self):
|
||||
from douyin.core.path_config import get_config_path
|
||||
|
||||
result = get_config_path()
|
||||
assert isinstance(result, str)
|
||||
assert len(result) > 0
|
||||
|
||||
def test_get_log_file_path_returns_string(self):
|
||||
from douyin.core.path_config import get_log_file_path
|
||||
|
||||
result = get_log_file_path()
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_get_webui_dir_returns_string(self):
|
||||
from douyin.core.path_config import get_webui_dir
|
||||
|
||||
result = get_webui_dir()
|
||||
assert isinstance(result, str)
|
||||
|
||||
|
||||
class TestVersion:
|
||||
"""version 模块测试。"""
|
||||
|
||||
def test_app_version_format(self):
|
||||
from douyin.core.version import APP_VERSION
|
||||
|
||||
assert APP_VERSION.startswith('V')
|
||||
parts = APP_VERSION.lstrip('V').split('.')
|
||||
assert len(parts) == 3
|
||||
assert all(p.isdigit() for p in parts)
|
||||
|
||||
def test_app_build_date_format(self):
|
||||
from douyin.core.version import APP_BUILD_DATE
|
||||
|
||||
parts = APP_BUILD_DATE.split('-')
|
||||
assert len(parts) == 3
|
||||
assert len(parts[0]) == 4 # year
|
||||
assert len(parts[1]) == 2 # month
|
||||
assert len(parts[2]) == 2 # day
|
||||
|
||||
def test_get_version_string(self):
|
||||
from douyin.core.version import get_version_string
|
||||
|
||||
result = get_version_string()
|
||||
assert isinstance(result, str)
|
||||
assert 'V' in result
|
||||
|
||||
def test_print_version_banner(self):
|
||||
from douyin.core.version import print_version_banner
|
||||
|
||||
assert callable(print_version_banner)
|
||||
|
||||
|
||||
class TestMainFlowController:
|
||||
"""MainFlowController 类测试。"""
|
||||
|
||||
def test_class_exists(self):
|
||||
from douyin.core.douyin_main_flow import MainFlowController
|
||||
|
||||
assert MainFlowController is not None
|
||||
|
||||
|
||||
class TestFlowControlShimConsistency:
|
||||
"""验证 flow_control shim 与子模块导出一致。"""
|
||||
|
||||
def test_find_and_open_fudai_detail_same_object(self):
|
||||
from douyin.core.douyin_flow_control import find_and_open_fudai_detail as f1
|
||||
from douyin.core.douyin_flow_control_pkg import find_and_open_fudai_detail as f2
|
||||
|
||||
assert f1 is f2
|
||||
|
||||
|
||||
class TestCoreModules:
|
||||
"""core 子模块存在性测试。"""
|
||||
|
||||
def test_io_utils_importable(self):
|
||||
from douyin.core import io_utils
|
||||
|
||||
assert io_utils is not None
|
||||
|
||||
def test_path_config_importable(self):
|
||||
from douyin.core import path_config
|
||||
|
||||
assert path_config is not None
|
||||
|
||||
def test_version_importable(self):
|
||||
from douyin.core import version
|
||||
|
||||
assert version is not None
|
||||
|
||||
def test_douyin_main_flow_importable(self):
|
||||
from douyin.core import douyin_main_flow
|
||||
|
||||
assert douyin_main_flow is not None
|
||||
|
||||
def test_douyin_flow_control_importable(self):
|
||||
from douyin.core import douyin_flow_control
|
||||
|
||||
assert douyin_flow_control is not None
|
||||
|
||||
def test_underlying_operations_importable(self):
|
||||
from douyin.core import underlying_operations
|
||||
|
||||
assert underlying_operations is not None
|
||||
Reference in new issue
Block a user