PCM_Report/test_report_with_mock_data.py

124 lines
4.1 KiB
Python
Raw Normal View History

2025-12-11 14:32:31 +08:00
#!/usr/bin/env python3
"""
使用模拟数据测试报告生成绕过InfluxDB问题
"""
import json
import sys
import os
from pathlib import Path
def create_test_script_data():
"""创建测试用的脚本数据"""
return {
"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 12:00:00"},
# 环境温度
{"row": 0, "col": 1, "value": "25.5"},
# 一些轴承温度数据
{"row": 4, "col": 0, "value": "45.2"},
{"row": 4, "col": 1, "value": "46.1"},
{"row": 4, "col": 2, "value": "44.8"},
{"row": 5, "col": 0, "value": "47.3"},
{"row": 5, "col": 1, "value": "46.9"},
{"row": 6, "col": 0, "value": "45.8"},
]
}]
}
def test_script_table_parsing():
"""测试脚本表格解析"""
try:
# 添加项目路径到sys.path
project_path = Path(__file__).parent
if str(project_path) not in sys.path:
sys.path.insert(0, str(project_path))
from report_generator import _parse_script_tables
test_data = create_test_script_data()
parsed_tables = _parse_script_tables(test_data)
print("✓ 脚本表格解析成功")
print(f" 解析到 {len(parsed_tables)} 个表格")
for token, table_spec in parsed_tables.items():
cells = table_spec.get('cells', [])
print(f" 表格 {token}: {len(cells)} 个单元格")
# 统计有效数据
valid_cells = [c for c in cells if c.get('value') and str(c.get('value')).strip()]
print(f" 有效数据: {len(valid_cells)} 个单元格")
return True, parsed_tables
except Exception as e:
print(f"✗ 脚本表格解析失败: {e}")
return False, {}
def simulate_word_table_filling(parsed_tables):
"""模拟Word表格填充过程"""
print("\n模拟Word表格填充:")
for token, table_spec in parsed_tables.items():
print(f"\n处理表格 {token}:")
cells = table_spec.get('cells', [])
processed_cells = 0
skipped_empty_cells = 0
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} 个空单元格")
def main():
print("报告生成测试 (使用模拟数据)")
print("=" * 50)
# 测试脚本表格解析
success, parsed_tables = test_script_table_parsing()
if success:
# 模拟Word表格填充
simulate_word_table_filling(parsed_tables)
print("\n✅ 测试完成!")
print("\n建议:")
print("1. 如果这个测试正常,说明报告生成逻辑没问题")
print("2. 客户机器卡住可能是由于:")
print(" - Word COM操作环境问题")
print(" - 模板文件问题")
print(" - 权限问题")
print("3. 可以尝试使用 temperature_table_robust.py 替代原脚本")
else:
print("\n❌ 测试失败需要检查report_generator.py")
if __name__ == "__main__":
main()