重构: 迁移后端代码到 app 目录,前端移动到 WebUI,添加完整测试套件
主要变更: - 后端代码从根目录迁移到 app/ 目录 - 前端代码从 frontend/ 重命名为 WebUI/ - 更新所有导入路径以适配新结构 - 提取公共 API 响应函数到 app/api/common.py - 精简验证器服务代码 - 更新启动脚本和文档 测试: - 新增完整测试套件 (tests/) - 单元测试: 模型、仓库层 - 集成测试: 覆盖所有 22+ API 端点 - E2E 测试: 4个完整工作流场景 - 添加 pytest 配置和测试运行脚本
This commit is contained in:
1
tests/unit/__init__.py
Normal file
1
tests/unit/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""单元测试"""
|
||||
BIN
tests/unit/__pycache__/test_models.cpython-311.pyc
Normal file
BIN
tests/unit/__pycache__/test_models.cpython-311.pyc
Normal file
Binary file not shown.
BIN
tests/unit/__pycache__/test_repositories.cpython-311.pyc
Normal file
BIN
tests/unit/__pycache__/test_repositories.cpython-311.pyc
Normal file
Binary file not shown.
136
tests/unit/test_models.py
Normal file
136
tests/unit/test_models.py
Normal file
@@ -0,0 +1,136 @@
|
||||
"""模型单元测试"""
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from app.models.domain import ProxyRaw, Proxy, PluginInfo
|
||||
from app.models.schemas import (
|
||||
ProxyCreate,
|
||||
ProxyListRequest,
|
||||
SettingsSchema,
|
||||
BatchDeleteRequest,
|
||||
)
|
||||
|
||||
|
||||
class TestProxyRaw:
|
||||
"""测试 ProxyRaw 领域模型"""
|
||||
|
||||
def test_create_proxy_raw(self):
|
||||
"""测试创建原始代理"""
|
||||
proxy = ProxyRaw("192.168.1.1", 8080, "http")
|
||||
assert proxy.ip == "192.168.1.1"
|
||||
assert proxy.port == 8080
|
||||
assert proxy.protocol == "http"
|
||||
|
||||
def test_protocol_normalization(self):
|
||||
"""测试协议标准化"""
|
||||
proxy = ProxyRaw("192.168.1.1", 8080, "HTTP")
|
||||
assert proxy.protocol == "http"
|
||||
|
||||
def test_invalid_protocol_defaults_to_http(self):
|
||||
"""测试无效协议默认为 http"""
|
||||
proxy = ProxyRaw("192.168.1.1", 8080, "invalid")
|
||||
assert proxy.protocol == "http"
|
||||
|
||||
|
||||
class TestProxy:
|
||||
"""测试 Proxy 领域模型"""
|
||||
|
||||
def test_create_proxy(self):
|
||||
"""测试创建代理实体"""
|
||||
proxy = Proxy(
|
||||
ip="192.168.1.1",
|
||||
port=8080,
|
||||
protocol="http",
|
||||
score=50,
|
||||
response_time_ms=100.5,
|
||||
last_check=datetime.now(),
|
||||
created_at=datetime.now(),
|
||||
)
|
||||
assert proxy.ip == "192.168.1.1"
|
||||
assert proxy.score == 50
|
||||
|
||||
|
||||
class TestPluginInfo:
|
||||
"""测试 PluginInfo 领域模型"""
|
||||
|
||||
def test_create_plugin_info(self):
|
||||
"""测试创建插件信息"""
|
||||
plugin = PluginInfo(
|
||||
id="test_plugin",
|
||||
name="test_plugin",
|
||||
display_name="测试插件",
|
||||
description="用于测试",
|
||||
enabled=True,
|
||||
success_count=10,
|
||||
failure_count=2,
|
||||
)
|
||||
assert plugin.id == "test_plugin"
|
||||
assert plugin.enabled is True
|
||||
assert plugin.success_count == 10
|
||||
|
||||
|
||||
class TestProxyCreateSchema:
|
||||
"""测试 ProxyCreate Pydantic 模型"""
|
||||
|
||||
def test_valid_proxy_create(self):
|
||||
"""测试有效的代理创建"""
|
||||
proxy = ProxyCreate(ip="192.168.1.1", port=8080, protocol="http")
|
||||
assert proxy.ip == "192.168.1.1"
|
||||
assert proxy.port == 8080
|
||||
|
||||
def test_port_validation(self):
|
||||
"""测试端口验证"""
|
||||
with pytest.raises(Exception):
|
||||
ProxyCreate(ip="192.168.1.1", port=70000) # 超出范围
|
||||
|
||||
def test_protocol_validation(self):
|
||||
"""测试协议验证"""
|
||||
with pytest.raises(Exception):
|
||||
ProxyCreate(ip="192.168.1.1", port=8080, protocol="invalid")
|
||||
|
||||
|
||||
class TestProxyListRequest:
|
||||
"""测试 ProxyListRequest 模式"""
|
||||
|
||||
def test_default_values(self):
|
||||
"""测试默认值"""
|
||||
request = ProxyListRequest()
|
||||
assert request.page == 1
|
||||
assert request.page_size == 20
|
||||
assert request.sort_by == "last_check"
|
||||
assert request.sort_order == "DESC"
|
||||
|
||||
def test_custom_values(self):
|
||||
"""测试自定义值"""
|
||||
request = ProxyListRequest(page=2, page_size=50, protocol="https")
|
||||
assert request.page == 2
|
||||
assert request.page_size == 50
|
||||
assert request.protocol == "https"
|
||||
|
||||
|
||||
class TestSettingsSchema:
|
||||
"""测试 SettingsSchema"""
|
||||
|
||||
def test_default_settings(self):
|
||||
"""测试默认设置"""
|
||||
settings = SettingsSchema()
|
||||
assert settings.crawl_timeout == 30
|
||||
assert settings.validation_timeout == 10
|
||||
assert settings.auto_validate is True
|
||||
|
||||
def test_custom_settings(self):
|
||||
"""测试自定义设置"""
|
||||
settings = SettingsSchema(crawl_timeout=60, auto_validate=False)
|
||||
assert settings.crawl_timeout == 60
|
||||
assert settings.auto_validate is False
|
||||
|
||||
|
||||
class TestBatchDeleteRequest:
|
||||
"""测试 BatchDeleteRequest"""
|
||||
|
||||
def test_valid_batch_delete(self):
|
||||
"""测试有效的批量删除"""
|
||||
request = BatchDeleteRequest(proxies=[
|
||||
{"ip": "192.168.1.1", "port": 8080},
|
||||
{"ip": "192.168.1.2", "port": 8081},
|
||||
])
|
||||
assert len(request.proxies) == 2
|
||||
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