1 | ## $Id: test_container.py 13165 2015-07-13 07:53:26Z 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 | """ |
---|
19 | Tests for students containers. |
---|
20 | """ |
---|
21 | import tempfile |
---|
22 | import shutil |
---|
23 | from zope.interface.verify import verifyClass, verifyObject |
---|
24 | from zope.component.hooks import setSite, clearSite |
---|
25 | from waeup.kofa.app import University |
---|
26 | from waeup.kofa.university.department import Department |
---|
27 | from waeup.kofa.students.interfaces import ( |
---|
28 | IStudentsContainer, |
---|
29 | ) |
---|
30 | from waeup.kofa.students.container import ( |
---|
31 | StudentsContainer, |
---|
32 | ) |
---|
33 | from waeup.kofa.testing import ( |
---|
34 | FunctionalLayer, FunctionalTestCase, remove_logger) |
---|
35 | |
---|
36 | class StudentsContainerTestCase(FunctionalTestCase): |
---|
37 | |
---|
38 | layer = FunctionalLayer |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | remove_logger('waeup.kofa.app.students') |
---|
42 | super(StudentsContainerTestCase, self).setUp() |
---|
43 | # Setup a sample site for each test |
---|
44 | # Prepopulate the ZODB... |
---|
45 | app = University() |
---|
46 | self.getRootFolder()['app'] = app |
---|
47 | self.app = self.getRootFolder()['app'] |
---|
48 | setSite(self.app) |
---|
49 | self.dc_root = tempfile.mkdtemp() |
---|
50 | app['datacenter'].setStoragePath(self.dc_root) |
---|
51 | return |
---|
52 | |
---|
53 | def tearDown(self): |
---|
54 | super(StudentsContainerTestCase, self).tearDown() |
---|
55 | clearSite() |
---|
56 | shutil.rmtree(self.dc_root) |
---|
57 | return |
---|
58 | |
---|
59 | def test_interfaces(self): |
---|
60 | # Make sure the correct interfaces are implemented. |
---|
61 | self.assertTrue( |
---|
62 | verifyClass( |
---|
63 | IStudentsContainer, StudentsContainer) |
---|
64 | ) |
---|
65 | self.assertTrue( |
---|
66 | verifyObject( |
---|
67 | IStudentsContainer, StudentsContainer()) |
---|
68 | ) |
---|
69 | return |
---|
70 | |
---|
71 | def test_base(self): |
---|
72 | # We cannot call the fundamental methods of a base in that case |
---|
73 | container = StudentsContainer() |
---|
74 | # We cannot add arbitrary objects |
---|
75 | department = Department() |
---|
76 | self.assertRaises( |
---|
77 | TypeError, container.addStudent, department) |
---|
78 | |
---|
79 | |
---|
80 | def test_logger(self): |
---|
81 | # We can get a logger from root |
---|
82 | logger = self.app['students'].logger |
---|
83 | assert logger is not None |
---|
84 | assert logger.name == 'waeup.kofa.app.students' |
---|
85 | handlers = logger.handlers |
---|
86 | assert len(handlers) == 1 |
---|
87 | filename = logger.handlers[0].baseFilename |
---|
88 | assert filename.endswith('students.log') |
---|
89 | assert filename.startswith(self.dc_root) |
---|
90 | |
---|
91 | def test_logger_multiple(self): |
---|
92 | # Make sure the logger is still working after 2nd call |
---|
93 | # First time we call it, it might be registered |
---|
94 | logger = self.app['students'].logger |
---|
95 | # At second call the already registered logger should be returned |
---|
96 | logger = self.app['students'].logger |
---|
97 | assert logger is not None |
---|
98 | assert logger.name == 'waeup.kofa.app.students' |
---|
99 | handlers = logger.handlers |
---|
100 | assert len(handlers) == 1 |
---|
101 | filename = logger.handlers[0].baseFilename |
---|
102 | assert filename.endswith('students.log') |
---|
103 | assert filename.startswith(self.dc_root) |
---|