|
| 1 | +"""HTML article text keeps everything in the scope, not just its ``<p>`` text (issue #121). |
| 2 | +
|
| 3 | +``HTMLExtractor`` used to return only ``<p>`` text whenever a scope held any |
| 4 | +``<p>``, so lists, tables, headings and bare ``<div>`` text were dropped and a |
| 5 | +correct quote of them failed. Nested or unclosed ``<p>`` also repeated and |
| 6 | +welded text. Every HTML full-text path (PMC, OpenAlex, downloaded HTML) goes |
| 7 | +through this extractor, so these tests pin the shared behaviour. |
| 8 | +""" |
| 9 | + |
| 10 | +from linkml_reference_validator.etl.extract.html import HTMLExtractor |
| 11 | +from linkml_reference_validator.etl.reference_fetcher import ( |
| 12 | + EXTRACTOR_CACHE_VERSION, |
| 13 | + HTML_FULL_TEXT_CACHE_VERSION, |
| 14 | + ReferenceFetcher, |
| 15 | +) |
| 16 | +from linkml_reference_validator.models import ReferenceContent, ReferenceValidationConfig |
| 17 | +from linkml_reference_validator.validation.supporting_text_validator import SupportingTextValidator |
| 18 | + |
| 19 | +MIXED = ( |
| 20 | + b"<article><h2>Results</h2><p>We found three things.</p>" |
| 21 | + b"<ul><li>POLG variants cause ataxia</li></ul>" |
| 22 | + b"<table><tr><td>GENE1</td><td>0.05</td></tr></table>" |
| 23 | + b"<div>Unwrapped finding text.</div></article>" |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def test_text_outside_paragraphs_is_kept(): |
| 28 | + """Headings, list items, table cells and bare div text all reach the output.""" |
| 29 | + text = HTMLExtractor().extract(MIXED) or "" |
| 30 | + for expected in ( |
| 31 | + "Results", |
| 32 | + "We found three things.", |
| 33 | + "POLG variants cause ataxia", |
| 34 | + "GENE1", |
| 35 | + "0.05", |
| 36 | + "Unwrapped finding text.", |
| 37 | + ): |
| 38 | + assert expected in text |
| 39 | + |
| 40 | + |
| 41 | +def test_blocks_stay_on_separate_lines(): |
| 42 | + """A block never welds onto its neighbour.""" |
| 43 | + text = HTMLExtractor().extract(MIXED) or "" |
| 44 | + assert "things.POLG" not in text |
| 45 | + assert "ataxiaGENE1" not in text |
| 46 | + |
| 47 | + |
| 48 | +def test_unclosed_paragraphs_are_neither_repeated_nor_welded(): |
| 49 | + """html.parser nests unclosed ``<p>``; each paragraph still appears once, on its own.""" |
| 50 | + text = HTMLExtractor().extract(b"<article><p>Para one<p>Para two</article>") or "" |
| 51 | + assert text.count("Para two") == 1 |
| 52 | + assert "Para onePara" not in text |
| 53 | + |
| 54 | + |
| 55 | +def test_blocks_inside_a_paragraph_are_separated(): |
| 56 | + """A ``<div>`` inside a ``<p>`` starts a new line instead of welding.""" |
| 57 | + text = HTMLExtractor().extract( |
| 58 | + b"<article><p>Intro<div>block A</div><div>block B</div></p></article>" |
| 59 | + ) or "" |
| 60 | + assert "Introblock" not in text |
| 61 | + assert "block Ablock" not in text |
| 62 | + assert all(part in text for part in ("Intro", "block A", "block B")) |
| 63 | + |
| 64 | + |
| 65 | +def test_inline_markup_keeps_its_spacing(): |
| 66 | + """The fix that motivated the old shortcut still holds: italics do not weld.""" |
| 67 | + text = HTMLExtractor().extract( |
| 68 | + b"<article><p>near (<i>GUSB</i>, <i>GRN</i>, and <i>NEU1</i>) here</p></article>" |
| 69 | + ) |
| 70 | + assert text == "near (GUSB, GRN, and NEU1) here" |
| 71 | + |
| 72 | + |
| 73 | +def test_a_list_item_quote_validates(): |
| 74 | + """End to end: a correct quote of a list item is found in the extracted text.""" |
| 75 | + text = HTMLExtractor().extract(MIXED) or "" |
| 76 | + reference = ReferenceContent(reference_id="PMID:1", content=text) |
| 77 | + match = SupportingTextValidator(ReferenceValidationConfig()).find_text_in_reference( |
| 78 | + "POLG variants cause ataxia", reference |
| 79 | + ) |
| 80 | + assert match.found |
| 81 | + |
| 82 | + |
| 83 | +def test_html_entries_from_the_paragraph_only_extractor_are_stale(): |
| 84 | + """Cached HTML full text from before this fix is re-fetched, not served.""" |
| 85 | + entry = ( |
| 86 | + "---\n" |
| 87 | + "reference_id: PMID:1\n" |
| 88 | + "content_type: full_text_html\n" |
| 89 | + f"extractor_version: {EXTRACTOR_CACHE_VERSION}\n" |
| 90 | + f"html_full_text_version: {HTML_FULL_TEXT_CACHE_VERSION - 1}\n" |
| 91 | + "---\nBody." |
| 92 | + ) |
| 93 | + assert HTML_FULL_TEXT_CACHE_VERSION >= 2 |
| 94 | + assert ReferenceFetcher._is_stale_cache_entry(entry) |
| 95 | + |
| 96 | + |
| 97 | +def _fetch_with_source_down(tmp_path, html_stamp: str): |
| 98 | + """Fetch a cached HTML entry while the source yields nothing (the stale fallback).""" |
| 99 | + from unittest.mock import patch |
| 100 | + |
| 101 | + (tmp_path / "PMID_30929739.md").write_text( |
| 102 | + "---\nreference_id: PMID:30929739\ntitle: A paper\n" |
| 103 | + f"extractor_version: {EXTRACTOR_CACHE_VERSION}\n{html_stamp}" |
| 104 | + "content_type: full_text_html\n---\n\n## Content\n\nA body sentence.\n", |
| 105 | + encoding="utf-8", |
| 106 | + ) |
| 107 | + fetcher = ReferenceFetcher( |
| 108 | + ReferenceValidationConfig( |
| 109 | + cache_dir=tmp_path, email="me@example.org", fetch_full_text=False |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + class _Source: |
| 114 | + def fetch(self, identifier, config): |
| 115 | + return None |
| 116 | + |
| 117 | + with patch( |
| 118 | + "linkml_reference_validator.etl.reference_fetcher" |
| 119 | + ".ReferenceSourceRegistry.get_source", |
| 120 | + return_value=_Source, |
| 121 | + ): |
| 122 | + return fetcher.fetch("PMID:30929739") |
| 123 | + |
| 124 | + |
| 125 | +def test_verified_html_from_an_older_extractor_is_served_when_refresh_fails(tmp_path): |
| 126 | + """A version-1 entry is a checked article body, only incomplete: serve it stale. |
| 127 | +
|
| 128 | + Withholding it would turn this extractor fix into lost evidence for every |
| 129 | + HTML entry that cannot be re-fetched, such as a PMC-only article (#95). |
| 130 | + """ |
| 131 | + reference = _fetch_with_source_down(tmp_path, "html_full_text_version: 1\n") |
| 132 | + assert reference is not None |
| 133 | + assert "A body sentence." in (reference.content or "") |
| 134 | + |
| 135 | + |
| 136 | +def test_unverified_html_is_still_withheld_when_refresh_fails(tmp_path): |
| 137 | + """An unstamped entry may be a landing page, so it is still not served.""" |
| 138 | + assert _fetch_with_source_down(tmp_path, "") is None |
0 commit comments