115 lines
3.4 KiB
Python
115 lines
3.4 KiB
Python
"""Pytest fixtures for Web module tests."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from web.api.app import create_app
|
|
from web.api.dependencies import init_concurrency_limiter
|
|
from web.config import SettingsCache, WebSettings
|
|
from web.fetch.client import FetchClient
|
|
from web.metasearch.client import SearXNGClient
|
|
from web.orchestrator import Orchestrator
|
|
from web.schemas.search import SearchResponse, SearchResult
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_settings_cache() -> None:
|
|
"""Clear settings cache before each test."""
|
|
SettingsCache.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def test_settings() -> WebSettings:
|
|
"""Create test settings with mocked values."""
|
|
return WebSettings(
|
|
searxng_base_url="http://localhost:55100",
|
|
llm_base_url="http://localhost:14011",
|
|
external_url="http://localhost:51100",
|
|
host="127.0.0.1",
|
|
port=51100,
|
|
api_tokens=None,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_search_response() -> SearchResponse:
|
|
"""Create a mock search response."""
|
|
return SearchResponse(
|
|
request_id="test-request-id",
|
|
results=[
|
|
SearchResult(
|
|
query="test query",
|
|
url="https://example.com/article",
|
|
title="Test Article Title",
|
|
snippet="This is a test snippet from the search result.",
|
|
rank=1,
|
|
site="example.com",
|
|
published_at="2024-01-15",
|
|
),
|
|
SearchResult(
|
|
query="test query",
|
|
url="https://news.example.com/story",
|
|
title="Another Test Result",
|
|
snippet="Another snippet for testing purposes.",
|
|
rank=2,
|
|
site="news.example.com",
|
|
published_at=None,
|
|
),
|
|
],
|
|
total_results=2,
|
|
execution_time_ms=150.5,
|
|
queries_processed=1,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_searxng_api_response() -> dict:
|
|
"""Create a mock SearXNG API response."""
|
|
return {
|
|
"results": [
|
|
{
|
|
"url": "https://example.com/article",
|
|
"title": "Test Article Title",
|
|
"content": "This is a test snippet from the search result.",
|
|
"publishedDate": "2024-01-15",
|
|
},
|
|
{
|
|
"url": "https://news.example.com/story",
|
|
"title": "Another Test Result",
|
|
"content": "Another snippet for testing purposes.",
|
|
"publishedDate": None,
|
|
},
|
|
]
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def client_with_mock(
|
|
test_settings: WebSettings, mock_search_response: SearchResponse
|
|
) -> SearXNGClient:
|
|
"""Create a SearXNGClient with mocked search method."""
|
|
SettingsCache.set(test_settings)
|
|
|
|
client = SearXNGClient(settings=test_settings)
|
|
client.search = AsyncMock(return_value=mock_search_response)
|
|
|
|
return client
|
|
|
|
|
|
@pytest.fixture
|
|
def app_client(test_settings: WebSettings) -> TestClient:
|
|
"""Create a FastAPI TestClient with test settings."""
|
|
SettingsCache.set(test_settings)
|
|
|
|
init_concurrency_limiter(test_settings.max_concurrent_requests)
|
|
|
|
app = create_app()
|
|
|
|
app.state.settings = test_settings
|
|
app.state.search_client = SearXNGClient(settings=test_settings)
|
|
app.state.fetch_client = FetchClient(settings=test_settings)
|
|
app.state.orchestrator = Orchestrator(settings=test_settings)
|
|
|
|
return TestClient(app)
|