Skip to content

Commit

Permalink
Add v2 test.
Browse files Browse the repository at this point in the history
  • Loading branch information
markpbaggett committed Dec 4, 2024
1 parent 8db7358 commit 9c16236
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
8 changes: 6 additions & 2 deletions iiif_prezi3/helpers/add_thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ..loader import monkeypatch_schema
from ..skeleton import (AccompanyingCanvas, Annotation, AnnotationCollection,
AnnotationPage, Canvas, Collection, Manifest,
PlaceholderCanvas, Range, Reference, ResourceItem, ServiceItem, ServiceItem1)
PlaceholderCanvas, Range, Reference, ResourceItem,
ServiceItem, ServiceItem1)


class AddThumbnail:
Expand All @@ -20,7 +21,7 @@ def add_thumbnail(self, image_url, **kwargs):
self.thumbnail = list()
self.thumbnail.append(new_thumbnail)
return new_thumbnail

def create_thumbnail_from_iiif(self, url, preferred_width=500, **kwargs):
"""Adds an image thumbnail to a manifest or canvas based on a IIIF service.
Expand Down Expand Up @@ -70,6 +71,9 @@ def create_thumbnail_from_iiif(self, url, preferred_width=500, **kwargs):
if 'sizes' in image_info:
new_thumbnail.height = best_fit_size.get('height')
new_thumbnail.width = best_fit_size.get('width')
elif 'height' in image_info and 'width' in image_info:
new_thumbnail.height = image_info['height']
new_thumbnail.width = image_info['width']

if not hasattr(self, 'thumbnail') or self.thumbnail is None:
self.thumbnail = []
Expand Down
55 changes: 55 additions & 0 deletions tests/test_add_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,58 @@ def test_create_thumbnail_from_iiif_v3(self, mockrequest_get):
# check thumbnail service
self.assertEqual(thumbnail.service[0].profile, "level0")
self.assertEqual(thumbnail.service[0].type, "ImageService3")

@patch('iiif_prezi3.helpers.set_hwd_from_iiif.requests.get')
def test_create_thumbnail_from_iiif_v2(self, mockrequest_get):
image_id = 'https://iiif.io/api/image/2.1/example/reference/918ecd18c2592080851777620de9bcb5-gottingen'
image_info_url = f'{image_id}/info.json'

# successful response with dimensions
mockresponse = Mock(status_code=200)
mockrequest_get.return_value = mockresponse
# set mock to return minimal image api response
mockresponse.json.return_value = {
"@context": "http://iiif.io/api/image/2/context.json",
"@id": "https://iiif.io/api/image/2.1/example/reference/918ecd18c2592080851777620de9bcb5-gottingen",
"height": 3024,
"profile": [
"http://iiif.io/api/image/2/level1.json",
{
"formats": [
"jpg",
"png"
],
"qualities": [
"default",
"color",
"gray"
]
}
],
"protocol": "http://iiif.io/api/image",
"tiles": [
{
"height": 512,
"scaleFactors": [
1,
2,
4
],
"width": 512
}
],
"width": 4032
}

thumbnail = self.manifest.create_thumbnail_from_iiif(image_info_url)[0]

# check thumbnail matches preferred size
self.assertEqual(thumbnail.height, 3024)
self.assertEqual(thumbnail.width, 4032)

# Since info_json has no sizes, use full/full
self.assertEqual(thumbnail.id, "https://iiif.io/api/image/2.1/example/reference/918ecd18c2592080851777620de9bcb5-gottingen/full/full/0/default.jpg")

# check thumbnail service
self.assertEqual(thumbnail.service[0].profile, "http://iiif.io/api/image/2/level1.json")
self.assertEqual(thumbnail.service[0].type, "ImageService2")

0 comments on commit 9c16236

Please sign in to comment.