|
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 | # SPDX-FileContributor: David Pape |
4 | 4 |
|
5 | | -from hermes.commands.marketplace import schema_org_hermes, SchemaOrgModel |
| 5 | +import requests_mock |
| 6 | + |
| 7 | +from hermes.commands.marketplace import ( |
| 8 | + schema_org_hermes, |
| 9 | + SchemaOrgModel, |
| 10 | + _is_hermes_reference, |
| 11 | +) |
6 | 12 |
|
7 | 13 |
|
8 | 14 | def test_schema_org_hermes_doi_is_absolute(): |
9 | 15 | """The HERMES DOI rendered to the plugins list must always be a full URL.""" |
10 | 16 | assert isinstance(schema_org_hermes, SchemaOrgModel) |
11 | 17 | assert schema_org_hermes.id_ is not None |
12 | 18 | assert schema_org_hermes.id_.startswith("https://doi.org/") |
| 19 | + |
| 20 | + |
| 21 | +def test_concept_doi_is_hermes_reference(): |
| 22 | + """The HERMES concept DOI is a reference to HERMES. |
| 23 | +
|
| 24 | + We must be able to figure this out without asking DataCite. |
| 25 | + """ |
| 26 | + with requests_mock.Mocker() as m: |
| 27 | + assert _is_hermes_reference( |
| 28 | + SchemaOrgModel( |
| 29 | + type_="SoftwareSourceCode", |
| 30 | + id_="https://doi.org/10.5281/zenodo.13221383", |
| 31 | + ) |
| 32 | + ) |
| 33 | + # DataCite API was not called |
| 34 | + assert m.call_count == 0 |
| 35 | + |
| 36 | + |
| 37 | +def test_is_hermes_reference_if_datacite_api_returns_concept_doi_as_rel_id(): |
| 38 | + with requests_mock.Mocker() as m: |
| 39 | + m.get( |
| 40 | + "https://api.datacite.org/dois/10.9999/fake.1000", |
| 41 | + text=""" |
| 42 | +{ |
| 43 | + "data": { |
| 44 | + "id": "10.9999/fake.1000", |
| 45 | + "type": "dois", |
| 46 | + "attributes": { |
| 47 | + "doi": "10.9999/fake.1000", |
| 48 | + "prefix": "10.9999", |
| 49 | + "suffix": "fake.1000", |
| 50 | + "relatedIdentifiers": [ |
| 51 | + { |
| 52 | + "relationType": "IsVersionOf", |
| 53 | + "relatedIdentifier": "10.5281/zenodo.13221383", |
| 54 | + "relatedIdentifierType": "DOI" |
| 55 | + } |
| 56 | + ] |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +""".strip(), |
| 61 | + ) |
| 62 | + # 10.5281/zenodo.13221383 retured from DataCite is HERMES concept DOI |
| 63 | + assert _is_hermes_reference( |
| 64 | + SchemaOrgModel( |
| 65 | + type_="SoftwareSourceCode", id_="https://doi.org/10.9999/fake.1000" |
| 66 | + ) |
| 67 | + ) |
| 68 | + # DataCite API was called once |
| 69 | + assert m.call_count == 1 |
| 70 | + |
| 71 | + |
| 72 | +def test_not_is_hermes_reference_if_datacite_api_returns_wrong_rel_id(): |
| 73 | + with requests_mock.Mocker() as m: |
| 74 | + m.get( |
| 75 | + "https://api.datacite.org/dois/10.9999/fake.2000", |
| 76 | + text=""" |
| 77 | +{ |
| 78 | + "data": { |
| 79 | + "id": "10.9999/fake.2000", |
| 80 | + "type": "dois", |
| 81 | + "attributes": { |
| 82 | + "doi": "10.9999/fake.2000", |
| 83 | + "prefix": "10.9999", |
| 84 | + "suffix": "fake.2000", |
| 85 | + "relatedIdentifiers": [ |
| 86 | + { |
| 87 | + "relationType": "IsVersionOf", |
| 88 | + "relatedIdentifier": "10.9999/fake.1999", |
| 89 | + "relatedIdentifierType": "DOI" |
| 90 | + } |
| 91 | + ] |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +""".strip(), |
| 96 | + ) |
| 97 | + # 10.9999/fake.1999 returned from DataCite is not HERMES concept DOI |
| 98 | + assert not _is_hermes_reference( |
| 99 | + SchemaOrgModel( |
| 100 | + type_="SoftwareSourceCode", id_="https://doi.org/10.9999/fake.2000" |
| 101 | + ) |
| 102 | + ) |
| 103 | + # DataCite API was called once |
| 104 | + assert m.call_count == 1 |
0 commit comments