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

Last change on this file since 6361 was 6120, checked in by uli, 14 years ago

Update helper tests to use valid ApplicantsContainer? and Applicant
instances and run all this in a functional environment. We need that
expensive setup because working catalogs require a fully functional
setup.

File size: 6.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(unittest.TestCase):
51
52    def test_interfaces(self):
53        # Make sure the correct interfaces are implemented.
54        self.assertTrue(
55            verifyClass(
56                interfaces.IApplicantsRoot, ApplicantsRoot)
57            )
58        self.assertTrue(
59            verifyObject(
60                interfaces.IApplicantsRoot, ApplicantsRoot())
61            )
62        return
63
64class ApplicantsRootPluginTestCase(unittest.TestCase):
65    def create_logger(self):
66        # create a logger suitable for local tests.
67        test_logger = logging.getLogger('waeup.sirp.applicants.testlogger')
68        log = StringIO()
69        handler = logging.StreamHandler(log)
70        handler.setLevel(logging.DEBUG)
71        test_logger.addHandler(handler)
72        test_logger.setLevel(logging.DEBUG)
73        self.logger = test_logger
74        self.log = log
75        self.handler = handler
76        return self.logger
77
78    def remove_logger(self):
79        del self.handler
80        del self.logger
81        del self.log
82        pass
83
84    def get_log(self):
85        self.log.seek(0)
86        return self.log.read()
87
88    def setUp(self):
89        self.create_logger()
90        return
91
92    def tearDown(self):
93        self.remove_logger()
94        return
95
96    # Real tests start here...
97    def test_pluginsetup(self):
98        # Make sure we can add ApplicantsRoot to sites.
99        site = FakeSite()
100        plugin = ApplicantsPlugin()
101        plugin.setup(site, 'blah', self.logger)
102        self.assertTrue('applicants' in site.keys())
103        log = self.get_log()
104        self.assertTrue('Installed applicants root.' in log)
105        return
106
107    def test_update_new(self):
108        # Run update on a site without applicants root.
109        site = FakeSite()
110        plugin = ApplicantsPlugin()
111        plugin.update(site, 'blah', self.logger)
112        self.assertTrue('applicants' in site.keys())
113        log = self.get_log()
114        self.assertTrue('Updating site at <Unnamed Site>' in log)
115        self.assertTrue('Installed applicants root.' in log)
116        return
117
118    def test_update_outdated(self):
119        # Run update on a site with outdated applicants root.
120        site = FakeSite()
121        root = object() # # This is not a proper applicants root
122        site['applicants'] = root
123        plugin = ApplicantsPlugin()
124        plugin.update(site, 'blah', self.logger)
125        self.assertTrue(site['applicants'] is not root)
126        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
127        log = self.get_log()
128        self.assertTrue('Outdated applicants folder detected' in log)
129        self.assertTrue('Updating site at <Unnamed Site>' in log)
130        self.assertTrue('Installed applicants root.' in log)
131        return
132
133    def test_update_uptodate(self):
134        # Run update on a site with proper applicants root.
135        site = FakeSite()
136        root = ApplicantsRoot()
137        site['applicants'] = root
138        plugin = ApplicantsPlugin()
139        plugin.update(site, 'blah', self.logger)
140        self.assertTrue(site['applicants'] is root)
141        log = self.get_log()
142        self.assertTrue('Updating site at <Unnamed Site>' in log)
143        self.assertTrue('Nothing to do' in log)
144        return
145
146    def test_update_log(self):
147        # Check that sitename is used in log messages on updates.
148        site = FakeSite()
149        site.__name__ = 'my_site'
150        plugin = ApplicantsPlugin()
151        plugin.update(site, 'blah', self.logger)
152        log = self.get_log()
153        self.assertTrue('Updating site at my_site.' in log)
154        return
155
156
157class HelperToolsTest(FunctionalTestCase):
158
159    layer = FunctionalLayer
160
161    def setUp(self):
162        super(HelperToolsTest, self).setUp()
163
164        # Setup a sample site for each test
165        app = University()
166        self.dc_root = tempfile.mkdtemp()
167        app['datacenter'].setStoragePath(self.dc_root)
168
169        # Prepopulate the ZODB...
170        self.getRootFolder()['app'] = app
171        self.app = self.getRootFolder()['app']
172        setSite(self.app)
173
174        # Insert some applicants containers and applicants...
175        self.container1 = ApplicantsContainer()
176        self.app['applicants']['foo'] = self.container1
177        self.container2 = ApplicantsContainer()
178        self.app['applicants']['bar'] = self.container2
179        self.ac = u'APP-99999'
180        self.applicant = Applicant()
181        self.applicant.access_code = self.ac
182        self.app['applicants']['foo'][self.ac] = self.applicant
183        return
184
185    def tearDown(self):
186        super(HelperToolsTest, self).tearDown()
187        shutil.rmtree(self.dc_root)
188        return
189
190    def test_application_exists(self):
191        result = application_exists('APP-99999')
192        assert result is True
193
194        result = application_exists('APP-44444')
195        assert result is False
196        return
197
198    def test_get_applicant_data(self):
199        result = get_applicant_data('APP-99999')
200        self.assertEqual(result, self.applicant)
201        return
202
203    def test_get_applicant_data_none(self):
204        result = get_applicant_data('NOT-EXISTIING')
205        assert result is None
206        return
207
208def suite():
209    suite = unittest.TestSuite()
210    for testcase in [
211            ApplicantsRootTestCase,
212            ApplicantsRootPluginTestCase,
213            HelperToolsTest,
214            ]:
215        suite.addTests(unittest.makeSuite(testcase))
216    return suite
217
218test_suite = suite
Note: See TracBrowser for help on using the repository browser.