Skip to content

Commit

Permalink
Fix resolver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zyzzyxdonta committed Oct 18, 2023
1 parent 682b844 commit 3898652
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
15 changes: 9 additions & 6 deletions src/hermes/commands/deposit/invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
_log = logging.getLogger("cli.deposit.invenio")


# TODO: Update docstrings
class InvenioResolver:
def __init__(self):
self.session = requests.Session()
self.session.headers.update({"User-Agent": hermes_user_agent})

def resolve_latest_id(
self, site_url, record_id=None, doi=None, codemeta_identifier=None
self, site_url, records_api_path="api/records", record_id=None, doi=None,
codemeta_identifier=None
) -> t.Tuple[str, dict]:
"""
Using the given metadata parameters, figure out the latest record id.
Expand Down Expand Up @@ -67,7 +67,7 @@ def resolve_latest_id(

if record_id is not None:
# If we got a record id by now, resolve it using the Invenio API to the latests record.
return self.resolve_record_id(site_url, record_id)
return self.resolve_record_id(site_url, record_id, records_api_path)

return None, {}

Expand Down Expand Up @@ -96,15 +96,17 @@ def resolve_doi(self, site_url, doi) -> str:
*_, record_id = page_url.path.split('/')
return record_id

def resolve_record_id(self, site_url: str, record_id: str) -> t.Tuple[str, dict]:
def resolve_record_id(
self, site_url: str, record_id: str, records_api_path: str = "api/records"
) -> t.Tuple[str, dict]:
"""
Find the latest version of a given record.
:param site_url: Root URL for the Invenio instance to use.
:param record_id: The record that sould be resolved.
:return: The record id of the latest version for the requested record.
"""
res = self.sesssion.get(f"{site_url}/{self.records_api_path}/{record_id}")
res = self.session.get(f"{site_url}/{records_api_path}/{record_id}")
if res.status_code != 200:
raise ValueError(f"Could not retrieve record from {res.url}: {res.text}")

Expand Down Expand Up @@ -185,7 +187,8 @@ def prepare(self) -> None:
codemeta_identifier = None

rec_id, rec_meta = self.resolver.resolve_latest_id(
site_url, record_id=rec_id, doi=doi, codemeta_identifier=codemeta_identifier
site_url, self.records_api_path, record_id=rec_id, doi=doi,
codemeta_identifier=codemeta_identifier
)

version = self.ctx["codemeta"].get("version")
Expand Down
18 changes: 12 additions & 6 deletions test/hermes_test/commands/deposit/test_invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def test_resolve_doi(requests_mock):
headers={'Location': 'https://foo.bar/record/6789'})
requests_mock.get('https://foo.bar/record/6789')

assert invenio._invenio_resolve_doi('https://foo.bar', '123.45/foo.bar-6789') == '6789'
resolver = invenio.InvenioResolver()
assert resolver.resolve_doi('https://foo.bar', '123.45/foo.bar-6789') == '6789'


def test_resolve_doi_wrong_host(requests_mock):
Expand All @@ -28,8 +29,9 @@ def test_resolve_doi_wrong_host(requests_mock):
headers={'Location': 'https://foo.baz/record/6789'})
requests_mock.get('https://foo.baz/record/6789')

resolver = invenio.InvenioResolver()
with pytest.raises(ValueError):
invenio._invenio_resolve_doi('https://foo.bar', '123.45/foo.bar-6789')
resolver.resolve_doi('https://foo.bar', '123.45/foo.bar-6789')


def test_resolve_doi_unknown(requests_mock):
Expand All @@ -40,32 +42,36 @@ def test_resolve_doi_unknown(requests_mock):
# To show how broken datacite is right now
requests_mock.get('https://datacite.org/404.html', status_code=200)

resolver = invenio.InvenioResolver()
with pytest.raises(ValueError):
invenio._invenio_resolve_doi('https://foo.bar', '123.45/foo.bar-6789')
resolver.resolve_doi('https://foo.bar', '123.45/foo.bar-6789')


def test_resolve_record_id(requests_mock):
requests_mock.get('https://foo.bar/api/records/6789',
text='{"links":{"latest":"https://foo.bar/api/records/12345"}}')
requests_mock.get('https://foo.bar/api/records/12345', text='{"id":"12345","metadata":{"mock":42}}')

assert invenio._invenio_resolve_record_id('https://foo.bar', '6789') == ('12345', {"mock": 42})
resolver = invenio.InvenioResolver()
assert resolver.resolve_record_id('https://foo.bar', '6789') == ('12345', {"mock": 42})


def test_resolve_record_id_unknown(requests_mock):
requests_mock.get('https://foo.bar/api/records/6789', status_code=404, text="Not found")

resolver = invenio.InvenioResolver()
with pytest.raises(ValueError):
invenio._invenio_resolve_record_id('https://foo.bar', '6789')
resolver.resolve_record_id('https://foo.bar', '6789')


def test_resolve_record_id_latest_unknown(requests_mock):
requests_mock.get('https://foo.bar/api/records/6789',
text='{"links":{"latest":"https://foo.bar/api/records/12345"}}')
requests_mock.get('https://foo.bar/api/records/12345', status_code=404)

resolver = invenio.InvenioResolver()
with pytest.raises(ValueError):
invenio._invenio_resolve_record_id('https://foo.bar', '6789')
resolver.resolve_record_id('https://foo.bar', '6789')


def test_get_access_modalities_closed():
Expand Down

0 comments on commit 3898652

Please sign in to comment.