288 lines
8.7 KiB
Python
288 lines
8.7 KiB
Python
"""PubMed search log, parser, and seed recall. No live HTTP."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
|
|
import pytest
|
|
from typer.testing import CliRunner
|
|
|
|
from cli.prisma import app
|
|
from lead.ledger.store import open_ledger
|
|
from prisma.search.base import (
|
|
SearchHit,
|
|
SearchLog,
|
|
SearchResult,
|
|
import_hits,
|
|
record,
|
|
seed_recall,
|
|
)
|
|
from prisma.search.due import due
|
|
from prisma.search.protocols import PROTOCOLS
|
|
from prisma.search.pubmed import parse_pubmed_xml
|
|
from zot.db import TYPE_MAP, Db
|
|
from zot.schema import create_db
|
|
|
|
runner = CliRunner()
|
|
|
|
_XML = """\
|
|
<?xml version="1.0"?>
|
|
<PubmedArticleSet>
|
|
<PubmedArticle>
|
|
<MedlineCitation>
|
|
<PMID>111</PMID>
|
|
<Article>
|
|
<ArticleTitle>Alpha frailty</ArticleTitle>
|
|
<Journal><JournalIssue><PubDate><Year>2020</Year></PubDate></JournalIssue></Journal>
|
|
<AuthorList><Author><LastName>Kim</LastName></Author></AuthorList>
|
|
</Article>
|
|
</MedlineCitation>
|
|
<PubmedData>
|
|
<ArticleIdList>
|
|
<ArticleId IdType="doi">10.1/alpha</ArticleId>
|
|
</ArticleIdList>
|
|
</PubmedData>
|
|
</PubmedArticle>
|
|
<PubmedArticle>
|
|
<MedlineCitation>
|
|
<PMID>222</PMID>
|
|
<Article>
|
|
<ArticleTitle>Beta readmission</ArticleTitle>
|
|
<Journal><JournalIssue><PubDate><Year>2021</Year></PubDate></JournalIssue></Journal>
|
|
<AuthorList><Author><LastName>Lee</LastName></Author></AuthorList>
|
|
</Article>
|
|
</MedlineCitation>
|
|
<PubmedData>
|
|
<ArticleIdList>
|
|
<ArticleId IdType="doi">10.1/beta</ArticleId>
|
|
</ArticleIdList>
|
|
</PubmedData>
|
|
</PubmedArticle>
|
|
</PubmedArticleSet>
|
|
"""
|
|
|
|
|
|
def test_parse_two_articles_and_hash_the_xml():
|
|
result = parse_pubmed_xml(_XML, query="frailty", searched_on="2026-10-31")
|
|
assert result.log.database == "pubmed"
|
|
assert result.log.hit_count == 2
|
|
assert result.log.export_sha256 == hashlib.sha256(_XML.encode()).hexdigest()
|
|
assert {hit.database for hit in result.hits} == {"pubmed"}
|
|
assert {hit.title for hit in result.hits} == {"Alpha frailty", "Beta readmission"}
|
|
assert {hit.year for hit in result.hits} == {2020, 2021}
|
|
assert {hit.doi for hit in result.hits} == {"10.1/alpha", "10.1/beta"}
|
|
assert {hit.pmid for hit in result.hits} == {"111", "222"}
|
|
assert {hit.first_author for hit in result.hits} == {"Kim", "Lee"}
|
|
|
|
|
|
def test_seed_recall():
|
|
assert seed_recall({"a", "b"}, {"a", "c"}) == 0.5
|
|
with pytest.raises(ValueError):
|
|
seed_recall(set(), set())
|
|
|
|
|
|
def test_same_xml_on_two_days_appends_two_search_rows(tmp_path):
|
|
con = open_ledger(str(tmp_path / "ledger.duckdb"))
|
|
try:
|
|
first = parse_pubmed_xml(_XML, query="frailty", searched_on="2026-10-31")
|
|
second = parse_pubmed_xml(_XML, query="frailty", searched_on="2026-11-01")
|
|
assert record(con, first, actor="tester") is True
|
|
assert record(con, second, actor="tester") is True
|
|
count = con.execute("select count(*) from lead_evidence.search").fetchone()[0]
|
|
assert count == 2
|
|
finally:
|
|
con.close()
|
|
|
|
|
|
def test_record_with_project_clears_due_and_keeps_projects_apart(tmp_path):
|
|
con = open_ledger(str(tmp_path / "ledger.duckdb"))
|
|
try:
|
|
result = parse_pubmed_xml(_XML, query="frailty", searched_on="2026-10-31")
|
|
assert record(con, result, actor="tester", project="lead-precursors") is True
|
|
assert record(con, result, actor="tester", project="lead-precursors") is False
|
|
assert due(con, project="lead-precursors", now="2026-10-31") == ()
|
|
assert record(con, result, actor="tester", project="lead-history") is True
|
|
count = con.execute("select count(*) from lead_evidence.search").fetchone()[0]
|
|
assert count == 2
|
|
assert due(con, project="lead-history", now="2026-10-31") == ()
|
|
finally:
|
|
con.close()
|
|
|
|
|
|
def test_blank_pubmed_xml_has_no_hits():
|
|
payload = " \n"
|
|
result = parse_pubmed_xml(payload, query="frailty", searched_on="2026-10-31")
|
|
assert result.hits == ()
|
|
assert result.log.hit_count == 0
|
|
assert result.log.database == "pubmed"
|
|
assert result.log.interface == "eutils"
|
|
assert result.log.export_sha256 == hashlib.sha256(payload.encode()).hexdigest()
|
|
|
|
|
|
def test_import_hits_matches_a_doi_stored_as_a_dx_url(tmp_path):
|
|
path = str(tmp_path / "z.sqlite")
|
|
con = create_db(path)
|
|
con.close()
|
|
hit = SearchHit(
|
|
title="Alpha frailty",
|
|
year=2020,
|
|
doi="10.1/alpha",
|
|
pmid="111",
|
|
first_author="Kim",
|
|
database="openalex",
|
|
)
|
|
result = SearchResult(
|
|
hits=(hit,),
|
|
log=SearchLog(
|
|
database="openalex",
|
|
interface="openalex",
|
|
searched_on="2026-10-31",
|
|
query="frailty",
|
|
filters="",
|
|
hit_count=1,
|
|
export_sha256="abc",
|
|
),
|
|
)
|
|
with Db(path) as db:
|
|
item_id = db.create_item(TYPE_MAP["journalArticle"])
|
|
db.set_fields(
|
|
item_id,
|
|
{"title": "Alpha frailty", "DOI": "https://dx.doi.org/10.1/alpha"},
|
|
)
|
|
assert import_hits(db, "lead-limits", result) == 0
|
|
articles = db.con.execute(
|
|
"select count(*) from items where itemTypeID = ?",
|
|
(TYPE_MAP["journalArticle"],),
|
|
).fetchone()[0]
|
|
assert articles == 1
|
|
|
|
|
|
def test_import_hits_is_idempotent_on_doi(tmp_path):
|
|
path = str(tmp_path / "z.sqlite")
|
|
con = create_db(path)
|
|
con.close()
|
|
hit = SearchHit(
|
|
title="Alpha frailty",
|
|
year=2020,
|
|
doi="10.1/alpha",
|
|
pmid="111",
|
|
first_author="Kim",
|
|
database="pubmed",
|
|
)
|
|
result = SearchResult(
|
|
hits=(hit,),
|
|
log=SearchLog(
|
|
database="pubmed",
|
|
interface="eutils",
|
|
searched_on="2026-10-31",
|
|
query="frailty",
|
|
filters="",
|
|
hit_count=1,
|
|
export_sha256="abc",
|
|
),
|
|
)
|
|
with Db(path) as db:
|
|
assert import_hits(db, "lead-precursors", result) == 1
|
|
names = {
|
|
row[0]
|
|
for row in db.con.execute(
|
|
"""
|
|
SELECT t.name FROM tags t
|
|
JOIN itemTags it ON it.tagID = t.tagID
|
|
"""
|
|
)
|
|
}
|
|
assert "found-in:pubmed" in names
|
|
assert "project:lead-precursors" in names
|
|
assert import_hits(db, "lead-precursors", result) == 0
|
|
articles = db.con.execute(
|
|
"select count(*) from items where itemTypeID = ?",
|
|
(TYPE_MAP["journalArticle"],),
|
|
).fetchone()[0]
|
|
assert articles == 1
|
|
|
|
|
|
def test_import_hits_tags_an_existing_doi_for_a_later_strand(tmp_path):
|
|
path = str(tmp_path / "z.sqlite")
|
|
con = create_db(path)
|
|
con.close()
|
|
hit = SearchHit(
|
|
title="Alpha frailty",
|
|
year=2020,
|
|
doi="10.1/alpha",
|
|
pmid="111",
|
|
first_author="Kim",
|
|
database="pubmed",
|
|
)
|
|
log = SearchLog(
|
|
database="pubmed",
|
|
interface="eutils",
|
|
searched_on="2026-10-31",
|
|
query="frailty",
|
|
filters="",
|
|
hit_count=1,
|
|
export_sha256="abc",
|
|
)
|
|
first = SearchResult(hits=(hit,), log=log)
|
|
second = SearchResult(
|
|
hits=(
|
|
SearchHit(
|
|
title="Alpha frailty",
|
|
year=2020,
|
|
doi="10.1/alpha",
|
|
pmid="111",
|
|
first_author="Kim",
|
|
database="openalex",
|
|
),
|
|
),
|
|
log=SearchLog(
|
|
database="openalex",
|
|
interface="openalex",
|
|
searched_on="2026-10-31",
|
|
query="frailty",
|
|
filters="",
|
|
hit_count=1,
|
|
export_sha256="def",
|
|
),
|
|
)
|
|
with Db(path) as db:
|
|
assert import_hits(db, "lead-precursors", first) == 1
|
|
assert import_hits(db, "lead-limits", second) == 0
|
|
articles = db.con.execute(
|
|
"select count(*) from items where itemTypeID = ?",
|
|
(TYPE_MAP["journalArticle"],),
|
|
).fetchone()[0]
|
|
assert articles == 1
|
|
names = {
|
|
row[0]
|
|
for row in db.con.execute(
|
|
"""
|
|
SELECT t.name FROM tags t
|
|
JOIN itemTags it ON it.tagID = t.tagID
|
|
"""
|
|
)
|
|
}
|
|
assert names >= {
|
|
"project:lead-precursors",
|
|
"project:lead-limits",
|
|
"found-in:pubmed",
|
|
"found-in:openalex",
|
|
}
|
|
|
|
|
|
def test_protocols_cover_the_five_projects():
|
|
assert set(PROTOCOLS) == {
|
|
"lead-history",
|
|
"lead-limits",
|
|
"lead-scan",
|
|
"lead-precursors",
|
|
"lead-external",
|
|
}
|
|
assert PROTOCOLS["lead-precursors"]
|
|
assert all(isinstance(q, str) and q for q in PROTOCOLS["lead-precursors"])
|
|
|
|
|
|
def test_search_command_is_registered():
|
|
result = runner.invoke(app, ["search", "--help"])
|
|
assert result.exit_code == 0
|