django-imagekit/imagekit/tests.py

55 lines
1.8 KiB
Python
Raw Normal View History

import os
import tempfile
2011-09-26 20:38:55 +00:00
import Image
from django.core.files.base import ContentFile
2009-01-04 22:14:13 +00:00
from django.db import models
2008-12-31 17:54:47 +00:00
from django.test import TestCase
2011-09-26 20:38:55 +00:00
from imagekit.models import ImageSpec, AdminThumbnailView
from imagekit.processors.resize import Crop
from imagekit.processors import Adjust
2008-12-31 17:54:47 +00:00
2011-09-26 20:38:55 +00:00
class Photo(models.Model):
original_image = models.ImageField(upload_to='photos')
thumbnail = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), Crop(50, 50)],
image_field='original_image', format='JPEG', quality=90)
admin_thumbnail_view = AdminThumbnailView(image_field='thumbnail')
class IKTest(TestCase):
""" Base TestCase class """
2009-09-02 18:20:43 +00:00
def generate_image(self):
tmp = tempfile.TemporaryFile()
Image.new('RGB', (800, 600)).save(tmp, 'JPEG')
tmp.seek(0)
return tmp
2011-09-26 20:38:55 +00:00
2008-12-31 17:54:47 +00:00
def setUp(self):
2011-09-26 20:38:55 +00:00
self.photo = Photo()
2009-09-02 18:20:43 +00:00
img = self.generate_image()
2011-09-26 20:38:55 +00:00
file = ContentFile(img.read())
self.photo.original_image = file
self.photo.original_image.save('test.jpeg', file)
self.photo.save()
2009-09-02 18:20:43 +00:00
img.close()
2011-09-26 20:38:55 +00:00
2009-09-02 18:20:43 +00:00
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)
def test_thumbnail_creation(self):
photo = Photo.objects.get(id=self.photo.id)
self.assertTrue(os.path.isfile(photo.thumbnail.file.name))
def test_thumbnail_size(self):
self.assertEqual(self.photo.thumbnail.width, 50)
self.assertEqual(self.photo.thumbnail.height, 50)
def test_thumbnail_source_file(self):
self.assertEqual(
self.photo.thumbnail.source_file, self.photo.original_image)