Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
Fix edge cases and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
obulat committed Jun 2, 2022
1 parent 6477aae commit 36a5a8d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
8 changes: 6 additions & 2 deletions openverse_catalog/dags/providers/provider_api_scripts/smk.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _handle_items_data(
image_url=img.get("image_url"),
height=img.get("height"),
width=img.get("width"),
filesize=img.get("image_size"),
filesize=img.get("filesize"),
filetype=extract_filetype(img.get("image_url", ""), "image"),
license_info=license_info,
thumbnail_url=img.get("thumbnail"),
Expand Down Expand Up @@ -137,14 +137,15 @@ def _get_images(item):

height = item.get("image_height")
width = item.get("image_width")

filesize = item.get("image_size") or item.get("size")
images.append(
{
"id": id,
"image_url": image_url,
"thumbnail": thumbnail_url,
"height": height,
"width": width,
"filesize": filesize,
}
)

Expand All @@ -160,13 +161,16 @@ def _get_images(item):
image_url, thumbnail_url = _get_image_urls(iiif_id)
height = alt_img.get("height")
width = alt_img.get("width")
filesize = alt_img.get("image_size") or alt_img.get("size")

images.append(
{
"id": iiif_id,
"image_url": image_url,
"thumbnail": thumbnail_url,
"height": height,
"width": width,
"filesize": filesize,
}
)
return images
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[
{
"filesize": 11784886,
"height": 1059,
"id": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.reconstructed.tif.jp2",
"image_url": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.reconstructed.tif.jp2/full/!2048,/0/default.jpg",
"thumbnail": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.reconstructed.tif.jp2/full/!400,/0/default.jpg",
"width": 3887
},
{
"filesize": 19269857,
"height": 1576,
"id": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.jp2",
"image_url": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.jp2/full/!2048,/0/default.jpg",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[
{
"filesize": 11784886,
"height": 1059,
"id": "1170012466_object",
"image_url": "https://api.smk.dk/api/v1/thumbnail/52f00edc-936e-42a7-950b-d0cd0df3864b.jpg",
"thumbnail": "https://api.smk.dk/api/v1/thumbnail/52f00edc-936e-42a7-950b-d0cd0df3864b.jpg",
"width": 3887
},
{
"filesize": 19269857,
"height": 1576,
"id": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.jp2",
"image_url": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.jp2/full/!2048,/0/default.jpg",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{
"filesize": 19269857,
"height": 1576,
"id": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.jp2",
"image_url": "https://iip.smk.dk/iiif/jp2/KKSgb6458.tif.jp2/full/!2048,/0/default.jpg",
Expand Down
40 changes: 37 additions & 3 deletions tests/dags/providers/provider_api_scripts/test_smk.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import json
import logging
import os
from pathlib import Path
from unittest.mock import MagicMock, patch

import requests
from common.licenses import LicenseInfo
from providers.provider_api_scripts import smk


RESOURCES = os.path.join(os.path.abspath(os.path.dirname(__file__)), "resources/smk")
RESOURCES = Path(__file__).parent.resolve() / "resources/smk"

logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s: %(message)s", level=logging.DEBUG
)

CC0 = LicenseInfo(
"cc0",
"1.0",
"https://creativecommons.org/publicdomain/zero/1.0/",
None,
)


def _get_resource_json(json_name):
with open(os.path.join(RESOURCES, json_name)) as f:
with open(RESOURCES / json_name) as f:
resource_json = json.load(f)
return resource_json

Expand Down Expand Up @@ -106,6 +114,32 @@ def test_handle_items_data_success():
assert actual_image_count == 1


def test_handle_items_data_success_data():
items = _get_resource_json("items_batch.json")
with patch.object(smk.image_store, "add_item") as mock_add_item:
smk._handle_items_data(items)
_, actual_image = mock_add_item.call_args
expected_image = {
"foreign_identifier": "https://iip.smk.dk/iiif/jp2/kks1615.tif.jp2",
"foreign_landing_url": "https://open.smk.dk/en/artwork/image/KKS1615",
"image_url": "https://iip.smk.dk/iiif/jp2/kks1615.tif.jp2/full/!2048,/0/default.jpg",
"height": 5141,
"width": 3076,
"filesize": 47466428,
"filetype": "jpg",
"license_info": CC0,
"thumbnail_url": "https://iip.smk.dk/iiif/jp2/kks1615.tif.jp2/full/!400,/0/default.jpg",
"creator": "Altdorfer, Albrecht",
"title": "Jomfru Maria med barnet og Sankt Anne ved vuggen",
"meta_data": {
"created_date": "2020-03-21T10:18:17Z",
"collection": "Gammel bestand",
"techniques": "Kobberstik",
},
}
assert actual_image == expected_image


def test_handle_items_data_failure():
items = []
with patch.object(smk.image_store, "add_item", return_value=None) as mock_add_item:
Expand Down

0 comments on commit 36a5a8d

Please sign in to comment.