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

Last change on this file since 5889 was 5867, checked in by uli, 14 years ago

Remove implementation and tests of addApplicantsContainer method.

File size: 6.3 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 unittest
27from StringIO import StringIO
28from zope.component.hooks import setSite, clearSite
29from zope.interface.verify import verifyClass, verifyObject
30from zope.site import LocalSiteManager
31from waeup.sirp.app import University
32from waeup.sirp.applicants import (
33    interfaces, application_exists, get_applicant_data, Applicant,
34    ApplicantsContainer,
35    )
36from waeup.sirp.applicants.root import (
37    ApplicantsRoot, ApplicantsPlugin,
38    )
39from waeup.sirp.testing import WAeUPSIRPUnitTestLayer
40
41
42class FakeSite(dict):
43    pass
44
45class ApplicantsRootTestCase(unittest.TestCase):
46
47    def test_interfaces(self):
48        # Make sure the correct interfaces are implemented.
49        self.assertTrue(
50            verifyClass(
51                interfaces.IApplicantsRoot, ApplicantsRoot)
52            )
53        self.assertTrue(
54            verifyObject(
55                interfaces.IApplicantsRoot, ApplicantsRoot())
56            )
57        return
58
59class ApplicantsRootPluginTestCase(unittest.TestCase):
60    def create_logger(self):
61        # create a logger suitable for local tests.
62        test_logger = logging.getLogger('waeup.sirp.applicants.testlogger')
63        log = StringIO()
64        handler = logging.StreamHandler(log)
65        handler.setLevel(logging.DEBUG)
66        test_logger.addHandler(handler)
67        test_logger.setLevel(logging.DEBUG)
68        self.logger = test_logger
69        self.log = log
70        self.handler = handler
71        return self.logger
72
73    def remove_logger(self):
74        del self.handler
75        del self.logger
76        del self.log
77        pass
78
79    def get_log(self):
80        self.log.seek(0)
81        return self.log.read()
82
83    def setUp(self):
84        self.create_logger()
85        return
86
87    def tearDown(self):
88        self.remove_logger()
89        return
90
91    # Real tests start here...
92    def test_pluginsetup(self):
93        # Make sure we can add ApplicantsRoot to sites.
94        site = FakeSite()
95        plugin = ApplicantsPlugin()
96        plugin.setup(site, 'blah', self.logger)
97        self.assertTrue('applicants' in site.keys())
98        log = self.get_log()
99        self.assertTrue('Installed applicants root.' in log)
100        return
101
102    def test_update_new(self):
103        # Run update on a site without applicants root.
104        site = FakeSite()
105        plugin = ApplicantsPlugin()
106        plugin.update(site, 'blah', self.logger)
107        self.assertTrue('applicants' in site.keys())
108        log = self.get_log()
109        self.assertTrue('Updating site at <Unnamed Site>' in log)
110        self.assertTrue('Installed applicants root.' in log)
111        return
112
113    def test_update_outdated(self):
114        # Run update on a site with outdated applicants root.
115        site = FakeSite()
116        root = object() # # This is not a proper applicants root
117        site['applicants'] = root
118        plugin = ApplicantsPlugin()
119        plugin.update(site, 'blah', self.logger)
120        self.assertTrue(site['applicants'] is not root)
121        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
122        log = self.get_log()
123        self.assertTrue('Outdated applicants folder detected' in log)
124        self.assertTrue('Updating site at <Unnamed Site>' in log)
125        self.assertTrue('Installed applicants root.' in log)
126        return
127
128    def test_update_uptodate(self):
129        # Run update on a site with proper applicants root.
130        site = FakeSite()
131        root = ApplicantsRoot()
132        site['applicants'] = root
133        plugin = ApplicantsPlugin()
134        plugin.update(site, 'blah', self.logger)
135        self.assertTrue(site['applicants'] is root)
136        log = self.get_log()
137        self.assertTrue('Updating site at <Unnamed Site>' in log)
138        self.assertTrue('Nothing to do' in log)
139        return
140
141    def test_update_log(self):
142        # Check that sitename is used in log messages on updates.
143        site = FakeSite()
144        site.__name__ = 'my_site'
145        plugin = ApplicantsPlugin()
146        plugin.update(site, 'blah', self.logger)
147        log = self.get_log()
148        self.assertTrue('Updating site at my_site.' in log)
149        return
150
151applicant = Applicant()
152fake_apps = {
153    'cest10' : ApplicantsContainer(),
154    'cest11' : ApplicantsContainer(),
155    }
156
157fake_apps['cest10']['APP-99999'] = applicant
158
159class HelperToolsTest(unittest.TestCase):
160
161    layer = WAeUPSIRPUnitTestLayer
162   
163    def setUp(self):
164        self.app = University()
165        # Insert some applicants...
166        for container, data in fake_apps.items():
167            self.app['applicants'][container] = data
168        self.app.setSiteManager(LocalSiteManager(self.app))
169        setSite(self.app)
170        return
171
172    def tearDown(self):
173        clearSite()
174        return
175
176    def test_application_exists(self):
177        result = application_exists('APP-99999')
178        assert result is True
179
180        result = application_exists('APP-44444')
181        assert result is False
182        return
183
184    def test_get_applicant_data(self):
185        result = get_applicant_data('APP-99999')
186        self.assertEqual(result, applicant)
187        return
188
189    def test_get_applicant_data_none(self):
190        result = get_applicant_data('NOT-EXISTIING')
191        assert result is None
192        return
193   
194def suite():
195    suite = unittest.TestSuite()
196    for testcase in [
197            ApplicantsRootTestCase,
198            ApplicantsRootPluginTestCase,
199            HelperToolsTest,
200            ]:
201        suite.addTests(unittest.makeSuite(testcase))
202    return suite
203
204test_suite = suite
Note: See TracBrowser for help on using the repository browser.