Skip to content

~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.

15.9 KB
Raw Blame
"""Cover journal DB query branches with a deterministic in-memory dataset.

The production accessor tests primarily consume a locally downloaded database
and therefore skip in clean CI. This battery populates the real SQLAlchemy
schema through db.py's own builders, then exercises lookup fallbacks, dashboard
queries, missing-data behavior, and session recovery without network access.
"""

from collections.abc import Iterator, Mapping
from pathlib import Path

import pytest
from sqlalchemy import create_engine, text
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from local_deep_research.journal_quality import db as db_module
from local_deep_research.journal_quality.db import (
    JournalQualityDB,
    _populate_abbreviations,
    _populate_institutions,
    _populate_predatory,
    _populate_sources,
)
from local_deep_research.journal_quality.models import JournalQualityBase


@pytest.fixture()
def offline_db() -> Iterator[JournalQualityDB]:
    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)
    predatory = {
        "journals": {"bad journal"},
        "publishers": {"dubious publishing group", "tiny"},
        "hijacked": {"hijacked clone"},
        "long_pubs": ["dubious publishing group"],
    }
    sources = {
        "S1": {
            "n": "The Added Prefix",
            "i": "1111-1111",
            "h": 1200,
            "cb": 10,
            "t": "j",
        },
        "S2": {"n": "Stripped Prefix", "i": "2222-2222", "h": 5, "cb": 20},
        "S3": {"n": "Special Proceedings", "i": "3333-3333", "h": 5, "cb": 30},
        "S4": {"n": "Molecular Therapy", "i": "4444-4444", "h": 5, "cb": 40},
        "S5": {"n": "Clinical Methods", "i": "5555-5555", "h": 5, "cb": 50},
        "S6": {"n": "Bad Journal", "i": "6666-6666", "h": 5, "cb": 60},
        # No `cb`, so it never joins the by-source_type quartile groups
        # the other rows depend on — its quality comes straight from
        # h_index, well outside tier "moderate" (5-6). Used to make the
        # dashboard tier filter load-bearing without disturbing the
        # quartile percentiles the other assertions rely on.
        "S7": {"n": "Extra Prefix Elite", "i": "9999-1111", "h": 999},
    }
    institutions = {
        "I1": {"n": "Example University", "r": "01example", "h": 500},
        "I2": {"n": "Other Institute", "r": "02other", "h": 50},
        # Matches the dashboard test's search="institute" alongside
        # "Other Institute", with a distinct h_index so a dropped/wrong
        # order clause is visible in the returned ordering.
        "I3": {"n": "Sample Institute", "r": "03sample", "h": 999},
    }
    with session_local() as session:
        _populate_predatory(session, predatory)
        _populate_sources(
            session,
            sources,
            {
                "77777777": {
                    "name": "Listed Journal",
                    "publisher": "Open Press",
                },
                # DOAJ-only crossref entry (no OpenAlex row shares its
                # name), so score_source="doaj". Matches the dashboard
                # test's search="prefix" and lands in tier "moderate"
                # (DOAJ_QUALITY_LISTED=5) — used to make the
                # score_source filter load-bearing.
                "88888888": {
                    "name": "Doaj Prefix Extra",
                    "publisher": "Some Press",
                },
            },
            predatory,
        )
        _populate_institutions(session, institutions)
        _populate_abbreviations(session, {"J Test": "Journal of Testing"})
        session.commit()

    database = JournalQualityDB()
    database._engine = engine
    database._SessionLocal = session_local
    try:
        yield database
    finally:
        engine.dispose()


@pytest.mark.parametrize(
    ("lookup", "expected"),
    (
        ({"source_id": "https://openalex.org/S1"}, "The Added Prefix"),
        ({"issn": "2222-2222"}, "Stripped Prefix"),
        ({"name": "Added Prefix"}, "The Added Prefix"),
        ({"name": "The Stripped Prefix"}, "Stripped Prefix"),
        (
            {"name": "Proceedings of the Conference on Special Proceedings"},
            "Special Proceedings",
        ),
        ({"name": "Molecular Therapy : A Long Subtitle"}, "Molecular Therapy"),
        ({"name": "Clinical Methods. Extended Edition"}, "Clinical Methods"),
    ),
)
def test_source_lookup_resolves_identifiers_and_name_fallbacks(
    offline_db: JournalQualityDB, lookup: Mapping[str, str], expected: str
) -> None:
    result = offline_db.lookup_openalex(**lookup)

    assert result is not None
    assert result["name"] == expected


@pytest.mark.parametrize(
    ("arguments", "matched", "source"),
    (
        ({"journal_name": "Bad Journal"}, True, "stop-predatory-journals"),
        ({"journal_name": "Hijacked Clone"}, True, "stop-predatory-hijacked"),
        (
            {"publisher_name": "Dubious Publishing Group"},
            True,
            "stop-predatory-publishers",
        ),
        (
            {"publisher_name": "Dubious Publishing Group Europe"},
            True,
            "stop-predatory-publishers",
        ),
        (
            # `pub_norm in entry` direction: "dubious" is short enough
            # that it never becomes its own PredatoryPublisher row, but
            # it IS a substring of the long, is_long=True "dubious
            # publishing group" entry.
            {"publisher_name": "Dubious"},
            True,
            "stop-predatory-publishers",
        ),
        (
            # Negative control for the is_long guard. "tiny" is a real
            # (short, is_long=False) predatory-publisher entry and is a
            # substring of "destiny press" — if the substring scan ever
            # stopped restricting itself to is_long=True rows, this
            # would incorrectly flag as predatory.
            {"publisher_name": "destiny press"},
            False,
            None,
        ),
    ),
)
def test_predatory_lookup_covers_each_reference_table(
    offline_db: JournalQualityDB,
    arguments: Mapping[str, str],
    matched: bool,
    source: str | None,
) -> None:
    assert offline_db.is_predatory(**arguments) == (matched, source)


def test_whitelist_and_abbreviation_paths_use_compiled_signals(
    offline_db: JournalQualityDB,
) -> None:
    assert offline_db.is_whitelisted(issn="7777-7777") is True
    assert offline_db.is_whitelisted(name="The Added Prefix") is True
    assert offline_db.is_whitelisted(name="Unknown Venue") is False
    assert offline_db.expand_abbreviation("J. Test.") == "Journal of Testing"


def test_institution_lookup_and_affiliation_scoring_accept_compact_ids(
    offline_db: JournalQualityDB,
) -> None:
    by_openalex = offline_db.lookup_institution(
        openalex_id="https://openalex.org/I1"
    )
    by_ror = offline_db.lookup_institution(ror_id="https://ror.org/01example/")
    by_name = offline_db.lookup_institution(name="Example University")

    assert by_openalex == by_ror == by_name
    assert by_name is not None
    assert by_name["country"] is None
    assert (
        offline_db.score_from_affiliations(
            [
                "Other Institute",
                {"id": "https://openalex.org/I1"},
                {"ror": "https://ror.org/02other/"},
                {"name": "Example University"},
            ]
        )
        == 6
    )
    # I1 (h=500) reachable ONLY via its compact openalex_id — no name
    # anywhere in this call. Dropping the `openalex_id`/`id` OR-clause
    # (or the `aff.get("id")` fallback) collapses this to `None`.
    assert (
        offline_db.score_from_affiliations([{"id": "https://openalex.org/I1"}])
        == 6
    )
    # I2 (h=50) reachable ONLY via its compact ROR id — no name anywhere
    # in this call. Dropping the ROR OR-clause collapses this to `None`.
    assert (
        offline_db.score_from_affiliations(
            [{"ror": "https://ror.org/02other/"}]
        )
        == 4
    )


def test_batch_and_predatory_count_normalize_inputs_across_chunk_boundary(
    offline_db: JournalQualityDB,
) -> None:
    # 900 unique names + 2 case-variant duplicates of a real name. The
    # `IN (...)` semantics of the batch query make in-list duplicates
    # invisible to the result either way (SQL set membership, not a
    # count) and `count_predatory_by_names` returns a plain `COUNT(*)`,
    # so this does NOT exercise the seen/uniq de-duplication step
    # (db.py:486-493) — it exercises normalization plus the CHUNK=900
    # boundary: the normalized target name lands at index 900, in the
    # second chunk, so truncating to one chunk would make it disappear.
    names = [f"unknown venue {index}" for index in range(900)]
    names.extend(["THE ADDED PREFIX", "The Added Prefix"])

    result = offline_db.lookup_sources_batch(names)

    assert list(result) == ["the added prefix"]
    assert (
        offline_db.count_predatory_by_names(
            ["Bad Journal", " bad journal ", "The Added Prefix"]
        )
        == 1
    )


def test_dashboard_queries_filter_sort_and_summarize_synthetic_rows(
    offline_db: JournalQualityDB,
) -> None:
    summary = offline_db.get_summary()
    quality = offline_db.get_quality_distribution()
    source = offline_db.get_source_distribution()
    journals, journal_total = offline_db.get_journals_page(
        page=0,
        per_page=20,
        search="prefix",
        tier="moderate",
        score_source="openalex",
        sort="tainted",
        order="tainted",
    )
    institutions, institution_total = offline_db.get_institutions_page(
        page=0,
        per_page=10,
        search="institute",
        sort="name",
        order="tainted",
    )

    assert summary["total"] == 9
    assert sum(quality.values()) == 9
    assert source == {"doaj": 2, "openalex": 7}
    assert journal_total == 2
    assert {row["name"] for row in journals} == {
        "Stripped Prefix",
        "The Added Prefix",
    }
    assert institution_total == 2
    # sort="name" + an invalid `order` value: the order allowlist
    # (db.py:909-910) must silently fall back to "desc" rather than
    # raise or silently behave as "asc" — "Sample Institute" > "Other
    # Institute" lexicographically, so it comes first only under desc.
    assert [row["name"] for row in institutions] == [
        "Sample Institute",
        "Other Institute",
    ]

    # --- tier filter is load-bearing ---
    # "Extra Prefix Elite" matches search="prefix" and score_source=
    # "openalex" but its quality (10, from h_index alone) falls outside
    # tier "moderate" (5-6), so it is correctly excluded above. Dropping
    # the `tier` where-clause (db.py:861-863) would let it back in.
    journals_no_tier, total_no_tier = offline_db.get_journals_page(
        page=0,
        per_page=20,
        search="prefix",
        tier="",
        score_source="openalex",
        sort="tainted",
        order="tainted",
    )
    assert total_no_tier == 3
    assert {row["name"] for row in journals_no_tier} == {
        "Stripped Prefix",
        "The Added Prefix",
        "Extra Prefix Elite",
    }

    # --- score_source filter is load-bearing ---
    # "Doaj Prefix Extra" matches search="prefix" and tier "moderate"
    # but has score_source="doaj", so it is correctly excluded above.
    # Dropping the `score_source` where-clause (db.py:864-865) would
    # let it back in.
    journals_no_score_source, total_no_score_source = (
        offline_db.get_journals_page(
            page=0,
            per_page=20,
            search="prefix",
            tier="moderate",
            score_source="",
            sort="tainted",
            order="tainted",
        )
    )
    assert total_no_score_source == 3
    assert {row["name"] for row in journals_no_score_source} == {
        "Stripped Prefix",
        "The Added Prefix",
        "Doaj Prefix Extra",
    }

    # NOTE: `max(1, page)` (db.py:874/928) is deliberately not asserted
    # against `page=0`/negative `page` here. Verified directly against
    # both raw sqlite3 and this project's SQLAlchemy engine: SQLite
    # treats any negative OFFSET as equivalent to OFFSET 0, so removing
    # the clamp is unobservable through any query result on this
    # backend — there is no black-box assertion that makes that line
    # load-bearing.


def test_missing_database_returns_empty_public_results(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    database = JournalQualityDB()

    def raise_missing() -> None:
        raise FileNotFoundError

    monkeypatch.setattr(database, "_ensure_engine", raise_missing)

    assert database.available is False
    assert database.lookup_openalex(name="Any") is None
    assert database.lookup_doaj(issn="1111-1111") is None
    assert database.lookup_sources_batch(["Any"]) == {}
    assert database.count_predatory_by_names(["Any"]) == 0
    assert database.is_predatory(journal_name="Any") == (False, None)
    assert database.lookup_institution(name="Any") is None
    assert database.score_from_affiliations(["Any"]) is None
    assert database.expand_abbreviation("Any") is None
    assert database.get_summary()["total"] == 0
    assert database.get_quality_distribution() == {}
    assert database.get_source_distribution() == {}
    assert database.get_journals_page() == ([], 0)
    assert database.get_institutions_page() == ([], 0)


def test_session_database_error_resets_cached_engine(
    offline_db: JournalQualityDB,
) -> None:
    with pytest.raises(OperationalError, match="no such table"):
        with offline_db.session() as session:
            session.execute(text("SELECT * FROM missing_table"))

    assert offline_db._engine is None
    assert offline_db._SessionLocal is None


@pytest.mark.parametrize(
    ("mock_mode", "message"),
    (
        # Every FileNotFoundError branch in `_build_or_raise` shares the
        # substring "not available", which doesn't distinguish them —
        # match on the branch-specific wording instead.
        (True, "LDR_TESTING_WITH_MOCKS"),
        (False, "Check your network"),
    ),
)
def test_build_or_raise_reports_unavailable_local_snapshots(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
    mock_mode: bool,
    message: str,
) -> None:
    from local_deep_research.journal_quality import downloader
    from local_deep_research.settings.env_definitions import testing

    download_requests: list[bool] = []

    def ensure_local_data(*, auto_download: bool) -> tuple[Path, bool]:
        download_requests.append(auto_download)
        return tmp_path, False

    monkeypatch.setattr(testing, "testing_with_mocks", lambda: mock_mode)
    monkeypatch.setattr(downloader, "ensure_journal_data", ensure_local_data)

    with pytest.raises(FileNotFoundError, match=message):
        JournalQualityDB()._build_or_raise(tmp_path / "missing.db")

    assert download_requests == [not mock_mode]


@pytest.mark.parametrize("mock_mode", (True, False))
def test_build_or_raise_uses_local_snapshots_with_requested_download_policy(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mock_mode: bool
) -> None:
    from local_deep_research.journal_quality import downloader
    from local_deep_research.settings.env_definitions import testing

    calls: list[tuple[Path, Path]] = []
    output = tmp_path / "local.db"
    download_requests: list[bool] = []

    def ensure_local_data(*, auto_download: bool) -> tuple[Path, bool]:
        download_requests.append(auto_download)
        return tmp_path, True

    monkeypatch.setattr(testing, "testing_with_mocks", lambda: mock_mode)
    monkeypatch.setattr(downloader, "ensure_journal_data", ensure_local_data)
    monkeypatch.setattr(
        db_module,
        "build_db",
        lambda *, data_dir, output_path: calls.append((data_dir, output_path)),
    )

    JournalQualityDB()._build_or_raise(output)

    assert calls == [(tmp_path, output)]
    assert download_requests == [not mock_mode]