1
0

test_bibtex_entry_from_pmid.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. import unittest.mock
  3. import urllib.error
  4. import pytest
  5. import pubmed_bibtex
  6. # pylint: disable=wrong-import-order; false positive
  7. from conftest import TEST_BIBTEX_ENTRY, TEST_PMID
  8. def test_bibtex_entry_from_pmid() -> None:
  9. assert pubmed_bibtex.bibtex_entry_from_pmid(pmid=TEST_PMID) == TEST_BIBTEX_ENTRY
  10. @unittest.mock.patch.object(
  11. pubmed_bibtex,
  12. "_TEXMED_URL_PATTERN",
  13. "https://www.bioinformatics.org/texmed/cgi-bin/42.cgi",
  14. )
  15. def test_bibtex_entry_from_pmid_not_found() -> None:
  16. with pytest.raises(urllib.error.HTTPError, match=r"^HTTP Error 404: Not Found$"):
  17. pubmed_bibtex.bibtex_entry_from_pmid(pmid=TEST_PMID)
  18. def test_bibtex_entry_from_pmid_retry(caplog) -> None:
  19. with unittest.mock.patch(
  20. "pubmed_bibtex._TeXMedHtmlParser.feed"
  21. ) as feed_mock, caplog.at_level(logging.WARNING):
  22. assert pubmed_bibtex.bibtex_entry_from_pmid(pmid=TEST_PMID, retries=2) is None
  23. assert feed_mock.call_count == 3
  24. assert caplog.record_tuples == [
  25. ("pubmed_bibtex", logging.WARNING, "attempt #1/3 to fetch bibtex entry failed"),
  26. ("pubmed_bibtex", logging.WARNING, "attempt #2/3 to fetch bibtex entry failed"),
  27. ("pubmed_bibtex", logging.ERROR, "attempt #3/3 to fetch bibtex entry failed"),
  28. ]