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

Last change on this file since 6480 was 6443, checked in by uli, 14 years ago

Add tests for applicants root logger.

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