Files
aitsc/test_api_fix.py

109 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
# 设置环境变量
os.environ['FLASK_ENV'] = 'development'
os.environ['SECRET_KEY'] = 'test-secret-key'
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_app_creation():
"""测试应用创建"""
try:
from src.flask_prompt_master import create_app
app = create_app()
print("✅ 应用创建成功")
return app
except Exception as e:
print(f"❌ 应用创建失败: {e}")
return None
def test_config_loading():
"""测试配置加载"""
try:
from src.flask_prompt_master import create_app
app = create_app()
# 检查关键配置
configs = [
'LLM_API_KEY',
'LLM_API_URL',
'WX_APPID',
'WX_SECRET',
'SECRET_KEY'
]
for config_name in configs:
value = app.config.get(config_name)
print(f"{config_name}: {value}")
return True
except Exception as e:
print(f"❌ 配置加载失败: {e}")
return False
def test_api_generation():
"""测试API生成功能"""
try:
from src.flask_prompt_master import create_app
app = create_app()
with app.app_context():
from src.flask_prompt_master.routes.routes import generate_with_llm
# 测试不同的输入
test_inputs = [
"写一个产品介绍",
"帮我写个邮件",
"测试文本",
"开发一个网站"
]
for input_text in test_inputs:
result = generate_with_llm(input_text)
print(f"✅ 输入: {input_text}")
print(f" 输出: {result[:100]}...")
print()
return True
except Exception as e:
print(f"❌ API生成测试失败: {e}")
return False
def main():
"""主测试函数"""
print("🔧 开始测试API修复...")
print("=" * 50)
# 测试1: 应用创建
print("1. 测试应用创建...")
app = test_app_creation()
if not app:
return False
print()
# 测试2: 配置加载
print("2. 测试配置加载...")
if not test_config_loading():
return False
print()
# 测试3: API生成
print("3. 测试API生成功能...")
if not test_api_generation():
return False
print("=" * 50)
print("🎉 所有测试通过API修复成功")
return True
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)