Skip to content

Commit

Permalink
Add none check for Cleveland image_data (#709)
Browse files Browse the repository at this point in the history
* Add none check for image_data

* Clarify function name
  • Loading branch information
stacimc authored Sep 8, 2022
1 parent bcda6e0 commit 6e9d02d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_record_data(self, data):
if foreign_id is None:
return None

image = self._get_image_type(data.get("images", {}))
image = self._get_image_data(data.get("images", {}))
if image is None or image.get("url") is None:
return None

Expand All @@ -70,8 +70,12 @@ def get_record_data(self, data):
}

@staticmethod
def _get_image_type(image_data):
# Returns the image url and key for the image in `image_data` dict.
def _get_image_data(image_data):
# Returns the best available image in the `image_data` dict,
# preferring `web` and falling back to other types.
if image_data is None:
return None

for key in ["web", "print", "full"]:
if keyed_image := image_data.get(key):
return keyed_image
Expand Down
21 changes: 13 additions & 8 deletions tests/dags/providers/provider_api_scripts/test_cleveland_museum.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def test_build_query_param_increments_offset():
assert actual_param == expected_param


def test_get_image_type_web():
def test_get_image_data_web():
image_data = _get_resource_json("image_type_web.json")
actual_image = clm._get_image_type(image_data)
actual_image = clm._get_image_data(image_data)

expected_image = {
"url": "https://openaccess-cdn.clevelandart.org/1335.1917/1335.1917_web.jpg",
Expand All @@ -72,9 +72,9 @@ def test_get_image_type_web():
assert actual_image == expected_image


def test_get_image_type_print():
def test_get_image_data_print():
image_data = _get_resource_json("image_type_print.json")
actual_image = clm._get_image_type(image_data)
actual_image = clm._get_image_data(image_data)

expected_image = {
"url": "https://openaccess-cdn.clevelandart.org/1335.1917/1335.1917_print.jpg",
Expand All @@ -87,9 +87,9 @@ def test_get_image_type_print():
assert actual_image == expected_image


def test_get_image_type_full():
def test_get_image_data_full():
image_data = _get_resource_json("image_type_full.json")
actual_image = clm._get_image_type(image_data)
actual_image = clm._get_image_data(image_data)

expected_image = {
"url": "https://openaccess-cdn.clevelandart.org/1335.1917/1335.1917_full.tif",
Expand All @@ -102,13 +102,18 @@ def test_get_image_type_full():
assert actual_image == expected_image


def test_get_image_type_none():
def test_get_image_data_none():
image_data = _get_resource_json("image_type_none.json")
actual_image = clm._get_image_type(image_data)
actual_image = clm._get_image_data(image_data)

assert actual_image is None


def test_get_image_data_no_data():
# When image_data is None
assert clm._get_image_data(None) is None


def test_get_metadata():
data = _get_resource_json("complete_data.json")
actual_metadata = clm._get_metadata(data)
Expand Down

0 comments on commit 6e9d02d

Please sign in to comment.