PCM_Report/test_report_generation.py

128 lines
4.0 KiB
Python
Raw Normal View History

2025-12-11 14:32:31 +08:00
#!/usr/bin/env python3
"""
测试报告生成过程验证修复是否有效
"""
import json
import tempfile
import os
from pathlib import Path
def create_test_data():
"""创建测试用的脚本数据"""
# 模拟修复后的数据没有NULL值减少空单元格
test_data = {
"tables": [{
"token": "scriptTable1",
"startRow": 0,
"startCol": 0,
"cells": [
# 只包含有数据的单元格
{"row": 1, "col": 1, "value": "2025-11-27 11:30:00"},
{"row": 1, "col": 3, "value": "2025-11-27 15:00:00"},
{"row": 0, "col": 1, "value": "25.5"}, # 修复后不再是null
# 可以添加更多测试数据
{"row": 4, "col": 0, "value": "25.1"},
{"row": 4, "col": 1, "value": "25.3"},
{"row": 4, "col": 2, "value": "25.0"},
]
}]
}
return test_data
def test_data_parsing():
"""测试数据解析功能"""
print("=== 测试数据解析 ===")
# 导入报告生成器的解析函数
try:
from report_generator import _parse_script_tables
test_data = create_test_data()
parsed_tables = _parse_script_tables(test_data)
print(f"解析结果: {len(parsed_tables)} 个表格")
for token, table_spec in parsed_tables.items():
print(f" 表格 {token}:")
print(f" startRow: {table_spec.get('startRow')}")
print(f" startCol: {table_spec.get('startCol')}")
print(f" cells: {len(table_spec.get('cells', []))}")
# 统计单元格类型
cells = table_spec.get('cells', [])
valid_cells = 0
empty_cells = 0
null_cells = 0
for cell in cells:
value = cell.get('value')
if value is None:
null_cells += 1
elif str(value).strip() == "":
empty_cells += 1
else:
valid_cells += 1
print(f" 统计: 有效={valid_cells}, 空白={empty_cells}, NULL={null_cells}")
return True
except Exception as e:
print(f"测试失败: {e}")
return False
def simulate_word_processing():
"""模拟Word处理过程"""
print("\n=== 模拟Word处理 ===")
test_data = create_test_data()
table_spec = test_data["tables"][0]
cells = table_spec.get("cells", [])
processed_cells = 0
skipped_empty_cells = 0
print(f"开始处理 {len(cells)} 个单元格...")
for cell_info in cells:
if not isinstance(cell_info, dict):
continue
# 模拟修复后的逻辑:跳过空单元格
value = cell_info.get("value", "")
if value is None:
skipped_empty_cells += 1
continue
text = str(value)
if text.strip() == "":
skipped_empty_cells += 1
continue
# 模拟Word COM操作
row = cell_info.get("row", 0)
col = cell_info.get("col", 0)
print(f" 处理单元格 ({row}, {col}): '{text}'")
processed_cells += 1
print(f"处理完成: 处理了 {processed_cells} 个单元格, 跳过了 {skipped_empty_cells} 个空单元格")
return True
def main():
print("测试报告生成修复")
print("=" * 50)
success1 = test_data_parsing()
success2 = simulate_word_processing()
if success1 and success2:
print("\n✅ 所有测试通过!修复应该有效。")
print("\n建议:")
print("1. 现在可以重新尝试生成报告")
print("2. 检查日志文件中的处理统计信息")
print("3. 如果仍然卡住可能需要检查Word模板或COM环境")
else:
print("\n❌ 测试失败,需要进一步调试")
if __name__ == "__main__":
main()