comparison mysite/photologue/tests.py @ 1:0dcfcdf50c62

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