django-imagekit/imagekit/tests.py

87 lines
2.4 KiB
Python
Raw Normal View History

import os
import tempfile
2008-12-31 17:54:47 +00:00
import unittest
from django.conf import settings
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
from imagekit import processors
2009-01-14 16:09:17 +00:00
from imagekit.models import ImageModel
2009-01-08 21:11:15 +00:00
from imagekit.specs import ImageSpec
from imagekit.lib import Image
2008-12-31 17:54:47 +00:00
class ResizeToWidth(processors.Resize):
width = 100
2009-01-06 19:19:10 +00:00
class ResizeToHeight(processors.Resize):
height = 100
class ResizeToFit(processors.Resize):
width = 100
height = 100
2009-01-06 19:19:10 +00:00
class ResizeCropped(ResizeToFit):
crop = ('center', 'center')
class TestResizeToWidth(ImageSpec):
access_as = 'to_width'
processors = [ResizeToWidth]
2009-01-06 19:19:10 +00:00
class TestResizeToHeight(ImageSpec):
access_as = 'to_height'
processors = [ResizeToHeight]
class TestResizeCropped(ImageSpec):
access_as = 'cropped'
processors = [ResizeCropped]
2009-01-14 16:09:17 +00:00
class TestPhoto(ImageModel):
2008-12-31 17:54:47 +00:00
""" Minimal ImageModel class for testing """
2009-01-08 18:37:15 +00:00
image = models.ImageField(upload_to='images')
2009-02-03 13:50:54 +00:00
class IKOptions:
spec_module = 'imagekit.tests'
class IKTest(TestCase):
""" Base TestCase class """
2008-12-31 17:54:47 +00:00
def setUp(self):
# create a test image using tempfile and PIL
self.tmp = tempfile.TemporaryFile()
Image.new('RGB', (800, 600)).save(self.tmp, 'JPEG')
self.tmp.seek(0)
self.p = TestPhoto()
self.p.image.save(os.path.basename('test.jpg'),
ContentFile(self.tmp.read()))
self.p.save()
# destroy temp file
self.tmp.close()
def test_setup(self):
self.assertEqual(self.p.image.width, 800)
self.assertEqual(self.p.image.height, 600)
2008-12-31 17:54:47 +00:00
def test_to_width(self):
self.assertEqual(self.p.to_width.width, 100)
self.assertEqual(self.p.to_width.height, 75)
2009-01-06 19:19:10 +00:00
def test_to_height(self):
self.assertEqual(self.p.to_height.width, 133)
self.assertEqual(self.p.to_height.height, 100)
def test_crop(self):
self.assertEqual(self.p.cropped.width, 100)
self.assertEqual(self.p.cropped.height, 100)
2008-12-31 17:54:47 +00:00
def test_url(self):
tup = (settings.MEDIA_URL, self.p._ik.cache_dir, 'test_to_width.jpg')
self.assertEqual(self.p.to_width.url, "%s%s/%s" % tup)
2008-12-31 17:54:47 +00:00
def tearDown(self):
# make sure image file is deleted
path = self.p.image.path
self.p.delete()
self.failIf(os.path.isfile(path))