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

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

Implement a new logging technique and use it.
Details are explained in waeup.sirp.utils.logger.

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