-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_thumbnails.py
109 lines (93 loc) · 3.74 KB
/
test_thumbnails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2016 University of Dundee & Open Microscopy Environment.
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Tests rendering of thumbnails."""
import base64
import json
from omeroweb.testlib import IWebTest
from omeroweb.testlib import get
from cStringIO import StringIO
import pytest
from django.core.urlresolvers import reverse
try:
from PIL import Image
except:
import Image
class TestThumbnails(IWebTest):
"""Tests loading of thumbnails."""
@pytest.mark.parametrize("size", [None, 100])
def test_default_thumb_size(self, size):
"""
Test that the default size of thumbnails is correct.
Default size is 96.
"""
# Create a square image
iId = self.create_test_image(size_x=125, size_y=125,
session=self.sf).getId().getValue()
args = [iId]
if size is not None:
args.append(size)
request_url = reverse('webgateway.views.render_thumbnail', args=args)
rsp = get(self.django_client, request_url)
thumb = Image.open(StringIO(rsp.content))
# Should be 96 on both sides
if size is None:
assert thumb.size == (96, 96)
else:
assert thumb.size == (size, size)
@pytest.mark.parametrize("size", [None, 100])
def test_base64_thumb(self, size):
"""
Test base64 encoded retrival of single thumbnail
"""
# Create a square image
iid = self.create_test_image(size_x=256, size_y=256,
session=self.sf).id.val
args = [iid]
if size is not None:
args.append(size)
request_url = reverse('webgateway.views.render_thumbnail', args=args)
rsp = get(self.django_client, request_url)
thumb = json.dumps(
"data:image/jpeg;base64,%s" % base64.b64encode(rsp.content))
request_url = reverse('webgateway.views.get_thumbnail_json',
args=args)
b64rsp = get(self.django_client, request_url).content
assert thumb == b64rsp
def test_base64_thumb_set(self):
"""
Test base64 encoded retrival of thumbnails in a batch
"""
# Create a square image
images = []
for i in range(2, 5):
iid = self.create_test_image(size_x=64*i, size_y=64*i,
session=self.sf).id.val
images.append(iid)
expected_thumbs = {}
for i in images:
request_url = reverse('webgateway.views.render_thumbnail',
args=[i])
rsp = get(self.django_client, request_url)
expected_thumbs[i] = \
"data:image/jpeg;base64,%s" % base64.b64encode(rsp.content)
iids = {'id': images}
request_url = reverse('webgateway.views.get_thumbnails_json')
b64rsp = get(self.django_client, request_url, iids).content
assert cmp(json.loads(b64rsp),
json.loads(json.dumps(expected_thumbs))) == 0
assert json.dumps(expected_thumbs) == b64rsp