#!/usr/bin/env python # -*- coding: utf-8 -*- """ 测试脚本执行和数据保存功能 """ import sqlite3 import json from pathlib import Path def test_script_data_storage(): """测试脚本数据存储功能""" print("=" * 60) print("测试脚本数据存储功能") print("=" * 60) db_path = Path(__file__).parent / "experiments.db" try: conn = sqlite3.connect(str(db_path)) cursor = conn.cursor() # 1. 查找最近的已结束实验 print("\n1. 查找已结束的实验...") cursor.execute(""" SELECT id, start_ts, end_ts, work_order_no, script_data FROM experiments WHERE end_ts IS NOT NULL ORDER BY id DESC LIMIT 5 """) experiments = cursor.fetchall() if not experiments: print("❌ 没有找到已结束的实验") conn.close() return print(f"✅ 找到 {len(experiments)} 个已结束的实验\n") for exp_id, start_ts, end_ts, work_order, script_data in experiments: print(f"实验 ID: {exp_id}") print(f" 工单号: {work_order or '无'}") print(f" 开始时间: {start_ts}") print(f" 结束时间: {end_ts}") if script_data: try: data = json.loads(script_data) print(f" ✅ 脚本数据: 已保存 ({len(script_data)} 字节)") if isinstance(data, dict): print(f" 数据键: {list(data.keys())}") except Exception as e: print(f" ⚠️ 脚本数据解析失败: {e}") else: print(f" ⚠️ 脚本数据: 未保存") print() # 2. 测试数据结构 print("\n2. 测试脚本数据结构...") test_data = { "tables": [ { "token": "experimentProcess", "cells": [ {"row": 0, "col": 0, "value": "测试数据1"}, {"row": 0, "col": 1, "value": "测试数据2"} ] } ], "charts": [] } test_json = json.dumps(test_data, ensure_ascii=False) print(f"✅ 测试数据生成成功 ({len(test_json)} 字节)") print(f" 数据内容: {test_json[:100]}...") conn.close() print("\n" + "=" * 60) print("测试完成") print("=" * 60) print("\n💡 使用说明:") print("1. 在UI中找到一个已结束的实验") print("2. 点击「执行脚本」按钮") print("3. 脚本数据将被保存到数据库") print("4. 点击「生成报告」时将直接使用保存的数据") print("5. 如果脚本执行失败,可以再次点击「执行脚本」重试") except Exception as e: print(f"❌ 测试失败: {e}") import traceback traceback.print_exc() if __name__ == "__main__": test_script_data_storage()