#!/usr/bin/env python # -*- coding: utf-8 -*- """ 验证全局参数集成 - 完整流程测试 此脚本验证全局参数从配置到 SQL Server 的完整数据流 """ import json from convert_table_to_sqlserver_format import convert_temperature_table_to_sqlserver print("=" * 80) print("全局参数集成验证") print("=" * 80) # 模拟脚本返回的数据 script_data = { "tables": [{ "token": "scriptTable1", "startRow": 0, "startCol": 0, "cells": [ # 环境温度 {"row": 0, "col": 1, "value": "13.4"}, # 时间 {"row": 1, "col": 1, "value": "2025-12-05 14:00:00"}, {"row": 1, "col": 3, "value": "2025-12-05 17:30:00"}, # 主轴承1温度数据 {"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": ""}, ] }] } # 模拟配置中的全局参数 global_params = { 'runin_date': '2025-12-10', 'operator_name': '张三', 'power_end_part_no': 'PWR-600-001', 'motor_speed_rpm': '980' } print("\n步骤 1: 配置中的全局参数") print("-" * 80) for key, value in global_params.items(): print(f" {key}: {value}") print("\n步骤 2: 调用转换函数") print("-" * 80) print(" convert_temperature_table_to_sqlserver(") print(" script_data=<脚本数据>,") print(" work_order_no='W2001150.001',") print(" global_params=<全局参数>") print(" )") # 执行转换 result = convert_temperature_table_to_sqlserver( script_data, "W2001150.001", global_params ) print("\n步骤 3: 转换结果") print("-" * 80) # 检查全局参数字段 global_param_fields = ['runin_date', 'operator_name', 'power_end_part_no', 'motor_speed_rpm'] print("\n全局参数字段:") for field in global_param_fields: value = result.get(field) status = "✅" if value is not None else "❌" print(f" {status} {field}: {value}") # 检查其他基本字段 print("\n其他基本字段:") basic_fields = ['order_no', 'ambient_temp_c', 'start_time', 'end_time'] for field in basic_fields: value = result.get(field) status = "✅" if value is not None else "❌" print(f" {status} {field}: {value}") # 检查温度字段 print("\n温度字段示例:") temp_sample = [ 'temp_main_1_t05', 'temp_main_1_t10', 'temp_main_1_t15', 'temp_main_1_t20', 'temp_main_1_t25', 'temp_main_1_t30', 'temp_main_1_t35' ] for field in temp_sample: value = result.get(field) status = "✅" if field in result else "❌" print(f" {status} {field}: {value}") print("\n步骤 4: 数据类型验证") print("-" * 80) # 验证类型 type_checks = [ ('runin_date', str, result.get('runin_date')), ('operator_name', str, result.get('operator_name')), ('power_end_part_no', str, result.get('power_end_part_no')), ('motor_speed_rpm', int, result.get('motor_speed_rpm')), ('ambient_temp_c', float, result.get('ambient_temp_c')), ] for field_name, expected_type, value in type_checks: if value is not None: actual_type = type(value) is_correct = isinstance(value, expected_type) status = "✅" if is_correct else "❌" print(f" {status} {field_name}: {actual_type.__name__} (期望: {expected_type.__name__})") else: print(f" ⚠️ {field_name}: None (未设置)") print("\n步骤 5: SQL Server 写入预览") print("-" * 80) print("\n将写入以下数据到 SQL Server:") print(json.dumps(result, ensure_ascii=False, indent=2)) print("\n" + "=" * 80) print("验证总结") print("=" * 80) # 统计 total_fields = len(result) global_param_count = sum(1 for f in global_param_fields if result.get(f) is not None) temp_field_count = sum(1 for k in result.keys() if k.startswith('temp_')) print(f"\n总字段数: {total_fields}") print(f"全局参数字段: {global_param_count}/{len(global_param_fields)}") print(f"温度字段: {temp_field_count}") # 最终状态 if global_param_count == len(global_param_fields): print("\n✅ 全局参数集成验证通过!") print(" 所有全局参数字段都已正确获取和转换") else: print(f"\n⚠️ 部分全局参数缺失 ({global_param_count}/{len(global_param_fields)})") print("\n" + "=" * 80)