Skip to content

Commit

Permalink
Update tests for nappy, museum_victoria, provider_data_ingester
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed May 4, 2023
1 parent fb0d606 commit efeff99
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
2 changes: 1 addition & 1 deletion catalog/dags/providers/provider_api_scripts/nappy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NappyDataIngester(ProviderDataIngester):
endpoint = "https://api.nappy.co/v1/openverse/images"
headers = {"User-Agent": prov.UA_STRING, "Accept": "application/json"}

# Hardoded to CC0, the only license Nappy.co uses
# Hardcoded to CC0, the only license Nappy.co uses
license_info = get_license_info(
"https://creativecommons.org/publicdomain/zero/1.0/"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ def test_no_duplicate_records():
assert actual_image_data is None


@pytest.mark.parametrize(
"falsy_parameter",
["id", "media"],
)
def test_get_record_data_returns_none_with_falsy_param(falsy_parameter):
media = _get_resource_json("record_data.json")
media[falsy_parameter] = ""
actual_image_data = mv.get_record_data(media)

assert actual_image_data is None


def test_get_images_success():
media = _get_resource_json("media_data_success.json")
actual_image_data = mv._get_images([media])
Expand All @@ -144,52 +156,29 @@ def test_get_media_info_failure():
assert actual_image_data is None


def test_get_image_data_large():
image_data = _get_resource_json("large_image_data.json")

actual_image_url, actual_height, actual_width, actual_filesize = mv._get_image_data(
image_data
)

assert actual_image_url == (
"https://collections.museumsvictoria.com.au/content/media/45/"
"329745-large.jpg"
)
assert actual_height == 2581
assert actual_width == 2785
assert actual_filesize == 890933


def test_get_image_data_medium():
image_data = _get_resource_json("medium_image_data.json")

actual_image_url, actual_height, actual_width, actual_filesize = mv._get_image_data(
image_data
)

assert actual_image_url == (
"https://collections.museumsvictoria.com.au/content/media/45/"
"329745-medium.jpg"
@pytest.mark.parametrize(
"image_size, expected_height, expected_width, expected_filesize",
[
pytest.param("large", 2581, 2785, 890933, id="large"),
pytest.param("medium", 1390, 1500, 170943, id="medium"),
pytest.param("small", 500, 540, 20109, id="small"),
],
)
def test_get_image_data(image_size, expected_height, expected_width, expected_filesize):
image_data = _get_resource_json(f"{image_size}_image_data.json")
expected_url = (
f"https://collections.museumsvictoria.com.au/content/media/45/"
f"329745-{image_size}.jpg"
)
assert actual_height == 1390
assert actual_width == 1500
assert actual_filesize == 170943


def test_get_image_data_small():
image_data = _get_resource_json("small_image_data.json")

actual_image_url, actual_height, actual_width, actual_filesize = mv._get_image_data(
image_data
)

assert actual_image_url == (
"https://collections.museumsvictoria.com.au/content/media/45/"
"329745-small.jpg"
)
assert actual_height == 500
assert actual_width == 540
assert actual_filesize == 20109
assert actual_image_url == expected_url
assert actual_height == expected_height
assert actual_width == expected_width
assert actual_filesize == expected_filesize


def test_get_image_data_none():
Expand Down
11 changes: 11 additions & 0 deletions catalog/tests/dags/providers/provider_api_scripts/test_nappy.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ def test_get_should_continue(response_json, expected_result):
[
pytest.param({}, None, id="empty_dict"),
pytest.param(FULL_BATCH_RESPONSE, None, id="no_urls"),
pytest.param(
{**SINGLE_ITEM, "foreign_landing_url": ""},
None,
id="falsy_foreign_landing_url",
),
pytest.param(
{**SINGLE_ITEM, "foreign_identifier": ""},
None,
id="falsy_foreign_identifier",
),
pytest.param({**SINGLE_ITEM, "url": ""}, None, id="falsy_url"),
pytest.param(
SINGLE_ITEM,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ def test_process_batch_handles_list_of_records():
assert image_store_mock.call_count == 1


def test_process_batch_handles_empty_dictionary_of_records_from_get_record_data():
with (
patch.object(audio_store, "add_item") as audio_store_mock,
patch.object(image_store, "add_item") as image_store_mock,
patch.object(ingester, "get_record_data") as get_record_data_mock,
):
# Mock `get_record_data` to return an empty list of records
get_record_data_mock.return_value = []

record_count = ingester.process_batch([{}])

# Both records are added, and to the appropriate stores
assert record_count == 0
assert audio_store_mock.call_count == 0
assert image_store_mock.call_count == 0


def test_process_batch_halts_processing_after_reaching_ingestion_limit():
# Set up an ingester with an ingestion limit of 1
ingester = MockProviderDataIngester()
Expand Down

0 comments on commit efeff99

Please sign in to comment.