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

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

Add test for addApplicantsContainer method.

File size: 5.1 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.interface.verify import verifyClass, verifyObject
29from waeup.sirp.applicants import interfaces
30from waeup.sirp.applicants.root import (
31    ApplicantsRoot, ApplicantsPlugin,
32    )
33
34class FakeSite(dict):
35    pass
36
37class ApplicantsRootTestCase(unittest.TestCase):
38
39    def test_interfaces(self):
40        # Make sure the correct interfaces are implemented.
41        self.assertTrue(
42            verifyClass(
43                interfaces.IApplicantsRoot, ApplicantsRoot)
44            )
45        self.assertTrue(
46            verifyObject(
47                interfaces.IApplicantsRoot, ApplicantsRoot())
48            )
49        return
50
51    def test_add_container(self):
52        root = ApplicantsRoot()
53        fake_container = object()
54        root.addApplicantsContainer(fake_container, name='foo')
55        self.assertTrue(root['foo'] ==  fake_container)
56        return
57
58class ApplicantsRootPluginTestCase(unittest.TestCase):
59    def create_logger(self):
60        # create a logger suitable for local tests.
61        test_logger = logging.getLogger('waeup.sirp.applicants.testlogger')
62        log = StringIO()
63        handler = logging.StreamHandler(log)
64        handler.setLevel(logging.DEBUG)
65        test_logger.addHandler(handler)
66        test_logger.setLevel(logging.DEBUG)
67        self.logger = test_logger
68        self.log = log
69        self.handler = handler
70        return self.logger
71
72    def remove_logger(self):
73        del self.handler
74        del self.logger
75        del self.log
76        pass
77
78    def get_log(self):
79        self.log.seek(0)
80        return self.log.read()
81
82    def setUp(self):
83        self.create_logger()
84        return
85
86    def tearDown(self):
87        self.remove_logger()
88        return
89
90    # Real tests start here...
91    def test_pluginsetup(self):
92        # Make sure we can add ApplicantsRoot to sites.
93        site = FakeSite()
94        plugin = ApplicantsPlugin()
95        plugin.setup(site, 'blah', self.logger)
96        self.assertTrue('applicants' in site.keys())
97        log = self.get_log()
98        self.assertTrue('Installed applicants root.' in log)
99        return
100
101    def test_update_new(self):
102        # Run update on a site without applicants root.
103        site = FakeSite()
104        plugin = ApplicantsPlugin()
105        plugin.update(site, 'blah', self.logger)
106        self.assertTrue('applicants' in site.keys())
107        log = self.get_log()
108        self.assertTrue('Updating site at <Unnamed Site>' in log)
109        self.assertTrue('Installed applicants root.' in log)
110        return
111
112    def test_update_outdated(self):
113        # Run update on a site with outdated applicants root.
114        site = FakeSite()
115        root = object() # # This is not a proper applicants root
116        site['applicants'] = root
117        plugin = ApplicantsPlugin()
118        plugin.update(site, 'blah', self.logger)
119        self.assertTrue(site['applicants'] is not root)
120        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
121        log = self.get_log()
122        self.assertTrue('Outdated applicants folder detected' in log)
123        self.assertTrue('Updating site at <Unnamed Site>' in log)
124        self.assertTrue('Installed applicants root.' in log)
125        return
126
127    def test_update_uptodate(self):
128        # Run update on a site with proper applicants root.
129        site = FakeSite()
130        root = ApplicantsRoot()
131        site['applicants'] = root
132        plugin = ApplicantsPlugin()
133        plugin.update(site, 'blah', self.logger)
134        self.assertTrue(site['applicants'] is root)
135        log = self.get_log()
136        self.assertTrue('Updating site at <Unnamed Site>' in log)
137        self.assertTrue('Nothing to do' in log)
138        return
139
140    def test_update_log(self):
141        # Check that sitename is used in log messages on updates.
142        site = FakeSite()
143        site.__name__ = 'my_site'
144        plugin = ApplicantsPlugin()
145        plugin.update(site, 'blah', self.logger)
146        log = self.get_log()
147        self.assertTrue('Updating site at my_site.' in log)
148        return
149
150def suite():
151    suite = unittest.TestSuite()
152    for testcase in [
153            ApplicantsRootTestCase,
154            ApplicantsRootPluginTestCase,
155            ]:
156        suite.addTests(unittest.TestLoader().loadTestsFromTestCase(testcase))
157    return suite
158
159test_suite = suite
Note: See TracBrowser for help on using the repository browser.