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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
# -*- coding: utf-8 -*-
import os
import re
import unittest
from subprocess import Popen, PIPE
from PIL import Image
from django.template.loader import render_to_string
from django.test import Client, TestCase
import pytest
from sorl.thumbnail.conf import settings
from sorl.thumbnail.engines.pil_engine import Engine as PILEngine
from .models import Item
from .utils import BaseTestCase, override_custom_settings, DATA_DIR
pytestmark = pytest.mark.django_db
class TemplateTestCaseA(BaseTestCase):
def test_model(self):
item = Item.objects.get(image='500x500.jpg')
val = render_to_string('thumbnail1.html', {'item': item, }).strip()
self.assertEqual(val, '<img style="margin:0px 0px 0px 0px" width="200" height="100">')
val = render_to_string('thumbnail2.html', {'item': item, }).strip()
self.assertEqual(val, '<img style="margin:0px 50px 0px 50px" width="100" height="100">')
def test_nested(self):
item = Item.objects.get(image='500x500.jpg')
val = render_to_string('thumbnail6.html', {'item': item, }).strip()
self.assertEqual(val, (
'<a href="/media/test/cache/fc/f6/fcf65c09cc4bb8671147de41997422bf.jpg">'
'<img src="/media/test/cache/67/6b/676b2331a071478b0cb280d0edba7818.jpg" '
'width="400" height="400"></a>'
))
def test_serialization_options(self):
item = Item.objects.get(image='500x500.jpg')
for _ in range(0, 20):
# we could be lucky...
val0 = render_to_string('thumbnail7.html', {
'item': item,
}).strip()
val1 = render_to_string('thumbnail7a.html', {
'item': item,
}).strip()
self.assertEqual(val0, val1)
def test_options(self):
item = Item.objects.get(image='500x500.jpg')
options = {
'crop': "center",
'upscale': True,
'quality': 77,
}
val0 = render_to_string('thumbnail8.html', {'item': item, 'options': options, }).strip()
val1 = render_to_string('thumbnail8a.html', {'item': item, }).strip()
self.assertEqual(val0, val1)
def test_progressive(self):
im = Item.objects.get(image='500x500.jpg').image
th = self.BACKEND.get_thumbnail(im, '100x100', progressive=True)
path = os.path.join(settings.MEDIA_ROOT, th.name)
p = Popen(['identify', '-verbose', path], stdout=PIPE)
p.wait()
m = re.search('Interlace: JPEG', str(p.stdout.read()))
p.stdout.close()
self.assertEqual(bool(m), True)
def test_nonprogressive(self):
im = Item.objects.get(image='500x500.jpg').image
th = self.BACKEND.get_thumbnail(im, '100x100', progressive=False)
path = os.path.join(settings.MEDIA_ROOT, th.name)
p = Popen(['identify', '-verbose', path], stdout=PIPE)
p.wait()
m = re.search('Interlace: None', str(p.stdout.read()))
p.stdout.close()
self.assertEqual(bool(m), True)
def test_orientation(self):
ref = Image.open(os.path.join(DATA_DIR, '1_topleft.jpg'))
top = ref.getpixel((14, 7))
left = ref.getpixel((7, 14))
engine = PILEngine()
def epsilon(x, y):
if isinstance(x, (tuple, list)):
x = sum(x) / len(x)
if isinstance(y, (tuple, list)):
y = sum(y) / len(y)
return abs(x - y)
data_images = (
'1_topleft.jpg',
'2_topright.jpg',
'3_bottomright.jpg',
'4_bottomleft.jpg',
'5_lefttop.jpg',
'6_righttop.jpg',
'7_rightbottom.jpg',
'8_leftbottom.jpg'
)
for name in data_images:
th = self.BACKEND.get_thumbnail('data/%s' % name, '30x30')
im = engine.get_image(th)
self.assertLess(epsilon(top, im.getpixel((14, 7))), 10)
self.assertLess(epsilon(left, im.getpixel((7, 14))), 10)
exif = im._getexif()
# no exif editor in GraphicsMagick
if exif and not (settings.THUMBNAIL_CONVERT.endswith('gm convert') or
'pgmagick_engine' in settings.THUMBNAIL_ENGINE):
self.assertEqual(exif.get(0x0112), 1)
class TemplateTestCaseB(BaseTestCase):
@unittest.skipIf(os.environ.get('LOCAL_BUILD', False), "No remote resources desired")
def test_url(self):
val = render_to_string('thumbnail3.html', {}).strip()
self.assertEqual(val, '<img style="margin:0px 0px 0px 0px" width="20" height="20">')
@unittest.skipIf(os.environ.get('LOCAL_BUILD', False), "No remote resources desired")
def test_portrait(self):
val = render_to_string('thumbnail4.html', {
'source': 'http://dummyimage.com/120x100/',
'dims': 'x66',
}).strip()
self.assertEqual(val,
'<img src="/media/test/cache/7b/cd/7bcd20922c6750649f431df7c3cdbc5e.jpg" '
'width="79" height="66" class="landscape">')
def test_empty(self):
val = render_to_string('thumbnail5.html', {}).strip()
self.assertEqual(val, '<p>empty</p>')
class TemplateTestCaseClient(TestCase):
def test_empty_error(self):
with override_custom_settings(settings, THUMBNAIL_DEBUG=False):
from django.core.mail import outbox
client = Client()
response = client.get('/thumbnail9.html')
self.assertEqual(response.content.strip(), b'<p>empty</p>')
self.assertEqual(outbox[0].subject, '[sorl-thumbnail] ERROR: Unknown URL')
end = outbox[0].body.split('\n\n')[-2].split(':')[1].strip()
self.assertEqual(end, '[Errno 2] No such file or directory')
class TemplateTestCaseTemplateTagAlias(BaseTestCase):
"""Testing alternative template tag (alias)."""
def test_model(self):
item = Item.objects.get(image='500x500.jpg')
val = render_to_string(
'thumbnail1_alias.html', {'item': item}
).strip()
self.assertEqual(
val,
'<img style="margin:0px 0px 0px 0px" width="200" height="100">'
)
val = render_to_string(
'thumbnail2_alias.html', {'item': item}
).strip()
self.assertEqual(
val,
'<img style="margin:0px 50px 0px 50px" width="100" height="100">'
)
def test_nested(self):
item = Item.objects.get(image='500x500.jpg')
val = render_to_string(
'thumbnail6_alias.html', {'item': item}
).strip()
self.assertEqual(
val,
(
'<a href="/media/test/cache/fc/f6/'
'fcf65c09cc4bb8671147de41997422bf.jpg">'
'<img src="/media/test/cache/67/6b/'
'676b2331a071478b0cb280d0edba7818.jpg" '
'width="400" height="400"></a>'
)
)
def test_serialization_options(self):
item = Item.objects.get(image='500x500.jpg')
for _ in range(0, 20):
# we could be lucky...
val0 = render_to_string('thumbnail7_alias.html', {
'item': item,
}).strip()
val1 = render_to_string('thumbnail7a_alias.html', {
'item': item,
}).strip()
self.assertEqual(val0, val1)
def test_options(self):
item = Item.objects.get(image='500x500.jpg')
options = {
'crop': "center",
'upscale': True,
'quality': 77,
}
val0 = render_to_string(
'thumbnail8_alias.html',
{'item': item, 'options': options}
).strip()
val1 = render_to_string(
'thumbnail8a_alias.html', {'item': item}
).strip()
self.assertEqual(val0, val1)
|