source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_root.py @ 6701

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

Add missing imports.

File size: 8.4 KB
Line 
1##
2## test_root.py
3## Login : <uli@pu.smp.net>
4## Started on  Thu Jan 20 09:26:54 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##
22"""
23Test applicants root.
24"""
25import logging
26import shutil
27import tempfile
28import unittest
29from StringIO import StringIO
30from zope.component.hooks import setSite, clearSite
31from zope.interface.verify import verifyClass, verifyObject
32from waeup.sirp.app import University
33from waeup.sirp.applicants import (
34    interfaces, application_exists, get_applicant_data, Applicant,
35    ApplicantsContainer,
36    )
37from waeup.sirp.applicants.root import (
38    ApplicantsRoot, ApplicantsPlugin,
39    )
40from waeup.sirp.testing import (
41    FunctionalLayer, FunctionalTestCase, get_all_loggers, remove_new_loggers,
42    remove_logger)
43
44
45class FakeSite(dict):
46    pass
47
48class ApplicantsRootTestCase(FunctionalTestCase):
49
50    layer = FunctionalLayer
51
52    def setUp(self):
53        remove_logger('waeup.sirp.app.applicants')
54        super(ApplicantsRootTestCase, self).setUp()
55        # Setup a sample site for each test
56        # Prepopulate the ZODB...
57        app = University()
58        self.getRootFolder()['app'] = app
59        self.app = self.getRootFolder()['app']
60        setSite(self.app)
61        self.dc_root = tempfile.mkdtemp()
62        app['datacenter'].setStoragePath(self.dc_root)
63        return
64
65    def tearDown(self):
66        super(ApplicantsRootTestCase, self).tearDown()
67        shutil.rmtree(self.dc_root)
68        clearSite()
69        return
70
71    def test_interfaces(self):
72        # Make sure the correct interfaces are implemented.
73        self.assertTrue(
74            verifyClass(
75                interfaces.IApplicantsRoot, ApplicantsRoot)
76            )
77        self.assertTrue(
78            verifyObject(
79                interfaces.IApplicantsRoot, ApplicantsRoot())
80            )
81        return
82
83    def test_logger(self):
84        # We can get a logger from root
85        logger = self.app['applicants'].logger
86        assert logger is not None
87        assert logger.name == 'waeup.sirp.app.applicants'
88        handlers = logger.handlers
89        assert len(handlers) == 1
90        filename = logger.handlers[0].baseFilename
91        assert filename.endswith('applicants.log')
92        assert filename.startswith(self.dc_root)
93
94    def test_logger_multiple(self):
95        # Make sure the logger is still working after 2nd call
96        # First time we call it, it might be registered
97        logger = self.app['applicants'].logger
98        # At second call the already registered logger should be returned
99        logger = self.app['applicants'].logger
100        assert logger is not None
101        assert logger.name == 'waeup.sirp.app.applicants'
102        handlers = logger.handlers
103        assert len(handlers) == 1
104        filename = logger.handlers[0].baseFilename
105        assert filename.endswith('applicants.log')
106        assert filename.startswith(self.dc_root)
107
108class ApplicantsRootPluginTestCase(unittest.TestCase):
109    def create_logger(self):
110        # create a logger suitable for local tests.
111        test_logger = logging.getLogger('waeup.sirp.applicants.testlogger')
112        log = StringIO()
113        handler = logging.StreamHandler(log)
114        handler.setLevel(logging.DEBUG)
115        test_logger.addHandler(handler)
116        test_logger.setLevel(logging.DEBUG)
117        self.logger = test_logger
118        self.log = log
119        self.handler = handler
120        return self.logger
121
122    def remove_logger(self):
123        del self.handler
124        del self.logger
125        del self.log
126        pass
127
128    def get_log(self):
129        self.log.seek(0)
130        return self.log.read()
131
132    def setUp(self):
133        self.create_logger()
134        return
135
136    def tearDown(self):
137        self.remove_logger()
138        return
139
140    # Real tests start here...
141    def test_pluginsetup(self):
142        # Make sure we can add ApplicantsRoot to sites.
143        site = FakeSite()
144        plugin = ApplicantsPlugin()
145        plugin.setup(site, 'blah', self.logger)
146        self.assertTrue('applicants' in site.keys())
147        log = self.get_log()
148        self.assertTrue('Installed applicants root.' in log)
149        return
150
151    def test_update_new(self):
152        # Run update on a site without applicants root.
153        site = FakeSite()
154        plugin = ApplicantsPlugin()
155        plugin.update(site, 'blah', self.logger)
156        self.assertTrue('applicants' in site.keys())
157        log = self.get_log()
158        self.assertTrue('Updating site at <Unnamed Site>' in log)
159        self.assertTrue('Installed applicants root.' in log)
160        return
161
162    def test_update_outdated(self):
163        # Run update on a site with outdated applicants root.
164        site = FakeSite()
165        root = object() # # This is not a proper applicants root
166        site['applicants'] = root
167        plugin = ApplicantsPlugin()
168        plugin.update(site, 'blah', self.logger)
169        self.assertTrue(site['applicants'] is not root)
170        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
171        log = self.get_log()
172        self.assertTrue('Outdated applicants folder detected' in log)
173        self.assertTrue('Updating site at <Unnamed Site>' in log)
174        self.assertTrue('Installed applicants root.' in log)
175        return
176
177    def test_update_uptodate(self):
178        # Run update on a site with proper applicants root.
179        site = FakeSite()
180        root = ApplicantsRoot()
181        site['applicants'] = root
182        plugin = ApplicantsPlugin()
183        plugin.update(site, 'blah', self.logger)
184        self.assertTrue(site['applicants'] is root)
185        log = self.get_log()
186        self.assertTrue('Updating site at <Unnamed Site>' in log)
187        self.assertTrue('Nothing to do' in log)
188        return
189
190    def test_update_log(self):
191        # Check that sitename is used in log messages on updates.
192        site = FakeSite()
193        site.__name__ = 'my_site'
194        plugin = ApplicantsPlugin()
195        plugin.update(site, 'blah', self.logger)
196        log = self.get_log()
197        self.assertTrue('Updating site at my_site.' in log)
198        return
199
200
201class HelperToolsTest(FunctionalTestCase):
202
203    layer = FunctionalLayer
204
205    def setUp(self):
206        super(HelperToolsTest, self).setUp()
207
208        # Setup a sample site for each test
209        app = University()
210        self.dc_root = tempfile.mkdtemp()
211        app['datacenter'].setStoragePath(self.dc_root)
212
213        # Prepopulate the ZODB...
214        self.getRootFolder()['app'] = app
215        self.app = self.getRootFolder()['app']
216        setSite(self.app)
217
218        # Insert some applicants containers and applicants...
219        self.container1 = ApplicantsContainer()
220        self.app['applicants']['foo'] = self.container1
221        self.container2 = ApplicantsContainer()
222        self.app['applicants']['bar'] = self.container2
223        self.ac = u'APP-99999'
224        self.applicant = Applicant()
225        self.applicant.access_code = self.ac
226        self.app['applicants']['foo'][self.ac] = self.applicant
227        return
228
229    def tearDown(self):
230        super(HelperToolsTest, self).tearDown()
231        shutil.rmtree(self.dc_root)
232        return
233
234    def test_application_exists(self):
235        result = application_exists('APP-99999')
236        assert result is True
237
238        result = application_exists('APP-44444')
239        assert result is False
240        return
241
242    def test_get_applicant_data(self):
243        result = get_applicant_data('APP-99999')
244        self.assertEqual(result, self.applicant)
245        return
246
247    def test_get_applicant_data_none(self):
248        result = get_applicant_data('NOT-EXISTIING')
249        assert result is None
250        return
251
252def suite():
253    suite = unittest.TestSuite()
254    for testcase in [
255            ApplicantsRootTestCase,
256            ApplicantsRootPluginTestCase,
257            HelperToolsTest,
258            ]:
259        suite.addTests(unittest.makeSuite(testcase))
260    return suite
261
262test_suite = suite
Note: See TracBrowser for help on using the repository browser.