django-imagekit/imagekit/tests.py
Matthew Tretter b9aa69e0c0 AdminThumbnailView is now AdminThumbnail
I never liked that the "AdminThumbnailView" was supposed to be defined
on the model, but never looked into it.

This commit puts the definition back where it belongs: in the admin.
Instead of requiring you to add a field (with view logic) to your
model, you now just add a property to your admin class and specify
that property in the `list_display` list.
2011-10-02 21:58:08 -04:00

54 lines
No EOL
1.7 KiB
Python

import os
import tempfile
import Image
from django.core.files.base import ContentFile
from django.db import models
from django.test import TestCase
from imagekit.models import ImageSpec
from imagekit.processors.resize import Crop
from imagekit.processors import Adjust
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)
class IKTest(TestCase):
""" Base TestCase class """
def generate_image(self):
tmp = tempfile.TemporaryFile()
Image.new('RGB', (800, 600)).save(tmp, 'JPEG')
tmp.seek(0)
return tmp
def setUp(self):
self.photo = Photo()
img = self.generate_image()
file = ContentFile(img.read())
self.photo.original_image = file
self.photo.original_image.save('test.jpeg', file)
self.photo.save()
img.close()
def test_save_image(self):
photo = Photo.objects.get(id=self.photo.id)
self.assertTrue(os.path.isfile(photo.original_image.path))
def test_setup(self):
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)