~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
"""
Deep coverage tests for utilities/db_utils.py.
Targets uncovered paths of the (deprecated but still live) get_db_session /
get_settings_manager helpers:
- get_db_session: username resolved via get_current_username() + db_manager
- get_settings_manager: request-context username extraction
Post-FastAPI-migration the username no longer comes from ``flask.session`` —
``get_db_session`` resolves it via ``get_current_username()`` (the
request-context contextvar); these tests drive that path.
"""
from unittest.mock import Mock, patch
class TestGetDbSessionUsernameResolution:
"""Cover the get_current_username() lookup path of get_db_session."""
def test_returns_session_for_resolved_username(self):
"""Username falls back to get_current_username()."""
from local_deep_research.utilities.db_utils import get_db_session
mock_session = Mock()
with patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value="testuser",
):
with patch(
"local_deep_research.utilities.db_utils.db_manager"
) as mock_mgr:
mock_mgr.get_session.return_value = mock_session
# Unique _namespace to avoid thread_specific_cache hits.
result = get_db_session(_namespace="test_resolved_user")
assert result == mock_session
mock_mgr.get_session.assert_called_with("testuser")
def test_raises_when_resolved_user_has_no_db(self):
"""Username resolves but db_manager has no open session → raise,
the same contract as the explicit-username branch. Returning None
here silently downgraded a logged-in user to default settings."""
import pytest
from local_deep_research.utilities.db_utils import get_db_session
with patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value="nodbuser",
):
with patch(
"local_deep_research.utilities.db_utils.db_manager"
) as mock_mgr:
mock_mgr.get_session.return_value = None
with pytest.raises(RuntimeError, match="nodbuser"):
get_db_session(_namespace="test_no_db_user")
def test_returns_none_when_no_username(self):
"""No resolved username → None."""
from local_deep_research.utilities.db_utils import get_db_session
with patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value=None,
):
result = get_db_session(_namespace="test_no_username")
assert result is None
class TestGetSettingsManagerRequestContext:
"""Cover get_settings_manager request-context username extraction."""
def test_extracts_username_from_request_context(self):
"""With no args, the username comes from get_current_username() (the
request-context contextvar) and is passed to get_db_session."""
from local_deep_research.utilities.db_utils import get_settings_manager
mock_session_obj = Mock()
with patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value="webuser",
):
with patch(
"local_deep_research.utilities.db_utils.get_db_session",
return_value=mock_session_obj,
) as mock_get_db:
with patch(
"local_deep_research.settings.SettingsManager"
) as MockSM:
mock_sm = Mock()
MockSM.return_value = mock_sm
result = get_settings_manager()
assert result is mock_sm
mock_get_db.assert_called_once_with(username="webuser")
class TestThreadpoolRequestContext:
"""Sync FastAPI route handlers run in the anyio threadpool — NOT on
MainThread. The migration's original guard refused every
non-MainThread caller, so every no-arg settings read from a sync
route silently fell back to anonymous default settings. With a
request context present (contextvars propagate into threadpool
workers), the user's session must be returned."""
def _run_in_thread(self, fn):
import threading
out = {}
def worker():
try:
out["result"] = fn()
except Exception as e:
out["error"] = e
t = threading.Thread(target=worker, name="AnyIO worker (test)")
t.start()
t.join()
return out
def test_worker_thread_with_context_gets_user_session(self):
from local_deep_research.utilities.db_utils import get_db_session
mock_session = Mock()
with (
patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value="webuser",
),
patch(
"local_deep_research.utilities.db_utils.db_manager"
) as mock_mgr,
):
mock_mgr.get_session.return_value = mock_session
out = self._run_in_thread(
lambda: get_db_session(_namespace="test_worker_ctx")
)
assert out.get("result") is mock_session
mock_mgr.get_session.assert_called_with("webuser")
def test_worker_thread_without_context_still_raises(self):
from local_deep_research.utilities.db_utils import get_db_session
with patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value=None,
):
out = self._run_in_thread(
lambda: get_db_session(_namespace="test_worker_noctx")
)
assert isinstance(out.get("error"), RuntimeError)
assert "request context" in str(out["error"])
def test_settings_manager_in_worker_uses_user_session_not_defaults(self):
"""The end-to-end symptom: get_settings_manager() from a sync
route's worker thread must bind the user's DB session instead of
silently serving file defaults."""
from local_deep_research.utilities.db_utils import (
get_settings_manager,
)
mock_session = Mock()
with (
patch(
"local_deep_research.utilities.db_utils.get_current_username",
return_value="webuser",
),
patch(
"local_deep_research.utilities.db_utils.db_manager"
) as mock_mgr,
patch("local_deep_research.settings.SettingsManager") as MockSM,
):
mock_mgr.get_session.return_value = mock_session
self._run_in_thread(get_settings_manager)
# Bound to the user's session — not SettingsManager(None).
assert MockSM.call_args[0][0] is mock_session
def test_session_cache_is_keyed_by_resolved_username(self):
"""Two users' requests served by the same worker thread must not
share one cached session entry (the cache sits below username
resolution)."""
from local_deep_research.utilities.db_utils import get_db_session
session_a, session_b = Mock(name="a"), Mock(name="b")
def run_both():
results = {}
with patch(
"local_deep_research.utilities.db_utils.db_manager"
) as mock_mgr:
mock_mgr.get_session.side_effect = lambda u: {
"alice": session_a,
"bob": session_b,
}[u]
with patch(
"local_deep_research.utilities.db_utils"
".get_current_username",
return_value="alice",
):
results["alice"] = get_db_session(
_namespace="test_cache_key"
)
with patch(
"local_deep_research.utilities.db_utils"
".get_current_username",
return_value="bob",
):
results["bob"] = get_db_session(_namespace="test_cache_key")
return results
out = self._run_in_thread(run_both)
results = out["result"]
assert results["alice"] is session_a
assert results["bob"] is session_b