django-imagekit/tests/core/tests.py

112 lines
3.7 KiB
Python
Raw Normal View History

from __future__ import with_statement
import os
import tempfile
2011-10-31 14:23:50 +00:00
from django.core.files.base import ContentFile
2009-01-04 22:14:13 +00:00
from django.db import models
from django.test import TestCase
2011-10-31 14:23:50 +00:00
from imagekit import utils
2011-10-31 14:23:50 +00:00
from imagekit.lib import Image
from imagekit.models import ImageSpecField
2011-09-26 20:38:55 +00:00
from imagekit.processors import Adjust
2012-02-11 01:46:16 +00:00
from imagekit.processors.resize import Fill
from imagekit.processors.crop import SmartCrop
2011-09-26 20:38:55 +00:00
class Photo(models.Model):
original_image = models.ImageField(upload_to='photos')
thumbnail = ImageSpecField([Adjust(contrast=1.2, sharpness=1.1),
2012-02-11 01:46:16 +00:00
Fill(50, 50)], image_field='original_image', format='JPEG',
options={'quality': 90})
smartcropped_thumbnail = ImageSpecField([Adjust(contrast=1.2,
sharpness=1.1), SmartCrop(50, 50)], image_field='original_image',
format='JPEG', options={'quality': 90})
class IKTest(TestCase):
def generate_image(self):
tmp = tempfile.TemporaryFile()
Image.new('RGB', (800, 600)).save(tmp, 'JPEG')
tmp.seek(0)
return tmp
def generate_lenna(self):
"""
See also:
http://en.wikipedia.org/wiki/Lenna
http://sipi.usc.edu/database/database.php?volume=misc&image=12
"""
tmp = tempfile.TemporaryFile()
lennapath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', 'lenna-800x600-white-border.jpg')
with open(lennapath, "r+b") as lennafile:
Image.open(lennafile).save(tmp, 'JPEG')
tmp.seek(0)
return tmp
2011-12-02 07:19:12 +00:00
def create_photo(self, name):
photo = Photo()
img = self.generate_lenna()
2011-09-26 20:38:55 +00:00
file = ContentFile(img.read())
2011-12-02 07:19:12 +00:00
photo.original_image = file
photo.original_image.save(name, file)
photo.save()
img.close()
2011-12-02 07:19:12 +00:00
return photo
2011-12-02 07:19:12 +00:00
def setUp(self):
self.photo = self.create_photo('test.jpg')
2011-12-02 07:19:27 +00:00
def test_nodelete(self):
"""Don't delete the spec file when the source image hasn't changed.
"""
filename = self.photo.thumbnail.file.name
thumbnail_timestamp = os.path.getmtime(filename)
self.photo.save()
self.assertTrue(self.photo.thumbnail.storage.exists(filename))
def test_save_image(self):
2011-09-26 20:38:55 +00:00
photo = Photo.objects.get(id=self.photo.id)
self.assertTrue(os.path.isfile(photo.original_image.path))
def test_setup(self):
2011-09-26 20:38:55 +00:00
self.assertEqual(self.photo.original_image.width, 800)
self.assertEqual(self.photo.original_image.height, 600)
2011-09-26 20:38:55 +00:00
def test_thumbnail_creation(self):
photo = Photo.objects.get(id=self.photo.id)
self.assertTrue(os.path.isfile(photo.thumbnail.file.name))
2011-09-26 20:38:55 +00:00
def test_thumbnail_size(self):
""" Explicit and smart-cropped thumbnail size """
2011-09-26 20:38:55 +00:00
self.assertEqual(self.photo.thumbnail.width, 50)
self.assertEqual(self.photo.thumbnail.height, 50)
self.assertEqual(self.photo.smartcropped_thumbnail.width, 50)
self.assertEqual(self.photo.smartcropped_thumbnail.height, 50)
2011-09-26 20:38:55 +00:00
def test_thumbnail_source_file(self):
self.assertEqual(
self.photo.thumbnail.source_file, self.photo.original_image)
class IKUtilsTest(TestCase):
def test_extension_to_format(self):
self.assertEqual(utils.extension_to_format('.jpeg'), 'JPEG')
self.assertEqual(utils.extension_to_format('.rgba'), 'SGI')
with self.assertRaises(utils.UnknownExtensionError):
utils.extension_to_format('.txt')
def test_format_to_extension_no_init(self):
self.assertEqual(utils.format_to_extension('PNG'), '.png')
self.assertEqual(utils.format_to_extension('ICO'), '.ico')
with self.assertRaises(utils.UnknownFormatError):
utils.format_to_extension('TXT')