source: main/waeup.sirp/branches/ulif-groktoolkit-1.4/src/waeup/sirp/utils/tests/test_helpers.py @ 9356

Last change on this file since 9356 was 7196, checked in by Henrik Bettermann, 13 years ago

More copyright adjustments (job finished).

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