167 lines
4.9 KiB
Python
167 lines
4.9 KiB
Python
"""
|
|
快速测试脚本 - 测试核心功能
|
|
"""
|
|
import sys
|
|
import os
|
|
|
|
# 添加当前目录到路径
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
def test_imports():
|
|
"""测试所有模块是否能正常导入"""
|
|
print("="*60)
|
|
print("1. 测试模块导入")
|
|
print("="*60)
|
|
|
|
modules = [
|
|
("Flask", "from flask import Flask"),
|
|
("AIService", "from services.ai_service import AIService"),
|
|
("FieldService", "from services.field_service import FieldService"),
|
|
("DocumentService", "from services.document_service import DocumentService"),
|
|
]
|
|
|
|
all_ok = True
|
|
for name, import_stmt in modules:
|
|
try:
|
|
exec(import_stmt)
|
|
print(f" ✓ {name} 导入成功")
|
|
except ImportError as e:
|
|
print(f" ✗ {name} 导入失败: {e}")
|
|
all_ok = False
|
|
|
|
return all_ok
|
|
|
|
def test_field_service():
|
|
"""测试字段服务"""
|
|
print("\n" + "="*60)
|
|
print("2. 测试字段服务")
|
|
print("="*60)
|
|
|
|
try:
|
|
from services.field_service import FieldService
|
|
field_service = FieldService()
|
|
|
|
# 测试通过fieldCode查询字段
|
|
field_codes = ['target_name', 'target_gender']
|
|
fields = field_service.get_output_fields_by_field_codes(field_codes)
|
|
|
|
if fields:
|
|
print(f" ✓ 成功通过fieldCode查询到 {len(fields)} 个字段")
|
|
for field in fields:
|
|
print(f" - {field.get('name')} ({field.get('field_code')})")
|
|
else:
|
|
print(f" ⚠ 未查询到字段,可能需要初始化数据库")
|
|
print(f" 请运行: python init_all_fields_from_excel.py")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f" ✗ 字段服务测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def test_document_service():
|
|
"""测试文档服务"""
|
|
print("\n" + "="*60)
|
|
print("3. 测试文档服务")
|
|
print("="*60)
|
|
|
|
try:
|
|
from services.document_service import DocumentService
|
|
doc_service = DocumentService()
|
|
|
|
print(f" ✓ 文档服务初始化成功")
|
|
print(f" MinIO端点: {doc_service.minio_config['endpoint']}")
|
|
print(f" 存储桶: {doc_service.bucket_name}")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f" ✗ 文档服务测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def test_ai_service():
|
|
"""测试AI服务"""
|
|
print("\n" + "="*60)
|
|
print("4. 测试AI服务")
|
|
print("="*60)
|
|
|
|
try:
|
|
from services.ai_service import AIService
|
|
ai_service = AIService()
|
|
|
|
print(f" ✓ AI服务初始化成功")
|
|
print(f" AI提供商: {ai_service.ai_provider}")
|
|
|
|
if ai_service.ai_provider == 'none':
|
|
print(f" ⚠ 警告: 未配置AI服务")
|
|
print(f" 请在 .env 文件中设置 SILICONFLOW_API_KEY")
|
|
elif ai_service.ai_provider == 'siliconflow':
|
|
print(f" ✓ 已配置硅基流动API")
|
|
print(f" 模型: {ai_service.siliconflow_model}")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f" ✗ AI服务测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def main():
|
|
"""主测试函数"""
|
|
print("\n" + "="*60)
|
|
print(" 智慧监督AI文书写作服务 - 快速测试")
|
|
print("="*60)
|
|
|
|
results = {
|
|
'imports': False,
|
|
'field_service': False,
|
|
'document_service': False,
|
|
'ai_service': False
|
|
}
|
|
|
|
# 测试导入
|
|
results['imports'] = test_imports()
|
|
if not results['imports']:
|
|
print("\n✗ 模块导入测试失败,请检查依赖是否安装")
|
|
print(" 运行: pip install -r requirements.txt")
|
|
return False
|
|
|
|
# 测试字段服务
|
|
results['field_service'] = test_field_service()
|
|
|
|
# 测试文档服务
|
|
results['document_service'] = test_document_service()
|
|
|
|
# 测试AI服务
|
|
results['ai_service'] = test_ai_service()
|
|
|
|
# 总结
|
|
print("\n" + "="*60)
|
|
print(" 测试总结")
|
|
print("="*60)
|
|
|
|
all_tests = ['imports', 'field_service', 'document_service', 'ai_service']
|
|
passed = sum(1 for test in all_tests if results[test])
|
|
|
|
for test in all_tests:
|
|
status = "✓" if results[test] else "✗"
|
|
print(f" {status} {test}")
|
|
|
|
print(f"\n通过: {passed}/{len(all_tests)}")
|
|
|
|
if passed == len(all_tests):
|
|
print("\n✓ 所有测试通过!可以使用以下命令启动服务:")
|
|
print(" python app.py")
|
|
else:
|
|
print("\n⚠ 部分测试失败,请根据上面的提示解决问题")
|
|
|
|
return passed == len(all_tests)
|
|
|
|
if __name__ == '__main__':
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|
|
|
|
|