~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 journal_quality/db.py accessor methods and utility functions
that lack direct coverage in test_db.py.
Uses the real bundled journal_quality.db where available (via the shared
``ref_db`` fixture in conftest.py, same skip-if-missing pattern as
test_db.py) and temp-file fixtures for filesystem utilities.
"""
import os
import sqlite3
import time
from pathlib import PurePosixPath, PureWindowsPath
from unittest.mock import patch
import pytest
from local_deep_research.journal_quality.db import (
JOURNAL_DB_UNOPENABLE,
JOURNAL_QUALITY_SCHEMA_VERSION,
JournalQualityDB,
_sweep_stale_tmp_files,
journal_db_file_is_valid,
journal_db_file_status,
sqlite_readonly_uri,
)
_JQ_DB_MODULE = "local_deep_research.journal_quality.db"
# PLoS ONE — open access, in DOAJ since the registry's early days, so a
# stable positive example. Nature is a subscription journal and therefore
# absent from DOAJ — a stable negative example that exists in the sources
# table (unlike a made-up ISSN, it exercises the is_in_doaj filter).
DOAJ_ISSN = "1932-6203" # PLoS ONE
NON_DOAJ_ISSN = "0028-0836" # Nature
# _sweep_stale_tmp_files removes tmp files older than 1 hour; backdate
# twice that so the test is comfortably past the cutoff.
SWEEP_CUTOFF_SECONDS = 3600
STALE_MTIME_AGE_SECONDS = 2 * SWEEP_CUTOFF_SECONDS
def _uninitialized_db() -> JournalQualityDB:
"""Instance that skips ``__init__`` (no engine, no lock).
Valid for ``_validate_existing_db`` and the early-return guard of
``expand_abbreviation`` because neither reads instance state before
the code under test runs. If those methods ever grow instance-state
dependencies, switch this to a properly constructed instance.
"""
return JournalQualityDB.__new__(JournalQualityDB)
def _openalex_id_for_ror(ref_db, ror_id):
"""Fetch an institution's OpenAlex ID straight from the SQLite file.
The public ``lookup_institution()`` dict deliberately omits
``openalex_id`` (see ``_institution_to_dict``), so tests that
exercise the openalex_id lookup path have to read it from the
table directly.
"""
conn = sqlite3.connect(
f"file:{ref_db._resolve_db_path()}?mode=ro", uri=True
)
try:
row = conn.execute(
"SELECT openalex_id FROM institutions WHERE ror_id = ?",
(ror_id,),
).fetchone()
finally:
conn.close()
return row[0] if row else None
# ---------------------------------------------------------------------------
# lookup_doaj / is_in_doaj
# ---------------------------------------------------------------------------
@pytest.fixture()
def doaj_db():
"""A ``JournalQualityDB`` backed by an in-memory DB with controlled rows.
The DOAJ accessor tests must not use the shared ``ref_db`` (real bundled
file): that fixture *skips* in CI where the file is absent, and worse it
makes these tests *fail* (not skip) on a machine whose local
``journal_quality.db`` was built without ``doaj_journals.json`` — in that
case ``is_in_doaj`` is 0 for every row, so the positive assertions can
never hold. Wiring a small fixed dataset makes ``lookup_doaj`` /
``is_in_doaj`` deterministic and lets them actually run in CI.
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from local_deep_research.journal_quality.models import (
JournalQualityBase,
Source,
)
from local_deep_research.utilities.citation_normalizer import (
normalize_issn,
)
# StaticPool + a single shared connection keeps the in-memory DB alive for
# the whole fixture (a plain sqlite:// would get a fresh empty DB per
# connection).
engine = create_engine(
"sqlite://",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
JournalQualityBase.metadata.create_all(engine)
session_local = sessionmaker(bind=engine, expire_on_commit=False)
with session_local() as s:
s.add(
Source(
name="PLoS ONE",
name_lower="plos one",
issn=normalize_issn(DOAJ_ISSN),
publisher="PLOS",
is_in_doaj=True,
score_source="doaj",
)
)
# Nature: present in sources but NOT in DOAJ — the negative example
# that exercises the is_in_doaj filter rather than a missing row.
s.add(
Source(
name="Nature",
name_lower="nature",
issn=normalize_issn(NON_DOAJ_ISSN),
publisher="Springer Nature",
is_in_doaj=False,
score_source="openalex",
)
)
s.commit()
db = JournalQualityDB()
# Pre-wire the engine/session so _ensure_engine() short-circuits and the
# accessor logic runs against our dataset, never the real DB path.
db._engine = engine
db._SessionLocal = session_local
try:
yield db
finally:
engine.dispose()
class TestLookupDoaj:
"""DOAJ lookup by ISSN (query against is_in_doaj flag on Source rows)."""
def test_lookup_doaj_journal_returns_dict(self, doaj_db):
result = doaj_db.lookup_doaj(issn=DOAJ_ISSN)
assert result is not None
assert result["name"] == "PLoS ONE"
assert result["publisher"] == "PLOS"
def test_lookup_non_doaj_journal_returns_none(self, doaj_db):
# Nature exists in the sources table but is not in DOAJ, so the
# DOAJ-only lookup must filter it out.
assert doaj_db.lookup_doaj(issn=NON_DOAJ_ISSN) is None
def test_lookup_nonexistent_issn_returns_none(self, doaj_db):
result = doaj_db.lookup_doaj(issn="0000-0000")
assert result is None
def test_lookup_none_issn_returns_none(self, doaj_db):
result = doaj_db.lookup_doaj(issn=None)
assert result is None
def test_lookup_empty_issn_returns_none(self, doaj_db):
result = doaj_db.lookup_doaj(issn="")
assert result is None
class TestIsInDoaj:
def test_doaj_journal_is_true(self, doaj_db):
assert doaj_db.is_in_doaj(DOAJ_ISSN) is True
def test_non_doaj_journal_is_false(self, doaj_db):
assert doaj_db.is_in_doaj(NON_DOAJ_ISSN) is False
def test_nonexistent_issn(self, doaj_db):
assert doaj_db.is_in_doaj("0000-0000") is False
# ---------------------------------------------------------------------------
# count_predatory_by_names
# ---------------------------------------------------------------------------
class TestCountPredatoryByNames:
def test_empty_iterable_returns_zero(self, ref_db):
assert ref_db.count_predatory_by_names([]) == 0
def test_none_and_blanks_filtered(self, ref_db):
assert ref_db.count_predatory_by_names([None, "", " "]) == 0
def test_legitimate_journals_count_zero(self, ref_db):
count = ref_db.count_predatory_by_names(["Nature", "Science"])
assert count == 0
def test_returns_int(self, ref_db):
result = ref_db.count_predatory_by_names(["Nature"])
assert isinstance(result, int)
# ---------------------------------------------------------------------------
# lookup_institution
# ---------------------------------------------------------------------------
class TestLookupInstitution:
def test_by_name_returns_dict(self, ref_db):
result = ref_db.lookup_institution(name="Harvard University")
assert result is not None
assert result["name"] == "Harvard University"
assert result["h_index"] is not None
def test_nonexistent_returns_none(self, ref_db):
result = ref_db.lookup_institution(name="ZZZ Nonexistent 99999")
assert result is None
def test_no_args_returns_none(self, ref_db):
result = ref_db.lookup_institution()
assert result is None
def test_by_openalex_id(self, ref_db):
# The accessor dict has no openalex_id key, so resolve it from
# the table via the ror_id and re-lookup through the public API.
by_name = ref_db.lookup_institution(name="Harvard University")
assert by_name is not None
openalex_id = _openalex_id_for_ror(ref_db, by_name["ror_id"])
assert openalex_id is not None
by_id = ref_db.lookup_institution(openalex_id=openalex_id)
assert by_id is not None
assert by_id["name"] == "Harvard University"
def test_by_ror_id(self, ref_db):
by_name = ref_db.lookup_institution(name="Harvard University")
assert by_name is not None
assert by_name["ror_id"]
by_ror = ref_db.lookup_institution(ror_id=by_name["ror_id"])
assert by_ror is not None
assert by_ror["name"] == "Harvard University"
# ---------------------------------------------------------------------------
# score_from_affiliations
# ---------------------------------------------------------------------------
class TestScoreFromAffiliations:
def test_empty_list_returns_none(self, ref_db):
assert ref_db.score_from_affiliations([]) is None
def test_string_affiliation(self, ref_db):
result = ref_db.score_from_affiliations(["Harvard University"])
assert isinstance(result, int)
def test_dict_affiliation_with_name(self, ref_db):
result = ref_db.score_from_affiliations(
[{"name": "Harvard University"}]
)
assert isinstance(result, int)
def test_dict_affiliation_with_openalex_id(self, ref_db):
by_name = ref_db.lookup_institution(name="Harvard University")
assert by_name is not None
openalex_id = _openalex_id_for_ror(ref_db, by_name["ror_id"])
assert openalex_id is not None
result = ref_db.score_from_affiliations([{"openalex_id": openalex_id}])
assert isinstance(result, int)
def test_nonexistent_returns_none(self, ref_db):
result = ref_db.score_from_affiliations(
["ZZZ Nonexistent Institution 99999"]
)
assert result is None
def test_dict_with_no_relevant_keys_returns_none(self, ref_db):
result = ref_db.score_from_affiliations([{"foo": "bar"}])
assert result is None
# ---------------------------------------------------------------------------
# expand_abbreviation
# ---------------------------------------------------------------------------
class TestExpandAbbreviation:
def test_empty_string_returns_none(self, ref_db):
assert ref_db.expand_abbreviation("") is None
def test_none_returns_none(self):
assert _uninitialized_db().expand_abbreviation(None) is None
def test_nonexistent_returns_none(self, ref_db):
result = ref_db.expand_abbreviation("ZZZNOTANABBREVIATION12345")
assert result is None
def test_known_abbreviation(self, ref_db):
# JACS comes from the bundled JabRef abbreviation list.
result = ref_db.expand_abbreviation("JACS")
assert result is not None
assert "american chemical society" in result.lower()
# ---------------------------------------------------------------------------
# is_predatory expanded branches
# ---------------------------------------------------------------------------
class TestIsPredatoryExpanded:
"""Expand TestIsPredatory with publisher and hijacked branches."""
def test_publisher_name_arg(self, ref_db):
# A legitimate publisher should not be flagged
is_pred, source = ref_db.is_predatory(publisher_name="Elsevier")
assert is_pred is False
def test_both_journal_and_publisher(self, ref_db):
is_pred, source = ref_db.is_predatory(
journal_name="Nature", publisher_name="Springer Nature"
)
assert is_pred is False
def test_long_publisher_name_handled(self, ref_db):
# The implementation checks substrings for long publisher names
is_pred, source = ref_db.is_predatory(publisher_name="A" * 500)
assert is_pred is False
# ---------------------------------------------------------------------------
# _validate_existing_db (temp-file tests, no real DB needed)
# ---------------------------------------------------------------------------
class TestValidateExistingDb:
def test_valid_db_with_matching_schema(self, tmp_path):
db_path = tmp_path / "test.db"
conn = sqlite3.connect(str(db_path))
conn.execute(
"PRAGMA user_version = %d" % JOURNAL_QUALITY_SCHEMA_VERSION
)
conn.execute("CREATE TABLE t (x)")
conn.commit()
conn.close()
assert _uninitialized_db()._validate_existing_db(db_path) is True
def test_stale_schema_triggers_rebuild(self, tmp_path):
db_path = tmp_path / "test.db"
conn = sqlite3.connect(str(db_path))
conn.execute("PRAGMA user_version = 999")
conn.execute("CREATE TABLE t (x)")
conn.commit()
conn.close()
result = _uninitialized_db()._validate_existing_db(db_path)
assert result is False
# File should have been removed by _unlink_unusable_db
assert not db_path.exists()
def test_grandfathered_zero_version(self, tmp_path):
db_path = tmp_path / "test.db"
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE t (x)")
conn.commit()
conn.close()
assert _uninitialized_db()._validate_existing_db(db_path) is True
def test_corrupted_file_returns_false(self, tmp_path):
db_path = tmp_path / "test.db"
db_path.write_text("this is not a database")
assert _uninitialized_db()._validate_existing_db(db_path) is False
def test_close_happens_before_unlink_on_unusable_db(self, tmp_path):
"""Windows-motivated ordering invariant (#6299): the SQLite
connection must be closed BEFORE the unusable file is unlinked —
unlinking first fails with "file in use" on Windows and breaks
the rebuild that follows. Linux CI cannot show the failure
itself, so pin the call sequence directly."""
from unittest.mock import patch
db_path = tmp_path / "test.db"
db_path.write_text("this is not a database")
calls = []
db = _uninitialized_db()
with (
patch(
"local_deep_research.utilities.resource_utils.safe_close",
side_effect=lambda conn, ctx: calls.append("close"),
),
patch.object(
JournalQualityDB,
"_unlink_unusable_db",
side_effect=lambda path: calls.append("unlink"),
),
):
assert db._validate_existing_db(db_path) is False
assert calls == ["close", "unlink"]
def test_missing_file_returns_false(self, tmp_path):
db_path = tmp_path / "nonexistent.db"
assert _uninitialized_db()._validate_existing_db(db_path) is False
def test_hostile_characters_in_directory_path(self, tmp_path):
"""A data directory containing '?', '#', '%' or a space must not
truncate the SQLite URI.
The probe used to build the URI by interpolating the raw path
into ``f"file:{path}?mode=ro"``. A literal ``?`` or ``#`` in the
directory name ends the URI's path component early — SQLite
parses everything from there on as query string / fragment —
which drops ``mode=ro`` and, since the truncated path usually
doesn't already exist, opens (and creates) a stray file there
instead of reading the real database. ``sqlite_readonly_uri``
percent-encodes those characters so the whole path round-trips.
"""
hostile_dir = tmp_path / "x?y#z%41 w"
hostile_dir.mkdir()
db_path = hostile_dir / "test.db"
conn = sqlite3.connect(str(db_path))
conn.execute(
"PRAGMA user_version = %d" % JOURNAL_QUALITY_SCHEMA_VERSION
)
conn.execute("CREATE TABLE t (x)")
conn.commit()
conn.close()
before = {
str(p.relative_to(tmp_path))
for p in tmp_path.rglob("*")
if p.is_file()
}
assert _uninitialized_db()._validate_existing_db(db_path) is True
after = {
str(p.relative_to(tmp_path))
for p in tmp_path.rglob("*")
if p.is_file()
}
assert after == before, (
"validating a DB in a hostile-character directory must not "
"create (or remove) any file, inside the directory or as a "
"truncated-path sibling of it"
)
def test_open_failure_on_an_existing_file_does_not_delete_it(
self, tmp_path
):
"""An existing DB that cannot even be OPENED must be left alone.
A failed open says nothing about the file's contents — it is a
permissions problem, an unreachable share, or (the regression
this pins) a URI form SQLite refuses: ``Path.as_uri()`` turns a
Windows UNC path into ``file://server/share/...`` and SQLite
rejects that authority. Treating the open failure as corruption
made ``_validate_existing_db`` unlink a valid DB on every init.
"""
db_path = tmp_path / "test.db"
conn = sqlite3.connect(str(db_path))
conn.execute(
"PRAGMA user_version = %d" % JOURNAL_QUALITY_SCHEMA_VERSION
)
conn.execute("CREATE TABLE t (x)")
conn.commit()
conn.close()
with patch(
f"{_JQ_DB_MODULE}.sqlite3.connect",
side_effect=sqlite3.OperationalError(
"invalid uri authority: server"
),
):
assert journal_db_file_status(db_path) == JOURNAL_DB_UNOPENABLE
assert journal_db_file_is_valid(db_path) is False
with pytest.raises(FileNotFoundError):
_uninitialized_db()._validate_existing_db(db_path)
assert db_path.exists(), (
"a DB that merely could not be opened was deleted as if it "
"were corrupt"
)
assert _uninitialized_db()._validate_existing_db(db_path) is True
class TestSqliteReadonlyUri:
"""The probe's URI must open on every path form SQLite can be handed.
SQLite accepts only an empty or ``localhost`` authority (stock
builds lack ``SQLITE_ALLOW_URI_AUTHORITY``), so a UNC host must go
into the path — ``file:////server/share`` — never the authority.
"""
def test_posix_path_percent_encodes_uri_delimiters(self):
uri = sqlite_readonly_uri(PurePosixPath("/data/x?y#z%41 w é/j.db"))
assert uri == ("file:///data/x%3Fy%23z%2541%20w%20%C3%A9/j.db?mode=ro")
@pytest.mark.skipif(
os.name == "nt",
reason="non-UTF-8 filesystem bytes in a str path are POSIX-only",
)
def test_posix_non_utf8_bytes_are_percent_encoded_verbatim(self):
# b"\xe9" is not valid UTF-8, so on POSIX it decodes to the lone
# surrogate "\udce9"; quote(str) would raise UnicodeEncodeError.
# The URI must carry the exact on-disk byte, %E9, not a UTF-8
# re-encoding of some substitute character.
path = PurePosixPath(os.fsdecode(b"/data\xe9 ?#/j.db"))
uri = sqlite_readonly_uri(path)
assert uri == "file:///data%E9%20%3F%23/j.db?mode=ro"
def test_immutable_flag_appends_engine_query(self):
uri = sqlite_readonly_uri(
PurePosixPath("/data/x?y#z%41 w/j.db"), immutable=True
)
assert uri == (
"file:///data/x%3Fy%23z%2541%20w/j.db?mode=ro&immutable=1"
)
def test_windows_drive_path(self):
uri = sqlite_readonly_uri(PureWindowsPath(r"C:\Users\a b\j.db"))
assert uri == "file:///C:/Users/a%20b/j.db?mode=ro"
def test_windows_unc_path_uses_an_empty_authority(self):
uri = sqlite_readonly_uri(
PureWindowsPath(r"\\server\share\x?y#z%41\j.db")
)
assert uri == "file:////server/share/x%3Fy%23z%2541/j.db?mode=ro"
@pytest.mark.skipif(
os.name == "nt",
reason="the four-slash rewrite of a drive path is POSIX-only",
)
def test_generated_forms_open_and_authority_form_does_not(self, tmp_path):
hostile_dir = tmp_path / "x?y#z%41 w é"
hostile_dir.mkdir()
db_path = hostile_dir / "j.db"
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE t (x)")
conn.commit()
conn.close()
uri = sqlite_readonly_uri(db_path)
# Same file through the four-slash (UNC-shaped) form: an empty
# authority with a path starting "//", which POSIX resolves to
# the same file — so SQLite demonstrably parses that shape.
four_slash = "file:/" + uri[len("file:") :]
assert four_slash.startswith("file:////")
for candidate in (uri, four_slash):
with sqlite3.connect(candidate, uri=True) as c:
assert c.execute("SELECT count(*) FROM t").fetchone() == (0,)
c.close()
with pytest.raises(sqlite3.OperationalError, match="authority"):
sqlite3.connect("file://server" + uri[len("file://") :], uri=True)
# ---------------------------------------------------------------------------
# _sweep_stale_tmp_files (temp-file tests)
# ---------------------------------------------------------------------------
class TestSweepStaleTmpFiles:
def test_removes_old_tmp_files(self, tmp_path):
stale = tmp_path / "journal_quality.db.tmp-old"
stale.write_text("stale")
old_mtime = time.time() - STALE_MTIME_AGE_SECONDS
os.utime(str(stale), (old_mtime, old_mtime))
fresh = tmp_path / "journal_quality.db.tmp-recent"
fresh.write_text("fresh")
_sweep_stale_tmp_files(tmp_path, "journal_quality.db")
assert not stale.exists()
assert fresh.exists()
def test_nonexistent_directory_is_noop(self, tmp_path):
missing = tmp_path / "does_not_exist"
_sweep_stale_tmp_files(missing, "journal_quality.db")
# No exception raised
def test_no_matching_files_is_noop(self, tmp_path):
other = tmp_path / "other.txt"
other.write_text("unrelated")
_sweep_stale_tmp_files(tmp_path, "journal_quality.db")
assert other.exists()
# _escape_like was consolidated into the shared ``sql_utils.escape_like``
# (see tests/utilities/test_sql_utils.py::TestEscapeLike for its coverage).