Files
zccrs-comfyui-dci/tests/test_pure_python_implementation.py
T
JiDe Zhang 0a6169bb7e refactor: 整理测试文件结构,统一移动到tests目录
将散落在项目根目录的测试文件统一整理到tests目录中,提高项目结构的清晰度和可维护性。

主要变更:
1. 移动测试文件:将根目录的test_*.py文件移动到tests/目录
2. 移动测试资源:将test_*.png图片文件移动到tests/目录
3. 移动测试输出:将test_output.dci文件移动到tests/目录
4. 清理环境:删除不需要的test_env虚拟环境目录
5. 更新文档:更新tests/README.md,详细记录所有测试文件分类

移动的文件包括:
- test_advanced_compression.py - 高级压缩功能测试
- test_border_color.py - 边框颜色功能测试
- test_categories.py - 分类系统测试
- test_quality_feature.py - 图片质量功能测试
- test_transparent_preview.py - 透明预览测试
- test_pure_python_implementation.py - 纯Python实现测试
- test_pure_python_simple.py - 简单纯Python测试
- simple_quality_test.py - 简单质量测试脚本
- test_preview_*.png - 预览测试图片
- test_transparent_preview.png - 透明预览测试图片

这次整理确保了:
- 项目根目录更加清洁,只保留核心配置文件
- 所有测试相关文件集中管理,便于维护和执行
- 测试文档完整记录了所有测试文件的用途和分类
- 符合标准的Python项目结构规范

refactor: Organize test file structure by moving all to tests directory

Consolidated scattered test files from project root to tests directory
to improve project structure clarity and maintainability.

Key changes:
1. Move test files: Relocated test_*.py files from root to tests/
2. Move test assets: Relocated test_*.png image files to tests/
3. Move test output: Relocated test_output.dci file to tests/
4. Clean environment: Removed unnecessary test_env virtual environment
5. Update documentation: Enhanced tests/README.md with detailed file classification

Moved files include:
- test_advanced_compression.py - Advanced compression feature tests
- test_border_color.py - Border color functionality tests
- test_categories.py - Category system tests
- test_quality_feature.py - Image quality feature tests
- test_transparent_preview.py - Transparent preview tests
- test_pure_python_implementation.py - Pure Python implementation tests
- test_pure_python_simple.py - Simple pure Python tests
- simple_quality_test.py - Simple quality testing script
- test_preview_*.png - Preview test images
- test_transparent_preview.png - Transparent preview test image

This reorganization ensures:
- Cleaner project root with only core configuration files
- Centralized test management for easier maintenance and execution
- Complete test documentation with proper file categorization
- Compliance with standard Python project structure conventions
2025-06-12 19:46:01 +08:00

180 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""
测试纯Python实现的ar功能
验证deb包的创建和解析都不依赖外部命令
"""
import os
import sys
import tempfile
import shutil
# 添加项目路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'py'))
from nodes.deb_packager_node import DebPackager
from nodes.deb_loader_node import DebLoader
def test_pure_python_ar_implementation():
"""测试纯Python ar实现"""
print("=== 测试纯Python ar实现 ===")
# 创建测试环境
with tempfile.TemporaryDirectory() as temp_dir:
print(f"测试目录: {temp_dir}")
# 1. 创建测试文件
test_files_dir = os.path.join(temp_dir, "test_files")
os.makedirs(test_files_dir)
# 创建一些测试DCI文件
test_dci_content = b"DCI test content for pure Python implementation"
for i in range(3):
dci_file = os.path.join(test_files_dir, f"test_{i}.dci")
with open(dci_file, 'wb') as f:
f.write(test_dci_content + f" {i}".encode())
print(f"创建了3个测试DCI文件")
# 2. 使用deb packager创建deb包(纯Python实现)
output_dir = os.path.join(temp_dir, "output")
os.makedirs(output_dir)
packager = DebPackager()
result = packager._execute_impl(
local_directory=test_files_dir,
file_filter="*.dci",
include_subdirectories=True,
install_target_path="/usr/share/test/icons",
output_directory=output_dir,
package_name="test-dci-package",
package_version="1.0.0",
maintainer_name="Test User",
maintainer_email="test@example.com",
package_description="Test DCI package for pure Python implementation"
)
deb_path, file_list = result
print(f"Deb包创建结果: {deb_path}")
print(f"包含文件数量: {len(file_list)}")
# 验证deb文件是否创建成功
if not os.path.exists(deb_path) or "错误" in deb_path:
print("❌ Deb包创建失败")
return False
deb_size = os.path.getsize(deb_path)
print(f"✓ Deb包创建成功: {deb_size} 字节")
# 3. 使用deb loader解析deb包(纯Python实现)
loader = DebLoader()
load_result = loader._execute_impl(
deb_file_path=deb_path,
file_filter="*.dci"
)
binary_data_list, relative_paths, image_list, image_relative_paths = load_result
print(f"解析结果:")
print(f" 二进制数据: {len(binary_data_list)} 个文件")
print(f" 相对路径: {len(relative_paths)} 个")
print(f" 图像数据: {len(image_list) if isinstance(image_list, list) else 'tensor'}")
print(f" 图像路径: {len(image_relative_paths)} 个")
# 验证解析结果
if len(binary_data_list) != 3:
print(f"❌ 解析的文件数量不正确,期望3个,实际{len(binary_data_list)}个")
return False
# 验证文件内容
for i, (data, path) in enumerate(zip(binary_data_list, relative_paths)):
expected_content = test_dci_content + f" {i}".encode()
if data != expected_content:
print(f"❌ 文件内容不匹配: {path}")
return False
print(f" ✓ 文件内容验证成功: {path}")
print("✓ 所有测试通过!纯Python实现工作正常")
return True
def test_ar_format_validation():
"""测试ar格式验证"""
print("\n=== 测试ar格式验证 ===")
with tempfile.TemporaryDirectory() as temp_dir:
# 创建一个简单的ar归档
packager = DebPackager()
# 创建测试文件
test_file = os.path.join(temp_dir, "test.txt")
with open(test_file, 'w') as f:
f.write("Hello, World!")
# 创建ar归档
ar_path = os.path.join(temp_dir, "test.ar")
success = packager._create_ar_archive_python(ar_path, ["test.txt"], temp_dir)
if not success:
print("❌ ar归档创建失败")
return False
# 验证ar格式
with open(ar_path, 'rb') as f:
magic = f.read(8)
if magic != b'!<arch>\n':
print(f"❌ ar魔数不正确: {magic}")
return False
ar_size = os.path.getsize(ar_path)
print(f"✓ ar归档创建成功: {ar_size} 字节")
print(f"✓ ar格式验证通过")
# 测试解析
extract_dir = os.path.join(temp_dir, "extract")
os.makedirs(extract_dir)
success = packager._extract_ar_archive_python(ar_path, extract_dir)
if not success:
print("❌ ar归档解析失败")
return False
# 验证提取的文件
extracted_file = os.path.join(extract_dir, "test.txt")
if not os.path.exists(extracted_file):
print("❌ 文件未正确提取")
return False
with open(extracted_file, 'r') as f:
content = f.read()
if content != "Hello, World!":
print(f"❌ 文件内容不正确: {content}")
return False
print("✓ ar归档解析验证通过")
return True
def main():
"""主测试函数"""
print("开始测试纯Python实现...")
# 测试ar格式
if not test_ar_format_validation():
print("❌ ar格式测试失败")
return False
# 测试完整的deb包流程
if not test_pure_python_ar_implementation():
print("❌ 完整流程测试失败")
return False
print("\n🎉 所有测试通过!纯Python实现完全正常工作")
print("✓ 不再依赖任何外部命令")
print("✓ 跨平台兼容性良好")
print("✓ ar格式完全兼容")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)