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

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

Add test to check applicants root logger.

File size: 7.8 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
96class ApplicantsRootPluginTestCase(unittest.TestCase):
97    def create_logger(self):
98        # create a logger suitable for local tests.
99        test_logger = logging.getLogger('waeup.sirp.applicants.testlogger')
100        log = StringIO()
101        handler = logging.StreamHandler(log)
102        handler.setLevel(logging.DEBUG)
103        test_logger.addHandler(handler)
104        test_logger.setLevel(logging.DEBUG)
105        self.logger = test_logger
106        self.log = log
107        self.handler = handler
108        return self.logger
109
110    def remove_logger(self):
111        del self.handler
112        del self.logger
113        del self.log
114        pass
115
116    def get_log(self):
117        self.log.seek(0)
118        return self.log.read()
119
120    def setUp(self):
121        self.create_logger()
122        return
123
124    def tearDown(self):
125        self.remove_logger()
126        return
127
128    # Real tests start here...
129    def test_pluginsetup(self):
130        # Make sure we can add ApplicantsRoot to sites.
131        site = FakeSite()
132        plugin = ApplicantsPlugin()
133        plugin.setup(site, 'blah', self.logger)
134        self.assertTrue('applicants' in site.keys())
135        log = self.get_log()
136        self.assertTrue('Installed applicants root.' in log)
137        return
138
139    def test_update_new(self):
140        # Run update on a site without applicants root.
141        site = FakeSite()
142        plugin = ApplicantsPlugin()
143        plugin.update(site, 'blah', self.logger)
144        self.assertTrue('applicants' in site.keys())
145        log = self.get_log()
146        self.assertTrue('Updating site at <Unnamed Site>' in log)
147        self.assertTrue('Installed applicants root.' in log)
148        return
149
150    def test_update_outdated(self):
151        # Run update on a site with outdated applicants root.
152        site = FakeSite()
153        root = object() # # This is not a proper applicants root
154        site['applicants'] = root
155        plugin = ApplicantsPlugin()
156        plugin.update(site, 'blah', self.logger)
157        self.assertTrue(site['applicants'] is not root)
158        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
159        log = self.get_log()
160        self.assertTrue('Outdated applicants folder detected' in log)
161        self.assertTrue('Updating site at <Unnamed Site>' in log)
162        self.assertTrue('Installed applicants root.' in log)
163        return
164
165    def test_update_uptodate(self):
166        # Run update on a site with proper applicants root.
167        site = FakeSite()
168        root = ApplicantsRoot()
169        site['applicants'] = root
170        plugin = ApplicantsPlugin()
171        plugin.update(site, 'blah', self.logger)
172        self.assertTrue(site['applicants'] is root)
173        log = self.get_log()
174        self.assertTrue('Updating site at <Unnamed Site>' in log)
175        self.assertTrue('Nothing to do' in log)
176        return
177
178    def test_update_log(self):
179        # Check that sitename is used in log messages on updates.
180        site = FakeSite()
181        site.__name__ = 'my_site'
182        plugin = ApplicantsPlugin()
183        plugin.update(site, 'blah', self.logger)
184        log = self.get_log()
185        self.assertTrue('Updating site at my_site.' in log)
186        return
187
188
189class HelperToolsTest(FunctionalTestCase):
190
191    layer = FunctionalLayer
192
193    def setUp(self):
194        super(HelperToolsTest, self).setUp()
195
196        # Setup a sample site for each test
197        app = University()
198        self.dc_root = tempfile.mkdtemp()
199        app['datacenter'].setStoragePath(self.dc_root)
200
201        # Prepopulate the ZODB...
202        self.getRootFolder()['app'] = app
203        self.app = self.getRootFolder()['app']
204        setSite(self.app)
205
206        # Insert some applicants containers and applicants...
207        self.container1 = ApplicantsContainer()
208        self.app['applicants']['foo'] = self.container1
209        self.container2 = ApplicantsContainer()
210        self.app['applicants']['bar'] = self.container2
211        self.ac = u'APP-99999'
212        self.applicant = Applicant()
213        self.applicant.access_code = self.ac
214        self.app['applicants']['foo'][self.ac] = self.applicant
215        return
216
217    def tearDown(self):
218        super(HelperToolsTest, self).tearDown()
219        shutil.rmtree(self.dc_root)
220        return
221
222    def test_application_exists(self):
223        result = application_exists('APP-99999')
224        assert result is True
225
226        result = application_exists('APP-44444')
227        assert result is False
228        return
229
230    def test_get_applicant_data(self):
231        result = get_applicant_data('APP-99999')
232        self.assertEqual(result, self.applicant)
233        return
234
235    def test_get_applicant_data_none(self):
236        result = get_applicant_data('NOT-EXISTIING')
237        assert result is None
238        return
239
240def suite():
241    suite = unittest.TestSuite()
242    for testcase in [
243            ApplicantsRootTestCase,
244            ApplicantsRootPluginTestCase,
245            HelperToolsTest,
246            ]:
247        suite.addTests(unittest.makeSuite(testcase))
248    return suite
249
250test_suite = suite
Note: See TracBrowser for help on using the repository browser.