2009-01-04 16:29:53 +00:00
|
|
|
import os
|
2009-01-06 19:00:53 +00:00
|
|
|
import tempfile
|
2008-12-31 17:54:47 +00:00
|
|
|
import unittest
|
2009-01-04 16:29:53 +00:00
|
|
|
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
|
|
|
|
|
|
2009-01-04 18:40:22 +00:00
|
|
|
from models import IKModel
|
|
|
|
|
from specs import ImageSpec
|
2009-01-06 19:00:53 +00:00
|
|
|
from imagekit import processors
|
2008-12-31 17:54:47 +00:00
|
|
|
|
2009-01-04 22:14:13 +00:00
|
|
|
from imagekit import Image
|
2008-12-31 17:54:47 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
|
|
|
|
|
class ResizeToWidth(processors.Resize):
|
|
|
|
|
width = 100
|
|
|
|
|
|
|
|
|
|
class ResizeToHeigh(processors.Resize):
|
|
|
|
|
height = 100
|
|
|
|
|
|
|
|
|
|
class ResizeToFit(processors.Resize):
|
|
|
|
|
width = 100
|
|
|
|
|
height = 100
|
|
|
|
|
|
|
|
|
|
class ResizeCrop(ResizeToFit):
|
|
|
|
|
crop = True
|
|
|
|
|
|
|
|
|
|
class TestResizeToWidth(ImageSpec):
|
|
|
|
|
access_as = 'to_width'
|
|
|
|
|
processors = [ResizeToWidth]
|
|
|
|
|
|
|
|
|
|
|
2009-01-04 22:14:13 +00:00
|
|
|
IMG_PATH = os.path.join(os.path.dirname(__file__), 'test.jpg')
|
2009-01-04 16:29:53 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
|
2008-12-31 17:54:47 +00:00
|
|
|
class TestPhoto(IKModel):
|
|
|
|
|
""" Minimal ImageModel class for testing """
|
2009-01-06 19:00:53 +00:00
|
|
|
image = models.ImageField(upload_to='images')
|
|
|
|
|
|
|
|
|
|
class IKConfig:
|
|
|
|
|
config_module = 'imagekit.tests'
|
|
|
|
|
|
2009-01-04 16:29:53 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
class IKTest(TestCase):
|
2009-01-04 16:29:53 +00:00
|
|
|
""" Base TestCase class """
|
2008-12-31 17:54:47 +00:00
|
|
|
def setUp(self):
|
2009-01-06 19:00:53 +00:00
|
|
|
# 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()))
|
2009-01-04 16:29:53 +00:00
|
|
|
self.p.save()
|
2009-01-06 19:00:53 +00:00
|
|
|
# destroy temp file
|
|
|
|
|
self.tmp.close()
|
|
|
|
|
|
|
|
|
|
def test_config(self):
|
|
|
|
|
self.assertEqual(self.p._ik.specs, [TestResizeToWidth])
|
2009-01-04 16:29:53 +00:00
|
|
|
|
|
|
|
|
def test_setup(self):
|
2009-01-06 19:00:53 +00:00
|
|
|
self.assertEqual(self.p.image.width, 800)
|
|
|
|
|
self.assertEqual(self.p.image.height, 600)
|
2008-12-31 17:54:47 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
def test_to_width(self):
|
|
|
|
|
self.assertEqual(self.p.to_width.width, 100)
|
|
|
|
|
self.assertEqual(self.p.to_width.height, 75)
|
2008-12-31 17:54:47 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
def test_url(self):
|
|
|
|
|
url = "%s/%s" % (self.p.cache_url, 'test_to_width.jpg')
|
|
|
|
|
self.assertEqual(self.p.to_width.url, url)
|
|
|
|
|
|
2008-12-31 17:54:47 +00:00
|
|
|
def tearDown(self):
|
2009-01-06 19:00:53 +00:00
|
|
|
# make sure image file is deleted
|
2009-01-04 16:29:53 +00:00
|
|
|
path = self.p.image.path
|
|
|
|
|
self.p.delete()
|
|
|
|
|
self.failIf(os.path.isfile(path))
|