#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试新的配置系统 """ import os import sys from dotenv import load_dotenv # 加载测试环境变量 env_file = '.env.test' if os.path.exists(env_file): load_dotenv(env_file) print(f"[OK] 已加载环境变量文件: {env_file}") else: print(f"[WARN] 环境变量文件不存在: {env_file}") print("正在检查默认环境变量...") load_dotenv() def test_config_system(): """测试配置系统""" print("\n[TEST] 测试配置系统...") try: # 导入新的配置系统 from config import get_config # 获取配置 config_class = get_config() print(f"[OK] 获取配置类: {config_class.__name__}") # 检查环境变量 env = os.environ.get('FLASK_ENV', 'development') print(f"[OK] 当前环境: {env}") # 创建配置实例 config = config_class() print(f"[OK] 配置实例创建成功") # 检查关键配置 print(f"[OK] SECRET_KEY: {'已设置' if config.SECRET_KEY else '未设置'}") print(f"[OK] DATABASE_URL: {'已设置' if config.SQLALCHEMY_DATABASE_URI else '未设置'}") print(f"[OK] LLM_API_KEY: {'已设置' if config.LLM_API_KEY else '未设置'}") return True except Exception as e: print(f"[ERROR] 配置系统测试失败: {str(e)}") import traceback traceback.print_exc() return False def test_old_config_compatibility(): """测试旧配置文件的兼容性""" print("\n[TEST] 测试旧配置文件兼容性...") try: # 测试旧的config.py import warnings # 捕获警告 with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # 导入旧的config.py(会触发警告) from config import Config as OldConfig if w: print(f"[OK] 检测到弃用警告: {w[0].message}") else: print("[WARN] 未检测到弃用警告(可能有问题)") # 尝试创建实例 try: old_config = OldConfig() print(f"[OK] 旧配置类可实例化") return True except ValueError as e: print(f"[OK] 旧配置类按要求抛出错误(正常): {e}") return True except Exception as e: print(f"[ERROR] 旧配置兼容性测试失败: {str(e)}") return False def test_app_factory(): """测试应用工厂""" print("\n[TEST] 测试应用工厂...") try: from src.flask_prompt_master import create_app # 创建应用(应该使用新的配置系统) app = create_app() print(f"[OK] 应用创建成功") # 检查应用配置 print(f"[OK] 应用调试模式: {app.config.get('DEBUG', '未设置')}") print(f"[OK] 数据库URI: {app.config.get('SQLALCHEMY_DATABASE_URI', '未设置')[:50]}...") return True except Exception as e: print(f"[ERROR] 应用工厂测试失败: {str(e)}") import traceback traceback.print_exc() return False def main(): """主测试函数""" print("=" * 60) print("aitsc 项目配置系统测试") print("=" * 60) # 显示当前工作目录 print(f"工作目录: {os.getcwd()}") # 检查环境变量文件 env_files = ['.env', '.env.test', 'env.example'] for env_file in env_files: if os.path.exists(env_file): print(f"[OK] 找到环境变量文件: {env_file}") else: print(f"[WARN] 未找到环境变量文件: {env_file}") # 运行测试 tests = [ ("配置系统", test_config_system), ("旧配置兼容性", test_old_config_compatibility), ("应用工厂", test_app_factory), ] results = [] for test_name, test_func in tests: success = test_func() results.append((test_name, success)) # 汇总结果 print("\n" + "=" * 60) print("测试结果汇总") print("=" * 60) all_passed = True for test_name, success in results: status = "[OK] 通过" if success else "[ERROR] 失败" print(f"{test_name}: {status}") if not success: all_passed = False print("\n" + "=" * 60) if all_passed: print("[SUCCESS] 所有测试通过!项目可以正常运行。") print("\n下一步操作:") print("1. 复制 .env.example 为 .env") print("2. 在 .env 中填写实际的环境变量值") print("3. 删除或备份旧的 .env 文件(包含硬编码密码)") print("4. 运行 python run_dev.py 启动应用") else: print("[WARN] 部分测试失败,需要进一步检查。") return all_passed if __name__ == '__main__': success = main() sys.exit(0 if success else 1)