1 | ## $Id: test_configuration.py 17787 2024-05-15 06:42:58Z 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 | """Tests for configuration containers and related. |
---|
19 | """ |
---|
20 | import os |
---|
21 | import shutil |
---|
22 | import tempfile |
---|
23 | import unittest |
---|
24 | from zope.component import queryUtility |
---|
25 | from zope.component.hooks import setSite, clearSite |
---|
26 | from zope.component.interfaces import IFactory |
---|
27 | from zope.interface import verify |
---|
28 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
29 | from waeup.kofa.app import University |
---|
30 | from waeup.kofa.configuration import ( |
---|
31 | ConfigurationContainer, SessionConfiguration, SessionConfigurationFactory, |
---|
32 | ConfigurationContainerExporter, SessionConfigurationExporter, |
---|
33 | ConfigurationContainerProcessor, SessionConfigurationProcessor) |
---|
34 | from waeup.kofa.interfaces import(ICSVExporter, IBatchProcessor, |
---|
35 | IConfigurationContainer, ISessionConfiguration, ISessionConfigurationAdd) |
---|
36 | |
---|
37 | class ConfigurationTest(FunctionalTestCase): |
---|
38 | |
---|
39 | layer = FunctionalLayer |
---|
40 | |
---|
41 | def setUp(self): |
---|
42 | super(ConfigurationTest, self).setUp() |
---|
43 | self.confcontainer = ConfigurationContainer() |
---|
44 | self.sessionconf = SessionConfiguration() |
---|
45 | return |
---|
46 | |
---|
47 | def tearDown(self): |
---|
48 | super(ConfigurationTest, self).tearDown() |
---|
49 | return |
---|
50 | |
---|
51 | def test_interfaces(self): |
---|
52 | verify.verifyClass(IConfigurationContainer, ConfigurationContainer) |
---|
53 | verify.verifyObject(IConfigurationContainer, self.confcontainer) |
---|
54 | verify.verifyClass(ISessionConfiguration, SessionConfiguration) |
---|
55 | verify.verifyObject(ISessionConfiguration, self.sessionconf) |
---|
56 | verify.verifyClass(ISessionConfigurationAdd, SessionConfiguration) |
---|
57 | verify.verifyObject(ISessionConfigurationAdd, self.sessionconf) |
---|
58 | |
---|
59 | class SessionConfigurationFactoryTest(FunctionalTestCase): |
---|
60 | |
---|
61 | layer = FunctionalLayer |
---|
62 | |
---|
63 | def setUp(self): |
---|
64 | super(SessionConfigurationFactoryTest, self).setUp() |
---|
65 | self.factory = SessionConfigurationFactory() |
---|
66 | |
---|
67 | def tearDown(self): |
---|
68 | super(SessionConfigurationFactoryTest, self).tearDown() |
---|
69 | |
---|
70 | def test_interfaces(self): |
---|
71 | verify.verifyClass(IFactory, SessionConfigurationFactory) |
---|
72 | verify.verifyObject(IFactory, self.factory) |
---|
73 | |
---|
74 | def test_factory(self): |
---|
75 | obj = self.factory() |
---|
76 | assert isinstance(obj, SessionConfiguration) |
---|
77 | |
---|
78 | def test_getInterfaces(self): |
---|
79 | implemented_by = self.factory.getInterfaces() |
---|
80 | assert implemented_by.isOrExtends(ISessionConfiguration) |
---|
81 | |
---|
82 | |
---|
83 | class ConfigurationImportExportSetup(FunctionalTestCase): |
---|
84 | |
---|
85 | layer = FunctionalLayer |
---|
86 | |
---|
87 | def setUp(self): |
---|
88 | super(ConfigurationImportExportSetup, self).setUp() |
---|
89 | # Setup a sample site for each test |
---|
90 | app = University() |
---|
91 | self.dc_root = tempfile.mkdtemp() |
---|
92 | app['datacenter'].setStoragePath(self.dc_root) |
---|
93 | self.getRootFolder()['app'] = app |
---|
94 | self.app = self.getRootFolder()['app'] |
---|
95 | setSite(app) |
---|
96 | self.workdir = tempfile.mkdtemp() |
---|
97 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
98 | return |
---|
99 | |
---|
100 | def tearDown(self): |
---|
101 | super(ConfigurationImportExportSetup, self).tearDown() |
---|
102 | shutil.rmtree(self.workdir) |
---|
103 | shutil.rmtree(self.dc_root) |
---|
104 | clearSite() |
---|
105 | return |
---|
106 | |
---|
107 | class BaseConfigurationExportTests(ConfigurationImportExportSetup): |
---|
108 | |
---|
109 | def test_ifaces(self): |
---|
110 | # make sure we fullfill interface contracts |
---|
111 | obj = ConfigurationContainerExporter() |
---|
112 | verify.verifyObject(ICSVExporter, obj) |
---|
113 | verify.verifyClass(ICSVExporter, ConfigurationContainerExporter) |
---|
114 | return |
---|
115 | |
---|
116 | def test_get_as_utility(self): |
---|
117 | # we can get a faculty exporter as utility |
---|
118 | result = queryUtility(ICSVExporter, name="base_configuration") |
---|
119 | self.assertTrue(result is not None) |
---|
120 | return |
---|
121 | |
---|
122 | def test_export_all(self): |
---|
123 | exporter = ConfigurationContainerExporter() |
---|
124 | exporter.export_all(self.app, self.outfile) |
---|
125 | result = open(self.outfile, 'rb').read() |
---|
126 | self.assertTrue( |
---|
127 | 'acronym,carry_over,current_academic_session,email_admin,' |
---|
128 | 'email_subject,export_disabled_message,frontpage,' |
---|
129 | 'maintmode_enabled_by,name,name_admin,next_matric_integer,' |
---|
130 | 'next_matric_integer_2,next_matric_integer_3,' |
---|
131 | 'next_matric_integer_4,smtp_mailer,curr_stud_id\r\n' |
---|
132 | 'WAeUP.Kofa,0,,contact@waeup.org,Kofa Contact,,' |
---|
133 | '"<h1>Welcome to WAeUP.Kofa\n<br>' in result |
---|
134 | ) |
---|
135 | return |
---|
136 | |
---|
137 | class SessionConfigurationExportTests(ConfigurationImportExportSetup): |
---|
138 | |
---|
139 | def test_ifaces(self): |
---|
140 | # make sure we fullfill interface contracts |
---|
141 | obj = SessionConfigurationExporter() |
---|
142 | verify.verifyObject(ICSVExporter, obj) |
---|
143 | verify.verifyClass(ICSVExporter, SessionConfigurationExporter) |
---|
144 | return |
---|
145 | |
---|
146 | def test_get_as_utility(self): |
---|
147 | # we can get a faculty exporter as utility |
---|
148 | result = queryUtility(ICSVExporter, name="sessionconfigurations") |
---|
149 | self.assertTrue(result is not None) |
---|
150 | return |
---|
151 | |
---|
152 | def test_export_all(self): |
---|
153 | self.sessionconfig = SessionConfiguration() |
---|
154 | self.sessionconfig.academic_session = 2022 |
---|
155 | self.sessionconfig.clearance_fee = 3000.6 |
---|
156 | self.app['configuration'].addSessionConfiguration(self.sessionconfig) |
---|
157 | exporter = SessionConfigurationExporter() |
---|
158 | exporter.export_all(self.app, self.outfile) |
---|
159 | result = open(self.outfile, 'rb').read() |
---|
160 | self.assertEqual(result, |
---|
161 | 'academic_session,booking_fee,clearance_enabled,clearance_fee,' |
---|
162 | 'coursereg_deadline,late_registration_fee,maint_fee,' |
---|
163 | 'payment_disabled,transcript_fee,transfer_fee\r\n' |
---|
164 | '2022,0.0,0,3000.6,,0.0,0.0,[],0.0,0.0\r\n' |
---|
165 | ) |
---|
166 | return |
---|
167 | |
---|
168 | BASECONFIG_SAMPLE_DATA = open( |
---|
169 | os.path.join(os.path.dirname(__file__), 'sample_baseconfig_data.csv'), |
---|
170 | 'rb').read() |
---|
171 | |
---|
172 | BASECONFIG_HEADER_FIELDS = BASECONFIG_SAMPLE_DATA.split( |
---|
173 | '\n')[0].split(',') |
---|
174 | |
---|
175 | class BaseConfigurationProcessorTests(ConfigurationImportExportSetup): |
---|
176 | |
---|
177 | layer = FunctionalLayer |
---|
178 | |
---|
179 | def setUp(self): |
---|
180 | super(BaseConfigurationProcessorTests, self).setUp() |
---|
181 | self.processor = ConfigurationContainerProcessor() |
---|
182 | |
---|
183 | def test_interface(self): |
---|
184 | # Make sure we fulfill the interface contracts. |
---|
185 | assert verify.verifyObject(IBatchProcessor, self.processor) is True |
---|
186 | assert verify.verifyClass( |
---|
187 | IBatchProcessor, ConfigurationContainerProcessor) is True |
---|
188 | |
---|
189 | def test_import(self): |
---|
190 | self.csv_file = os.path.join( |
---|
191 | self.workdir, 'sample_baseconfig_data.csv') |
---|
192 | open(self.csv_file, 'wb').write(BASECONFIG_SAMPLE_DATA) |
---|
193 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
194 | self.csv_file, BASECONFIG_HEADER_FIELDS,'update') |
---|
195 | self.assertEqual(num_warns,0) |
---|
196 | configcontainer = self.processor.getEntry(dict(), self.app) |
---|
197 | self.assertEqual(configcontainer.frontpage, 'Hello World') |
---|
198 | |
---|
199 | SESSIONCONFIG_SAMPLE_DATA = open( |
---|
200 | os.path.join(os.path.dirname(__file__), 'sample_sessionconfig_data.csv'), |
---|
201 | 'rb').read() |
---|
202 | |
---|
203 | SESSIONCONFIG_HEADER_FIELDS = SESSIONCONFIG_SAMPLE_DATA.split( |
---|
204 | '\n')[0].split(',') |
---|
205 | |
---|
206 | class SessionConfigurationProcessorTests(ConfigurationImportExportSetup): |
---|
207 | |
---|
208 | layer = FunctionalLayer |
---|
209 | |
---|
210 | def setUp(self): |
---|
211 | super(SessionConfigurationProcessorTests, self).setUp() |
---|
212 | self.processor = SessionConfigurationProcessor() |
---|
213 | |
---|
214 | def test_interface(self): |
---|
215 | # Make sure we fulfill the interface contracts. |
---|
216 | assert verify.verifyObject(IBatchProcessor, self.processor) is True |
---|
217 | assert verify.verifyClass( |
---|
218 | IBatchProcessor, SessionConfigurationProcessor) is True |
---|
219 | |
---|
220 | def test_import(self): |
---|
221 | self.csv_file = os.path.join( |
---|
222 | self.workdir, 'sample_sessionconfig_data.csv') |
---|
223 | open(self.csv_file, 'wb').write(SESSIONCONFIG_SAMPLE_DATA) |
---|
224 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
225 | self.csv_file, SESSIONCONFIG_HEADER_FIELDS,'create') |
---|
226 | self.assertEqual(num_warns,0) |
---|
227 | sessionconfig = self.processor.getEntry({'academic_session':'2017'}, self.app) |
---|
228 | self.assertEqual(sessionconfig.payment_disabled[0], 'sf_all') |
---|
229 | |
---|
230 | |
---|