Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/test_pubmed_structured_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Regression tests for PubMed structured abstracts."""

from __future__ import annotations

from wenxian.feeder.pubmed import Pubmed


def _xml(abstract: str) -> bytes:
return f"""
<PubmedArticleSet>
<PubmedArticle>
<MedlineCitation>
<Article>
<ArticleTitle>Example.</ArticleTitle>
{abstract}
<Journal>
<Title>Example Journal</Title>
<JournalIssue><PubDate><Year>2024</Year></PubDate></JournalIssue>
</Journal>
</Article>
</MedlineCitation>
<PubmedData />
</PubmedArticle>
</PubmedArticleSet>
""".encode()


def test_structured_abstract_keeps_all_sections():
"""Test all sibling AbstractText sections are preserved in document order."""
reference = Pubmed()._from_content(
_xml(
"""
<Abstract>
<AbstractText Label="BACKGROUND">Background text.</AbstractText>
<AbstractText Label="METHODS">Methods <i>nested</i> text.</AbstractText>
<AbstractText Label="RESULTS">Results text.</AbstractText>
</Abstract>
"""
)
)
assert reference is not None
assert reference.annote == "Background text. Methods nested text. Results text."


def test_single_abstract_section_is_unchanged():
"""Test a conventional single-section abstract retains its content."""
reference = Pubmed()._from_content(
_xml("<Abstract><AbstractText>Only section.</AbstractText></Abstract>")
)
assert reference is not None
assert reference.annote == "Only section."


def test_missing_abstract_remains_none():
"""Test records without an abstract still produce no abstract value."""
reference = Pubmed()._from_content(_xml(""))
assert reference is not None
assert reference.annote is None
11 changes: 10 additions & 1 deletion wenxian/feeder/pubmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,16 @@ def _from_content(
return None
rets = {}
for key, path in self.PUBMED_PATH.items():
if key != "author":
if key == "abstract":
abstract_sections = [
self._text(node)
for node in tree.findall(self.PUBMED_PATH["abstract"])
]
rets[key] = (
" ".join(section for section in abstract_sections if section)
or None
)
elif key != "author":
rets[key] = self._text(tree.find(path))

if rets["journal"] == "Physical chemistry chemical physics : PCCP":
Expand Down