#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ SOP 功能测试脚本 用于验证 SOP 文件管理功能是否正常工作 """ import os import sys import sqlite3 # 添加 server 目录到路径 sys.path.insert(0, 'server') def test_database_table(): """测试数据库表是否创建成功""" print("测试 1: 检查数据库表...") db_path = 'server/data.db' if not os.path.exists(db_path): print(" ❌ 数据库文件不存在") return False conn = sqlite3.connect(db_path) cursor = conn.cursor() # 检查 sop_files 表是否存在 cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='sop_files'") result = cursor.fetchone() if result: print(" ✅ sop_files 表已创建") # 检查表结构 cursor.execute("PRAGMA table_info(sop_files)") columns = cursor.fetchall() print(f" ✅ 表结构包含 {len(columns)} 列:") for col in columns: print(f" - {col[1]} ({col[2]})") conn.close() return True else: print(" ❌ sop_files 表不存在") conn.close() return False def test_sop_directory(): """测试 SOP 文件存储目录""" print("\n测试 2: 检查 SOP 文件目录...") sop_dir = 'frontend/sop_files' if os.path.exists(sop_dir): print(f" ✅ 目录已存在: {sop_dir}") # 检查目录权限 if os.access(sop_dir, os.W_OK): print(" ✅ 目录可写") else: print(" ⚠️ 目录不可写,可能需要调整权限") # 列出现有文件 files = os.listdir(sop_dir) if files: print(f" 📁 目录中已有 {len(files)} 个文件:") for f in files[:5]: # 只显示前5个 print(f" - {f}") else: print(" 📁 目录为空") return True else: print(f" ⚠️ 目录不存在: {sop_dir}") print(" 💡 系统会在首次上传时自动创建") return True def test_api_routes(): """测试 API 路由是否注册""" print("\n测试 3: 检查 API 路由...") try: from app import app # 获取所有路由 routes = [] for rule in app.url_map.iter_rules(): if 'sop' in rule.rule: routes.append(f"{rule.rule} [{', '.join(rule.methods - {'HEAD', 'OPTIONS'})}]") if routes: print(" ✅ SOP API 路由已注册:") for route in routes: print(f" - {route}") return True else: print(" ❌ 未找到 SOP API 路由") return False except Exception as e: print(f" ❌ 导入失败: {e}") return False def test_frontend_files(): """测试前端文件是否更新""" print("\n测试 4: 检查前端文件...") files_to_check = { 'frontend/js/api.js': ['listSopFiles', 'uploadSopFile', 'deleteSopFile'], 'frontend/js/components/upload.js': ['renderSop', 'bindSopEvents', 'loadSopList'], 'frontend/js/router.js': ['sop'], 'frontend/index.html': ['upload/sop'] } all_ok = True for filepath, keywords in files_to_check.items(): if not os.path.exists(filepath): print(f" ❌ 文件不存在: {filepath}") all_ok = False continue with open(filepath, 'r', encoding='utf-8') as f: content = f.read() missing = [kw for kw in keywords if kw not in content] if missing: print(f" ⚠️ {filepath} 缺少关键字: {', '.join(missing)}") all_ok = False else: print(f" ✅ {filepath} 已更新") return all_ok def main(): """运行所有测试""" print("=" * 60) print("SOP 功能测试") print("=" * 60) results = [] # 运行测试 results.append(("数据库表", test_database_table())) results.append(("文件目录", test_sop_directory())) results.append(("API 路由", test_api_routes())) results.append(("前端文件", test_frontend_files())) # 汇总结果 print("\n" + "=" * 60) print("测试结果汇总") print("=" * 60) for name, result in results: status = "✅ 通过" if result else "❌ 失败" print(f"{name}: {status}") all_passed = all(r[1] for r in results) print("\n" + "=" * 60) if all_passed: print("🎉 所有测试通过!SOP 功能已准备就绪。") print("\n下一步:") print("1. 重启服务器") print("2. 登录系统(使用管理员账号)") print("3. 进入 '上传' → 'SOP' 菜单") print("4. 尝试上传测试文件: sop_template_example.csv") else: print("⚠️ 部分测试未通过,请检查上述错误信息。") print("=" * 60) return 0 if all_passed else 1 if __name__ == '__main__': sys.exit(main())