Skip to content

Commit

Permalink
refactor: fail 'has_metadata' gracefully
Browse files Browse the repository at this point in the history
Handle HTTP errors gracefully in the 'has_metadata' function to prevent
systematic failures.

Employ pytest-mock to simulate both success and failure conditions.
  • Loading branch information
clnsmth committed Sep 14, 2023
1 parent a0e9515 commit 7381637
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/gbif_registrar/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from json import loads
import pandas as pd
import requests
from requests import get
from gbif_registrar.config import PASTA_ENVIRONMENT, GBIF_API


Expand Down Expand Up @@ -134,7 +133,10 @@ def has_metadata(gbif_dataset_uuid):
The presence of a dataset title indicates that the dataset has been
crawled by GBIF and the metadata document has been created.
"""
resp = get(url=GBIF_API + "/" + gbif_dataset_uuid, timeout=60)
resp.raise_for_status()
resp = requests.get(url=GBIF_API + "/" + gbif_dataset_uuid, timeout=60)
if resp.status_code != 200:
print("HTTP request failed with status code: " + str(resp.status_code))
print(resp.reason)
return False
details = loads(resp.text)
return bool(details.get("title"))
35 changes: 33 additions & 2 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,38 @@ def test_read_local_dataset_metadata_failure(mocker):
assert metadata is None


def test_has_metadata_returns_expected_type():
"""Test that the has_metadata function returns a boolean."""
def test_has_metadata_success(mocker):
"""Test that has_metadata returns True on success."""
# Create a JSON string simulating a successful response from the GBIF API
json_content = """{"title":"This is a title"}"""
mock_response = mocker.Mock()
mock_response.status_code = 200
mock_response.text = json_content
mocker.patch("requests.get", return_value=mock_response)
res = has_metadata("cfb3f6d5-ed7d-4fff-9f1b-f032ed1de485")
assert isinstance(res, bool)


def test_has_metadata_failure(mocker):
"""Test that has_metadata returns False on failure.
Failure may occur if the GBIF API returns a 404 status code or if the
JSON response doesn't contain a title."""

# Case 1: GBIF API returns a 404 status code
mock_response = mocker.Mock()
mock_response.status_code = 404
mock_response.reason = "Not Found"
mocker.patch("requests.get", return_value=mock_response)
res = has_metadata("cfb3f6d5-ed7d-4fff-9f1b-f032ed1de485")
assert res is False

# Case 2: GBIF API returns a 200 status code but the JSON response doesn't
# contain a title
json_content = """{"description":"This is a description"}"""
mock_response = mocker.Mock()
mock_response.status_code = 200
mock_response.text = json_content
mocker.patch("requests.get", return_value=mock_response)
res = has_metadata("cfb3f6d5-ed7d-4fff-9f1b-f032ed1de485")
assert res is False

0 comments on commit 7381637

Please sign in to comment.