主要变更: - 后端代码从根目录迁移到 app/ 目录 - 前端代码从 frontend/ 重命名为 WebUI/ - 更新所有导入路径以适配新结构 - 提取公共 API 响应函数到 app/api/common.py - 精简验证器服务代码 - 更新启动脚本和文档 测试: - 新增完整测试套件 (tests/) - 单元测试: 模型、仓库层 - 集成测试: 覆盖所有 22+ API 端点 - E2E 测试: 4个完整工作流场景 - 添加 pytest 配置和测试运行脚本
57 lines
1.4 KiB
Python
57 lines
1.4 KiB
Python
"""pytest 配置文件和 fixtures"""
|
|
import pytest
|
|
import asyncio
|
|
from typing import AsyncGenerator, Generator
|
|
from httpx import AsyncClient, ASGITransport
|
|
|
|
from app.api import create_app
|
|
from app.core.db import init_db, get_db
|
|
from app.repositories.proxy_repo import ProxyRepository
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
|
|
"""创建事件循环"""
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
async def app():
|
|
"""创建应用实例"""
|
|
# 初始化测试数据库
|
|
await init_db()
|
|
app = create_app()
|
|
return app
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(app) -> AsyncGenerator[AsyncClient, None]:
|
|
"""创建异步 HTTP 客户端"""
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
yield client
|
|
|
|
|
|
@pytest.fixture
|
|
async def db():
|
|
"""获取数据库连接"""
|
|
async with get_db() as db:
|
|
yield db
|
|
|
|
|
|
@pytest.fixture
|
|
async def proxy_repo():
|
|
"""获取代理仓库"""
|
|
return ProxyRepository()
|
|
|
|
|
|
@pytest.fixture
|
|
async def sample_proxy(db, proxy_repo):
|
|
"""创建一个测试代理"""
|
|
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
|
yield {"ip": "192.168.1.1", "port": 8080, "protocol": "http", "score": 50}
|
|
# 清理
|
|
await proxy_repo.delete(db, "192.168.1.1", 8080)
|