修复问题: - 添加缺失的 httpx 依赖到 requirements.txt - 修复前端批量删除参数格式与后端不匹配(数组->对象数组) - 移除 app/api/main.py 中重复创建 app 的冗余代码 - 修复 Plugins.vue v-model 直接修改 store 状态的 Vue 警告 - 修复 README 端口/启动命令文档与实际配置不一致 - 修正 pytest.ini 过时配置 (asyncio_default_fixture_loop_scope) - 修复 WebUI index.html 语言设置为 zh-CN - 修复 .gitignore 错误忽略 tests/ 目录 后端优化: - 修复调度器默认间隔从 5 秒改为 30 分钟,避免无节制验证 - 修复 validate_all_now 在调度器停止时无法执行的 bug - 设置保存后热更新运行中调度器的验证间隔 - 将 update_score 优化为原子单事务 SQL,消除并发竞态 - 导出功能改为真正的流式分批读取(iter_batches),降低大导出内存占用 - ProxyResponse Schema 补齐 response_time_ms 字段 - 日志级别改为从配置动态读取,不再硬编码 INFO - 清理 validator_service 中的冗余 try/finally 代码 插件健壮性: - 修复 ip3366/ip89/kuaidaili/proxylist_download/speedx/yundaili/proxyscrape 的端口范围检查和 IPv6 地址解析问题(改用 rsplit + 1-65535 校验) - 修复 PluginService.list_plugins 并发竞争条件 - 修复 run_all_plugins 去重逻辑与数据库 UNIQUE 约束保持一致 - 修复 proxyscrape 异常时错误跳过 fallback 的 bug 测试: - 新增 7 个插件解析单元测试 - 新增 update_score 自动删除和 iter_batches 流式读取测试 - 全部 74 个测试通过
127 lines
4.4 KiB
Python
127 lines
4.4 KiB
Python
"""仓库层单元测试"""
|
|
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_update_score_deletes_on_zero_or_below(self, db, proxy_repo):
|
|
"""测试分数降至 0 及以下时自动删除代理"""
|
|
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 5)
|
|
|
|
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 is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_iter_batches(self, db, proxy_repo):
|
|
"""测试流式分批读取"""
|
|
# 插入 5 条测试数据
|
|
for i in range(5):
|
|
await proxy_repo.insert_or_update(db, f"192.168.1.{i}", 8000 + i, "http", 10)
|
|
|
|
batches = []
|
|
async for batch in proxy_repo.iter_batches(db, batch_size=2):
|
|
batches.append(batch)
|
|
|
|
assert len(batches) == 3
|
|
assert len(batches[0]) == 2
|
|
assert len(batches[1]) == 2
|
|
assert len(batches[2]) == 1
|
|
|
|
# 清理
|
|
for i in range(5):
|
|
await proxy_repo.delete(db, f"192.168.1.{i}", 8000 + i)
|
|
|
|
@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
|