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

Last change on this file since 5827 was 5814, checked in by uli, 14 years ago

Fix test.

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