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
|
2008-12-31 17:54:47 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
2009-01-08 21:11:15 +00:00
|
|
|
from imagekit.lib import Image
|
2008-12-31 17:54:47 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
|
|
|
|
|
class IKTest(TestCase):
|
2009-01-04 16:29:53 +00:00
|
|
|
""" 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
|
2009-12-19 16:01:54 +00:00
|
|
|
|
2008-12-31 17:54:47 +00:00
|
|
|
def setUp(self):
|
2009-01-06 19:00:53 +00:00
|
|
|
self.p = TestPhoto()
|
2009-09-02 18:20:43 +00:00
|
|
|
img = self.generate_image()
|
|
|
|
|
self.p.save_image('test.jpeg', ContentFile(img.read()))
|
2009-01-04 16:29:53 +00:00
|
|
|
self.p.save()
|
2009-09-02 18:20:43 +00:00
|
|
|
img.close()
|
2009-12-19 16:01:54 +00:00
|
|
|
|
2009-09-02 18:20:43 +00:00
|
|
|
def test_save_image(self):
|
|
|
|
|
img = self.generate_image()
|
|
|
|
|
path = self.p.image.path
|
|
|
|
|
self.p.save_image('test2.jpeg', ContentFile(img.read()))
|
|
|
|
|
self.failIf(os.path.isfile(path))
|
|
|
|
|
path = self.p.image.path
|
|
|
|
|
img.seek(0)
|
|
|
|
|
self.p.save_image('test.jpeg', ContentFile(img.read()))
|
|
|
|
|
self.failIf(os.path.isfile(path))
|
|
|
|
|
img.close()
|
2009-12-19 16:01:54 +00:00
|
|
|
|
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)
|
2009-12-19 16:01:54 +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)
|
2009-12-19 16:01:54 +00:00
|
|
|
|
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)
|
2009-12-19 16:01:54 +00:00
|
|
|
|
2009-01-06 19:19:10 +00:00
|
|
|
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
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
def test_url(self):
|
2009-07-19 19:26:53 +00:00
|
|
|
tup = (settings.MEDIA_URL, self.p._ik.cache_dir,
|
|
|
|
|
'images/test_to_width.jpeg')
|
2009-01-08 17:45:06 +00:00
|
|
|
self.assertEqual(self.p.to_width.url, "%s%s/%s" % tup)
|
2009-12-19 16:01:54 +00:00
|
|
|
|
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))
|