bgneal@1: import os bgneal@1: import unittest bgneal@1: from django.conf import settings bgneal@1: from django.core.files.base import ContentFile bgneal@1: from django.test import TestCase bgneal@1: bgneal@1: from models import * bgneal@1: bgneal@1: # Path to sample image bgneal@1: RES_DIR = os.path.join(os.path.dirname(__file__), 'res') bgneal@1: LANDSCAPE_IMAGE_PATH = os.path.join(RES_DIR, 'test_landscape.jpg') bgneal@1: PORTRAIT_IMAGE_PATH = os.path.join(RES_DIR, 'test_portrait.jpg') bgneal@1: SQUARE_IMAGE_PATH = os.path.join(RES_DIR, 'test_square.jpg') bgneal@1: bgneal@1: bgneal@1: class TestPhoto(ImageModel): bgneal@1: """ Minimal ImageModel class for testing """ bgneal@1: name = models.CharField(max_length=30) bgneal@1: bgneal@1: bgneal@1: class PLTest(TestCase): bgneal@1: """ Base TestCase class """ bgneal@1: def setUp(self): bgneal@1: self.s = PhotoSize(name='test', width=100, height=100) bgneal@1: self.s.save() bgneal@1: self.pl = TestPhoto(name='landscape') bgneal@1: self.pl.image.save(os.path.basename(LANDSCAPE_IMAGE_PATH), bgneal@1: ContentFile(open(LANDSCAPE_IMAGE_PATH, 'rb').read())) bgneal@1: self.pl.save() bgneal@1: bgneal@1: def tearDown(self): bgneal@1: path = self.pl.image.path bgneal@1: self.pl.delete() bgneal@1: self.failIf(os.path.isfile(path)) bgneal@1: self.s.delete() bgneal@1: bgneal@1: bgneal@1: class PhotoTest(PLTest): bgneal@1: def test_new_photo(self): bgneal@1: self.assertEqual(TestPhoto.objects.count(), 1) bgneal@1: self.failUnless(os.path.isfile(self.pl.image.path)) bgneal@1: self.assertEqual(os.path.getsize(self.pl.image.path), bgneal@1: os.path.getsize(LANDSCAPE_IMAGE_PATH)) bgneal@1: bgneal@1: def test_exif(self): bgneal@1: self.assert_(len(self.pl.EXIF.keys()) > 0) bgneal@1: bgneal@1: def test_paths(self): bgneal@1: self.assertEqual(os.path.normpath(str(self.pl.cache_path())).lower(), bgneal@1: os.path.normpath(os.path.join(settings.MEDIA_ROOT, bgneal@1: PHOTOLOGUE_DIR, bgneal@1: 'photos', bgneal@1: 'cache')).lower()) bgneal@1: self.assertEqual(self.pl.cache_url(), bgneal@1: settings.MEDIA_URL + PHOTOLOGUE_DIR + '/photos/cache') bgneal@1: bgneal@1: def test_count(self): bgneal@1: for i in range(5): bgneal@1: self.pl.get_test_url() bgneal@1: self.assertEquals(self.pl.view_count, 0) bgneal@1: self.s.increment_count = True bgneal@1: self.s.save() bgneal@1: for i in range(5): bgneal@1: self.pl.get_test_url() bgneal@1: self.assertEquals(self.pl.view_count, 5) bgneal@1: bgneal@1: def test_precache(self): bgneal@1: # set the thumbnail photo size to pre-cache bgneal@1: self.s.pre_cache = True bgneal@1: self.s.save() bgneal@1: # make sure it created the file bgneal@1: self.failUnless(os.path.isfile(self.pl.get_test_filename())) bgneal@1: self.s.pre_cache = False bgneal@1: self.s.save() bgneal@1: # clear the cache and make sure the file's deleted bgneal@1: self.pl.clear_cache() bgneal@1: self.failIf(os.path.isfile(self.pl.get_test_filename())) bgneal@1: bgneal@1: def test_accessor_methods(self): bgneal@1: self.assertEquals(self.pl.get_test_photosize(), self.s) bgneal@1: self.assertEquals(self.pl.get_test_size(), bgneal@1: Image.open(self.pl.get_test_filename()).size) bgneal@1: self.assertEquals(self.pl.get_test_url(), bgneal@1: self.pl.cache_url() + '/' + \ bgneal@1: self.pl._get_filename_for_size(self.s)) bgneal@1: self.assertEquals(self.pl.get_test_filename(), bgneal@1: os.path.join(self.pl.cache_path(), bgneal@1: self.pl._get_filename_for_size(self.s))) bgneal@1: bgneal@1: bgneal@1: class ImageResizeTest(PLTest): bgneal@1: def setUp(self): bgneal@1: super(ImageResizeTest, self).setUp() bgneal@1: self.pp = TestPhoto(name='portrait') bgneal@1: self.pp.image.save(os.path.basename(PORTRAIT_IMAGE_PATH), bgneal@1: ContentFile(open(PORTRAIT_IMAGE_PATH, 'rb').read())) bgneal@1: self.pp.save() bgneal@1: self.ps = TestPhoto(name='square') bgneal@1: self.ps.image.save(os.path.basename(SQUARE_IMAGE_PATH), bgneal@1: ContentFile(open(SQUARE_IMAGE_PATH, 'rb').read())) bgneal@1: self.ps.save() bgneal@1: bgneal@1: def tearDown(self): bgneal@1: super(ImageResizeTest, self).tearDown() bgneal@1: self.pp.delete() bgneal@1: self.ps.delete() bgneal@1: bgneal@1: def test_resize_to_fit(self): bgneal@1: self.assertEquals(self.pl.get_test_size(), (100, 75)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (75, 100)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (100, 100)) bgneal@1: bgneal@1: def test_resize_to_fit_width(self): bgneal@1: self.s.size = (100, 0) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (100, 75)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (100, 133)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (100, 100)) bgneal@1: bgneal@1: def test_resize_to_fit_width_enlarge(self): bgneal@1: self.s.size = (2000, 0) bgneal@1: self.s.upscale = True bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (2000, 1500)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (2000, 2667)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (2000, 2000)) bgneal@1: bgneal@1: def test_resize_to_fit_height(self): bgneal@1: self.s.size = (0, 100) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (133, 100)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (75, 100)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (100, 100)) bgneal@1: bgneal@1: def test_resize_to_fit_height_enlarge(self): bgneal@1: self.s.size = (0, 2000) bgneal@1: self.s.upscale = True bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (2667, 2000)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (1500, 2000)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (2000, 2000)) bgneal@1: bgneal@1: def test_resize_and_crop(self): bgneal@1: self.s.crop = True bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), self.s.size) bgneal@1: self.assertEquals(self.pp.get_test_size(), self.s.size) bgneal@1: self.assertEquals(self.ps.get_test_size(), self.s.size) bgneal@1: bgneal@1: def test_resize_rounding_to_fit(self): bgneal@1: self.s.size = (113, 113) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (113, 85)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (85, 113)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (113, 113)) bgneal@1: bgneal@1: def test_resize_rounding_cropped(self): bgneal@1: self.s.size = (113, 113) bgneal@1: self.s.crop = True bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), self.s.size) bgneal@1: self.assertEquals(self.pp.get_test_size(), self.s.size) bgneal@1: self.assertEquals(self.ps.get_test_size(), self.s.size) bgneal@1: bgneal@1: def test_resize_one_dimension_width(self): bgneal@1: self.s.size = (1500, 1200) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (1500, 1125)) bgneal@1: bgneal@1: def test_resize_one_dimension_height(self): bgneal@1: self.s.size = (1600, 1100) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (1467, 1100)) bgneal@1: bgneal@1: def test_resize_no_upscale(self): bgneal@1: self.s.size = (2000, 2000) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (1600, 1200)) bgneal@1: bgneal@1: def test_resize_no_upscale_mixed_height(self): bgneal@1: self.s.size = (3200, 600) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (800, 600)) bgneal@1: bgneal@1: def test_resize_no_upscale_mixed_width(self): bgneal@1: self.s.size = (800, 2400) bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (800, 600)) bgneal@1: bgneal@1: def test_resize_no_upscale_crop(self): bgneal@1: self.s.size = (2000, 2000) bgneal@1: self.s.crop = True bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (2000, 2000)) bgneal@1: bgneal@1: def test_resize_upscale(self): bgneal@1: self.s.size = (2000, 2000) bgneal@1: self.s.upscale = True bgneal@1: self.s.save() bgneal@1: self.assertEquals(self.pl.get_test_size(), (2000, 1500)) bgneal@1: self.assertEquals(self.pp.get_test_size(), (1500, 2000)) bgneal@1: self.assertEquals(self.ps.get_test_size(), (2000, 2000)) bgneal@1: bgneal@1: bgneal@1: class PhotoEffectTest(PLTest): bgneal@1: def test(self): bgneal@1: effect = PhotoEffect(name='test') bgneal@1: im = Image.open(self.pl.image.path) bgneal@1: self.assert_(isinstance(effect.pre_process(im), Image.Image)) bgneal@1: self.assert_(isinstance(effect.post_process(im), Image.Image)) bgneal@1: self.assert_(isinstance(effect.process(im), Image.Image)) bgneal@1: bgneal@1: bgneal@1: class PhotoSizeCacheTest(PLTest): bgneal@1: def test(self): bgneal@1: cache = PhotoSizeCache() bgneal@1: self.assertEqual(cache.sizes['test'], self.s) bgneal@1: