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.

3.9 KB
Raw Blame
"""Regression: LIKE wildcards in journal_quality search must be literal (#3094).

``get_journals_page`` / ``get_institutions_page`` substring-match the user's
search term against ``name_lower`` via ``LIKE``. Without escaping, a term
containing ``%`` or ``_`` acts as a SQL wildcard and returns unrelated
journals/institutions.

These inject a seeded in-memory SQLite engine into ``JournalQualityDB``
(``_ensure_engine`` short-circuits when ``_engine`` is already set), bypassing
the read-only-file machinery so SQLite itself evaluates the ``LIKE ... ESCAPE``
pattern. They fail if the escaping in ``get_journals_page`` /
``get_institutions_page`` is reverted. The bundled ``ref_db`` search tests only
use metacharacter-free terms (``"nature"``), so this is the only fail-on-revert
coverage for these two escape sites.
"""

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.journal_quality.db import JournalQualityDB
from local_deep_research.journal_quality.models import (
    Institution,
    JournalQualityBase,
    Source,
)


def _db_with(sources=(), institutions=()):
    """A JournalQualityDB backed by a seeded in-memory SQLite engine.

    Setting ``_engine``/``_SessionLocal`` directly makes ``_ensure_engine``
    return early, so the read-only-file path (``_resolve_db_path`` / ``mode=ro``
    / schema-version validation) is skipped and queries run against the seeded
    in-memory DB.
    """
    engine = create_engine("sqlite:///:memory:")
    JournalQualityBase.metadata.create_all(engine)
    session_local = sessionmaker(bind=engine, expire_on_commit=False)
    seed = session_local()
    seed.add_all(list(sources) + list(institutions))
    seed.commit()
    seed.close()

    db = JournalQualityDB()
    db._engine = engine
    db._SessionLocal = session_local
    return db


@pytest.fixture
def journal_db_factory():
    """Create test databases and reset their engines after each test."""
    databases = []

    def _make(*, sources=(), institutions=()):
        db = _db_with(sources=sources, institutions=institutions)
        databases.append(db)
        return db

    yield _make

    for db in reversed(databases):
        db.reset()


def _source(name, name_lower):
    return Source(
        name=name,
        name_lower=name_lower,
        is_in_doaj=False,
        is_predatory=False,
        score_source="openalex",
    )


def test_journal_search_percent_treated_as_literal(journal_db_factory):
    """Searching journals for '100%' must not match '100pages'."""
    db = journal_db_factory(
        sources=[
            _source("Journal 100%", "journal 100%"),
            _source("Journal 100pages", "journal 100pages"),
        ]
    )
    names = [
        j["name"] for j in db.get_journals_page(search="100%", per_page=10)[0]
    ]
    assert "Journal 100%" in names
    assert "Journal 100pages" not in names


def test_journal_search_underscore_treated_as_literal(journal_db_factory):
    """Searching journals for 'a_b' must not match 'aXb'."""
    db = journal_db_factory(
        sources=[
            _source("Journal a_b", "journal a_b"),
            _source("Journal aXb", "journal axb"),
        ]
    )
    names = [
        j["name"] for j in db.get_journals_page(search="a_b", per_page=10)[0]
    ]
    assert "Journal a_b" in names
    assert "Journal aXb" not in names


def test_institution_search_underscore_treated_as_literal(journal_db_factory):
    """Searching institutions for 'a_b' must not match 'aXb'."""
    db = journal_db_factory(
        institutions=[
            Institution(
                openalex_id="I1", name="Univ a_b", name_lower="univ a_b"
            ),
            Institution(
                openalex_id="I2", name="Univ aXb", name_lower="univ axb"
            ),
        ]
    )
    names = [
        i["name"]
        for i in db.get_institutions_page(search="a_b", per_page=10)[0]
    ]
    assert "Univ a_b" in names
    assert "Univ aXb" not in names