测试并修复api导入路径错误
This commit is contained in:
77
tests/api/test_api_integration.py
Executable file
77
tests/api/test_api_integration.py
Executable file
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
测试新增 API 集成
|
||||
|
||||
验证新的认证、用户管理和审计日志接口是否正确集成
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import pytest
|
||||
|
||||
# 将项目根目录添加到 sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"module_name, desc",
|
||||
[
|
||||
("app.core.encryption", "加密模块"),
|
||||
("app.core.security", "安全模块"),
|
||||
("app.core.audit", "审计模块"),
|
||||
("app.domain.models.role", "角色模型"),
|
||||
("app.domain.schemas.user", "用户Schema"),
|
||||
("app.domain.schemas.audit", "审计Schema"),
|
||||
("app.auth.permissions", "权限控制"),
|
||||
("app.api.v1.endpoints.auth", "认证接口"),
|
||||
("app.api.v1.endpoints.user_management", "用户管理接口"),
|
||||
("app.api.v1.endpoints.audit", "审计日志接口"),
|
||||
("app.infra.repositories.user_repository", "用户仓储"),
|
||||
("app.infra.repositories.audit_repository", "审计仓储"),
|
||||
("app.infra.audit.middleware", "审计中间件"),
|
||||
],
|
||||
)
|
||||
def test_module_imports(module_name, desc):
|
||||
"""检查关键模块是否可以导入"""
|
||||
try:
|
||||
__import__(module_name)
|
||||
except ImportError as e:
|
||||
pytest.fail(f"无法导入 {desc} ({module_name}): {e}")
|
||||
|
||||
|
||||
def test_router_configuration():
|
||||
"""检查路由配置"""
|
||||
try:
|
||||
from app.api.v1 import router
|
||||
|
||||
# 检查 router 中是否包含新增的路由
|
||||
api_router = router.api_router
|
||||
routes = [r.path for r in api_router.routes if hasattr(r, "path")]
|
||||
|
||||
# 验证基础路径是否存在
|
||||
assert any("/auth" in r for r in routes), "缺少认证相关路由 (/auth)"
|
||||
assert any("/users" in r for r in routes), "缺少用户管理路由 (/users)"
|
||||
assert any("/audit" in r for r in routes), "缺少审计日志路由 (/audit)"
|
||||
|
||||
except Exception as e:
|
||||
pytest.fail(f"路由配置检查失败: {e}")
|
||||
|
||||
|
||||
def test_main_app_initialization():
|
||||
"""检查 main.py 配置"""
|
||||
try:
|
||||
from app.main import app
|
||||
|
||||
assert app is not None
|
||||
assert app.title != ""
|
||||
|
||||
# 检查中间件 (简单检查是否存在)
|
||||
middleware_names = [m.cls.__name__ for m in app.user_middleware]
|
||||
# 检查是否包含审计中间件或其他关键中间件(根据实际类名修改)
|
||||
assert "AuditMiddleware" in middleware_names, "缺少审计中间件"
|
||||
|
||||
# 检查路由总数
|
||||
assert len(app.routes) > 0
|
||||
|
||||
except Exception as e:
|
||||
pytest.fail(f"main.py 配置检查失败: {e}")
|
||||
@@ -1,37 +1,41 @@
|
||||
"""
|
||||
测试加密功能
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
|
||||
def test_encryption():
|
||||
"""测试加密和解密功能"""
|
||||
from app.core.encryption import Encryptor
|
||||
|
||||
|
||||
# 生成测试密钥
|
||||
key = Encryptor.generate_key()
|
||||
print(f"✓ 生成密钥: {key}")
|
||||
|
||||
|
||||
# 创建加密器
|
||||
encryptor = Encryptor(key=key.encode())
|
||||
|
||||
|
||||
# 测试加密
|
||||
test_data = "这是敏感数据 - 数据库密码: password123"
|
||||
encrypted = encryptor.encrypt(test_data)
|
||||
print(f"✓ 加密成功: {encrypted[:50]}...")
|
||||
|
||||
|
||||
# 测试解密
|
||||
decrypted = encryptor.decrypt(encrypted)
|
||||
assert decrypted == test_data, "解密数据不匹配!"
|
||||
print(f"✓ 解密成功: {decrypted}")
|
||||
|
||||
|
||||
# 测试空数据
|
||||
assert encryptor.encrypt("") == ""
|
||||
assert encryptor.decrypt("") == ""
|
||||
print("✓ 空数据处理正确")
|
||||
|
||||
|
||||
print("\n✅ 所有加密测试通过!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_encryption()
|
||||
@@ -1,7 +1,11 @@
|
||||
from api_ex.pipeline_health_analyzer import PipelineHealthAnalyzer
|
||||
"""
|
||||
tests.unit.test_pipeline_health_analyzer 的 Docstring
|
||||
"""
|
||||
|
||||
|
||||
def test_pipeline_health_analyzer():
|
||||
from app.algorithms.api_ex.pipeline_health_analyzer import PipelineHealthAnalyzer
|
||||
|
||||
# 初始化分析器,假设模型文件路径为'models/rsf_model.joblib'
|
||||
analyzer = PipelineHealthAnalyzer()
|
||||
# 创建示例输入数据(9个样本)
|
||||
@@ -51,7 +55,7 @@ def test_pipeline_health_analyzer():
|
||||
), "每个生存函数应包含x和y属性"
|
||||
|
||||
# 可选:测试绘图功能(不显示图表)
|
||||
analyzer.plot_survival(survival_functions, show_plot=True)
|
||||
analyzer.plot_survival(survival_functions, show_plot=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user