~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
"""
Tests for the check-deprecated-db pre-commit hook.
Covers regressions:
- File-level 'get_user_db_session' guard used to whitelist whole files;
one correct call silently allowed raw sqlite3.connect elsewhere.
- get_db_connection / import patterns had no comment/docstring skip — a
TODO or migration note referencing the deprecated API was flagged.
- Path exemptions matched substrings, so production names such as
``request_test_handler.py`` skipped every check. They are now decided
per component of the path as committed: a ``tests`` or ``*_tests``
directory, ``migrations``, ``.pre-commit-hooks``, a ``test_*`` basename,
or an exact helper path. A symlink or a ``..`` component never earns one.
Note: SHARED_DB is built at runtime to avoid embedding the literal
shared-database filename in this test file (a separate hook scans every
file for that literal).
"""
import subprocess
import sys
import tempfile
from pathlib import Path
import pytest
HOOK_SCRIPT = (
Path(__file__).parent.parent.parent
/ ".pre-commit-hooks"
/ "check-deprecated-db.py"
)
# Built at runtime to avoid tripping check-ldr-db (a separate hook that
# scans every file for the literal "ldr.db" substring).
SHARED_DB = "ldr" + ".db"
def _run_hook(
content: str, filename: str = "service.py"
) -> subprocess.CompletedProcess:
with tempfile.TemporaryDirectory() as d:
path = Path(d) / filename
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
return subprocess.run(
[sys.executable, str(HOOK_SCRIPT), filename],
capture_output=True,
text=True,
cwd=d,
)
class TestFileLevelGuardRemoved:
"""A correct get_user_db_session call no longer whitelists the whole file."""
def test_correct_call_does_not_exempt_raw_sqlite_connect(self):
content = (
'with get_user_db_session("alice") as session:\n'
" pass\n"
f'conn = sqlite3.connect("{SHARED_DB}")\n'
)
result = _run_hook(content)
assert result.returncode == 1
assert "Direct SQLite connection to shared database" in result.stdout
def test_raw_sqlite_connect_without_sibling_still_flagged(self):
result = _run_hook(f'conn = sqlite3.connect("{SHARED_DB}")\n')
assert result.returncode == 1
def test_noqa_sentinel_exempts_single_line(self):
content = f'conn = sqlite3.connect("{SHARED_DB}") # noqa: shared-db\n'
result = _run_hook(content)
assert result.returncode == 0
class TestCommentSkip:
"""Comments and docstrings mentioning deprecated APIs must not fire."""
def test_todo_comment_about_get_db_connection_not_flagged(self):
result = _run_hook(
"# TODO: replace get_db_connection() with per-user session\n"
)
assert result.returncode == 0
def test_docstring_mention_not_flagged(self):
content = '"""get_db_connection() is deprecated — use get_user_db_session."""\n'
result = _run_hook(content)
assert result.returncode == 0
def test_real_get_db_connection_call_flagged(self):
result = _run_hook("conn = get_db_connection()\n")
assert result.returncode == 1
assert "deprecated get_db_connection" in result.stdout
def test_comment_about_deprecated_import_not_flagged(self):
result = _run_hook(
"# Historical: from ..web.models.database import get_db_connection\n"
)
assert result.returncode == 0
def test_real_deprecated_import_flagged(self):
result = _run_hook(
"from ..web.models.database import get_db_connection\n"
)
assert result.returncode == 1
class TestExistingAllowlistStillApplies:
"""The actual helper modules and test paths remain exempt."""
@pytest.mark.parametrize(
"filename",
[
"src/local_deep_research/web/models/database.py",
"src/local_deep_research/database/session_context.py",
"src/local_deep_research/database/encrypted_db.py",
"src/local_deep_research/utilities/db_utils.py",
],
)
def test_exact_source_helper_exempt(self, filename):
result = _run_hook("conn = get_db_connection()\n", filename=filename)
assert result.returncode == 0
@pytest.mark.parametrize(
"filename",
[
"tests/fixtures.py",
".semgrep/tests/ldr-security.py",
"release_tests/security/conftest.py",
"ui_tests/helpers.py",
"src/local_deep_research/test_helper.py",
"migrations/versions/0001.py",
".pre-commit-hooks/example.py",
],
)
def test_test_and_tool_paths_exempt(self, filename):
result = _run_hook("conn = get_db_connection()\n", filename=filename)
assert result.returncode == 0, (filename, result.stdout, result.stderr)
def test_release_test_tree_may_open_raw_sessions(self):
"""A pytest tree named ``*_tests`` is test code like ``tests/``.
This mirrors a release-test conftest that opens a raw session and
closes it with ``with session:``. The substring rule exempted it
through "tests/", and an exact ``tests`` component alone would not.
"""
content = (
"def db(self):\n"
" session = db_manager.get_session(self.username)\n"
" with session:\n"
" yield session\n"
)
result = _run_hook(
content, filename="release_tests/security/conftest.py"
)
assert result.returncode == 0, (result.stdout, result.stderr)
class TestProductionPathsCannotInheritExemptions:
"""A substring in a production name must never bypass the real hook."""
@pytest.mark.parametrize(
"filename",
[
"src/local_deep_research/request_test_handler.py",
"src/local_deep_research/mytests/service.py",
"src/local_deep_research/migrations_helper.py",
"src/local_deep_research/request_session_context.py",
"src/local_deep_research/request_encrypted_db.py",
"src/local_deep_research/request_db_utils.py",
"src/local_deep_research/other/session_context.py",
"src/local_deep_research/other/encrypted_db.py",
"src/local_deep_research/other/db_utils.py",
"src/local_deep_research/web/models/database.py_helper.py",
"tests/../src/local_deep_research/service.py",
"vendor/src/local_deep_research/utilities/db_utils.py",
"src/local_deep_research/database/connection_helper.py",
"src/local_deep_research/utilities/other.py",
"src/local_deep_research/utilities/db_utils.py/nested.py",
# "tests" inside a component, but not a test-tree name.
"src/local_deep_research/contests/service.py",
"src/local_deep_research/tests_util/service.py",
"src/local_deep_research/release_tests_runner/service.py",
"src/local_deep_research/release_tests.py",
],
)
def test_suspicious_production_path_is_checked(self, filename):
result = _run_hook("conn = get_db_connection()\n", filename=filename)
assert result.returncode == 1, (filename, result.stdout, result.stderr)
assert "Usage of deprecated get_db_connection()" in result.stdout
def test_absolute_file_outside_checkout_cannot_inherit_parent_exemption(
self,
):
with tempfile.TemporaryDirectory() as d:
path = Path(d) / "tests" / "service.py"
path.parent.mkdir()
path.write_text("conn = get_db_connection()\n", encoding="utf-8")
result = subprocess.run(
[sys.executable, str(HOOK_SCRIPT), str(path)],
cwd=HOOK_SCRIPT.parent.parent,
capture_output=True,
text=True,
)
assert result.returncode == 1
assert "Usage of deprecated get_db_connection()" in result.stdout
def _link(link: Path, target: str) -> None:
link.parent.mkdir(parents=True, exist_ok=True)
try:
link.symlink_to(target)
except (OSError, NotImplementedError) as exc:
pytest.skip(f"cannot create symlinks here: {exc}")
def _run_hook_on(root: Path, filename: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, str(HOOK_SCRIPT), filename],
capture_output=True,
text=True,
cwd=root,
)
class TestExemptionUsesCommittedPath:
"""The committed path decides exemption, never a symlink target."""
def _write(self, root: Path, filename: str) -> None:
path = root / filename
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("conn = get_db_connection()\n", encoding="utf-8")
def test_file_symlink_into_test_tree_is_checked(self, tmp_path):
self._write(tmp_path, "tests/helper.py")
_link(
tmp_path / "src/local_deep_research/service.py",
"../../tests/helper.py",
)
result = _run_hook_on(tmp_path, "src/local_deep_research/service.py")
assert result.returncode == 1, (result.stdout, result.stderr)
assert "Usage of deprecated get_db_connection()" in result.stdout
def test_directory_symlink_into_test_tree_is_checked(self, tmp_path):
self._write(tmp_path, "tests/helper.py")
_link(tmp_path / "src/local_deep_research/linked", "../../tests")
result = _run_hook_on(
tmp_path, "src/local_deep_research/linked/helper.py"
)
assert result.returncode == 1, (result.stdout, result.stderr)
assert "Usage of deprecated get_db_connection()" in result.stdout
def test_symlink_inside_test_tree_is_checked(self, tmp_path):
self._write(tmp_path, "src/local_deep_research/service.py")
_link(
tmp_path / "tests/linked_service.py",
"../src/local_deep_research/service.py",
)
result = _run_hook_on(tmp_path, "tests/linked_service.py")
assert result.returncode == 1, (result.stdout, result.stderr)
assert "Usage of deprecated get_db_connection()" in result.stdout
def test_parent_traversal_into_test_tree_is_checked(self, tmp_path):
"""Pre-commit never passes ``..``; such a path earns no exemption."""
self._write(tmp_path, "tests/fixtures.py")
(tmp_path / "src").mkdir()
result = _run_hook_on(tmp_path, "src/../tests/fixtures.py")
assert result.returncode == 1, (result.stdout, result.stderr)
assert "Usage of deprecated get_db_connection()" in result.stdout