164 lines
5.2 KiB
Python
164 lines
5.2 KiB
Python
"""Parsers for Europe PMC, OpenAlex, Crossref, Semantic Scholar, and arXiv."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from prisma.search.arxiv import parse_arxiv
|
|
from prisma.search.base import bare_doi
|
|
from prisma.search.crossref import parse_crossref
|
|
from prisma.search.europepmc import parse_europepmc
|
|
from prisma.search.openalex import parse_openalex
|
|
from prisma.search.semanticscholar import parse_semanticscholar
|
|
|
|
_EUROPEPMC = """\
|
|
<response><resultList><result>
|
|
<title>Report title</title>
|
|
<doi>10.1/e</doi>
|
|
<pubYear>2020</pubYear>
|
|
<authorList><author><lastName>Kim</lastName></author></authorList>
|
|
</result></resultList></response>
|
|
"""
|
|
|
|
_OPENALEX = """\
|
|
{"results":[{"title":"Report title","publication_year":2020,
|
|
"doi":"https://doi.org/10.1/o",
|
|
"authorships":[{"author":{"display_name":"Ada Kim"}}]}]}
|
|
"""
|
|
|
|
_CROSSREF = """\
|
|
{"message":{"items":[{"title":["Report title"],"DOI":"10.1/c",
|
|
"issued":{"date-parts":[[2020,1,2]]},
|
|
"author":[{"family":"Kim","given":"Ada"}]}]}}
|
|
"""
|
|
|
|
_S2 = """\
|
|
{"data":[{"title":"Report title","year":2020,
|
|
"externalIds":{"DOI":"10.1/s"},
|
|
"authors":[{"name":"Ada Kim"}]}]}
|
|
"""
|
|
|
|
_ARXIV = """\
|
|
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:arxiv="http://arxiv.org/schemas/atom">
|
|
<entry>
|
|
<title>Report title</title>
|
|
<published>2020-01-02T00:00:00Z</published>
|
|
<author><name>Ada Kim</name></author>
|
|
<arxiv:doi>10.1/a</arxiv:doi>
|
|
</entry>
|
|
</feed>
|
|
"""
|
|
|
|
_CASES = [
|
|
(parse_europepmc, _EUROPEPMC, "europepmc", "10.1/e", 2020, "Kim"),
|
|
(parse_openalex, _OPENALEX, "openalex", "10.1/o", 2020, "Kim"),
|
|
(parse_crossref, _CROSSREF, "crossref", "10.1/c", 2020, "Kim"),
|
|
(parse_semanticscholar, _S2, "semanticscholar", "10.1/s", 2020, "Kim"),
|
|
(parse_arxiv, _ARXIV, "arxiv", "10.1/a", 2020, "Kim"),
|
|
]
|
|
|
|
_SPARSE = [
|
|
(
|
|
parse_europepmc,
|
|
"<response><resultList><result></result></resultList></response>",
|
|
),
|
|
(parse_openalex, '{"results":[{}]}'),
|
|
(parse_crossref, '{"message":{"items":[{}]}}'),
|
|
(parse_semanticscholar, '{"data":[{}]}'),
|
|
(parse_arxiv, '<feed xmlns="http://www.w3.org/2005/Atom"><entry></entry></feed>'),
|
|
]
|
|
|
|
|
|
def test_bare_doi_strips_a_dx_doi_org_prefix_and_keeps_a_trailing_period():
|
|
assert bare_doi("https://dx.doi.org/10.1/ab") == "10.1/ab"
|
|
assert bare_doi("https://dx.doi.org/10.1/ab.") == "10.1/ab."
|
|
|
|
|
|
def test_openalex_reads_a_pubmed_url_pmid():
|
|
payload = (
|
|
'{"results":[{"title":"Report title","publication_year":2020,'
|
|
'"doi":"https://doi.org/10.1/o","ids":{"pmid":'
|
|
'"https://pubmed.ncbi.nlm.nih.gov/42"},'
|
|
'"authorships":[{"author":{"display_name":"Ada Kim"}}]}]}'
|
|
)
|
|
hit = parse_openalex(payload, query="frailty", searched_on="2026-10-31").hits[0]
|
|
assert hit.pmid == "42"
|
|
|
|
|
|
def test_semanticscholar_reads_external_pubmed_id():
|
|
payload = (
|
|
'{"data":[{"title":"Report title","year":2020,'
|
|
'"externalIds":{"DOI":"10.1/s","PubMed":"999"},'
|
|
'"authors":[{"name":"Ada Kim"}]}]}'
|
|
)
|
|
hit = parse_semanticscholar(
|
|
payload, query="frailty", searched_on="2026-10-31"
|
|
).hits[0]
|
|
assert hit.pmid == "999"
|
|
|
|
|
|
def test_crossref_reads_a_pmid_field():
|
|
payload = (
|
|
'{"message":{"items":[{"title":["Report title"],"DOI":"10.1/c",'
|
|
'"pmid":"12345","issued":{"date-parts":[[2020,1,2]]},'
|
|
'"author":[{"family":"Kim","given":"Ada"}]}]}}'
|
|
)
|
|
hit = parse_crossref(payload, query="frailty", searched_on="2026-10-31").hits[0]
|
|
assert hit.pmid == "12345"
|
|
|
|
|
|
def test_arxiv_reads_a_pmid_element():
|
|
payload = """\
|
|
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:arxiv="http://arxiv.org/schemas/atom">
|
|
<entry>
|
|
<title>Report title</title>
|
|
<published>2020-01-02T00:00:00Z</published>
|
|
<author><name>Ada Kim</name></author>
|
|
<arxiv:doi>10.1/a</arxiv:doi>
|
|
<arxiv:pmid>77</arxiv:pmid>
|
|
</entry>
|
|
</feed>
|
|
"""
|
|
hit = parse_arxiv(payload, query="frailty", searched_on="2026-10-31").hits[0]
|
|
assert hit.pmid == "77"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("parser", "payload", "database", "doi", "year", "author"), _CASES
|
|
)
|
|
def test_one_record(parser, payload, database, doi, year, author):
|
|
result = parser(payload, query="frailty", searched_on="2026-10-31")
|
|
assert result.log.hit_count == 1
|
|
assert result.log.database == database
|
|
assert len(result.hits) == 1
|
|
hit = result.hits[0]
|
|
assert hit.title == "Report title"
|
|
assert hit.database == database
|
|
assert hit.doi == doi
|
|
assert hit.year == year
|
|
assert hit.first_author == author
|
|
assert hit.pmid == ""
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("parser", "_payload", "database", "_doi", "_year", "_author"), _CASES
|
|
)
|
|
def test_empty_payload_returns_no_hits(
|
|
parser, _payload, database, _doi, _year, _author
|
|
):
|
|
result = parser("", query="frailty", searched_on="2026-10-31")
|
|
assert result.hits == ()
|
|
assert result.log.hit_count == 0
|
|
assert result.log.database == database
|
|
|
|
|
|
@pytest.mark.parametrize(("parser", "payload"), _SPARSE)
|
|
def test_missing_fields_are_empty(parser, payload):
|
|
result = parser(payload, query="frailty", searched_on="2026-10-31")
|
|
assert result.log.hit_count == 1
|
|
hit = result.hits[0]
|
|
assert hit.title == ""
|
|
assert hit.doi == ""
|
|
assert hit.first_author == ""
|
|
assert hit.year is None
|