~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Coverage tests for SerperSearchEngine focusing on logic paths not covered
by the existing test_search_engine_serper.py.
Covers:
- _get_previews: URL parse failure, sitelinks/date/attributes preserved,
unmapped time_period
- Rate limit raised from RequestException path
- Init: full_search creation with include_full_content, ImportError fallback
"""
from unittest.mock import Mock, patch
import pytest
import requests
from local_deep_research.web_search_engines.engines.search_engine_serper import (
SerperSearchEngine,
)
from local_deep_research.web_search_engines.rate_limiting import RateLimitError
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _make_engine(**kwargs):
"""Create a SerperSearchEngine with sensible defaults for testing."""
defaults = {"api_key": "test-key"}
defaults.update(kwargs)
return SerperSearchEngine(**defaults)
def _mock_response(json_data, status_code=200):
"""Build a mock response object mimicking requests.Response."""
resp = Mock()
resp.status_code = status_code
resp.json.return_value = json_data
resp.raise_for_status = Mock()
return resp
SERPER_POST = "local_deep_research.web_search_engines.engines.search_engine_serper.safe_post"
# ---------------------------------------------------------------------------
# _get_previews -- URL parse failure
# ---------------------------------------------------------------------------
class TestGetPreviewsUrlParseFailure:
"""When urlparse raises, displayed_link should fall back to empty string."""
def test_url_parse_exception_yields_empty_displayed_link(self):
engine = _make_engine()
resp = _mock_response(
{
"organic": [
{
"title": "Bad URL result",
"link": "not-a-valid-url",
"snippet": "Some snippet",
"position": 1,
}
]
}
)
with (
patch(SERPER_POST, return_value=resp),
patch(
"local_deep_research.web_search_engines.search_engine_base.urlparse",
side_effect=Exception("parse boom"),
),
):
previews = engine._search_previews("test query")
assert len(previews) == 1
assert previews[0]["displayed_link"] == ""
assert previews[0]["title"] == "Bad URL result"
# ---------------------------------------------------------------------------
# _get_previews -- sitelinks, date, attributes preserved
# ---------------------------------------------------------------------------
class TestGetPreviewsOptionalFields:
"""Sitelinks, date, and attributes from the organic result are preserved."""
def test_sitelinks_date_attributes_included(self):
organic = {
"title": "Rich result",
"link": "https://example.com/page",
"snippet": "A snippet",
"position": 1,
"sitelinks": [{"title": "Sub", "link": "https://example.com/sub"}],
"date": "2025-01-15",
"attributes": {"Author": "Jane Doe"},
}
resp = _mock_response({"organic": [organic]})
with patch(SERPER_POST, return_value=resp):
engine = _make_engine()
previews = engine._search_previews("rich query")
p = previews[0]
assert p["sitelinks"] == organic["sitelinks"]
assert p["date"] == "2025-01-15"
assert p["attributes"] == {"Author": "Jane Doe"}
def test_optional_fields_absent_when_not_in_result(self):
organic = {
"title": "Plain result",
"link": "https://example.com",
"snippet": "Plain",
"position": 1,
}
resp = _mock_response({"organic": [organic]})
with patch(SERPER_POST, return_value=resp):
engine = _make_engine()
previews = engine._search_previews("plain query")
p = previews[0]
assert "sitelinks" not in p
assert "date" not in p
assert "attributes" not in p
# ---------------------------------------------------------------------------
# _get_previews -- unmapped time_period
# ---------------------------------------------------------------------------
class TestGetPreviewsUnmappedTimePeriod:
"""An unrecognised time_period value should not add 'tbs' to the payload."""
def test_unmapped_time_period_omits_tbs(self):
resp = _mock_response({"organic": []})
with patch(SERPER_POST, return_value=resp) as mock_post:
engine = _make_engine(time_period="decade")
engine._search_previews("query")
payload = mock_post.call_args[1]["json"]
assert "tbs" not in payload
# ---------------------------------------------------------------------------
# Rate limit from RequestException
# ---------------------------------------------------------------------------
class TestRateLimitFromRequestException:
"""When a RequestException contains rate-limit text, RateLimitError is raised."""
def test_request_exception_with_rate_limit_raises(self):
exc = requests.exceptions.RequestException(
"429 Too Many Requests rate limit"
)
with patch(SERPER_POST, side_effect=exc):
engine = _make_engine()
# _raise_if_rate_limit is called with the exception; if it detects
# rate-limit language it raises RateLimitError
with pytest.raises(RateLimitError):
engine._search_previews("query")
# Note: TestInitFullSearchCreation and TestInitImportErrorFallback were removed
# because Serper no longer self-wraps; the factory wrapper handles full content.