1 | # Tests for FacultyContainer. |
---|
2 | import logging |
---|
3 | import unittest |
---|
4 | from StringIO import StringIO |
---|
5 | from zope.interface.verify import verifyObject, verifyClass |
---|
6 | from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer |
---|
7 | from waeup.sirp.university.interfaces import IFacultyContainer |
---|
8 | from waeup.sirp.university import FacultyContainer |
---|
9 | from waeup.sirp.university.facultycontainer import AcademicsPlugin |
---|
10 | |
---|
11 | class FakeLogger(object): |
---|
12 | |
---|
13 | _stream = None |
---|
14 | |
---|
15 | def get_logger(self): |
---|
16 | logger = logging.getLogger('test') |
---|
17 | logger.propagate = False |
---|
18 | self._stream = StringIO() |
---|
19 | self.handler = logging.StreamHandler(self._stream) |
---|
20 | logger.setLevel(logging.DEBUG) |
---|
21 | logger.addHandler(self.handler) |
---|
22 | return logger |
---|
23 | |
---|
24 | def close(self, logger): |
---|
25 | # Remove handler from logger |
---|
26 | handler = self.handler |
---|
27 | if handler not in logger.handlers: |
---|
28 | return |
---|
29 | del logger.handlers[logger.handlers.index(handler)] |
---|
30 | return |
---|
31 | |
---|
32 | def get_messages(self): |
---|
33 | self._stream.seek(0) |
---|
34 | result = self._stream.read() |
---|
35 | self._stream.seek(0, 2) # Seek to end of stream |
---|
36 | return result |
---|
37 | |
---|
38 | class FacultyContainerTests(unittest.TestCase): |
---|
39 | |
---|
40 | def test_ifaces(self): |
---|
41 | container = FacultyContainer() |
---|
42 | self.assertTrue(verifyClass(IFacultyContainer, FacultyContainer)) |
---|
43 | self.assertTrue(verifyObject(IFacultyContainer, container)) |
---|
44 | |
---|
45 | class AcademicsPluginTests(unittest.TestCase): |
---|
46 | |
---|
47 | def setUp(self): |
---|
48 | self._logger_factory = FakeLogger() |
---|
49 | self.logger = self._logger_factory.get_logger() |
---|
50 | |
---|
51 | def tearDown(self): |
---|
52 | self._logger_factory.close(self.logger) |
---|
53 | |
---|
54 | def test_setup(self): |
---|
55 | site = dict() |
---|
56 | logger = self.logger |
---|
57 | plugin = AcademicsPlugin() |
---|
58 | plugin.setup(site, 'testsite', logger) |
---|
59 | self.assertTrue('faculties' in site.keys()) |
---|
60 | self.assertTrue(isinstance(site['faculties'], FacultyContainer)) |
---|
61 | self.assertEqual( |
---|
62 | self._logger_factory.get_messages(), |
---|
63 | 'Created faculty container for University\n' |
---|
64 | ) |
---|
65 | |
---|
66 | def test_setup_already_set_up(self): |
---|
67 | site = dict(faculties=object()) |
---|
68 | logger = self.logger |
---|
69 | plugin = AcademicsPlugin() |
---|
70 | plugin.setup(site, 'testsite', logger) |
---|
71 | self.assertTrue('faculties' in site.keys()) |
---|
72 | self.assertEqual( |
---|
73 | self._logger_factory.get_messages(), |
---|
74 | 'Could not create faculty container in WAeUP SIRP.\n' |
---|
75 | ) |
---|
76 | |
---|
77 | def test_update(self): |
---|
78 | site = dict() |
---|
79 | logger = self.logger |
---|
80 | plugin = AcademicsPlugin() |
---|
81 | plugin.update(site, 'testsite', logger) |
---|
82 | self.assertTrue('faculties' in site.keys()) |
---|
83 | self.assertTrue(isinstance(site['faculties'], FacultyContainer)) |
---|
84 | self.assertEqual( |
---|
85 | self._logger_factory.get_messages(), |
---|
86 | 'Created faculty container for University\n' |
---|
87 | ) |
---|
88 | |
---|
89 | def test_update_already_set_up(self): |
---|
90 | site = dict(faculties=object()) |
---|
91 | logger = self.logger |
---|
92 | plugin = AcademicsPlugin() |
---|
93 | plugin.update(site, 'testsite', logger) |
---|
94 | self.assertTrue('faculties' in site.keys()) |
---|
95 | self.assertEqual( |
---|
96 | self._logger_factory.get_messages(), '') |
---|