1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | ## $Id: test_helpers.py 8466 2012-05-17 16:30:02Z uli $ |
---|
4 | ## |
---|
5 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
6 | ## This program is free software; you can redistribute it and/or modify |
---|
7 | ## it under the terms of the GNU General Public License as published by |
---|
8 | ## the Free Software Foundation; either version 2 of the License, or |
---|
9 | ## (at your option) any later version. |
---|
10 | ## |
---|
11 | ## This program is distributed in the hope that it will be useful, |
---|
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ## GNU General Public License for more details. |
---|
15 | ## |
---|
16 | ## You should have received a copy of the GNU General Public License |
---|
17 | ## along with this program; if not, write to the Free Software |
---|
18 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | ## |
---|
20 | |
---|
21 | import datetime |
---|
22 | import os |
---|
23 | import pytz |
---|
24 | import shutil |
---|
25 | import tempfile |
---|
26 | import unittest |
---|
27 | import doctest |
---|
28 | from cStringIO import StringIO |
---|
29 | from zope import schema |
---|
30 | from zope.interface import Interface, Attribute, implements |
---|
31 | from zope.security.testing import Principal, Participation |
---|
32 | from zope.security.management import newInteraction, endInteraction |
---|
33 | from waeup.kofa.utils import helpers |
---|
34 | |
---|
35 | class IFakeObject(Interface): |
---|
36 | """Some marker interface.""" |
---|
37 | |
---|
38 | class FakeObject(object): |
---|
39 | implements(IFakeObject) |
---|
40 | |
---|
41 | class RemoveFileOrDirectoryTestCase(unittest.TestCase): |
---|
42 | |
---|
43 | def setUp(self): |
---|
44 | self.dirpath = tempfile.mkdtemp() |
---|
45 | self.filepath = os.path.join(self.dirpath, 'somefile') |
---|
46 | self.non_file = os.path.join(self.dirpath, 'nonfile') |
---|
47 | open(self.filepath, 'wb').write('Hi!') |
---|
48 | return |
---|
49 | |
---|
50 | def tearDown(self): |
---|
51 | if os.path.exists(self.dirpath): |
---|
52 | shutil.rmtree(self.dirpath) |
---|
53 | return |
---|
54 | |
---|
55 | def test_handle_not_existing_path(self): |
---|
56 | result = helpers.remove_file_or_directory(self.non_file) |
---|
57 | self.assertTrue(result is None) |
---|
58 | return |
---|
59 | |
---|
60 | def test_handle_dir(self): |
---|
61 | helpers.remove_file_or_directory(self.dirpath) |
---|
62 | self.assertFalse( |
---|
63 | os.path.exists(self.dirpath) |
---|
64 | ) |
---|
65 | return |
---|
66 | |
---|
67 | def test_handle_file(self): |
---|
68 | helpers.remove_file_or_directory(self.filepath) |
---|
69 | self.assertFalse( |
---|
70 | os.path.exists(self.filepath) |
---|
71 | ) |
---|
72 | return |
---|
73 | |
---|
74 | class CopyFileSystemTreeTestCase(unittest.TestCase): |
---|
75 | # Test edge cases of copy_filesystem_tree(). |
---|
76 | # |
---|
77 | # This is a typical case of tests not written as doctest as it is |
---|
78 | # normally not interesting for developers and we only want to make |
---|
79 | # sure everything works as expected. |
---|
80 | def setUp(self): |
---|
81 | self.existing_src = tempfile.mkdtemp() |
---|
82 | self.filepath = os.path.join(self.existing_src, 'somefile') |
---|
83 | open(self.filepath, 'wb').write('Hi!') |
---|
84 | self.existing_dst = tempfile.mkdtemp() |
---|
85 | self.not_existing_dir = tempfile.mkdtemp() |
---|
86 | shutil.rmtree(self.not_existing_dir) |
---|
87 | |
---|
88 | pass |
---|
89 | |
---|
90 | def tearDown(self): |
---|
91 | shutil.rmtree(self.existing_src) |
---|
92 | shutil.rmtree(self.existing_dst) |
---|
93 | pass |
---|
94 | |
---|
95 | def test_source_and_dst_existing(self): |
---|
96 | helpers.copy_filesystem_tree(self.existing_src, self.existing_dst) |
---|
97 | self.assertTrue( |
---|
98 | os.path.exists( |
---|
99 | os.path.join(self.existing_dst, 'somefile') |
---|
100 | ) |
---|
101 | ) |
---|
102 | return |
---|
103 | |
---|
104 | def test_source_not_existing(self): |
---|
105 | self.assertRaises( |
---|
106 | ValueError, |
---|
107 | helpers.copy_filesystem_tree, |
---|
108 | self.not_existing_dir, |
---|
109 | self.existing_dst |
---|
110 | ) |
---|
111 | return |
---|
112 | |
---|
113 | def test_dest_not_existing(self): |
---|
114 | self.assertRaises( |
---|
115 | ValueError, |
---|
116 | helpers.copy_filesystem_tree, |
---|
117 | self.existing_src, |
---|
118 | self.not_existing_dir |
---|
119 | ) |
---|
120 | return |
---|
121 | |
---|
122 | def test_src_not_a_dir(self): |
---|
123 | self.assertRaises( |
---|
124 | ValueError, |
---|
125 | helpers.copy_filesystem_tree, |
---|
126 | self.filepath, |
---|
127 | self.existing_dst |
---|
128 | ) |
---|
129 | return |
---|
130 | |
---|
131 | def test_dst_not_a_dir(self): |
---|
132 | self.assertRaises( |
---|
133 | ValueError, |
---|
134 | helpers.copy_filesystem_tree, |
---|
135 | self.existing_src, |
---|
136 | self.filepath |
---|
137 | ) |
---|
138 | return |
---|
139 | |
---|
140 | class ReST2HTMLTestCase(unittest.TestCase): |
---|
141 | |
---|
142 | def setUp(self): |
---|
143 | self.expected = u'<div class="document">\n\n\n<p>Some ' |
---|
144 | self.expected += u'test with \xfcmlaut</p>\n</div>' |
---|
145 | return |
---|
146 | |
---|
147 | def test_ascii_umlauts(self): |
---|
148 | # Make sure we convert umlauts correctly to unicode. |
---|
149 | source = 'Some test with ümlaut' |
---|
150 | result = helpers.ReST2HTML(source) |
---|
151 | self.assertEqual(result, self.expected) |
---|
152 | |
---|
153 | def test_unicode_umlauts(self): |
---|
154 | # Make sure we convert umlauts correctly to unicode. |
---|
155 | source = u'Some test with ümlaut' |
---|
156 | result = helpers.ReST2HTML(source) |
---|
157 | self.assertEqual(result, self.expected) |
---|
158 | |
---|
159 | def test_unicode_output_from_ascii(self): |
---|
160 | source = 'Some test with ümlaut' |
---|
161 | self.assertTrue(isinstance(helpers.ReST2HTML(source), unicode)) |
---|
162 | |
---|
163 | def test_unicode_output_from_unicode(self): |
---|
164 | source = u'Some test with ümlaut' |
---|
165 | self.assertTrue(isinstance(helpers.ReST2HTML(source), unicode)) |
---|
166 | |
---|
167 | |
---|
168 | class FactoryBaseTestCase(unittest.TestCase): |
---|
169 | |
---|
170 | def test_ifaces(self): |
---|
171 | # We test all relevant parts in the docstring. But the interfaces |
---|
172 | # method has to be tested to please the coverage report as well. |
---|
173 | factory = helpers.FactoryBase() |
---|
174 | factory.factory = FakeObject |
---|
175 | self.assertTrue(factory.getInterfaces()(IFakeObject)) |
---|
176 | return |
---|
177 | |
---|
178 | class CurrentPrincipalTestCase(unittest.TestCase): |
---|
179 | |
---|
180 | def tearDown(test): |
---|
181 | endInteraction() # Just in case, one is still lingering around |
---|
182 | |
---|
183 | def test_existing_principal(self): |
---|
184 | # We can get the current principal if one is involved |
---|
185 | principal = Principal('myprincipal') |
---|
186 | newInteraction(Participation(principal)) |
---|
187 | result = helpers.get_current_principal() |
---|
188 | self.assertTrue(result is principal) |
---|
189 | |
---|
190 | def test_no_participation(self): |
---|
191 | # Interactions without participation are handled correctly |
---|
192 | newInteraction() |
---|
193 | result = helpers.get_current_principal() |
---|
194 | self.assertTrue(result is None) |
---|
195 | |
---|
196 | def test_not_existing_principal(self): |
---|
197 | # Missing interactions do not raise errors. |
---|
198 | result = helpers.get_current_principal() |
---|
199 | self.assertTrue(result is None) |
---|
200 | |
---|
201 | class CmpFilesTestCase(unittest.TestCase): |
---|
202 | |
---|
203 | def setUp(self): |
---|
204 | self.workdir = tempfile.mkdtemp() |
---|
205 | |
---|
206 | def tearDown(self): |
---|
207 | shutil.rmtree(self.workdir) |
---|
208 | |
---|
209 | def test_equal(self): |
---|
210 | p1 = os.path.join(self.workdir, 'sample1') |
---|
211 | p2 = os.path.join(self.workdir, 'sample2') |
---|
212 | open(p1, 'wb').write('Hi!') |
---|
213 | open(p2, 'wb').write('Hi!') |
---|
214 | assert helpers.cmp_files(open(p1, 'r'), open(p2, 'r')) is True |
---|
215 | |
---|
216 | def test_unequal(self): |
---|
217 | p1 = os.path.join(self.workdir, 'sample1') |
---|
218 | p2 = os.path.join(self.workdir, 'sample2') |
---|
219 | open(p1, 'wb').write('Hi!') |
---|
220 | open(p2, 'wb').write('Ho!') |
---|
221 | assert helpers.cmp_files(open(p1, 'r'), open(p2, 'r')) is False |
---|
222 | |
---|
223 | class FileSizeTestCase(unittest.TestCase): |
---|
224 | |
---|
225 | def setUp(self): |
---|
226 | self.workdir = tempfile.mkdtemp() |
---|
227 | |
---|
228 | def tearDown(self): |
---|
229 | shutil.rmtree(self.workdir) |
---|
230 | |
---|
231 | def test_real_file(self): |
---|
232 | # we can get the size of real files |
---|
233 | path = os.path.join(self.workdir, 'sample.txt') |
---|
234 | open(path, 'wb').write('My content') |
---|
235 | self.assertEqual( |
---|
236 | int(helpers.file_size(open(path, 'rb'))), 10) |
---|
237 | return |
---|
238 | |
---|
239 | def test_stringio_file(self): |
---|
240 | # we can get the size of file-like objects |
---|
241 | self.assertEqual( |
---|
242 | helpers.file_size(StringIO('my sample content')), 17) |
---|
243 | |
---|
244 | class IfaceNamesTestCase(unittest.TestCase): |
---|
245 | |
---|
246 | def test_iface_names(self): |
---|
247 | class I1(Interface): |
---|
248 | foo = Attribute("""Some Foo""") |
---|
249 | def bar(blah): |
---|
250 | pass |
---|
251 | i1_name = schema.TextLine(title=u'i1 name') |
---|
252 | class I2(I1): |
---|
253 | baz = schema.TextLine(title=u'some baz') |
---|
254 | class I3(I2): |
---|
255 | pass |
---|
256 | |
---|
257 | result1 = helpers.iface_names(I3) |
---|
258 | result2 = helpers.iface_names(I2) |
---|
259 | result3 = helpers.iface_names(I1) |
---|
260 | result4 = helpers.iface_names(I3, exclude_attribs=False) |
---|
261 | result5 = helpers.iface_names(I3, exclude_methods=False) |
---|
262 | result6 = helpers.iface_names(I3, omit='i1_name') |
---|
263 | self.assertEqual(sorted(result1), ['baz', 'i1_name']) |
---|
264 | self.assertEqual(sorted(result2), ['baz', 'i1_name']) |
---|
265 | self.assertEqual(sorted(result3), ['i1_name']) |
---|
266 | self.assertEqual(sorted(result4), ['baz', 'foo', 'i1_name']) |
---|
267 | self.assertEqual(sorted(result5), ['bar', 'baz', 'i1_name']) |
---|
268 | self.assertEqual(sorted(result6), ['baz']) |
---|
269 | return |
---|
270 | |
---|
271 | class DateTimeHelpersTestCase(unittest.TestCase): |
---|
272 | |
---|
273 | def test_now(self): |
---|
274 | tz_berlin = pytz.timezone('Europe/Berlin') |
---|
275 | result1 = helpers.now() |
---|
276 | result2 = helpers.now(tz_berlin) |
---|
277 | self.assertEqual(result1.tzinfo, pytz.utc) |
---|
278 | self.assertFalse(result2.tzinfo == pytz.utc) |
---|
279 | self.assertFalse(result2.tzinfo is None) |
---|
280 | return |
---|
281 | |
---|
282 | def test_to_timezone(self): |
---|
283 | fmt = '%Y-%m-%d %H:%M:%S %Z%z' |
---|
284 | tz_berlin = pytz.timezone('Europe/Berlin') |
---|
285 | tz_lagos = pytz.timezone('Africa/Lagos') |
---|
286 | dt1 = datetime.datetime(2012, 1, 1, 0, 0) |
---|
287 | dt2 = datetime.datetime(2012, 1, 1, 0, 0, tzinfo=tz_berlin) |
---|
288 | dt3 = datetime.datetime(2012, 6, 1, 0, 0, tzinfo=tz_lagos) |
---|
289 | dt4 = datetime.datetime(2012, 6, 1, 0, 0, tzinfo=tz_berlin) |
---|
290 | result1 = helpers.to_timezone(dt1) |
---|
291 | result2 = helpers.to_timezone(dt1, pytz.utc) |
---|
292 | result3 = helpers.to_timezone(dt2) |
---|
293 | result4 = helpers.to_timezone(dt2, tz_lagos) |
---|
294 | result5 = helpers.to_timezone(dt3, tz_berlin) |
---|
295 | result6 = helpers.to_timezone(dt4, tz_lagos) |
---|
296 | self.assertEqual( |
---|
297 | result1.strftime(fmt), '2012-01-01 00:00:00 UTC+0000') |
---|
298 | self.assertEqual( |
---|
299 | result2.strftime(fmt), '2012-01-01 00:00:00 UTC+0000') |
---|
300 | self.assertEqual( |
---|
301 | result3.strftime(fmt), '2011-12-31 23:00:00 UTC+0000') |
---|
302 | self.assertEqual( |
---|
303 | result4.strftime(fmt), '2012-01-01 00:00:00 WAT+0100') |
---|
304 | self.assertEqual( |
---|
305 | result5.strftime(fmt), '2012-06-01 01:46:00 CEST+0200') |
---|
306 | self.assertEqual( |
---|
307 | result6.strftime(fmt), '2012-06-01 00:00:00 WAT+0100') |
---|
308 | return |
---|
309 | |
---|
310 | def test_to_timezone_no_dt(self): |
---|
311 | # the to_timezone function copes with dates (!= datetimes) |
---|
312 | d = datetime.date(2012, 12, 1) |
---|
313 | result1 = helpers.to_timezone(d) |
---|
314 | result2 = helpers.to_timezone(d, pytz.utc) |
---|
315 | self.assertEqual(result1, d) |
---|
316 | self.assertEqual(result2, d) |
---|
317 | return |
---|
318 | |
---|
319 | class GetFileFormatTestCase(unittest.TestCase): |
---|
320 | # Tests for the get_fileformat helper. |
---|
321 | |
---|
322 | def setUp(self): |
---|
323 | self.valid_jpg_path = os.path.join( |
---|
324 | os.path.dirname(__file__), 'sample_jpg_valid.jpg') |
---|
325 | self.valid_jpg = open(self.valid_jpg_path, 'rb').read() |
---|
326 | self.valid_png_path = os.path.join( |
---|
327 | os.path.dirname(__file__), 'sample_png_valid.png') |
---|
328 | self.valid_png = open(self.valid_png_path, 'rb').read() |
---|
329 | self.valid_pdf_path = os.path.join( |
---|
330 | os.path.dirname(__file__), 'sample_pdf_valid.pdf') |
---|
331 | self.valid_pdf = open(self.valid_pdf_path, 'rb').read() |
---|
332 | return |
---|
333 | |
---|
334 | def test_none(self): |
---|
335 | # ``None`` is not a file and not a valid file format |
---|
336 | self.assertEqual(helpers.get_fileformat(None), None) |
---|
337 | return |
---|
338 | |
---|
339 | def test_path_and_bytestream(self): |
---|
340 | # get_fileformat accepts bytestreams and paths as arg. |
---|
341 | self.assertEqual( |
---|
342 | helpers.get_fileformat(None, self.valid_jpg), 'jpg') |
---|
343 | self.assertEqual( |
---|
344 | helpers.get_fileformat(self.valid_jpg_path), 'jpg') |
---|
345 | # path is ignored when giving a bytestream |
---|
346 | self.assertEqual( |
---|
347 | helpers.get_fileformat('blah', self.valid_jpg), 'jpg') |
---|
348 | return |
---|
349 | |
---|
350 | def test_jpg(self): |
---|
351 | # we recognize jpeg images. |
---|
352 | self.assertEqual( |
---|
353 | helpers.get_fileformat(self.valid_jpg_path), 'jpg') |
---|
354 | self.assertEqual( |
---|
355 | helpers.get_fileformat(None, self.valid_jpg), 'jpg') |
---|
356 | return |
---|
357 | |
---|
358 | def test_png(self): |
---|
359 | # we recognize png images. |
---|
360 | self.assertEqual( |
---|
361 | helpers.get_fileformat(self.valid_png_path), 'png') |
---|
362 | self.assertEqual( |
---|
363 | helpers.get_fileformat(None, self.valid_png), 'png') |
---|
364 | return |
---|
365 | |
---|
366 | def test_pdf(self): |
---|
367 | # we recognize pdf documents. |
---|
368 | self.assertEqual( |
---|
369 | helpers.get_fileformat(self.valid_pdf_path), 'pdf') |
---|
370 | self.assertEqual( |
---|
371 | helpers.get_fileformat(None, self.valid_pdf), 'pdf') |
---|
372 | return |
---|
373 | |
---|
374 | def test_suite(): |
---|
375 | suite = unittest.TestSuite() |
---|
376 | # Register local test cases... |
---|
377 | for testcase in [ |
---|
378 | ReST2HTMLTestCase, |
---|
379 | FactoryBaseTestCase, |
---|
380 | CopyFileSystemTreeTestCase, |
---|
381 | RemoveFileOrDirectoryTestCase, |
---|
382 | CurrentPrincipalTestCase, |
---|
383 | CmpFilesTestCase, |
---|
384 | FileSizeTestCase, |
---|
385 | IfaceNamesTestCase, |
---|
386 | DateTimeHelpersTestCase, |
---|
387 | GetFileFormatTestCase, |
---|
388 | ]: |
---|
389 | suite.addTests( |
---|
390 | unittest.TestLoader().loadTestsFromTestCase(testcase) |
---|
391 | ) |
---|
392 | # Add tests from docstrings in helpers.py... |
---|
393 | suite.addTests( |
---|
394 | doctest.DocTestSuite( |
---|
395 | helpers, |
---|
396 | optionflags = doctest.ELLIPSIS + doctest.REPORT_NDIFF, |
---|
397 | ) |
---|
398 | ) |
---|
399 | return suite |
---|
400 | |
---|
401 | if __name__ == '__main__': |
---|
402 | unittest.main(defaultTest='test_suite') |
---|