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

Last change on this file since 5772 was 5739, checked in by uli, 14 years ago

Add tests for removeFileOrDirectory. Here we also use unittests
instead of doctests in docstrings, because it would not be very
interesting to developers to see how file removal works. We can tell
that this function removes files and developers will know what that
means even without examples.

The unittest, however, checks that the funtion really removes files
when called.

Test coverage for waeup.sirp.utils.helpers now at 100% :)

File size: 4.9 KB
Line 
1##
2## test_helpers.py
3## Login : <uli@pu.smp.net>
4## Started on  Sat Feb 12 14:39:42 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22import os
23import shutil
24import tempfile
25import unittest
26import doctest
27from waeup.sirp.utils import helpers
28
29from zope.interface import Interface, implements
30class IFakeObject(Interface):
31    """Some marker interface."""
32
33class FakeObject(object):
34    implements(IFakeObject)
35
36class RemoveFileOrDirectoryTestCase(unittest.TestCase):
37
38    def setUp(self):
39        self.dirpath = tempfile.mkdtemp()
40        self.filepath = os.path.join(self.dirpath, 'somefile')
41        self.non_file = os.path.join(self.dirpath, 'nonfile')
42        open(self.filepath, 'wb').write('Hi!')
43        return
44
45    def tearDown(self):
46        if os.path.exists(self.dirpath):
47            shutil.rmtree(self.dirpath)
48        return
49
50    def test_handle_not_existing_path(self):
51        result = helpers.removeFileOrDirectory(self.non_file)
52        self.assertTrue(result is None)
53        return
54
55    def test_handle_dir(self):
56        helpers.removeFileOrDirectory(self.dirpath)
57        self.assertFalse(
58            os.path.exists(self.dirpath)
59            )
60        return
61
62    def test_handle_file(self):
63        helpers.removeFileOrDirectory(self.filepath)
64        self.assertFalse(
65            os.path.exists(self.filepath)
66            )
67        return
68
69class CopyFileSystemTreeTestCase(unittest.TestCase):
70    # Test edge cases of copyFileSystemTree().
71    #
72    # This is a typical case of tests not written as doctest as it is
73    # normally not interesting for developers and we only want to make
74    # sure everything works as expected.
75    def setUp(self):
76        self.existing_src = tempfile.mkdtemp()
77        self.filepath = os.path.join(self.existing_src, 'somefile')
78        open(self.filepath, 'wb').write('Hi!')
79        self.existing_dst = tempfile.mkdtemp()
80        self.not_existing_dir = tempfile.mkdtemp()
81        shutil.rmtree(self.not_existing_dir)
82
83        pass
84
85    def tearDown(self):
86        shutil.rmtree(self.existing_src)
87        shutil.rmtree(self.existing_dst)
88        pass
89
90    def test_source_and_dst_existing(self):
91        helpers.copyFileSystemTree(self.existing_src, self.existing_dst)
92        self.assertTrue(
93            os.path.exists(
94                os.path.join(self.existing_dst, 'somefile')
95                )
96            )
97        return
98
99    def test_source_not_existing(self):
100        self.assertRaises(
101            ValueError,
102            helpers.copyFileSystemTree,
103            self.not_existing_dir,
104            self.existing_dst
105            )
106        return
107
108    def test_dest_not_existing(self):
109        self.assertRaises(
110            ValueError,
111            helpers.copyFileSystemTree,
112            self.existing_src,
113            self.not_existing_dir
114            )
115        return
116
117    def test_src_not_a_dir(self):
118        self.assertRaises(
119            ValueError,
120            helpers.copyFileSystemTree,
121            self.filepath,
122            self.existing_dst
123            )
124        return
125
126    def test_dst_not_a_dir(self):
127        self.assertRaises(
128            ValueError,
129            helpers.copyFileSystemTree,
130            self.existing_src,
131            self.filepath
132            )
133        return
134
135
136class FactoryBaseTestCase(unittest.TestCase):
137
138    def test_ifaces(self):
139        # We test all relevant parts in the docstring. But the interfaces
140        # method has to be tested to please the coverage report as well.
141        factory = helpers.FactoryBase()
142        factory.factory = FakeObject
143        self.assertTrue(factory.getInterfaces()(IFakeObject))
144        return
145       
146def test_suite():
147    suite = unittest.TestSuite()
148    # Register local test cases...
149    for testcase in [
150        FactoryBaseTestCase,
151        CopyFileSystemTreeTestCase,
152        RemoveFileOrDirectoryTestCase,
153        ]:
154        suite.addTests(
155            unittest.TestLoader().loadTestsFromTestCase(testcase)
156            )
157    # Add tests from docstrings in helpers.py...
158    suite.addTests(
159        doctest.DocTestSuite(
160            helpers,
161            optionflags = doctest.ELLIPSIS + doctest.REPORT_NDIFF,
162            )
163        )
164    return suite
165
166if __name__ == '__main__':
167    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.