Complete serialization with pretty printing. Works both in FF and Webkit. This fixes...
[redakcja.git] / apps / sorl / thumbnail / tests / base.py
1 import unittest
2 import os
3 from PIL import Image
4 from django.conf import settings
5 from sorl.thumbnail.base import Thumbnail
6
7 try:
8     set
9 except NameError:
10     from sets import Set as set     # For Python 2.3
11
12
13 def get_default_settings():
14     from sorl.thumbnail import defaults
15     def_settings = {}
16     for key in dir(defaults):
17         if key == key.upper() and key not in ['WVPS', 'CONVERT']:
18             def_settings[key] = getattr(defaults, key)
19     return def_settings
20
21
22 DEFAULT_THUMBNAIL_SETTINGS = get_default_settings()
23 RELATIVE_PIC_NAME = "sorl-thumbnail-test_source.jpg"
24 PIC_NAME = os.path.join(settings.MEDIA_ROOT, RELATIVE_PIC_NAME)
25 THUMB_NAME = os.path.join(settings.MEDIA_ROOT, "sorl-thumbnail-test_%02d.jpg")
26 PIC_SIZE = (800, 600)
27
28
29 class ChangeSettings:
30     def __init__(self):
31         self.default_settings = DEFAULT_THUMBNAIL_SETTINGS.copy()
32
33     def change(self, override=None):
34         if override is not None:
35             self.default_settings.update(override)
36         for setting, default in self.default_settings.items():
37             settings_s = 'THUMBNAIL_%s' % setting
38             self_s = 'original_%s' % setting
39             if hasattr(settings, settings_s) and not hasattr(self, self_s):
40                 setattr(self, self_s, getattr(settings, settings_s))
41             if hasattr(settings, settings_s) or \
42                default != DEFAULT_THUMBNAIL_SETTINGS[setting]:
43                 setattr(settings, settings_s, default)
44
45     def revert(self):
46         for setting in self.default_settings:
47             settings_s = 'THUMBNAIL_%s' % setting
48             self_s = 'original_%s' % setting
49             if hasattr(self, self_s):
50                 setattr(settings, settings_s, getattr(self, self_s))
51                 delattr(self, self_s)
52
53
54 class BaseTest(unittest.TestCase):
55     def setUp(self):
56         self.images_to_delete = set()
57         # Create the test image
58         Image.new('RGB', PIC_SIZE).save(PIC_NAME, 'JPEG')
59         self.images_to_delete.add(PIC_NAME)
60         # Change settings so we know they will be constant
61         self.change_settings = ChangeSettings()
62         self.change_settings.change()
63
64     def verify_thumbnail(self, expected_size, thumbnail=None,
65                          expected_filename=None, expected_mode=None):
66         assert thumbnail is not None or expected_filename is not None, \
67             'verify_thumbnail should be passed at least a thumbnail or an' \
68             'expected filename.'
69
70         if thumbnail is not None:
71             # Verify that the templatetag method returned a Thumbnail instance
72             self.assertTrue(isinstance(thumbnail, Thumbnail))
73             thumb_name = thumbnail.dest
74         else:
75             thumb_name = expected_filename
76
77         if isinstance(thumb_name, basestring):
78             # Verify that the thumbnail file exists
79             self.assert_(os.path.isfile(thumb_name),
80                          'Thumbnail file not found')
81
82             # Remember to delete the file
83             self.images_to_delete.add(thumb_name)
84
85             # If we got an expected_filename, check that it is right
86             if expected_filename is not None and thumbnail is not None:
87                 self.assertEqual(thumbnail.dest, expected_filename)
88
89         image = Image.open(thumb_name)
90
91         # Verify the thumbnail has the expected dimensions
92         self.assertEqual(image.size, expected_size)
93
94         if expected_mode is not None:
95             self.assertEqual(image.mode, expected_mode)
96
97     def tearDown(self):
98         # Remove all the files that have been created
99         for image in self.images_to_delete:
100             try:
101                 os.remove(image)
102             except:
103                 pass
104         # Change settings back to original
105         self.change_settings.revert()