source: main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_helpers.py @ 6373

Last change on this file since 6373 was 6373, checked in by uli, 13 years ago

Test new method.

File size: 6.9 KB
Line 
1# -*- coding: utf-8 -*-
2
3##
4## test_helpers.py
5## Login : <uli@pu.smp.net>
6## Started on  Sat Feb 12 14:39:42 2011 Uli Fouquet
7## $Id$
8##
9## Copyright (C) 2011 Uli Fouquet
10## This program is free software; you can redistribute it and/or modify
11## it under the terms of the GNU General Public License as published by
12## the Free Software Foundation; either version 2 of the License, or
13## (at your option) any later version.
14##
15## This program is distributed in the hope that it will be useful,
16## but WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with this program; if not, write to the Free Software
22## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23##
24import os
25import shutil
26import tempfile
27import unittest
28import doctest
29from zope.security.testing import Principal, Participation
30from zope.security.management import newInteraction, endInteraction
31from waeup.sirp.utils import helpers
32
33from zope.interface import Interface, implements
34class IFakeObject(Interface):
35    """Some marker interface."""
36
37class FakeObject(object):
38    implements(IFakeObject)
39
40class RemoveFileOrDirectoryTestCase(unittest.TestCase):
41
42    def setUp(self):
43        self.dirpath = tempfile.mkdtemp()
44        self.filepath = os.path.join(self.dirpath, 'somefile')
45        self.non_file = os.path.join(self.dirpath, 'nonfile')
46        open(self.filepath, 'wb').write('Hi!')
47        return
48
49    def tearDown(self):
50        if os.path.exists(self.dirpath):
51            shutil.rmtree(self.dirpath)
52        return
53
54    def test_handle_not_existing_path(self):
55        result = helpers.removeFileOrDirectory(self.non_file)
56        self.assertTrue(result is None)
57        return
58
59    def test_handle_dir(self):
60        helpers.removeFileOrDirectory(self.dirpath)
61        self.assertFalse(
62            os.path.exists(self.dirpath)
63            )
64        return
65
66    def test_handle_file(self):
67        helpers.removeFileOrDirectory(self.filepath)
68        self.assertFalse(
69            os.path.exists(self.filepath)
70            )
71        return
72
73class CopyFileSystemTreeTestCase(unittest.TestCase):
74    # Test edge cases of copyFileSystemTree().
75    #
76    # This is a typical case of tests not written as doctest as it is
77    # normally not interesting for developers and we only want to make
78    # sure everything works as expected.
79    def setUp(self):
80        self.existing_src = tempfile.mkdtemp()
81        self.filepath = os.path.join(self.existing_src, 'somefile')
82        open(self.filepath, 'wb').write('Hi!')
83        self.existing_dst = tempfile.mkdtemp()
84        self.not_existing_dir = tempfile.mkdtemp()
85        shutil.rmtree(self.not_existing_dir)
86
87        pass
88
89    def tearDown(self):
90        shutil.rmtree(self.existing_src)
91        shutil.rmtree(self.existing_dst)
92        pass
93
94    def test_source_and_dst_existing(self):
95        helpers.copyFileSystemTree(self.existing_src, self.existing_dst)
96        self.assertTrue(
97            os.path.exists(
98                os.path.join(self.existing_dst, 'somefile')
99                )
100            )
101        return
102
103    def test_source_not_existing(self):
104        self.assertRaises(
105            ValueError,
106            helpers.copyFileSystemTree,
107            self.not_existing_dir,
108            self.existing_dst
109            )
110        return
111
112    def test_dest_not_existing(self):
113        self.assertRaises(
114            ValueError,
115            helpers.copyFileSystemTree,
116            self.existing_src,
117            self.not_existing_dir
118            )
119        return
120
121    def test_src_not_a_dir(self):
122        self.assertRaises(
123            ValueError,
124            helpers.copyFileSystemTree,
125            self.filepath,
126            self.existing_dst
127            )
128        return
129
130    def test_dst_not_a_dir(self):
131        self.assertRaises(
132            ValueError,
133            helpers.copyFileSystemTree,
134            self.existing_src,
135            self.filepath
136            )
137        return
138
139class ReST2HTMLTestCase(unittest.TestCase):
140
141    def setUp(self):
142        self.expected = u'<div class="document">\n\n\n<p>Some '
143        self.expected += u'test with \xfcmlaut</p>\n</div>'
144        return
145
146    def test_ascii_umlauts(self):
147        # Make sure we convert umlauts correctly to unicode.
148        source = 'Some test with ümlaut'
149        result = helpers.ReST2HTML(source)
150        self.assertEqual(result, self.expected)
151
152    def test_unicode_umlauts(self):
153        # Make sure we convert umlauts correctly to unicode.
154        source = u'Some test with ümlaut'
155        result = helpers.ReST2HTML(source)
156        self.assertEqual(result, self.expected)
157
158    def test_unicode_output_from_ascii(self):
159        source = 'Some test with ümlaut'
160        self.assertTrue(isinstance(helpers.ReST2HTML(source), unicode))
161
162    def test_unicode_output_from_unicode(self):
163        source = u'Some test with ümlaut'
164        self.assertTrue(isinstance(helpers.ReST2HTML(source), unicode))
165
166
167class FactoryBaseTestCase(unittest.TestCase):
168
169    def test_ifaces(self):
170        # We test all relevant parts in the docstring. But the interfaces
171        # method has to be tested to please the coverage report as well.
172        factory = helpers.FactoryBase()
173        factory.factory = FakeObject
174        self.assertTrue(factory.getInterfaces()(IFakeObject))
175        return
176
177class CurrentPrincipalTestCase(unittest.TestCase):
178
179    def tearDown(test):
180        endInteraction() # Just in case, one is still lingering around
181
182    def test_existing_principal(self):
183        # We can get the current principal if one is involved
184        principal = Principal('myprincipal')
185        newInteraction(Participation(principal))
186        result = helpers.get_current_principal()
187        self.assertTrue(result is principal)
188
189    def test_no_participation(self):
190        # Interactions without participation are handled correctly
191        newInteraction()
192        result = helpers.get_current_principal()
193        self.assertTrue(result is None)
194
195    def test_not_existing_principal(self):
196        # Missing interactions do not raise errors.
197        result = helpers.get_current_principal()
198        self.assertTrue(result is None)
199
200def test_suite():
201    suite = unittest.TestSuite()
202    # Register local test cases...
203    for testcase in [
204        ReST2HTMLTestCase,
205        FactoryBaseTestCase,
206        CopyFileSystemTreeTestCase,
207        RemoveFileOrDirectoryTestCase,
208        CurrentPrincipalTestCase,
209        ]:
210        suite.addTests(
211            unittest.TestLoader().loadTestsFromTestCase(testcase)
212            )
213    # Add tests from docstrings in helpers.py...
214    suite.addTests(
215        doctest.DocTestSuite(
216            helpers,
217            optionflags = doctest.ELLIPSIS + doctest.REPORT_NDIFF,
218            )
219        )
220    return suite
221
222if __name__ == '__main__':
223    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.