Skip to content

Commit

Permalink
Refactor to reduce code complexity and increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jermnelson committed Jan 9, 2024
1 parent ae429d1 commit d4fc8d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 14 additions & 7 deletions ils_middleware/tasks/sinopia/metadata_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import rdflib
import requests # type: ignore

from typing import Optional
from typing import Optional, Union

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -33,6 +33,16 @@ def _query_for_ils_info(graph_jsonld: str, uri: str) -> dict:
return output


def _get_relationships(resource_uri: str) -> Union[list, None]:
result = requests.get(f"{resource_uri}/relationships")
if result.status_code > 399:
msg = f"Failed to retrieve {resource_uri}: {result.status_code}\n{result.text}"
logging.error(msg)
return None

return result.json().get("sinopiaHasLocalAdminMetadataInferredRefs")


def _get_retrieve_metadata_resource(uri: str) -> Optional[dict]:
"""Retrieves AdminMetadata resource and extracts any ILS identifiers"""
metadata_result = requests.get(uri)
Expand Down Expand Up @@ -76,13 +86,10 @@ def _retrieve_all_resource_refs(
if target_resource_id:
retrieved_resources[resource_uri] = [{default_ils: target_resource_id}]
continue
result = requests.get(f"{resource_uri}/relationships")
if result.status_code > 399:
msg = f"Failed to retrieve {resource_uri}: {result.status_code}\n{result.text}"
logging.error(msg)
continue

metadata_uris = result.json().get("sinopiaHasLocalAdminMetadataInferredRefs")
metadata_uris = _get_relationships(resource_uri)
if metadata_uris is None:
continue

ils_info = _retrieve_all_metadata(metadata_uris)
if ils_info:
Expand Down
9 changes: 9 additions & 0 deletions tests/tasks/sinopia/test_metadata_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ils_middleware.tasks.sinopia.metadata_check import (
existing_metadata_check,
_get_retrieve_metadata_resource,
_get_relationships,
_retrieve_all_metadata,
)

Expand Down Expand Up @@ -184,3 +185,11 @@ def test_dups_in_retrieve_all_metadata(mock_requests):
)

assert result == [{"SIRSI": "13704749"}]


def test_get_relationships_no_resource(mock_requests, caplog):
missing_result = _get_relationships("https://sinopia.io/resource/no-resource")
assert missing_result is None
assert (
"Failed to retrieve https://sinopia.io/resource/no-resource: 401" in caplog.text
)

0 comments on commit d4fc8d8

Please sign in to comment.