~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
test(journal_quality): add offline DB build and accessor batteries (#6124)
LearningCircuitd6da8dc30c47
diff --git a/tests/journal_quality/test_db_offline_accessors.py b/tests/journal_quality/test_db_offline_accessors.pynew file mode 100644index 0000000000..2637ef688c--- /dev/null+++ b/tests/journal_quality/test_db_offline_accessors.py@@ -0,0 +1,441 @@+"""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+++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()+++ ("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+++ ("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+++ ("mock_mode", "message"),+ (+ # The two branches at db.py:333-345 share 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]++[email protected]("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]diff --git a/tests/journal_quality/test_db_offline_build.py b/tests/journal_quality/test_db_offline_build.pynew file mode 100644index 0000000000..7d0e84fad2--- /dev/null+++ b/tests/journal_quality/test_db_offline_build.py@@ -0,0 +1,345 @@+"""Exercise journal DB compilation without relying on downloaded snapshots.++The legacy build tests skip when the large bundled database is absent, which+leaves the writer, compact-input loaders, and atomic replacement path dark in+CI. These tests create the smallest representative snapshots under pytest's+temporary directory and compile them through the production build entrypoint.+"""++from __future__ import annotations++import gzip+import json+import sqlite3+import stat+from contextlib import closing+from pathlib import Path+from typing import Iterator, Mapping++import pytest+from sqlalchemy import text+from sqlalchemy.exc import OperationalError++from local_deep_research.journal_quality import db as db_module+from local_deep_research.journal_quality.db import (+ JOURNAL_QUALITY_SCHEMA_VERSION,+ JournalQualityDB,+ _load_abbreviations,+ _load_doaj,+ _load_institutions,+ _load_openalex,+ _load_predatory,+ build_db,+)+++type JsonValue = (+ None | bool | int | float | str | list[JsonValue] | dict[str, JsonValue]+)+++def _write_gzip_json(path: Path, payload: Mapping[str, JsonValue]) -> None:+ with gzip.open(path, "wt", encoding="utf-8") as stream:+ json.dump(payload, stream)+++def compact_snapshots(tmp_path: Path) -> Path:+ _write_gzip_json(+ tmp_path / "openalex_sources.json.gz",+ {+ "s": {+ "S1": {+ "n": "Listed Trap",+ "i": "1111-1111",+ "p": "Ordinary Press",+ "h": 5,+ "cb": 10,+ "t": "j",+ },+ "S2": {+ "n": "Publisher Victim",+ "i": "2222-2222",+ "p": "Dubious Publishing Group",+ "h": 7,+ "cb": 20,+ "t": "c",+ },+ "S3": {+ "n": "Hijacked Clone",+ "i": "3333-3333",+ "h": 9,+ "cb": 30,+ },+ "S4": {+ "n": "Duplicate Venue",+ "i": "4444-4444",+ "h": 2,+ "cb": 40,+ },+ "S5": {+ "n": "Duplicate Venue",+ "i": "4444-4444",+ "h": 20,+ "cb": 50,+ },+ "S6": {"n": " ", "i": "5555-5555"},+ }+ },+ )+ (tmp_path / "doaj_journals.json").write_text(+ json.dumps(+ {+ "journals": {+ "11111111": {+ "name": "Listed Trap",+ "publisher": "Ordinary Press",+ },+ "66666666": {+ "name": "DOAJ Only",+ "publisher": "Open Press",+ },+ "77777777": {"name": " "},+ }+ }+ ),+ encoding="utf-8",+ )+ (tmp_path / "predatory.json").write_text(+ json.dumps(+ {+ "journals": [{"name": "Listed Trap"}, {"name": ""}],+ "publishers": [+ {"name": "Dubious Publishing Group"},+ {"name": "Tiny"},+ ],+ "hijacked": [{"hijacked_name": "Hijacked Clone"}],+ }+ ),+ encoding="utf-8",+ )+ _write_gzip_json(+ tmp_path / "openalex_institutions.json.gz",+ {+ "i": {+ "I1": {+ "n": "Example University",+ "r": "01example",+ "c": "US",+ "t": "education",+ "h": 500,+ "w": 1000,+ "cb": 2000,+ },+ "I2": {"n": " "},+ }+ },+ )+ _write_gzip_json(+ tmp_path / "jabref_abbreviations.json.gz",+ {+ "abbrev_to_full": {+ "J. Test.": "Journal of Testing",+ "J. Test.": "Duplicate Normalized Abbreviation",+ "": "Ignored",+ }+ },+ )+ return tmp_path+++def test_build_compiles_compact_snapshots_and_replaces_existing_file(+ compact_snapshots: Path,+) -> None:+ output = compact_snapshots / "compiled.db"+ output.write_text("old database", encoding="utf-8")++ build_db(data_dir=compact_snapshots, output_path=output)++ assert stat.S_IMODE(output.stat().st_mode) == 0o444+ with closing(+ sqlite3.connect(f"file:{output}?mode=ro", uri=True)+ ) as connection:+ assert connection.execute("PRAGMA user_version").fetchone()[0] == (+ JOURNAL_QUALITY_SCHEMA_VERSION+ )+ rows = connection.execute(+ "SELECT name, h_index, is_in_doaj, is_predatory, predatory_source "+ "FROM sources ORDER BY name"+ ).fetchall()+ assert rows == [+ ("DOAJ Only", None, 1, 0, None),+ ("Duplicate Venue", 20, 0, 0, None),+ ("Hijacked Clone", 9, 0, 1, "stop-predatory-hijacked"),+ ("Listed Trap", 5, 1, 0, None),+ (+ "Publisher Victim",+ 7,+ 0,+ 1,+ "stop-predatory-publishers",+ ),+ ]+ assert (+ connection.execute("SELECT COUNT(*) FROM institutions").fetchone()[+ 0+ ]+ == 1+ )+ assert (+ connection.execute("SELECT COUNT(*) FROM abbreviations").fetchone()[+ 0+ ]+ == 1+ )+++def test_built_database_opens_through_read_only_accessor(+ compact_snapshots: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ output = compact_snapshots / "compiled.db"+ build_db(data_dir=compact_snapshots, output_path=output)+ database = JournalQualityDB()+ monkeypatch.setattr(database, "_resolve_db_path", lambda: output)++ result = database.lookup_openalex(source_id="https://openalex.org/S5")++ assert result is not None+ assert result["name"] == "Duplicate Venue"++ # The module's headline invariant (db.py:3-6: "this class never+ # writes") — this is the one fail-on-revert coverage of it that+ # runs in clean CI (the sibling in test_db.py uses `ref_db`, which+ # skips there). Drive a real write through the same `mode=ro&+ # immutable=1` connection `_make_ro_conn` builds, against a real+ # table in the just-built file, and confirm SQLite itself refuses.+ with pytest.raises(OperationalError, match="readonly"):+ with database.session() as session:+ session.execute(+ text(+ "INSERT INTO sources (name, name_lower, score_source) "+ "VALUES ('x', 'x', 'openalex')"+ )+ )+ session.commit()++ database.reset()+++def test_optional_loaders_return_empty_when_snapshots_are_absent(+ tmp_path: Path,+) -> None:+ with pytest.raises(+ FileNotFoundError, match="OpenAlex source file not found"+ ):+ _load_openalex(tmp_path)++ assert _load_doaj(tmp_path) == {}+ assert _load_predatory(tmp_path) == {+ "journals": set(),+ "publishers": set(),+ "hijacked": set(),+ "long_pubs": [],+ }+ assert _load_institutions(tmp_path) == {}+ assert _load_abbreviations(tmp_path) == {}+++def loguru_sink() -> Iterator[list]:+ """Capture loguru log records — caplog doesn't see loguru output.++ Same technique as the sibling fixture in+ ``TestStaleDataVersionWarning`` (test_db.py:754), duplicated here+ since pytest fixtures defined on a class in another module aren't+ importable across files.+ """+ from loguru import logger as loguru_logger++ records: list = []++ def _sink(message):+ records.append(message.record)++ handler_id = loguru_logger.add(_sink, level="WARNING", diagnose=False)+ loguru_logger.enable("local_deep_research")+ yield records+ loguru_logger.disable("local_deep_research")+ loguru_logger.remove(handler_id)+++def test_malformed_version_metadata_is_ignored(+ tmp_path: Path, loguru_sink: list+) -> None:+ (tmp_path / "version.json").write_text("{broken", encoding="utf-8")+ database = JournalQualityDB()++ database._warn_on_stale_data_version(tmp_path)++ assert database._stale_version_warned is False+ # Like its siblings (test_db.py:779-835): a malformed version.json+ # must fail silently, not emit a spurious WARNING.+ assert not loguru_sink+++def test_reset_db_forwards_to_existing_singleton(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ calls: list[bool] = []+ database = JournalQualityDB()+ monkeypatch.setattr(database, "reset", lambda: calls.append(True))+ monkeypatch.setattr(db_module, "_db", database)++ db_module.reset_db()++ assert calls == [True]+++def test_deprecated_build_alias_forwards_paths(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ calls: list[tuple[Path, Path]] = []+ output = tmp_path / "alias.db"+ monkeypatch.setattr(+ db_module,+ "build_db",+ lambda *, data_dir, output_path: calls.append((data_dir, output_path)),+ )++ db_module.build_reference_db(data_dir=tmp_path, output_path=output)++ assert calls == [(tmp_path, output)]+++def test_failed_build_removes_its_unique_temporary_database(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ _write_gzip_json(tmp_path / "openalex_sources.json.gz", {"s": ["invalid"]})+ output = tmp_path / "failed.db"++ # Record (rather than fix) what secrets.token_hex(4) actually+ # returns, so the test still observes real per-build uniqueness+ # instead of asserting against a value it dictated itself. This+ # also stays a passthrough — it does not stub out the module-level+ # `secrets` import build_db relies on for its next real caller.+ real_token_hex = db_module.secrets.token_hex+ generated: list[str] = []++ def recording_token_hex(length: int) -> str:+ value = real_token_hex(length)+ generated.append(value)+ return value++ monkeypatch.setattr(db_module.secrets, "token_hex", recording_token_hex)++ for _ in range(2):+ with pytest.raises(ValueError, match="dictionary update sequence"):+ build_db(data_dir=tmp_path, output_path=output)++ assert not output.exists()+ assert not list(tmp_path.glob("failed.db.tmp-*"))+ # Two independent failed builds must not reuse the same temp name —+ # this is what secrets.token_hex(4) buys over os.getpid() alone+ # (which is constant across both calls in this one test process).+ assert len(generated) == 2+ assert generated[0] != generated[1]