|
| 1 | +"""Tests for isolating expected metadata-source failures.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import wenxian.from_identifier as identifier_module |
| 10 | +from wenxian.reference import Reference |
| 11 | + |
| 12 | + |
| 13 | +def test_sync_doi_keeps_good_result_when_one_source_has_bad_payload(monkeypatch): |
| 14 | + """Test one malformed source does not discard usable DOI metadata.""" |
| 15 | + monkeypatch.setattr( |
| 16 | + identifier_module.Pubmed, |
| 17 | + "from_doi", |
| 18 | + lambda self, doi: Reference(title="Usable", journal="Journal"), |
| 19 | + ) |
| 20 | + monkeypatch.setattr( |
| 21 | + identifier_module.Crossref, |
| 22 | + "from_doi", |
| 23 | + lambda self, doi: (_ for _ in ()).throw(KeyError("message")), |
| 24 | + ) |
| 25 | + monkeypatch.setattr(identifier_module.Arxiv, "from_doi", lambda self, doi: None) |
| 26 | + monkeypatch.setattr(identifier_module.Chemrxiv, "from_doi", lambda self, doi: None) |
| 27 | + monkeypatch.setattr( |
| 28 | + identifier_module.Semanticscholar, "from_doi", lambda self, doi: None |
| 29 | + ) |
| 30 | + |
| 31 | + assert identifier_module.from_doi("10.1234/example") == Reference( |
| 32 | + title="Usable", journal="Journal" |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def test_async_doi_keeps_good_result_when_one_source_has_bad_payload(monkeypatch): |
| 37 | + """Test async aggregation also isolates malformed source payloads.""" |
| 38 | + |
| 39 | + async def good(self, doi): |
| 40 | + return Reference(title="Usable", journal="Journal") |
| 41 | + |
| 42 | + async def bad(self, doi): |
| 43 | + raise TypeError("malformed payload") |
| 44 | + |
| 45 | + async def missing(self, doi): |
| 46 | + return None |
| 47 | + |
| 48 | + monkeypatch.setattr(identifier_module.Pubmed, "async_from_doi", good) |
| 49 | + monkeypatch.setattr(identifier_module.Crossref, "async_from_doi", bad) |
| 50 | + monkeypatch.setattr(identifier_module.Arxiv, "async_from_doi", missing) |
| 51 | + monkeypatch.setattr(identifier_module.Chemrxiv, "async_from_doi", missing) |
| 52 | + monkeypatch.setattr(identifier_module.Semanticscholar, "async_from_doi", missing) |
| 53 | + |
| 54 | + assert asyncio.run( |
| 55 | + identifier_module.async_from_doi("10.1234/example") |
| 56 | + ) == Reference(title="Usable", journal="Journal") |
| 57 | + |
| 58 | + |
| 59 | +def test_native_programming_error_still_escapes(monkeypatch): |
| 60 | + """Test unexpected programming errors are not swallowed on native Python.""" |
| 61 | + monkeypatch.setattr(identifier_module.sys, "platform", "linux") |
| 62 | + |
| 63 | + def broken(identifier): |
| 64 | + raise RuntimeError("bug") |
| 65 | + |
| 66 | + with pytest.raises(RuntimeError, match="bug"): |
| 67 | + identifier_module._fetch_safely("test", broken, "id") |
0 commit comments