1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | ## $Id: test_helpers.py 7942 2012-03-22 01:52:04Z 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 os |
---|
22 | import shutil |
---|
23 | import tempfile |
---|
24 | import unittest |
---|
25 | import doctest |
---|
26 | from cStringIO import StringIO |
---|
27 | from zope import schema |
---|
28 | from zope.interface import Interface, Attribute, implements |
---|
29 | from zope.security.testing import Principal, Participation |
---|
30 | from zope.security.management import newInteraction, endInteraction |
---|
31 | from waeup.kofa.utils import helpers |
---|
32 | |
---|
33 | class IFakeObject(Interface): |
---|
34 | """Some marker interface.""" |
---|
35 | |
---|
36 | class FakeObject(object): |
---|
37 | implements(IFakeObject) |
---|
38 | |
---|
39 | class RemoveFileOrDirectoryTestCase(unittest.TestCase): |
---|
40 | |
---|
41 | def setUp(self): |
---|
42 | self.dirpath = tempfile.mkdtemp() |
---|
43 | self.filepath = os.path.join(self.dirpath, 'somefile') |
---|
44 | self.non_file = os.path.join(self.dirpath, 'nonfile') |
---|
45 | open(self.filepath, 'wb').write('Hi!') |
---|
46 | return |
---|
47 | |
---|
48 | def tearDown(self): |
---|
49 | if os.path.exists(self.dirpath): |
---|
50 | shutil.rmtree(self.dirpath) |
---|
51 | return |
---|
52 | |
---|
53 | def test_handle_not_existing_path(self): |
---|
54 | result = helpers.remove_file_or_directory(self.non_file) |
---|
55 | self.assertTrue(result is None) |
---|
56 | return |
---|
57 | |
---|
58 | def test_handle_dir(self): |
---|
59 | helpers.remove_file_or_directory(self.dirpath) |
---|
60 | self.assertFalse( |
---|
61 | os.path.exists(self.dirpath) |
---|
62 | ) |
---|
63 | return |
---|
64 | |
---|
65 | def test_handle_file(self): |
---|
66 | helpers.remove_file_or_directory(self.filepath) |
---|
67 | self.assertFalse( |
---|
68 | os.path.exists(self.filepath) |
---|
69 | ) |
---|
70 | return |
---|
71 | |
---|
72 | class CopyFileSystemTreeTestCase(unittest.TestCase): |
---|
73 | # Test edge cases of copy_filesystem_tree(). |
---|
74 | # |
---|
75 | # This is a typical case of tests not written as doctest as it is |
---|
76 | # normally not interesting for developers and we only want to make |
---|
77 | # sure everything works as expected. |
---|
78 | def setUp(self): |
---|
79 | self.existing_src = tempfile.mkdtemp() |
---|
80 | self.filepath = os.path.join(self.existing_src, 'somefile') |
---|
81 | open(self.filepath, 'wb').write('Hi!') |
---|
82 | self.existing_dst = tempfile.mkdtemp() |
---|
83 | self.not_existing_dir = tempfile.mkdtemp() |
---|
84 | shutil.rmtree(self.not_existing_dir) |
---|
85 | |
---|
86 | pass |
---|
87 | |
---|
88 | def tearDown(self): |
---|
89 | shutil.rmtree(self.existing_src) |
---|
90 | shutil.rmtree(self.existing_dst) |
---|
91 | pass |
---|
92 | |
---|
93 | def test_source_and_dst_existing(self): |
---|
94 | helpers.copy_filesystem_tree(self.existing_src, self.existing_dst) |
---|
95 | self.assertTrue( |
---|
96 | os.path.exists( |
---|
97 | os.path.join(self.existing_dst, 'somefile') |
---|
98 | ) |
---|
99 | ) |
---|
100 | return |
---|
101 | |
---|
102 | def test_source_not_existing(self): |
---|
103 | self.assertRaises( |
---|
104 | ValueError, |
---|
105 | helpers.copy_filesystem_tree, |
---|
106 | self.not_existing_dir, |
---|
107 | self.existing_dst |
---|
108 | ) |
---|
109 | return |
---|
110 | |
---|
111 | def test_dest_not_existing(self): |
---|
112 | self.assertRaises( |
---|
113 | ValueError, |
---|
114 | helpers.copy_filesystem_tree, |
---|
115 | self.existing_src, |
---|
116 | self.not_existing_dir |
---|
117 | ) |
---|
118 | return |
---|
119 | |
---|
120 | def test_src_not_a_dir(self): |
---|
121 | self.assertRaises( |
---|
122 | ValueError, |
---|
123 | helpers.copy_filesystem_tree, |
---|
124 | self.filepath, |
---|
125 | self.existing_dst |
---|
126 | ) |
---|
127 | return |
---|
128 | |
---|
129 | def test_dst_not_a_dir(self): |
---|
130 | self.assertRaises( |
---|
131 | ValueError, |
---|
132 | helpers.copy_filesystem_tree, |
---|
133 | self.existing_src, |
---|
134 | self.filepath |
---|
135 | ) |
---|
136 | return |
---|
137 | |
---|
138 | class ReST2HTMLTestCase(unittest.TestCase): |
---|
139 | |
---|
140 | def setUp(self): |
---|
141 | self.expected = u'<div class="document">\n\n\n<p>Some ' |
---|
142 | self.expected += u'test with \xfcmlaut</p>\n</div>' |
---|
143 | return |
---|
144 | |
---|
145 | def test_ascii_umlauts(self): |
---|
146 | # Make sure we convert umlauts correctly to unicode. |
---|
147 | source = 'Some test with ümlaut' |
---|
148 | result = helpers.ReST2HTML(source) |
---|
149 | self.assertEqual(result, self.expected) |
---|
150 | |
---|
151 | def test_unicode_umlauts(self): |
---|
152 | # Make sure we convert umlauts correctly to unicode. |
---|
153 | source = u'Some test with ümlaut' |
---|
154 | result = helpers.ReST2HTML(source) |
---|
155 | self.assertEqual(result, self.expected) |
---|
156 | |
---|
157 | def test_unicode_output_from_ascii(self): |
---|
158 | source = 'Some test with ümlaut' |
---|
159 | self.assertTrue(isinstance(helpers.ReST2HTML(source), unicode)) |
---|
160 | |
---|
161 | def test_unicode_output_from_unicode(self): |
---|
162 | source = u'Some test with ümlaut' |
---|
163 | self.assertTrue(isinstance(helpers.ReST2HTML(source), unicode)) |
---|
164 | |
---|
165 | |
---|
166 | class FactoryBaseTestCase(unittest.TestCase): |
---|
167 | |
---|
168 | def test_ifaces(self): |
---|
169 | # We test all relevant parts in the docstring. But the interfaces |
---|
170 | # method has to be tested to please the coverage report as well. |
---|
171 | factory = helpers.FactoryBase() |
---|
172 | factory.factory = FakeObject |
---|
173 | self.assertTrue(factory.getInterfaces()(IFakeObject)) |
---|
174 | return |
---|
175 | |
---|
176 | class CurrentPrincipalTestCase(unittest.TestCase): |
---|
177 | |
---|
178 | def tearDown(test): |
---|
179 | endInteraction() # Just in case, one is still lingering around |
---|
180 | |
---|
181 | def test_existing_principal(self): |
---|
182 | # We can get the current principal if one is involved |
---|
183 | principal = Principal('myprincipal') |
---|
184 | newInteraction(Participation(principal)) |
---|
185 | result = helpers.get_current_principal() |
---|
186 | self.assertTrue(result is principal) |
---|
187 | |
---|
188 | def test_no_participation(self): |
---|
189 | # Interactions without participation are handled correctly |
---|
190 | newInteraction() |
---|
191 | result = helpers.get_current_principal() |
---|
192 | self.assertTrue(result is None) |
---|
193 | |
---|
194 | def test_not_existing_principal(self): |
---|
195 | # Missing interactions do not raise errors. |
---|
196 | result = helpers.get_current_principal() |
---|
197 | self.assertTrue(result is None) |
---|
198 | |
---|
199 | class CmpFilesTestCase(unittest.TestCase): |
---|
200 | |
---|
201 | def setUp(self): |
---|
202 | self.workdir = tempfile.mkdtemp() |
---|
203 | |
---|
204 | def tearDown(self): |
---|
205 | shutil.rmtree(self.workdir) |
---|
206 | |
---|
207 | def test_equal(self): |
---|
208 | p1 = os.path.join(self.workdir, 'sample1') |
---|
209 | p2 = os.path.join(self.workdir, 'sample2') |
---|
210 | open(p1, 'wb').write('Hi!') |
---|
211 | open(p2, 'wb').write('Hi!') |
---|
212 | assert helpers.cmp_files(open(p1, 'r'), open(p2, 'r')) is True |
---|
213 | |
---|
214 | def test_unequal(self): |
---|
215 | p1 = os.path.join(self.workdir, 'sample1') |
---|
216 | p2 = os.path.join(self.workdir, 'sample2') |
---|
217 | open(p1, 'wb').write('Hi!') |
---|
218 | open(p2, 'wb').write('Ho!') |
---|
219 | assert helpers.cmp_files(open(p1, 'r'), open(p2, 'r')) is False |
---|
220 | |
---|
221 | class FileSizeTestCase(unittest.TestCase): |
---|
222 | |
---|
223 | def setUp(self): |
---|
224 | self.workdir = tempfile.mkdtemp() |
---|
225 | |
---|
226 | def tearDown(self): |
---|
227 | shutil.rmtree(self.workdir) |
---|
228 | |
---|
229 | def test_real_file(self): |
---|
230 | # we can get the size of real files |
---|
231 | path = os.path.join(self.workdir, 'sample.txt') |
---|
232 | open(path, 'wb').write('My content') |
---|
233 | self.assertEqual( |
---|
234 | int(helpers.file_size(open(path, 'rb'))), 10) |
---|
235 | return |
---|
236 | |
---|
237 | def test_stringio_file(self): |
---|
238 | # we can get the size of file-like objects |
---|
239 | self.assertEqual( |
---|
240 | helpers.file_size(StringIO('my sample content')), 17) |
---|
241 | |
---|
242 | class IfaceNamesTestCase(unittest.TestCase): |
---|
243 | |
---|
244 | def test_iface_names(self): |
---|
245 | class I1(Interface): |
---|
246 | foo = Attribute("""Some Foo""") |
---|
247 | def bar(blah): |
---|
248 | pass |
---|
249 | i1_name = schema.TextLine(title=u'i1 name') |
---|
250 | class I2(I1): |
---|
251 | baz = schema.TextLine(title=u'some baz') |
---|
252 | |
---|
253 | result1 = helpers.iface_names(I2) |
---|
254 | result2 = helpers.iface_names(I1) |
---|
255 | result3 = helpers.iface_names(I2, exclude_attribs=False) |
---|
256 | result4 = helpers.iface_names(I2, exclude_methods=False) |
---|
257 | result5 = helpers.iface_names(I2, omit='i1_name') |
---|
258 | self.assertEqual(sorted(result1), ['baz', 'i1_name']) |
---|
259 | self.assertEqual(sorted(result2), ['i1_name']) |
---|
260 | self.assertEqual(sorted(result3), ['baz', 'foo', 'i1_name']) |
---|
261 | self.assertEqual(sorted(result4), ['bar', 'baz', 'i1_name']) |
---|
262 | self.assertEqual(sorted(result5), ['baz']) |
---|
263 | return |
---|
264 | |
---|
265 | def test_suite(): |
---|
266 | suite = unittest.TestSuite() |
---|
267 | # Register local test cases... |
---|
268 | for testcase in [ |
---|
269 | ReST2HTMLTestCase, |
---|
270 | FactoryBaseTestCase, |
---|
271 | CopyFileSystemTreeTestCase, |
---|
272 | RemoveFileOrDirectoryTestCase, |
---|
273 | CurrentPrincipalTestCase, |
---|
274 | CmpFilesTestCase, |
---|
275 | FileSizeTestCase, |
---|
276 | IfaceNamesTestCase, |
---|
277 | ]: |
---|
278 | suite.addTests( |
---|
279 | unittest.TestLoader().loadTestsFromTestCase(testcase) |
---|
280 | ) |
---|
281 | # Add tests from docstrings in helpers.py... |
---|
282 | suite.addTests( |
---|
283 | doctest.DocTestSuite( |
---|
284 | helpers, |
---|
285 | optionflags = doctest.ELLIPSIS + doctest.REPORT_NDIFF, |
---|
286 | ) |
---|
287 | ) |
---|
288 | return suite |
---|
289 | |
---|
290 | if __name__ == '__main__': |
---|
291 | unittest.main(defaultTest='test_suite') |
---|