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

Last change on this file since 6067 was 5875, checked in by uli, 14 years ago

Add tests for ReST2HTML to check for unicode-compliance.

File size: 5.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 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.removeFileOrDirectory(self.non_file)
54        self.assertTrue(result is None)
55        return
56
57    def test_handle_dir(self):
58        helpers.removeFileOrDirectory(self.dirpath)
59        self.assertFalse(
60            os.path.exists(self.dirpath)
61            )
62        return
63
64    def test_handle_file(self):
65        helpers.removeFileOrDirectory(self.filepath)
66        self.assertFalse(
67            os.path.exists(self.filepath)
68            )
69        return
70
71class CopyFileSystemTreeTestCase(unittest.TestCase):
72    # Test edge cases of copyFileSystemTree().
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.copyFileSystemTree(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.copyFileSystemTree,
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.copyFileSystemTree,
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.copyFileSystemTree,
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.copyFileSystemTree,
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       
175def test_suite():
176    suite = unittest.TestSuite()
177    # Register local test cases...
178    for testcase in [
179        ReST2HTMLTestCase,
180        FactoryBaseTestCase,
181        CopyFileSystemTreeTestCase,
182        RemoveFileOrDirectoryTestCase,
183        ]:
184        suite.addTests(
185            unittest.TestLoader().loadTestsFromTestCase(testcase)
186            )
187    # Add tests from docstrings in helpers.py...
188    suite.addTests(
189        doctest.DocTestSuite(
190            helpers,
191            optionflags = doctest.ELLIPSIS + doctest.REPORT_NDIFF,
192            )
193        )
194    return suite
195
196if __name__ == '__main__':
197    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.