#!/usr/bin/env python # -*- coding: utf-8 -*- """ 测试完整的数据转换和 SQL Server 写入流程 """ import json from convert_table_to_sqlserver_format import convert_temperature_table_to_sqlserver # 实际的脚本返回数据 script_data = { "tables": [{ "token": "scriptTable1", "startRow": 0, "startCol": 0, "cells": [ {"row": 4, "col": 0, "value": "14.0"}, {"row": 4, "col": 1, "value": "14.2"}, {"row": 4, "col": 2, "value": "14.2"}, {"row": 4, "col": 3, "value": "14.3"}, {"row": 4, "col": 4, "value": "14.1"}, {"row": 4, "col": 5, "value": "13.9"}, {"row": 4, "col": 6, "value": ""}, {"row": 5, "col": 0, "value": "13.7"}, {"row": 5, "col": 1, "value": "13.9"}, {"row": 5, "col": 2, "value": "13.9"}, {"row": 5, "col": 3, "value": "13.9"}, {"row": 5, "col": 4, "value": "13.7"}, {"row": 5, "col": 5, "value": "13.6"}, {"row": 5, "col": 6, "value": ""}, {"row": 6, "col": 0, "value": "14.5"}, {"row": 6, "col": 1, "value": "14.7"}, {"row": 6, "col": 2, "value": "14.8"}, {"row": 6, "col": 3, "value": "14.8"}, {"row": 6, "col": 4, "value": "14.8"}, {"row": 6, "col": 5, "value": "14.7"}, {"row": 6, "col": 6, "value": ""}, {"row": 7, "col": 0, "value": "13.9"}, {"row": 7, "col": 1, "value": "14.1"}, {"row": 7, "col": 2, "value": "14.2"}, {"row": 7, "col": 3, "value": "14.3"}, {"row": 7, "col": 4, "value": "14.2"}, {"row": 7, "col": 5, "value": "14.1"}, {"row": 7, "col": 6, "value": ""}, {"row": 9, "col": 0, "value": "13.8"}, {"row": 9, "col": 1, "value": "13.9"}, {"row": 9, "col": 2, "value": "14.0"}, {"row": 9, "col": 3, "value": "14.0"}, {"row": 9, "col": 4, "value": "13.9"}, {"row": 9, "col": 5, "value": "13.9"}, {"row": 9, "col": 6, "value": ""}, {"row": 10, "col": 0, "value": "13.8"}, {"row": 10, "col": 1, "value": "14.0"}, {"row": 10, "col": 2, "value": "14.1"}, {"row": 10, "col": 3, "value": "14.2"}, {"row": 10, "col": 4, "value": "14.1"}, {"row": 10, "col": 5, "value": "14.0"}, {"row": 10, "col": 6, "value": ""}, {"row": 11, "col": 0, "value": "12.5"}, {"row": 11, "col": 1, "value": "12.7"}, {"row": 11, "col": 2, "value": "12.8"}, {"row": 11, "col": 3, "value": "12.9"}, {"row": 11, "col": 4, "value": "12.8"}, {"row": 11, "col": 5, "value": "12.7"}, {"row": 11, "col": 6, "value": ""}, {"row": 12, "col": 0, "value": "14.0"}, {"row": 12, "col": 1, "value": "14.2"}, {"row": 12, "col": 2, "value": "14.3"}, {"row": 12, "col": 3, "value": "14.4"}, {"row": 12, "col": 4, "value": "14.3"}, {"row": 12, "col": 5, "value": "14.2"}, {"row": 12, "col": 6, "value": ""}, {"row": 13, "col": 0, "value": "13.5"}, {"row": 13, "col": 1, "value": "13.7"}, {"row": 13, "col": 2, "value": "13.7"}, {"row": 13, "col": 3, "value": "13.8"}, {"row": 13, "col": 4, "value": "13.7"}, {"row": 13, "col": 5, "value": "13.5"}, {"row": 13, "col": 6, "value": ""}, {"row": 14, "col": 0, "value": "13.4"}, {"row": 14, "col": 1, "value": "13.5"}, {"row": 14, "col": 2, "value": "13.5"}, {"row": 14, "col": 3, "value": "13.6"}, {"row": 14, "col": 4, "value": "13.5"}, {"row": 14, "col": 5, "value": "13.4"}, {"row": 14, "col": 6, "value": ""}, {"row": 15, "col": 0, "value": "14.8"}, {"row": 15, "col": 1, "value": "15.0"}, {"row": 15, "col": 2, "value": "15.1"}, {"row": 15, "col": 3, "value": "15.2"}, {"row": 15, "col": 4, "value": "15.1"}, {"row": 15, "col": 5, "value": "15.0"}, {"row": 15, "col": 6, "value": ""}, {"row": 1, "col": 1, "value": "2025-12-05 14:00:00"}, {"row": 1, "col": 3, "value": "2025-12-05 17:30:00"}, {"row": 0, "col": 1, "value": "13.4"} ] }] } work_order_no = "W2001150.001" print("=" * 80) print("完整数据转换流程测试") print("=" * 80) # 步骤1: 转换数据 print("\n步骤1: 转换表格数据为 SQL Server 格式") print("-" * 80) converted_data = convert_temperature_table_to_sqlserver(script_data, work_order_no) print(f"✅ 转换完成,共 {len(converted_data)} 个字段") print(f" - 工单号: {converted_data.get('order_no')}") print(f" - 环境温度: {converted_data.get('ambient_temp_c')}°C") print(f" - 开始时间: {converted_data.get('start_time')}") print(f" - 结束时间: {converted_data.get('end_time')}") # 统计温度字段 temp_fields = [k for k in converted_data.keys() if k.startswith('temp_')] print(f" - 温度字段: {len(temp_fields)} 个") # 步骤2: 验证数据完整性 print("\n步骤2: 验证数据完整性") print("-" * 80) # 检查必填字段 required_fields = ['order_no'] missing_required = [f for f in required_fields if f not in converted_data or not converted_data[f]] if missing_required: print(f"❌ 缺少必填字段: {', '.join(missing_required)}") else: print("✅ 所有必填字段都存在") # 检查温度数据 expected_parts = [ 'main_1', 'main_2', 'main_3', 'main_4', 'crosshead_1', 'crosshead_2', 'crosshead_3', 'gbox_small_1', 'gbox_small_2', 'gbox_big_3', 'gbox_big_4' ] expected_times = ['t05', 't10', 't15', 't20', 't25', 't30'] # 只检查有数据的时间点 total_expected = len(expected_parts) * len(expected_times) actual_temp_fields = len(temp_fields) print(f"✅ 温度字段: {actual_temp_fields}/{total_expected} (预期有数据的字段)") # 步骤3: 模拟 SQL Server 写入 print("\n步骤3: 模拟 SQL Server 写入") print("-" * 80) print("准备写入的数据示例:") print(json.dumps({ "order_no": converted_data.get('order_no'), "ambient_temp_c": converted_data.get('ambient_temp_c'), "start_time": converted_data.get('start_time'), "end_time": converted_data.get('end_time'), "temp_main_1_t05": converted_data.get('temp_main_1_t05'), "temp_main_1_t10": converted_data.get('temp_main_1_t10'), "...": "... (其他 64 个温度字段)" }, ensure_ascii=False, indent=2)) print("\n✅ 数据格式正确,可以写入 SQL Server") # 步骤4: 显示完整数据(用于调试) print("\n步骤4: 完整转换数据(JSON格式)") print("-" * 80) print(json.dumps(converted_data, ensure_ascii=False, indent=2)) print("\n" + "=" * 80) print("测试完成") print("=" * 80) print("\n总结:") print(f" - 数据转换: ✅ 成功") print(f" - 字段数量: {len(converted_data)}") print(f" - 温度字段: {len(temp_fields)}") print(f" - 可以写入 SQL Server: ✅ 是")