重构: 迁移后端代码到 app 目录,前端移动到 WebUI,添加完整测试套件
主要变更: - 后端代码从根目录迁移到 app/ 目录 - 前端代码从 frontend/ 重命名为 WebUI/ - 更新所有导入路径以适配新结构 - 提取公共 API 响应函数到 app/api/common.py - 精简验证器服务代码 - 更新启动脚本和文档 测试: - 新增完整测试套件 (tests/) - 单元测试: 模型、仓库层 - 集成测试: 覆盖所有 22+ API 端点 - E2E 测试: 4个完整工作流场景 - 添加 pytest 配置和测试运行脚本
This commit is contained in:
95
tests/unit/test_repositories.py
Normal file
95
tests/unit/test_repositories.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""仓库层单元测试"""
|
||||
import pytest
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class TestProxyRepository:
|
||||
"""测试 ProxyRepository"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_insert_or_update(self, db, proxy_repo):
|
||||
"""测试插入或更新代理"""
|
||||
result = await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
assert result is True
|
||||
|
||||
# 验证插入成功
|
||||
proxy = await proxy_repo.get_by_ip_port(db, "192.168.1.1", 8080)
|
||||
assert proxy is not None
|
||||
assert proxy.ip == "192.168.1.1"
|
||||
assert proxy.port == 8080
|
||||
|
||||
# 清理
|
||||
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_random(self, db, proxy_repo):
|
||||
"""测试获取随机代理"""
|
||||
# 先插入一个代理
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
|
||||
proxy = await proxy_repo.get_random(db)
|
||||
# 可能有也可能没有(取决于数据库状态)
|
||||
if proxy:
|
||||
assert hasattr(proxy, 'ip')
|
||||
assert hasattr(proxy, 'port')
|
||||
|
||||
# 清理
|
||||
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_all(self, db, proxy_repo):
|
||||
"""测试列出所有代理"""
|
||||
# 插入测试数据
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.2", 8081, "https", 60)
|
||||
|
||||
proxies = await proxy_repo.list_all(db, limit=100)
|
||||
assert isinstance(proxies, list)
|
||||
|
||||
# 清理
|
||||
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
||||
await proxy_repo.delete(db, "192.168.1.2", 8081)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_score(self, db, proxy_repo):
|
||||
"""测试更新分数"""
|
||||
# 插入代理
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
|
||||
# 更新分数
|
||||
result = await proxy_repo.update_score(db, "192.168.1.1", 8080, 10)
|
||||
assert result is True
|
||||
|
||||
# 验证
|
||||
proxy = await proxy_repo.get_by_ip_port(db, "192.168.1.1", 8080)
|
||||
assert proxy.score == 60
|
||||
|
||||
# 清理
|
||||
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_batch_delete(self, db, proxy_repo):
|
||||
"""测试批量删除"""
|
||||
# 插入测试数据
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.2", 8081, "http", 50)
|
||||
|
||||
# 批量删除
|
||||
count = await proxy_repo.batch_delete(db, [("192.168.1.1", 8080), ("192.168.1.2", 8081)])
|
||||
assert count == 2
|
||||
|
||||
# 验证删除
|
||||
proxy1 = await proxy_repo.get_by_ip_port(db, "192.168.1.1", 8080)
|
||||
proxy2 = await proxy_repo.get_by_ip_port(db, "192.168.1.2", 8081)
|
||||
assert proxy1 is None
|
||||
assert proxy2 is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_stats(self, db, proxy_repo):
|
||||
"""测试获取统计信息"""
|
||||
stats = await proxy_repo.get_stats(db)
|
||||
assert "total" in stats
|
||||
assert "available" in stats
|
||||
assert "avg_score" in stats
|
||||
assert "http_count" in stats
|
||||
Reference in New Issue
Block a user