source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_root.py @ 15833

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

Rename all non-locales stuff from sirp to kofa.

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1## $Id: test_root.py 7811 2012-03-08 19:00:51Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19Test applicants root.
20"""
21import logging
22import shutil
23import tempfile
24import unittest
25from StringIO import StringIO
26from zope.component.hooks import setSite, clearSite
27from zope.interface.verify import verifyClass, verifyObject
28from waeup.kofa.app import University
29from waeup.kofa.applicants import (
30    interfaces, Applicant, ApplicantsContainer,
31    )
32from waeup.kofa.applicants.root import (
33    ApplicantsRoot, ApplicantsPlugin,
34    )
35from waeup.kofa.testing import (
36    FunctionalLayer, FunctionalTestCase, remove_logger)
37
38
39class FakeSite(dict):
40    pass
41
42class ApplicantsRootTestCase(FunctionalTestCase):
43
44    layer = FunctionalLayer
45
46    def setUp(self):
47        remove_logger('waeup.kofa.app.applicants')
48        super(ApplicantsRootTestCase, self).setUp()
49        # Setup a sample site for each test
50        # Prepopulate the ZODB...
51        app = University()
52        self.getRootFolder()['app'] = app
53        self.app = self.getRootFolder()['app']
54        setSite(self.app)
55        self.dc_root = tempfile.mkdtemp()
56        app['datacenter'].setStoragePath(self.dc_root)
57        return
58
59    def tearDown(self):
60        super(ApplicantsRootTestCase, self).tearDown()
61        shutil.rmtree(self.dc_root)
62        clearSite()
63        return
64
65    def test_interfaces(self):
66        # Make sure the correct interfaces are implemented.
67        self.assertTrue(
68            verifyClass(
69                interfaces.IApplicantsRoot, ApplicantsRoot)
70            )
71        self.assertTrue(
72            verifyObject(
73                interfaces.IApplicantsRoot, ApplicantsRoot())
74            )
75        return
76
77    def test_logger(self):
78        # We can get a logger from root
79        logger = self.app['applicants'].logger
80        assert logger is not None
81        assert logger.name == 'waeup.kofa.app.applicants'
82        handlers = logger.handlers
83        assert len(handlers) == 1
84        filename = logger.handlers[0].baseFilename
85        assert filename.endswith('applicants.log')
86        assert filename.startswith(self.dc_root)
87
88    def test_logger_multiple(self):
89        # Make sure the logger is still working after 2nd call
90        # First time we call it, it might be registered
91        logger = self.app['applicants'].logger
92        # At second call the already registered logger should be returned
93        logger = self.app['applicants'].logger
94        assert logger is not None
95        assert logger.name == 'waeup.kofa.app.applicants'
96        handlers = logger.handlers
97        assert len(handlers) == 1
98        filename = logger.handlers[0].baseFilename
99        assert filename.endswith('applicants.log')
100        assert filename.startswith(self.dc_root)
101
102class ApplicantsRootPluginTestCase(unittest.TestCase):
103    def create_logger(self):
104        # create a logger suitable for local tests.
105        test_logger = logging.getLogger('waeup.kofa.applicants.testlogger')
106        log = StringIO()
107        handler = logging.StreamHandler(log)
108        handler.setLevel(logging.DEBUG)
109        test_logger.addHandler(handler)
110        test_logger.setLevel(logging.DEBUG)
111        self.logger = test_logger
112        self.log = log
113        self.handler = handler
114        return self.logger
115
116    def remove_logger(self):
117        del self.handler
118        del self.logger
119        del self.log
120        pass
121
122    def get_log(self):
123        self.log.seek(0)
124        return self.log.read()
125
126    def setUp(self):
127        self.create_logger()
128        return
129
130    def tearDown(self):
131        self.remove_logger()
132        return
133
134    # Real tests start here...
135    def test_pluginsetup(self):
136        # Make sure we can add ApplicantsRoot to sites.
137        site = FakeSite()
138        plugin = ApplicantsPlugin()
139        plugin.setup(site, 'blah', self.logger)
140        self.assertTrue('applicants' in site.keys())
141        log = self.get_log()
142        self.assertTrue('Installed applicants root.' in log)
143        return
144
145    def test_update_new(self):
146        # Run update on a site without applicants root.
147        site = FakeSite()
148        plugin = ApplicantsPlugin()
149        plugin.update(site, 'blah', self.logger)
150        self.assertTrue('applicants' in site.keys())
151        log = self.get_log()
152        self.assertTrue('Updating site at <Unnamed Site>' in log)
153        self.assertTrue('Installed applicants root.' in log)
154        return
155
156    def test_update_outdated(self):
157        # Run update on a site with outdated applicants root.
158        site = FakeSite()
159        root = object() # # This is not a proper applicants root
160        site['applicants'] = root
161        plugin = ApplicantsPlugin()
162        plugin.update(site, 'blah', self.logger)
163        self.assertTrue(site['applicants'] is not root)
164        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
165        log = self.get_log()
166        self.assertTrue('Outdated applicants folder detected' in log)
167        self.assertTrue('Updating site at <Unnamed Site>' in log)
168        self.assertTrue('Installed applicants root.' in log)
169        return
170
171    def test_update_uptodate(self):
172        # Run update on a site with proper applicants root.
173        site = FakeSite()
174        root = ApplicantsRoot()
175        site['applicants'] = root
176        plugin = ApplicantsPlugin()
177        plugin.update(site, 'blah', self.logger)
178        self.assertTrue(site['applicants'] is root)
179        log = self.get_log()
180        self.assertTrue('Updating site at <Unnamed Site>' in log)
181        self.assertTrue('Nothing to do' in log)
182        return
183
184    def test_update_log(self):
185        # Check that sitename is used in log messages on updates.
186        site = FakeSite()
187        site.__name__ = 'my_site'
188        plugin = ApplicantsPlugin()
189        plugin.update(site, 'blah', self.logger)
190        log = self.get_log()
191        self.assertTrue('Updating site at my_site.' in log)
192        return
193
194def suite():
195    suite = unittest.TestSuite()
196    for testcase in [
197            ApplicantsRootTestCase,
198            ApplicantsRootPluginTestCase,
199            ]:
200        suite.addTests(unittest.makeSuite(testcase))
201    return suite
202
203test_suite = suite
Note: See TracBrowser for help on using the repository browser.