"""Юнит-тесты парсера PubMed Central (PMC) — чистая логика (без сети). _parse_article работает с реальными xml.etree.ElementTree узлами (JATS XML из efetch), поэтому тесты строят минимальные JATS-фрагменты, а не мокают HTTP. """ import xml.etree.ElementTree as ET from pmc import PMCParser ARTICLE_XML = """
Journal of Testing 1234567 10.1000/test.123 A Study of Testing IvanovIvan NotAnAuthorX 2024

This is the abstract text.

First paragraph of the body.

Second paragraph with a ref inline.

""" def _article() -> ET.Element: return ET.fromstring(ARTICLE_XML) def test_parse_article_extracts_all_fields(): raw = PMCParser()._parse_article(_article()) assert raw["id"] == "1234567" assert raw["doi"] == "10.1000/test.123" assert raw["title"] == "A Study of Testing" # вложенный склеен assert raw["journal"] == "Journal of Testing" assert raw["year"] == 2024 assert raw["abstract"] == "This is the abstract text." assert raw["full_text"] == "First paragraph of the body. Second paragraph with a ref inline." def test_parse_article_only_includes_authors_not_editors(): raw = PMCParser()._parse_article(_article()) assert raw["authors"] == [{"last_name": "Ivanov", "first_name": "Ivan"}] def test_parse_article_without_article_meta_is_empty(): assert PMCParser()._parse_article(ET.fromstring("
")) == {} def test_transform_maps_to_unified_schema(): raw = PMCParser()._parse_article(_article()) t = PMCParser().transform(raw) assert t["source"] == "pmc" assert t["ext_id"] == "pmc:1234567" assert t["lang"] == "en" assert t["url"] == "https://www.ncbi.nlm.nih.gov/pmc/articles/PMC1234567/" assert t["authors"][0]["last_name"] == "Ivanov" assert t["full_text"].startswith("First paragraph") def test_transform_empty_without_id(): assert PMCParser().transform({"title": "x"}) == {} def test_parse_articles_batch(): xml = f"{ARTICLE_XML}{ARTICLE_XML}" parsed = PMCParser()._parse_articles(xml) assert len(parsed) == 2 assert all(p["id"] == "1234567" for p in parsed) def test_parse_articles_malformed_xml_returns_empty(): assert PMCParser()._parse_articles("