Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add `filetype` test to Metropolitan script (#568)

Olga Bulatae122cc78e1c
diff --git a/tests/dags/providers/provider_api_scripts/test_metropolitan_museum.py b/tests/dags/providers/provider_api_scripts/test_metropolitan_museum.py
index e2bee629e..7967770c5 100644
--- a/tests/dags/providers/provider_api_scripts/test_metropolitan_museum.py
+++ b/tests/dags/providers/provider_api_scripts/test_metropolitan_museum.py
@@ -1,6 +1,6 @@
import json
import logging
-import os
+from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
@@ -9,16 +9,17 @@ from common.licenses import LicenseInfo
from providers.provider_api_scripts import metropolitan_museum as mma
-RESOURCES = os.path.join(
- os.path.abspath(os.path.dirname(__file__)),
- "resources/metropolitan_museum_of_art",
-)
+RESOURCES = Path(__file__).parent / "resources/metropolitan_museum_of_art"
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s: %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
+CC0 = LicenseInfo(
+ "cc0", "1.0", "https://creativecommons.org/publicdomain/zero/1.0/", None
+)
+
def test_get_object_ids():
r = requests.Response()
@@ -50,7 +51,7 @@ def test_create_meta_data():
exact_response = {
"accessionNumber": "36.100.45",
"classification": "Paintings",
- "creditLine": ("The Howard Mansfield Collection, Purchase, Rogers Fund, 1936"),
+ "creditLine": "The Howard Mansfield Collection, Purchase, Rogers Fund, 1936",
"culture": "Japan",
"objectDate": "late 17th century",
"medium": "Hanging scroll; ink and color on silk",
@@ -58,7 +59,7 @@ def test_create_meta_data():
exact_meta_data = {
"accession_number": "36.100.45",
"classification": "Paintings",
- "credit_line": ("The Howard Mansfield Collection, Purchase, Rogers Fund, 1936"),
+ "credit_line": "The Howard Mansfield Collection, Purchase, Rogers Fund, 1936",
"culture": "Japan",
"date": "late 17th century",
"medium": "Hanging scroll; ink and color on silk",
@@ -93,35 +94,38 @@ def test_get_data_for_image_with_non_ok():
def test_get_data_for_image_returns_response_json_when_all_ok(monkeypatch):
- with open(os.path.join(RESOURCES, "sample_response_without_additional.json")) as f:
+ with open(RESOURCES / "sample_response_without_additional.json") as f:
actual_response_json = json.load(f)
def mock_get_response_json(query_params, retries=0):
return actual_response_json
monkeypatch.setattr(mma, "_get_response_json", mock_get_response_json)
- with open(os.path.join(RESOURCES, "sample_additional_image_data.json")) as f:
+ with open(RESOURCES / "sample_additional_image_data.json") as f:
image_data = json.load(f)
r = requests.Response()
r.status_code = 200
r.json = MagicMock(return_value=image_data)
- with patch.object(mma.image_store, "add_item", return_value=image_data) as mock_add:
+ with patch.object(
+ mma.image_store, "save_item", return_value=image_data
+ ) as mock_save:
mma._get_data_for_image(45733)
- mock_add.assert_called_with(
- creator="",
- foreign_identifier="45733-79_2_414b_S1_sf",
- foreign_landing_url=("https://www.metmuseum.org/art/collection/search/47533"),
- image_url=(
- "https://images.metmuseum.org/CRDImages/as/original/79_2_414b_S1" "_sf.jpg"
- ),
- license_info=(
- LicenseInfo(
- "cc0", "1.0", "https://creativecommons.org/publicdomain/zero/1.0/", None
- )
- ),
- meta_data={
+ actual_image = mock_save.call_args[0][0]
+
+ expected_image = {
+ "creator": "",
+ "foreign_identifier": "45733-79_2_414b_S1_sf",
+ "foreign_landing_url": "https://www.metmuseum.org/art/collection/search/47533",
+ "url": "https://images.metmuseum.org/CRDImages/as/original/79_2_414b_S1_sf.jpg",
+ "license_": CC0.license,
+ "license_version": CC0.version,
+ "filetype": "jpg",
+ "filesize": None,
+ "meta_data": {
+ "license_url": CC0.url,
+ "raw_license_url": CC0.raw_url,
"accession_number": "79.2.414b",
"classification": "Ceramics",
"culture": "China",
@@ -129,21 +133,23 @@ def test_get_data_for_image_returns_response_json_when_all_ok(monkeypatch):
"medium": "Porcelain painted in underglaze blue",
"credit_line": "Purchase by subscription, 1879",
},
- title="Cover",
- )
+ "title": "Cover",
+ }
- assert mock_add.call_count == 1
+ assert mock_save.call_count == 1
+ for key, value in expected_image.items():
+ assert getattr(actual_image, key) == value
def test_get_data_for_image_returns_response_json_with_additional_images(monkeypatch):
- with open(os.path.join(RESOURCES, "sample_response.json")) as f:
+ with open(RESOURCES / "sample_response.json") as f:
actual_response_json = json.load(f)
def mock_get_response_json(query_params, retries=0):
return actual_response_json
monkeypatch.setattr(mma, "_get_response_json", mock_get_response_json)
- with open(os.path.join(RESOURCES, "sample_additional_image_data.json")) as f:
+ with open(RESOURCES / "sample_additional_image_data.json") as f:
image_data = json.load(f)
r = requests.Response()