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

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

Use a better understandable string when reporting applicant plugin actions on unnamed sites.

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