137 lines
4.2 KiB
Python
137 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
測試系統啟動的腳本
|
|
用於檢查各個模組是否可以正常導入和初始化
|
|
"""
|
|
|
|
def test_imports():
|
|
"""測試所有模組導入"""
|
|
print("🔧 測試模組導入...")
|
|
|
|
try:
|
|
print(" ├─ 測試基本 Flask 組件...")
|
|
from flask import Flask
|
|
from flask_login import LoginManager
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
print(" │ ✅ Flask 基本組件導入成功")
|
|
|
|
print(" ├─ 測試資料庫模型...")
|
|
from models import db, User, TempSpec
|
|
print(" │ ✅ 資料庫模型導入成功")
|
|
|
|
print(" ├─ 測試工具函式...")
|
|
from utils import admin_required, editor_or_admin_required, send_email
|
|
print(" │ ✅ 工具函式導入成功")
|
|
|
|
print(" ├─ 測試 LDAP 工具...")
|
|
from ldap_utils import authenticate_ldap_user, search_ldap_principals
|
|
print(" │ ✅ LDAP 工具導入成功")
|
|
|
|
print(" ├─ 測試路由模組...")
|
|
from routes.auth import auth_bp
|
|
print(" │ ✅ auth 路由導入成功")
|
|
|
|
from routes.temp_spec import temp_spec_bp
|
|
print(" │ ✅ temp_spec 路由導入成功")
|
|
|
|
from routes.admin import admin_bp
|
|
print(" │ ✅ admin 路由導入成功")
|
|
|
|
from routes.api import api_bp
|
|
print(" │ ✅ api 路由導入成功")
|
|
|
|
from routes.upload import upload_bp
|
|
print(" │ ✅ upload 路由導入成功")
|
|
|
|
print(" ├─ 測試排程任務...")
|
|
from tasks import check_expiring_specs
|
|
print(" │ ✅ 排程任務導入成功")
|
|
|
|
print(" └─ 所有模組導入成功!")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f" ❌ 模組導入失敗: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def test_app_creation():
|
|
"""測試 Flask 應用程式建立"""
|
|
print("\n🔧 測試應用程式建立...")
|
|
|
|
try:
|
|
from app import app
|
|
print(" ✅ Flask 應用程式建立成功")
|
|
|
|
# 測試路由註冊
|
|
with app.app_context():
|
|
print(f" ✅ 應用程式名稱: {app.name}")
|
|
print(f" ✅ 註冊的藍圖數量: {len(app.blueprints)}")
|
|
for bp_name in app.blueprints:
|
|
print(f" - {bp_name}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f" ❌ 應用程式建立失敗: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def test_config():
|
|
"""測試設定檔"""
|
|
print("\n🔧 測試設定檔...")
|
|
|
|
try:
|
|
from config import Config
|
|
print(" ✅ 設定檔導入成功")
|
|
|
|
# 檢查重要設定
|
|
config = Config()
|
|
print(f" ✅ SECRET_KEY 已設定: {'是' if hasattr(config, 'SECRET_KEY') and config.SECRET_KEY else '否'}")
|
|
print(f" ✅ DATABASE_URL 已設定: {'是' if hasattr(config, 'SQLALCHEMY_DATABASE_URI') and config.SQLALCHEMY_DATABASE_URI else '否'}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f" ❌ 設定檔測試失敗: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def main():
|
|
"""主測試函式"""
|
|
print("=" * 50)
|
|
print("🚀 暫時規範管理系統 V3 - 啟動測試")
|
|
print("=" * 50)
|
|
|
|
success_count = 0
|
|
total_tests = 3
|
|
|
|
# 測試模組導入
|
|
if test_imports():
|
|
success_count += 1
|
|
|
|
# 測試設定檔
|
|
if test_config():
|
|
success_count += 1
|
|
|
|
# 測試應用程式建立
|
|
if test_app_creation():
|
|
success_count += 1
|
|
|
|
# 結果統計
|
|
print("\n" + "=" * 50)
|
|
print(f"📊 測試結果: {success_count}/{total_tests} 個測試通過")
|
|
|
|
if success_count == total_tests:
|
|
print("🎉 所有測試通過!系統可以正常啟動。")
|
|
return True
|
|
else:
|
|
print("⚠️ 部分測試失敗,請檢查上述錯誤訊息。")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
exit(0 if success else 1) |