annotate photologue/tests.py @ 71:e2868ad47a1e

For Django 1.4, using the new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Apr 2012 16:40:29 -0500
parents madeira/photologue/tests.py@63e4211628e1
children
rev   line source
bgneal@1 1 import os
bgneal@1 2 import unittest
bgneal@1 3 from django.conf import settings
bgneal@1 4 from django.core.files.base import ContentFile
bgneal@1 5 from django.test import TestCase
bgneal@1 6
bgneal@1 7 from models import *
bgneal@1 8
bgneal@1 9 # Path to sample image
bgneal@1 10 RES_DIR = os.path.join(os.path.dirname(__file__), 'res')
bgneal@1 11 LANDSCAPE_IMAGE_PATH = os.path.join(RES_DIR, 'test_landscape.jpg')
bgneal@1 12 PORTRAIT_IMAGE_PATH = os.path.join(RES_DIR, 'test_portrait.jpg')
bgneal@1 13 SQUARE_IMAGE_PATH = os.path.join(RES_DIR, 'test_square.jpg')
bgneal@1 14
bgneal@1 15
bgneal@1 16 class TestPhoto(ImageModel):
bgneal@1 17 """ Minimal ImageModel class for testing """
bgneal@1 18 name = models.CharField(max_length=30)
bgneal@1 19
bgneal@1 20
bgneal@1 21 class PLTest(TestCase):
bgneal@1 22 """ Base TestCase class """
bgneal@1 23 def setUp(self):
bgneal@1 24 self.s = PhotoSize(name='test', width=100, height=100)
bgneal@1 25 self.s.save()
bgneal@1 26 self.pl = TestPhoto(name='landscape')
bgneal@1 27 self.pl.image.save(os.path.basename(LANDSCAPE_IMAGE_PATH),
bgneal@1 28 ContentFile(open(LANDSCAPE_IMAGE_PATH, 'rb').read()))
bgneal@1 29 self.pl.save()
bgneal@1 30
bgneal@1 31 def tearDown(self):
bgneal@1 32 path = self.pl.image.path
bgneal@1 33 self.pl.delete()
bgneal@1 34 self.failIf(os.path.isfile(path))
bgneal@1 35 self.s.delete()
bgneal@1 36
bgneal@1 37
bgneal@1 38 class PhotoTest(PLTest):
bgneal@1 39 def test_new_photo(self):
bgneal@1 40 self.assertEqual(TestPhoto.objects.count(), 1)
bgneal@1 41 self.failUnless(os.path.isfile(self.pl.image.path))
bgneal@1 42 self.assertEqual(os.path.getsize(self.pl.image.path),
bgneal@1 43 os.path.getsize(LANDSCAPE_IMAGE_PATH))
bgneal@1 44
bgneal@1 45 def test_exif(self):
bgneal@1 46 self.assert_(len(self.pl.EXIF.keys()) > 0)
bgneal@1 47
bgneal@1 48 def test_paths(self):
bgneal@1 49 self.assertEqual(os.path.normpath(str(self.pl.cache_path())).lower(),
bgneal@1 50 os.path.normpath(os.path.join(settings.MEDIA_ROOT,
bgneal@1 51 PHOTOLOGUE_DIR,
bgneal@1 52 'photos',
bgneal@1 53 'cache')).lower())
bgneal@1 54 self.assertEqual(self.pl.cache_url(),
bgneal@1 55 settings.MEDIA_URL + PHOTOLOGUE_DIR + '/photos/cache')
bgneal@1 56
bgneal@1 57 def test_count(self):
bgneal@1 58 for i in range(5):
bgneal@1 59 self.pl.get_test_url()
bgneal@1 60 self.assertEquals(self.pl.view_count, 0)
bgneal@1 61 self.s.increment_count = True
bgneal@1 62 self.s.save()
bgneal@1 63 for i in range(5):
bgneal@1 64 self.pl.get_test_url()
bgneal@1 65 self.assertEquals(self.pl.view_count, 5)
bgneal@1 66
bgneal@1 67 def test_precache(self):
bgneal@1 68 # set the thumbnail photo size to pre-cache
bgneal@1 69 self.s.pre_cache = True
bgneal@1 70 self.s.save()
bgneal@1 71 # make sure it created the file
bgneal@1 72 self.failUnless(os.path.isfile(self.pl.get_test_filename()))
bgneal@1 73 self.s.pre_cache = False
bgneal@1 74 self.s.save()
bgneal@1 75 # clear the cache and make sure the file's deleted
bgneal@1 76 self.pl.clear_cache()
bgneal@1 77 self.failIf(os.path.isfile(self.pl.get_test_filename()))
bgneal@1 78
bgneal@1 79 def test_accessor_methods(self):
bgneal@1 80 self.assertEquals(self.pl.get_test_photosize(), self.s)
bgneal@1 81 self.assertEquals(self.pl.get_test_size(),
bgneal@1 82 Image.open(self.pl.get_test_filename()).size)
bgneal@1 83 self.assertEquals(self.pl.get_test_url(),
bgneal@1 84 self.pl.cache_url() + '/' + \
bgneal@1 85 self.pl._get_filename_for_size(self.s))
bgneal@1 86 self.assertEquals(self.pl.get_test_filename(),
bgneal@1 87 os.path.join(self.pl.cache_path(),
bgneal@1 88 self.pl._get_filename_for_size(self.s)))
bgneal@1 89
bgneal@1 90
bgneal@1 91 class ImageResizeTest(PLTest):
bgneal@1 92 def setUp(self):
bgneal@1 93 super(ImageResizeTest, self).setUp()
bgneal@1 94 self.pp = TestPhoto(name='portrait')
bgneal@1 95 self.pp.image.save(os.path.basename(PORTRAIT_IMAGE_PATH),
bgneal@1 96 ContentFile(open(PORTRAIT_IMAGE_PATH, 'rb').read()))
bgneal@1 97 self.pp.save()
bgneal@1 98 self.ps = TestPhoto(name='square')
bgneal@1 99 self.ps.image.save(os.path.basename(SQUARE_IMAGE_PATH),
bgneal@1 100 ContentFile(open(SQUARE_IMAGE_PATH, 'rb').read()))
bgneal@1 101 self.ps.save()
bgneal@1 102
bgneal@1 103 def tearDown(self):
bgneal@1 104 super(ImageResizeTest, self).tearDown()
bgneal@1 105 self.pp.delete()
bgneal@1 106 self.ps.delete()
bgneal@1 107
bgneal@1 108 def test_resize_to_fit(self):
bgneal@1 109 self.assertEquals(self.pl.get_test_size(), (100, 75))
bgneal@1 110 self.assertEquals(self.pp.get_test_size(), (75, 100))
bgneal@1 111 self.assertEquals(self.ps.get_test_size(), (100, 100))
bgneal@1 112
bgneal@1 113 def test_resize_to_fit_width(self):
bgneal@1 114 self.s.size = (100, 0)
bgneal@1 115 self.s.save()
bgneal@1 116 self.assertEquals(self.pl.get_test_size(), (100, 75))
bgneal@1 117 self.assertEquals(self.pp.get_test_size(), (100, 133))
bgneal@1 118 self.assertEquals(self.ps.get_test_size(), (100, 100))
bgneal@1 119
bgneal@1 120 def test_resize_to_fit_width_enlarge(self):
bgneal@1 121 self.s.size = (2000, 0)
bgneal@1 122 self.s.upscale = True
bgneal@1 123 self.s.save()
bgneal@1 124 self.assertEquals(self.pl.get_test_size(), (2000, 1500))
bgneal@1 125 self.assertEquals(self.pp.get_test_size(), (2000, 2667))
bgneal@1 126 self.assertEquals(self.ps.get_test_size(), (2000, 2000))
bgneal@1 127
bgneal@1 128 def test_resize_to_fit_height(self):
bgneal@1 129 self.s.size = (0, 100)
bgneal@1 130 self.s.save()
bgneal@1 131 self.assertEquals(self.pl.get_test_size(), (133, 100))
bgneal@1 132 self.assertEquals(self.pp.get_test_size(), (75, 100))
bgneal@1 133 self.assertEquals(self.ps.get_test_size(), (100, 100))
bgneal@1 134
bgneal@1 135 def test_resize_to_fit_height_enlarge(self):
bgneal@1 136 self.s.size = (0, 2000)
bgneal@1 137 self.s.upscale = True
bgneal@1 138 self.s.save()
bgneal@1 139 self.assertEquals(self.pl.get_test_size(), (2667, 2000))
bgneal@1 140 self.assertEquals(self.pp.get_test_size(), (1500, 2000))
bgneal@1 141 self.assertEquals(self.ps.get_test_size(), (2000, 2000))
bgneal@1 142
bgneal@1 143 def test_resize_and_crop(self):
bgneal@1 144 self.s.crop = True
bgneal@1 145 self.s.save()
bgneal@1 146 self.assertEquals(self.pl.get_test_size(), self.s.size)
bgneal@1 147 self.assertEquals(self.pp.get_test_size(), self.s.size)
bgneal@1 148 self.assertEquals(self.ps.get_test_size(), self.s.size)
bgneal@1 149
bgneal@1 150 def test_resize_rounding_to_fit(self):
bgneal@1 151 self.s.size = (113, 113)
bgneal@1 152 self.s.save()
bgneal@1 153 self.assertEquals(self.pl.get_test_size(), (113, 85))
bgneal@1 154 self.assertEquals(self.pp.get_test_size(), (85, 113))
bgneal@1 155 self.assertEquals(self.ps.get_test_size(), (113, 113))
bgneal@1 156
bgneal@1 157 def test_resize_rounding_cropped(self):
bgneal@1 158 self.s.size = (113, 113)
bgneal@1 159 self.s.crop = True
bgneal@1 160 self.s.save()
bgneal@1 161 self.assertEquals(self.pl.get_test_size(), self.s.size)
bgneal@1 162 self.assertEquals(self.pp.get_test_size(), self.s.size)
bgneal@1 163 self.assertEquals(self.ps.get_test_size(), self.s.size)
bgneal@1 164
bgneal@1 165 def test_resize_one_dimension_width(self):
bgneal@1 166 self.s.size = (1500, 1200)
bgneal@1 167 self.s.save()
bgneal@1 168 self.assertEquals(self.pl.get_test_size(), (1500, 1125))
bgneal@1 169
bgneal@1 170 def test_resize_one_dimension_height(self):
bgneal@1 171 self.s.size = (1600, 1100)
bgneal@1 172 self.s.save()
bgneal@1 173 self.assertEquals(self.pl.get_test_size(), (1467, 1100))
bgneal@1 174
bgneal@1 175 def test_resize_no_upscale(self):
bgneal@1 176 self.s.size = (2000, 2000)
bgneal@1 177 self.s.save()
bgneal@1 178 self.assertEquals(self.pl.get_test_size(), (1600, 1200))
bgneal@1 179
bgneal@1 180 def test_resize_no_upscale_mixed_height(self):
bgneal@1 181 self.s.size = (3200, 600)
bgneal@1 182 self.s.save()
bgneal@1 183 self.assertEquals(self.pl.get_test_size(), (800, 600))
bgneal@1 184
bgneal@1 185 def test_resize_no_upscale_mixed_width(self):
bgneal@1 186 self.s.size = (800, 2400)
bgneal@1 187 self.s.save()
bgneal@1 188 self.assertEquals(self.pl.get_test_size(), (800, 600))
bgneal@1 189
bgneal@1 190 def test_resize_no_upscale_crop(self):
bgneal@1 191 self.s.size = (2000, 2000)
bgneal@1 192 self.s.crop = True
bgneal@1 193 self.s.save()
bgneal@1 194 self.assertEquals(self.pl.get_test_size(), (2000, 2000))
bgneal@1 195
bgneal@1 196 def test_resize_upscale(self):
bgneal@1 197 self.s.size = (2000, 2000)
bgneal@1 198 self.s.upscale = True
bgneal@1 199 self.s.save()
bgneal@1 200 self.assertEquals(self.pl.get_test_size(), (2000, 1500))
bgneal@1 201 self.assertEquals(self.pp.get_test_size(), (1500, 2000))
bgneal@1 202 self.assertEquals(self.ps.get_test_size(), (2000, 2000))
bgneal@1 203
bgneal@1 204
bgneal@1 205 class PhotoEffectTest(PLTest):
bgneal@1 206 def test(self):
bgneal@1 207 effect = PhotoEffect(name='test')
bgneal@1 208 im = Image.open(self.pl.image.path)
bgneal@1 209 self.assert_(isinstance(effect.pre_process(im), Image.Image))
bgneal@1 210 self.assert_(isinstance(effect.post_process(im), Image.Image))
bgneal@1 211 self.assert_(isinstance(effect.process(im), Image.Image))
bgneal@1 212
bgneal@1 213
bgneal@1 214 class PhotoSizeCacheTest(PLTest):
bgneal@1 215 def test(self):
bgneal@1 216 cache = PhotoSizeCache()
bgneal@1 217 self.assertEqual(cache.sizes['test'], self.s)
bgneal@1 218