Files
ProxyPool/tests
祀梦 4ef7931941 fix: 全面修复代码问题并优化架构
修复问题:
- 添加缺失的 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 个测试通过
2026-04-04 21:03:43 +08:00
..

测试说明

测试结构

tests/
├── conftest.py              # pytest 配置和 fixtures
├── README.md                # 本文件
├── unit/                    # 单元测试
│   ├── test_models.py       # 模型测试
│   └── test_repositories.py # 仓库层测试
├── integration/             # 集成测试
│   ├── test_proxies_api.py  # 代理 API 测试
│   ├── test_plugins_api.py  # 插件 API 测试
│   ├── test_scheduler_api.py # 调度器 API 测试
│   ├── test_settings_api.py # 设置 API 测试
│   └── test_health_api.py   # 健康检查测试
└── e2e/                     # 端到端测试
    └── test_full_workflow.py # 完整工作流测试

运行测试

安装测试依赖

pip install pytest pytest-asyncio httpx

运行所有测试

pytest

运行特定类型的测试

# 仅运行单元测试
pytest tests/unit -v

# 仅运行集成测试
pytest tests/integration -v

# 仅运行 E2E 测试
pytest tests/e2e -v

运行特定测试文件

pytest tests/integration/test_proxies_api.py -v

运行特定测试函数

pytest tests/integration/test_proxies_api.py::TestProxiesAPI::test_get_stats -v

测试覆盖的 API

代理 API (/api/proxies/*)

  • GET /api/proxies/stats - 获取统计信息
  • POST /api/proxies - 列出代理
  • GET /api/proxies/random - 获取随机代理
  • GET /api/proxies/export/{format} - 导出代理 (csv, txt, json)
  • DELETE /api/proxies/{ip}/{port} - 删除代理
  • POST /api/proxies/batch-delete - 批量删除
  • DELETE /api/proxies/clean-invalid - 清理无效代理

插件 API (/api/plugins/*)

  • GET /api/plugins - 列出插件
  • PUT /api/plugins/{id}/toggle - 切换插件状态
  • GET /api/plugins/{id}/config - 获取插件配置
  • POST /api/plugins/{id}/config - 更新插件配置
  • POST /api/plugins/{id}/crawl - 触发单个插件爬取
  • POST /api/plugins/crawl-all - 触发所有插件爬取

调度器 API (/api/scheduler/*)

  • GET /api/scheduler/status - 获取调度器状态
  • POST /api/scheduler/start - 启动调度器
  • POST /api/scheduler/stop - 停止调度器
  • POST /api/scheduler/validate-now - 立即验证

设置 API (/api/settings)

  • GET /api/settings - 获取设置
  • POST /api/settings - 保存设置

健康检查

  • GET / - 根端点
  • GET /health - 健康检查

测试 Fixtures

client

异步 HTTP 客户端,用于发送请求到测试应用。

async def test_example(client):
    response = await client.get("/api/proxies/stats")
    assert response.status_code == 200

db

数据库连接 fixture。

async def test_example(db, proxy_repo):
    await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)

sample_proxy

创建一个测试代理并自动清理。

async def test_example(client, sample_proxy):
    # sample_proxy = {"ip": "192.168.1.1", "port": 8080, "protocol": "http", "score": 50}
    response = await client.delete(f"/api/proxies/{sample_proxy['ip']}/{sample_proxy['port']}")
    assert response.status_code == 200

编写新测试

单元测试示例

# tests/unit/test_new_feature.py
import pytest
from app.models.domain import ProxyRaw


class TestProxyRaw:
    def test_create(self):
        proxy = ProxyRaw("192.168.1.1", 8080, "http")
        assert proxy.ip == "192.168.1.1"

集成测试示例

# tests/integration/test_new_api.py
import pytest


class TestNewAPI:
    @pytest.mark.asyncio
    async def test_new_endpoint(self, client):
        response = await client.get("/api/new-endpoint")
        assert response.status_code == 200