[7195] | 1 | ## $Id: test_facultiescontainer.py 7819 2012-03-08 22:28:46Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | |
---|
[7333] | 19 | # Tests for FacultiesContainer. |
---|
[6576] | 20 | import logging |
---|
[6574] | 21 | import unittest |
---|
[6576] | 22 | from StringIO import StringIO |
---|
[6574] | 23 | from zope.interface.verify import verifyObject, verifyClass |
---|
[7811] | 24 | from waeup.kofa.testing import FunctionalTestCase, FunctionalLayer |
---|
| 25 | from waeup.kofa.university.interfaces import IFacultiesContainer |
---|
| 26 | from waeup.kofa.university import FacultiesContainer |
---|
| 27 | from waeup.kofa.university.facultiescontainer import AcademicsPlugin |
---|
[6574] | 28 | |
---|
[6576] | 29 | class FakeLogger(object): |
---|
| 30 | |
---|
| 31 | _stream = None |
---|
| 32 | |
---|
| 33 | def get_logger(self): |
---|
| 34 | logger = logging.getLogger('test') |
---|
| 35 | logger.propagate = False |
---|
| 36 | self._stream = StringIO() |
---|
| 37 | self.handler = logging.StreamHandler(self._stream) |
---|
| 38 | logger.setLevel(logging.DEBUG) |
---|
| 39 | logger.addHandler(self.handler) |
---|
| 40 | return logger |
---|
| 41 | |
---|
| 42 | def close(self, logger): |
---|
| 43 | # Remove handler from logger |
---|
| 44 | handler = self.handler |
---|
| 45 | if handler not in logger.handlers: |
---|
| 46 | return |
---|
| 47 | del logger.handlers[logger.handlers.index(handler)] |
---|
| 48 | return |
---|
| 49 | |
---|
| 50 | def get_messages(self): |
---|
| 51 | self._stream.seek(0) |
---|
| 52 | result = self._stream.read() |
---|
| 53 | self._stream.seek(0, 2) # Seek to end of stream |
---|
| 54 | return result |
---|
| 55 | |
---|
[7333] | 56 | class FacultiesContainerTests(unittest.TestCase): |
---|
[6574] | 57 | |
---|
| 58 | def test_ifaces(self): |
---|
[7333] | 59 | container = FacultiesContainer() |
---|
| 60 | self.assertTrue(verifyClass(IFacultiesContainer, FacultiesContainer)) |
---|
| 61 | self.assertTrue(verifyObject(IFacultiesContainer, container)) |
---|
[6576] | 62 | |
---|
| 63 | class AcademicsPluginTests(unittest.TestCase): |
---|
| 64 | |
---|
| 65 | def setUp(self): |
---|
| 66 | self._logger_factory = FakeLogger() |
---|
| 67 | self.logger = self._logger_factory.get_logger() |
---|
| 68 | |
---|
| 69 | def tearDown(self): |
---|
| 70 | self._logger_factory.close(self.logger) |
---|
| 71 | |
---|
| 72 | def test_setup(self): |
---|
| 73 | site = dict() |
---|
| 74 | logger = self.logger |
---|
| 75 | plugin = AcademicsPlugin() |
---|
| 76 | plugin.setup(site, 'testsite', logger) |
---|
| 77 | self.assertTrue('faculties' in site.keys()) |
---|
[7333] | 78 | self.assertTrue(isinstance(site['faculties'], FacultiesContainer)) |
---|
[6576] | 79 | self.assertEqual( |
---|
| 80 | self._logger_factory.get_messages(), |
---|
[7333] | 81 | 'Container for faculties created\n' |
---|
[6576] | 82 | ) |
---|
| 83 | |
---|
| 84 | def test_setup_already_set_up(self): |
---|
| 85 | site = dict(faculties=object()) |
---|
| 86 | logger = self.logger |
---|
| 87 | plugin = AcademicsPlugin() |
---|
| 88 | plugin.setup(site, 'testsite', logger) |
---|
| 89 | self.assertTrue('faculties' in site.keys()) |
---|
| 90 | self.assertEqual( |
---|
| 91 | self._logger_factory.get_messages(), |
---|
[7819] | 92 | 'Could not create container for faculties in Kofa.\n' |
---|
[6576] | 93 | ) |
---|
| 94 | |
---|
| 95 | def test_update(self): |
---|
| 96 | site = dict() |
---|
| 97 | logger = self.logger |
---|
| 98 | plugin = AcademicsPlugin() |
---|
| 99 | plugin.update(site, 'testsite', logger) |
---|
| 100 | self.assertTrue('faculties' in site.keys()) |
---|
[7333] | 101 | self.assertTrue(isinstance(site['faculties'], FacultiesContainer)) |
---|
[6576] | 102 | self.assertEqual( |
---|
| 103 | self._logger_factory.get_messages(), |
---|
[7333] | 104 | 'Container for faculties created\n' |
---|
[6576] | 105 | ) |
---|
| 106 | |
---|
| 107 | def test_update_already_set_up(self): |
---|
| 108 | site = dict(faculties=object()) |
---|
| 109 | logger = self.logger |
---|
| 110 | plugin = AcademicsPlugin() |
---|
| 111 | plugin.update(site, 'testsite', logger) |
---|
| 112 | self.assertTrue('faculties' in site.keys()) |
---|
| 113 | self.assertEqual( |
---|
| 114 | self._logger_factory.get_messages(), '') |
---|