refactor(backend): optimize database safety, validator performance, and scheduler concurrency
- Fix SQL injection risks in proxy_repo and task_repo - Atomic acquire_pending with UPDATE ... RETURNING - Reuse aiohttp ClientSession in ValidatorService - Replace polling with asyncio.Event in SchedulerService - Optimize ValidationQueue.drain with asyncio.Condition - Concurrent plugin crawling with asyncio.gather - Unify ProxyRaw model import path - Fix test baseline and remove tracked __pycache__ files
This commit is contained in:
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
"""pytest 配置文件和 fixtures"""
|
||||
import pytest
|
||||
import asyncio
|
||||
from typing import AsyncGenerator, Generator
|
||||
import pytest_asyncio
|
||||
from typing import AsyncGenerator
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
|
||||
from app.api import create_app
|
||||
@@ -9,24 +9,17 @@ 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")
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
async def app():
|
||||
"""创建应用实例"""
|
||||
# 初始化测试数据库
|
||||
await init_db()
|
||||
app = create_app()
|
||||
return app
|
||||
async with app.router.lifespan_context(app):
|
||||
yield app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest_asyncio.fixture
|
||||
async def client(app) -> AsyncGenerator[AsyncClient, None]:
|
||||
"""创建异步 HTTP 客户端"""
|
||||
transport = ASGITransport(app=app)
|
||||
@@ -34,20 +27,20 @@ async def client(app) -> AsyncGenerator[AsyncClient, None]:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest_asyncio.fixture
|
||||
async def db():
|
||||
"""获取数据库连接"""
|
||||
async with get_db() as db:
|
||||
yield db
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest_asyncio.fixture
|
||||
async def proxy_repo():
|
||||
"""获取代理仓库"""
|
||||
return ProxyRepository()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest_asyncio.fixture
|
||||
async def sample_proxy(db, proxy_repo):
|
||||
"""创建一个测试代理"""
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -121,14 +121,14 @@ class TestProxiesAPI:
|
||||
"""测试 GET /api/proxies/export/csv"""
|
||||
response = await client.get("/api/proxies/export/csv")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "text/csv"
|
||||
assert response.headers["content-type"].startswith("text/csv")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_export_proxies_txt(self, client):
|
||||
"""测试 GET /api/proxies/export/txt"""
|
||||
response = await client.get("/api/proxies/export/txt")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "text/plain"
|
||||
assert response.headers["content-type"].startswith("text/plain")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_export_proxies_json(self, client):
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user