| 34 | |
| 35 | |
| 36 | class ResolveDoiTests(unittest.IsolatedAsyncioTestCase): |
| 37 | async def test_full_metadata_mapped(self): |
| 38 | doi = "10.1234/example.2024.001" |
| 39 | with respx.mock(assert_all_called=True) as router: |
| 40 | router.get(f"{CROSSREF_BASE_URL}/{doi}").mock( |
| 41 | return_value=httpx.Response(200, json=_crossref_payload()) |
| 42 | ) |
| 43 | result = await resolve_doi(doi) |
| 44 | |
| 45 | self.assertIsInstance(result, CrossrefMetadata) |
| 46 | self.assertEqual(result.doi, doi) |
| 47 | self.assertEqual(result.title, "A Worked Example of DOI Resolution") |
| 48 | self.assertEqual(result.authors, ("Alice Smith", "Bob Jones")) |
| 49 | self.assertEqual(result.journal, "Journal of Synthetic Tests") |
| 50 | self.assertEqual(result.publisher, "Example Press") |
| 51 | self.assertEqual(result.primary_url, "https://example.com/paper") |
| 52 | self.assertIsNotNone(result.published_at) |
| 53 | assert result.published_at is not None |
| 54 | self.assertEqual(result.published_at.year, 2024) |
| 55 | self.assertEqual(result.published_at.month, 4) |
| 56 | self.assertEqual(result.published_at.tzinfo, timezone.utc) |
| 57 | |
| 58 | async def test_strips_https_prefix(self): |
| 59 | doi = "10.1234/example.2024.001" |
| 60 | with respx.mock(assert_all_called=True) as router: |
| 61 | router.get(f"{CROSSREF_BASE_URL}/{doi}").mock( |
| 62 | return_value=httpx.Response(200, json=_crossref_payload()) |
| 63 | ) |
| 64 | result = await resolve_doi(f"https://doi.org/{doi}") |
| 65 | self.assertEqual(result.doi, doi) |
| 66 | |
| 67 | async def test_strips_doi_prefix(self): |
| 68 | doi = "10.1234/example.2024.001" |
| 69 | with respx.mock(assert_all_called=True) as router: |
| 70 | router.get(f"{CROSSREF_BASE_URL}/{doi}").mock( |
| 71 | return_value=httpx.Response(200, json=_crossref_payload()) |
| 72 | ) |
| 73 | result = await resolve_doi(f"doi:{doi}") |
| 74 | self.assertEqual(result.doi, doi) |
| 75 | |
| 76 | async def test_year_only_date(self): |
| 77 | doi = "10.1234/example.2024.001" |
| 78 | with respx.mock(assert_all_called=True) as router: |
| 79 | router.get(f"{CROSSREF_BASE_URL}/{doi}").mock( |
| 80 | return_value=httpx.Response( |
| 81 | 200, |
| 82 | json=_crossref_payload(issued={"date-parts": [[2024]]}), |
| 83 | ) |
| 84 | ) |
| 85 | result = await resolve_doi(doi) |
| 86 | assert result.published_at is not None |
| 87 | self.assertEqual(result.published_at.month, 1) |
| 88 | self.assertEqual(result.published_at.day, 1) |
| 89 | |
| 90 | async def test_missing_optional_fields(self): |
| 91 | doi = "10.1234/example.2024.001" |
| 92 | sparse = { |
| 93 | "status": "ok", |
nothing calls this directly
no outgoing calls
no test coverage detected