133 lines
3.7 KiB
Python
133 lines
3.7 KiB
Python
"""
|
||
测试服务是否能正常启动和运行
|
||
"""
|
||
import sys
|
||
import os
|
||
|
||
# 添加当前目录到路径
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
def test_imports():
|
||
"""测试所有模块是否能正常导入"""
|
||
print("="*60)
|
||
print("测试模块导入...")
|
||
print("="*60)
|
||
|
||
try:
|
||
from flask import Flask
|
||
print("✓ Flask 导入成功")
|
||
except ImportError as e:
|
||
print(f"✗ Flask 导入失败: {e}")
|
||
return False
|
||
|
||
try:
|
||
from services.ai_service import AIService
|
||
print("✓ AIService 导入成功")
|
||
except ImportError as e:
|
||
print(f"✗ AIService 导入失败: {e}")
|
||
return False
|
||
|
||
try:
|
||
from services.field_service import FieldService
|
||
print("✓ FieldService 导入成功")
|
||
except ImportError as e:
|
||
print(f"✗ FieldService 导入失败: {e}")
|
||
return False
|
||
|
||
try:
|
||
from utils.response import success_response, error_response
|
||
print("✓ response 工具导入成功")
|
||
except ImportError as e:
|
||
print(f"✗ response 工具导入失败: {e}")
|
||
return False
|
||
|
||
return True
|
||
|
||
def test_field_service():
|
||
"""测试字段服务"""
|
||
print("\n" + "="*60)
|
||
print("测试字段服务...")
|
||
print("="*60)
|
||
|
||
try:
|
||
from services.field_service import FieldService
|
||
field_service = FieldService()
|
||
|
||
# 测试获取字段(使用新的方法)
|
||
field_codes = ['target_name', 'target_gender']
|
||
fields = field_service.get_output_fields_by_field_codes(field_codes)
|
||
print(f"✓ 成功获取 {len(fields)} 个输出字段(通过fieldCode查询)")
|
||
|
||
if fields:
|
||
print(f" 示例字段: {fields[0].get('name', 'N/A')} ({fields[0].get('field_code', 'N/A')})")
|
||
|
||
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("测试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(" ⚠ 警告: 未配置AI服务,请设置SILICONFLOW_API_KEY")
|
||
elif ai_service.ai_provider == 'siliconflow':
|
||
print(" ✓ 已配置硅基流动API")
|
||
elif ai_service.ai_provider == 'huawei':
|
||
print(" ✓ 已配置华为大模型API")
|
||
|
||
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)
|
||
|
||
# 测试导入
|
||
if not test_imports():
|
||
print("\n✗ 模块导入测试失败,请检查依赖是否安装")
|
||
return False
|
||
|
||
# 测试字段服务
|
||
if not test_field_service():
|
||
print("\n✗ 字段服务测试失败")
|
||
return False
|
||
|
||
# 测试AI服务
|
||
if not test_ai_service():
|
||
print("\n✗ AI服务测试失败")
|
||
return False
|
||
|
||
print("\n" + "="*60)
|
||
print("✓ 所有测试通过!")
|
||
print("="*60)
|
||
print("\n可以使用以下命令启动服务:")
|
||
print(" python app.py")
|
||
print("\n然后在浏览器访问: http://localhost:5000/")
|
||
|
||
return True
|
||
|
||
if __name__ == '__main__':
|
||
success = main()
|
||
sys.exit(0 if success else 1)
|
||
|
||
|