|
| 1 | +"""Regression tests for PubMed structured abstracts.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from wenxian.feeder.pubmed import Pubmed |
| 6 | + |
| 7 | + |
| 8 | +def _xml(abstract: str) -> bytes: |
| 9 | + return f""" |
| 10 | +<PubmedArticleSet> |
| 11 | + <PubmedArticle> |
| 12 | + <MedlineCitation> |
| 13 | + <Article> |
| 14 | + <ArticleTitle>Example.</ArticleTitle> |
| 15 | + {abstract} |
| 16 | + <Journal> |
| 17 | + <Title>Example Journal</Title> |
| 18 | + <JournalIssue><PubDate><Year>2024</Year></PubDate></JournalIssue> |
| 19 | + </Journal> |
| 20 | + </Article> |
| 21 | + </MedlineCitation> |
| 22 | + <PubmedData /> |
| 23 | + </PubmedArticle> |
| 24 | +</PubmedArticleSet> |
| 25 | +""".encode() |
| 26 | + |
| 27 | + |
| 28 | +def test_structured_abstract_keeps_all_sections(): |
| 29 | + """Test all sibling AbstractText sections are preserved in document order.""" |
| 30 | + reference = Pubmed()._from_content( |
| 31 | + _xml( |
| 32 | + """ |
| 33 | + <Abstract> |
| 34 | + <AbstractText Label="BACKGROUND">Background text.</AbstractText> |
| 35 | + <AbstractText Label="METHODS">Methods <i>nested</i> text.</AbstractText> |
| 36 | + <AbstractText Label="RESULTS">Results text.</AbstractText> |
| 37 | + </Abstract> |
| 38 | + """ |
| 39 | + ) |
| 40 | + ) |
| 41 | + assert reference is not None |
| 42 | + assert reference.annote == "Background text. Methods nested text. Results text." |
| 43 | + |
| 44 | + |
| 45 | +def test_single_abstract_section_is_unchanged(): |
| 46 | + """Test a conventional single-section abstract retains its content.""" |
| 47 | + reference = Pubmed()._from_content( |
| 48 | + _xml("<Abstract><AbstractText>Only section.</AbstractText></Abstract>") |
| 49 | + ) |
| 50 | + assert reference is not None |
| 51 | + assert reference.annote == "Only section." |
| 52 | + |
| 53 | + |
| 54 | +def test_missing_abstract_remains_none(): |
| 55 | + """Test records without an abstract still produce no abstract value.""" |
| 56 | + reference = Pubmed()._from_content(_xml("")) |
| 57 | + assert reference is not None |
| 58 | + assert reference.annote is None |
0 commit comments