|
@@ -4,10 +4,10 @@ from django.test import TestCase |
|
|
from django.core.files.images import ImageFile |
|
|
from django.core.files.images import ImageFile |
|
|
from django.conf import settings |
|
|
from django.conf import settings |
|
|
from django.utils.six import BytesIO |
|
|
from django.utils.six import BytesIO |
|
|
from django.core.urlresolvers import reverse |
|
|
|
|
|
from django_images.models import Image, Thumbnail |
|
|
from django_images.models import Image, Thumbnail |
|
|
from django_images.templatetags.images import at_size |
|
|
from django_images.templatetags.images import at_size |
|
|
from django_images.utils import scale_and_crop |
|
|
|
|
|
|
|
|
from django_images.utils import scale_and_crop_single |
|
|
|
|
|
from PIL import Image as PILImage |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageModelTest(TestCase): |
|
|
class ImageModelTest(TestCase): |
|
@@ -20,7 +20,7 @@ class ImageModelTest(TestCase): |
|
|
|
|
|
|
|
|
def test_get_by_size(self): |
|
|
def test_get_by_size(self): |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
Thumbnail.objects.get_or_create_at_sizes(self.image, [size]) |
|
|
self.image.get_by_size(size) |
|
|
self.image.get_by_size(size) |
|
|
|
|
|
|
|
|
def test_get_absolute_url(self): |
|
|
def test_get_absolute_url(self): |
|
@@ -28,14 +28,9 @@ class ImageModelTest(TestCase): |
|
|
self.assertEqual(url, self.image.image.url) |
|
|
self.assertEqual(url, self.image.image.url) |
|
|
# For thumbnail |
|
|
# For thumbnail |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0] |
|
|
url = self.image.get_absolute_url(size) |
|
|
url = self.image.get_absolute_url(size) |
|
|
self.assertEqual(url, thumb.image.url) |
|
|
self.assertEqual(url, thumb.image.url) |
|
|
# Fallback on creation url |
|
|
|
|
|
size = list(settings.IMAGE_SIZES.keys())[1] |
|
|
|
|
|
url = self.image.get_absolute_url(size) |
|
|
|
|
|
fallback_url = reverse('image-thumbnail', args=(self.image.id, size)) |
|
|
|
|
|
self.assertEqual(url, fallback_url) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ThumbnailManagerModelTest(TestCase): |
|
|
class ThumbnailManagerModelTest(TestCase): |
|
@@ -48,17 +43,17 @@ class ThumbnailManagerModelTest(TestCase): |
|
|
self.size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
self.size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
|
|
|
|
|
|
def test_unknown_size(self): |
|
|
def test_unknown_size(self): |
|
|
self.assertRaises(ValueError, Thumbnail.objects.get_or_create_at_size, |
|
|
|
|
|
self.image.id, 'foo') |
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, Thumbnail.objects.get_or_create_at_sizes, |
|
|
|
|
|
self.image, ['foo']) |
|
|
|
|
|
|
|
|
# TODO: Test the image object and data |
|
|
# TODO: Test the image object and data |
|
|
def test_create(self): |
|
|
def test_create(self): |
|
|
Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) |
|
|
|
|
|
|
|
|
Thumbnail.objects.get_or_create_at_sizes(self.image, [self.size]) |
|
|
self.assertEqual(self.image.thumbnail_set.count(), 1) |
|
|
self.assertEqual(self.image.thumbnail_set.count(), 1) |
|
|
|
|
|
|
|
|
def test_get(self): |
|
|
def test_get(self): |
|
|
thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) |
|
|
|
|
|
thumb2 = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) |
|
|
|
|
|
|
|
|
thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [self.size])[0] |
|
|
|
|
|
thumb2 = Thumbnail.objects.get_or_create_at_sizes(self.image, [self.size])[0] |
|
|
self.assertEqual(thumb.id, thumb2.id) |
|
|
self.assertEqual(thumb.id, thumb2.id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -70,7 +65,7 @@ class ThumbnailModelTest(TestCase): |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0] |
|
|
|
|
|
|
|
|
def test_get_absolute_url(self): |
|
|
def test_get_absolute_url(self): |
|
|
url = self.thumb.get_absolute_url() |
|
|
url = self.thumb.get_absolute_url() |
|
@@ -85,11 +80,11 @@ class PostSaveSignalOriginalChangedTestCase(TestCase): |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0] |
|
|
|
|
|
|
|
|
def test_post_save_signal_original_changed(self): |
|
|
def test_post_save_signal_original_changed(self): |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
Thumbnail.objects.get_or_create_at_sizes(self.image, [size]) |
|
|
self.image.delete() |
|
|
self.image.delete() |
|
|
self.assertFalse(Thumbnail.objects.exists()) |
|
|
self.assertFalse(Thumbnail.objects.exists()) |
|
|
|
|
|
|
|
@@ -102,7 +97,7 @@ class PostDeleteSignalDeleteImageFileTest(TestCase): |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0] |
|
|
|
|
|
|
|
|
@mock.patch('django_images.models.IMAGE_AUTO_DELETE', True) |
|
|
@mock.patch('django_images.models.IMAGE_AUTO_DELETE', True) |
|
|
def test_post_delete_signal_delete_image_files_enabled(self): |
|
|
def test_post_delete_signal_delete_image_files_enabled(self): |
|
@@ -136,7 +131,7 @@ class AtSizeTemplateTagTest(TestCase): |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) |
|
|
|
|
|
|
|
|
self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0] |
|
|
|
|
|
|
|
|
def test_at_size(self): |
|
|
def test_at_size(self): |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
size = list(settings.IMAGE_SIZES.keys())[0] |
|
@@ -144,65 +139,37 @@ class AtSizeTemplateTagTest(TestCase): |
|
|
self.assertEqual(url, self.thumb.image.url) |
|
|
self.assertEqual(url, self.thumb.image.url) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ThumbnailViewTest(TestCase): |
|
|
|
|
|
def setUp(self): |
|
|
|
|
|
image_obj = BytesIO() |
|
|
|
|
|
qrcode_obj = qrcode.make('https://mirumee.com/') |
|
|
|
|
|
qrcode_obj.save(image_obj) |
|
|
|
|
|
self.image = Image.objects.create(width=370, height=370, |
|
|
|
|
|
image=ImageFile(image_obj, '01.png')) |
|
|
|
|
|
self.size = list(settings.IMAGE_SIZES.keys())[0] |
|
|
|
|
|
self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) |
|
|
|
|
|
|
|
|
|
|
|
def test_redirect(self): |
|
|
|
|
|
url = reverse('image-thumbnail', args=[self.image.id, self.size]) |
|
|
|
|
|
response = self.client.get(url) |
|
|
|
|
|
self.assertNotEqual(url, self.thumb.image.url) |
|
|
|
|
|
self.assertEqual(response.status_code, 302) |
|
|
|
|
|
self.assertEqual(response.url, self.thumb.image.url) |
|
|
|
|
|
|
|
|
|
|
|
def test_not_found(self): |
|
|
|
|
|
url = reverse('image-thumbnail', args=['42', self.size]) |
|
|
|
|
|
response = self.client.get(url) |
|
|
|
|
|
self.assertEqual(response.status_code, 404) |
|
|
|
|
|
|
|
|
|
|
|
def test_size_not_found(self): |
|
|
|
|
|
url = reverse('image-thumbnail', args=[self.image.id, '42']) |
|
|
|
|
|
response = self.client.get(url) |
|
|
|
|
|
self.assertEqual(response.status_code, 404) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UtilsScaleAndDropTest(TestCase): |
|
|
class UtilsScaleAndDropTest(TestCase): |
|
|
def setUp(self): |
|
|
def setUp(self): |
|
|
image_obj = BytesIO() |
|
|
image_obj = BytesIO() |
|
|
qrcode_obj = qrcode.make('https://mirumee.com/') |
|
|
qrcode_obj = qrcode.make('https://mirumee.com/') |
|
|
qrcode_obj.save(image_obj) |
|
|
qrcode_obj.save(image_obj) |
|
|
self.imagefile = ImageFile(image_obj, '01.png') |
|
|
|
|
|
|
|
|
self.image = PILImage.open(image_obj) |
|
|
|
|
|
|
|
|
def test_change_size(self): |
|
|
def test_change_size(self): |
|
|
new_size = (10, 10) |
|
|
new_size = (10, 10) |
|
|
image = scale_and_crop(self.imagefile, new_size) |
|
|
|
|
|
self.assertEqual(new_size, image.im.size) |
|
|
|
|
|
|
|
|
image = scale_and_crop_single(self.image, new_size) |
|
|
|
|
|
self.assertEqual(new_size, image.size) |
|
|
|
|
|
|
|
|
def test_crop(self): |
|
|
def test_crop(self): |
|
|
new_size = (10, 10) |
|
|
new_size = (10, 10) |
|
|
image = scale_and_crop(self.imagefile, new_size, crop=True) |
|
|
|
|
|
self.assertEqual(new_size, image.im.size) |
|
|
|
|
|
|
|
|
image = scale_and_crop_single(self.image, new_size, crop=True) |
|
|
|
|
|
self.assertEqual(new_size, image.size) |
|
|
|
|
|
|
|
|
def test_disabled_upscale(self): |
|
|
def test_disabled_upscale(self): |
|
|
image = scale_and_crop(self.imagefile, (740, 740), upscale=False) |
|
|
|
|
|
self.assertLess(image.im.size[0], 371) |
|
|
|
|
|
self.assertLess(image.im.size[1], 371) |
|
|
|
|
|
|
|
|
image = scale_and_crop_single(self.image, (740, 740), upscale=False) |
|
|
|
|
|
self.assertLess(image.size[0], 371) |
|
|
|
|
|
self.assertLess(image.size[1], 371) |
|
|
|
|
|
|
|
|
def test_enaabled_upscale(self): |
|
|
def test_enaabled_upscale(self): |
|
|
image = scale_and_crop(self.imagefile, (740, 740), upscale=True) |
|
|
|
|
|
self.assertGreater(image.im.size[0], 371) |
|
|
|
|
|
self.assertGreater(image.im.size[1], 371) |
|
|
|
|
|
|
|
|
image = scale_and_crop_single(self.image, (740, 740), upscale=True) |
|
|
|
|
|
self.assertGreater(image.size[0], 371) |
|
|
|
|
|
self.assertGreater(image.size[1], 371) |
|
|
|
|
|
|
|
|
def test_not_change_quality(self): |
|
|
def test_not_change_quality(self): |
|
|
image = scale_and_crop(self.imagefile, (10, 10), quality=None) |
|
|
|
|
|
|
|
|
image = scale_and_crop_single(self.image, (10, 10), quality=None) |
|
|
self.assertEqual(image.info.get('quality'), None) |
|
|
self.assertEqual(image.info.get('quality'), None) |
|
|
|
|
|
|
|
|
def test_change_quality(self): |
|
|
def test_change_quality(self): |
|
|
image = scale_and_crop(self.imagefile, (10, 10), quality=50) |
|
|
|
|
|
|
|
|
image = scale_and_crop_single(self.image, (10, 10), quality=50) |
|
|
self.assertEqual(image.info.get('quality'), 50) |
|
|
self.assertEqual(image.info.get('quality'), 50) |