Skip to content

Commit

Permalink
refactor: fail 'request_gbif_dataset_uuid' gracefully
Browse files Browse the repository at this point in the history
Handle HTTP errors gracefully in the 'request_gbif_dataset_uuid'
function to prevent systematic failures. Employ pytest-mock to simulate
both success and failure conditions.
  • Loading branch information
clnsmth committed Sep 13, 2023
1 parent 4943677 commit f1bca32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
14 changes: 6 additions & 8 deletions src/gbif_registrar/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,13 @@ def get_gbif_dataset_uuid(local_dataset_group_id, rgstrs):


def request_gbif_dataset_uuid():
"""Request a gbif_dataset_uuid value from GBIF.
"""Request a GBIF dataset UUID value from GBIF.
Returns
-------
str
The gbif_dataset_uuid value. This is the UUID assigned by GBIF to the
The GBIF dataset UUID value. This is the UUID assigned by GBIF to the
local dataset group.
Notes
-----
The gbif_dataset_uuid value is an arbitrary UUID value generated and
returned by the GBIF server.
"""
title = "Placeholder title, to be written over by EML metadata from EDI"
data = {
Expand All @@ -257,5 +252,8 @@ def request_gbif_dataset_uuid():
headers=headers,
timeout=60,
)
resp.raise_for_status()
if resp.status_code != 201:
print("HTTP request failed with status code: " + str(resp.status_code))
print(resp.reason)
return None
return resp.json()
28 changes: 18 additions & 10 deletions tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,26 @@ def test_get_gbif_dataset_uuid(rgstrs):
# res = get_gbif_dataset_uuid(local_dataset_group_id, rgstrs)


def test_request_gbif_dataset_uuid():
"""Test request_gbif_dataset_uuid() function.
def test_request_gbif_dataset_uuid_success(mocker):
"""Test that the request_gbif_dataset_uuid function returns a UUID string
when the HTTP request is successful."""
mock_response = mocker.Mock()
mock_response.status_code = 201
mock_response.json.return_value = "4e70c80e-cf22-49a5-8bf7-280994500324"
mocker.patch('requests.post', return_value=mock_response)
res = request_gbif_dataset_uuid()
assert res == "4e70c80e-cf22-49a5-8bf7-280994500324"


The expected value is a UUID string on success or None in the case of a
failed HTTP request."""
# Case 1: UUID is returned.
def test_request_gbif_dataset_uuid_failure(mocker):
"""Test that the request_gbif_dataset_uuid function returns None when the
HTTP request fails."""
mock_response = mocker.Mock()
mock_response.status_code = 400
mock_response.reason = "Bad Request"
mocker.patch('requests.post', return_value=mock_response)
res = request_gbif_dataset_uuid()
assert isinstance(res, str)
assert res is not None
# Case 2: HTTP request fails.
# TODO: Stub out the GBIF API call to test this case.
# res = request_gbif_dataset_uuid()
assert res is None


def test_register(local_dataset_id, tmp_path):
Expand Down

0 comments on commit f1bca32

Please sign in to comment.