129 lines
3.5 KiB
Python
129 lines
3.5 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
快速测试脚本,验证项目配置是否正确
|
|||
|
|
"""
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
# 添加项目根目录到Python路径
|
|||
|
|
project_root = Path(__file__).parent
|
|||
|
|
sys.path.insert(0, str(project_root))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_config():
|
|||
|
|
"""测试配置加载"""
|
|||
|
|
print("🔧 测试配置加载...")
|
|||
|
|
|
|||
|
|
# 测试开发环境配置
|
|||
|
|
os.environ["ENVIRONMENT"] = "development"
|
|||
|
|
try:
|
|||
|
|
from app.config.settings import settings
|
|||
|
|
print(f"✅ 开发环境配置加载成功")
|
|||
|
|
print(f" 应用名称: {settings.config.name}")
|
|||
|
|
print(
|
|||
|
|
f" 数据库: {settings.config.database.host}:{settings.config.database.port}/{settings.config.database.database}")
|
|||
|
|
print(f" 调试模式: {settings.config.debug}")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 开发环境配置加载失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_database():
|
|||
|
|
"""测试数据库连接"""
|
|||
|
|
print("\n🔍 测试数据库连接...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from app.utils.database import DatabaseUtils
|
|||
|
|
result = DatabaseUtils.test_connection()
|
|||
|
|
|
|||
|
|
if result["status"] == "success":
|
|||
|
|
print(f"✅ 数据库连接成功")
|
|||
|
|
print(f" 测试结果: {result['test_result']}")
|
|||
|
|
|
|||
|
|
# 测试获取表名
|
|||
|
|
tables_result = DatabaseUtils.get_all_tables()
|
|||
|
|
if tables_result["status"] == "success":
|
|||
|
|
print(f"✅ 获取表名成功,共 {tables_result['count']} 张表")
|
|||
|
|
if tables_result["tables"]:
|
|||
|
|
print(f" 前5张表: {tables_result['tables'][:5]}")
|
|||
|
|
else:
|
|||
|
|
print(f"⚠️ 获取表名失败: {tables_result['message']}")
|
|||
|
|
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 数据库连接失败: {result['message']}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 数据库测试失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_app_start():
|
|||
|
|
"""测试应用是否可以正常启动"""
|
|||
|
|
print("\n🚀 测试应用启动...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from app.main import app
|
|||
|
|
print(f"✅ FastAPI应用创建成功")
|
|||
|
|
print(f" 应用标题: {app.title}")
|
|||
|
|
print(f" 路由数量: {len(app.routes)}")
|
|||
|
|
|
|||
|
|
# 检查路由
|
|||
|
|
routes = [route.path for route in app.routes if hasattr(route, 'path')]
|
|||
|
|
print(f" 主要路由: {routes}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 应用启动测试失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主测试函数"""
|
|||
|
|
print("=" * 50)
|
|||
|
|
print("🧪 WMS FastAPI 项目配置测试")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
tests = [
|
|||
|
|
("配置加载", test_config),
|
|||
|
|
("数据库连接", test_database),
|
|||
|
|
("应用启动", test_app_start)
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
passed = 0
|
|||
|
|
failed = 0
|
|||
|
|
|
|||
|
|
for test_name, test_func in tests:
|
|||
|
|
try:
|
|||
|
|
if test_func():
|
|||
|
|
passed += 1
|
|||
|
|
else:
|
|||
|
|
failed += 1
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ {test_name}测试异常: {e}")
|
|||
|
|
failed += 1
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 50)
|
|||
|
|
print(f"📊 测试结果: 通过 {passed}, 失败 {failed}")
|
|||
|
|
|
|||
|
|
if failed == 0:
|
|||
|
|
print("🎉 所有测试通过!项目配置正确,可以启动应用。")
|
|||
|
|
print("\n💡 启动命令:")
|
|||
|
|
print(" python run.py")
|
|||
|
|
print("\n📚 访问文档:")
|
|||
|
|
print(" http://localhost:12315/docs")
|
|||
|
|
else:
|
|||
|
|
print("⚠️ 部分测试失败,请检查配置。")
|
|||
|
|
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|