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

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

Set value Id for property svn:keywords in all Python files.

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