1 | ## |
---|
2 | ## test_jambtables.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Thu Jun 24 08:08:12 2010 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2010 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 | """ |
---|
23 | Tests for JAMB tables. |
---|
24 | |
---|
25 | """ |
---|
26 | import logging |
---|
27 | import os |
---|
28 | import shutil |
---|
29 | import tempfile |
---|
30 | import unittest |
---|
31 | |
---|
32 | import grok |
---|
33 | |
---|
34 | from cStringIO import StringIO |
---|
35 | from datetime import datetime |
---|
36 | |
---|
37 | from zope.interface.verify import verifyClass, verifyObject |
---|
38 | from zope.site import LocalSiteManager |
---|
39 | from zope.site.hooks import setSite, clearSite |
---|
40 | |
---|
41 | from waeup.sirp.app import University |
---|
42 | from waeup.sirp.datacenter import DataCenter |
---|
43 | from waeup.sirp.applicants.jambtables import ( |
---|
44 | JAMBDataTable, JAMBDataPlugin, JAMBDataRoot, |
---|
45 | ) |
---|
46 | from waeup.sirp.applicants.jambtables.interfaces import IJAMBDataTable |
---|
47 | |
---|
48 | class FakeSite(dict): |
---|
49 | pass |
---|
50 | |
---|
51 | class JAMBTableTestCaseBase(unittest.TestCase): |
---|
52 | |
---|
53 | def setUp(self): |
---|
54 | self.tempdir = tempfile.mkdtemp() |
---|
55 | self.datafile = os.path.join( |
---|
56 | self.tempdir, 'jambsample.csv' |
---|
57 | ) |
---|
58 | open(self.datafile, 'wb').write( |
---|
59 | 'reg_no,fst_sit_fname,lastname,firstname,middlenames,sex,date_of_birth,jamb_state,jamb_lga,course1,screening_date,screening_venue,entry_session,screening_type,ignore_this_col\n' + |
---|
60 | '91100546DD,ISUOSUO MOSES ODOMERO,ISUOSUO,MOSES,ODOMERO,M,25/5/1982,DEL,ISO-S,BSCPOL,2009/09/25 09:00:00 GMT+1,REPRINT SLIP AS FROM WED 23/09/2009,9,pde,blah\n' + |
---|
61 | '91111834CC,DURUIHEOMA AUGUSTINA ADANNA,DURUIHEOMA,AUGUSTINA,ADANNA,F,15/4/1986,IMO,MBAIT,BSCPOL,2009/09/25 09:00:00 GMT+1,REPRINT SLIP AS FROM WED 23/09/2009,9,pde,blah\n' + |
---|
62 | '91109351AC,ARISERE EBIKEBUNA COMFORT,ARISERE,EBIKEBUNA,COMFORT,F,6/1/1984,EDO,OV-SW,BSCPOL,2009/09/25 09:00:00 GMT+1,REPRINT SLIP AS FROM WED 23/09/2009,9,pde,blah\n' |
---|
63 | ) |
---|
64 | setSite() |
---|
65 | return |
---|
66 | |
---|
67 | def tearDown(self): |
---|
68 | shutil.rmtree(self.tempdir) |
---|
69 | return |
---|
70 | |
---|
71 | class JAMBTableTestCase(JAMBTableTestCaseBase): |
---|
72 | def test_createJAMBTable(self): |
---|
73 | table = JAMBDataTable() |
---|
74 | self.assertEqual(table._data_len, 0) |
---|
75 | self.assertEqual(table._temporary, False) |
---|
76 | self.assertEqual(table._datafile_path, None) |
---|
77 | return |
---|
78 | |
---|
79 | def test_iterator(self): |
---|
80 | table = JAMBDataTable() |
---|
81 | self.assertEqual(len(list(table)), 0) |
---|
82 | table.importFromCSV(self.datafile) |
---|
83 | self.assertEqual(len(list(table)), 3) |
---|
84 | |
---|
85 | def test_import(self): |
---|
86 | table = JAMBDataTable() |
---|
87 | self.assertEqual(table.import_datetime, None) |
---|
88 | table.importFromCSV(self.datafile) |
---|
89 | self.assertNotEqual(table.import_datetime, None) |
---|
90 | |
---|
91 | def test_clear(self): |
---|
92 | table = JAMBDataTable() |
---|
93 | table.importFromCSV(self.datafile) |
---|
94 | path = table._datafile_path |
---|
95 | table.clear() |
---|
96 | exists = os.path.exists(path) |
---|
97 | self.assertEqual(exists, False) |
---|
98 | self.assertEqual(0, len(list(table))) |
---|
99 | |
---|
100 | def test_clear_broken_path(self): |
---|
101 | table = JAMBDataTable() |
---|
102 | table.importFromCSV(self.datafile) |
---|
103 | path = table._datafile_path |
---|
104 | table._datafile_path = 'not-existent-path' |
---|
105 | table.clear() |
---|
106 | |
---|
107 | def test_import_datetime(self): |
---|
108 | table = JAMBDataTable() |
---|
109 | self.assertTrue(table.import_datetime is None) |
---|
110 | table.importFromCSV(self.datafile) |
---|
111 | now = datetime.now() |
---|
112 | diff = now - table.import_datetime |
---|
113 | self.assertTrue(diff.seconds < 5) |
---|
114 | table.clear() |
---|
115 | self.assertTrue(table.import_datetime is None) |
---|
116 | |
---|
117 | def test_importer_username(self): |
---|
118 | table = JAMBDataTable() |
---|
119 | self.assertTrue(table.importer_username is None) |
---|
120 | table.importFromCSV(self.datafile, 'manfred') |
---|
121 | self.assertEqual(table.importer_username, u'manfred') |
---|
122 | self.assertTrue(isinstance(table.importer_username, unicode)) |
---|
123 | table.importFromCSV(self.datafile, 'fred') |
---|
124 | self.assertEqual(table.importer_username, u'fred') |
---|
125 | table.importFromCSV(self.datafile) |
---|
126 | self.assertTrue(table.importer_username is None) |
---|
127 | table.clear() |
---|
128 | self.assertTrue(table.importer_username is None) |
---|
129 | |
---|
130 | def test_keys(self): |
---|
131 | table = JAMBDataTable() |
---|
132 | self.assertFalse('91100546DD' in table.keys()) |
---|
133 | table.importFromCSV(self.datafile) |
---|
134 | self.assertTrue('91100546DD' in table.keys()) |
---|
135 | self.assertFalse('91100546DE' in table.keys()) |
---|
136 | |
---|
137 | def test_items(self): |
---|
138 | table = JAMBDataTable() |
---|
139 | self.assertEqual([], list(table.items())) |
---|
140 | table.importFromCSV(self.datafile) |
---|
141 | self.assertNotEqual([], list(table.items())) |
---|
142 | |
---|
143 | def test_interface_compliance(self): |
---|
144 | self.assertTrue(verifyClass(IJAMBDataTable, JAMBDataTable)) |
---|
145 | table = JAMBDataTable() |
---|
146 | self.assertTrue(verifyObject(IJAMBDataTable, table)) |
---|
147 | table.importFromCSV(self.datafile) |
---|
148 | self.assertTrue(verifyObject(IJAMBDataTable, table)) |
---|
149 | |
---|
150 | class JAMBTableFunctionalTestCase(JAMBTableTestCaseBase): |
---|
151 | """A test case where we check JAMB tables inside a site. |
---|
152 | """ |
---|
153 | |
---|
154 | def setUp(self): |
---|
155 | super(JAMBTableFunctionalTestCase, self).setUp() |
---|
156 | |
---|
157 | # We need at least these to register factories for a University. |
---|
158 | grok.testing.grok('waeup.sirp.student') |
---|
159 | grok.testing.grok('waeup.sirp.hostel') |
---|
160 | grok.testing.grok('waeup.sirp.university') |
---|
161 | |
---|
162 | self.site = University() |
---|
163 | sm = LocalSiteManager(self.site) |
---|
164 | self.site.setSiteManager(sm) |
---|
165 | self.datacenter_dir = tempfile.mkdtemp() |
---|
166 | self.datacenter = self.site['datacenter'] |
---|
167 | self.datacenter.setStoragePath(self.datacenter_dir) |
---|
168 | # If the old datacenter already had a jambtables dir, remove it... |
---|
169 | if os.path.isdir( |
---|
170 | os.path.join(self.datacenter_dir, 'jambdata') |
---|
171 | ): |
---|
172 | shutil.rmtree(os.path.join(self.datacenter_dir, 'jambdata')) |
---|
173 | return |
---|
174 | |
---|
175 | def tearDown(self): |
---|
176 | shutil.rmtree(self.tempdir) |
---|
177 | shutil.rmtree(self.datacenter_dir) |
---|
178 | # Unregister site... |
---|
179 | setSite() |
---|
180 | return |
---|
181 | |
---|
182 | def test_createJAMBTableInSite(self): |
---|
183 | table = JAMBDataTable() |
---|
184 | self.assertEqual(table._data_len, 0) |
---|
185 | self.assertEqual(table._temporary, False) |
---|
186 | self.assertEqual(table._datafile_path, None) |
---|
187 | table.importFromCSV(self.datafile) |
---|
188 | return |
---|
189 | |
---|
190 | def test_createJAMBTableOutsideSite(self): |
---|
191 | clearSite(self.site) |
---|
192 | table = JAMBDataTable() |
---|
193 | self.assertEqual(table._data_len, 0) |
---|
194 | self.assertEqual(table._temporary, False) |
---|
195 | self.assertEqual(table._datafile_path, None) |
---|
196 | table.importFromCSV(self.datafile) |
---|
197 | return |
---|
198 | |
---|
199 | class JAMBDataPluginTestCase(unittest.TestCase): |
---|
200 | def create_logger(self): |
---|
201 | # create a logger suitable for local tests. |
---|
202 | test_logger = logging.getLogger('waeup.sirp.jambdata.testlogger') |
---|
203 | log = StringIO() |
---|
204 | handler = logging.StreamHandler(log) |
---|
205 | handler.setLevel(logging.DEBUG) |
---|
206 | test_logger.addHandler(handler) |
---|
207 | test_logger.setLevel(logging.DEBUG) |
---|
208 | self.logger = test_logger |
---|
209 | self.log = log |
---|
210 | self.handler = handler |
---|
211 | return self.logger |
---|
212 | |
---|
213 | def remove_logger(self): |
---|
214 | del self.handler |
---|
215 | del self.logger |
---|
216 | del self.log |
---|
217 | pass |
---|
218 | |
---|
219 | def get_log(self): |
---|
220 | self.log.seek(0) |
---|
221 | return self.log.read() |
---|
222 | |
---|
223 | def setUp(self): |
---|
224 | self.create_logger() |
---|
225 | return |
---|
226 | |
---|
227 | def tearDown(self): |
---|
228 | self.remove_logger() |
---|
229 | return |
---|
230 | |
---|
231 | # Real tests start here... |
---|
232 | def test_pluginsetup(self): |
---|
233 | # Make sure we can add ApplicantsRoot to sites. |
---|
234 | site = FakeSite() |
---|
235 | plugin = JAMBDataPlugin() |
---|
236 | plugin.setup(site, 'blah', self.logger) |
---|
237 | self.assertTrue('jambdata' in site.keys()) |
---|
238 | log = self.get_log() |
---|
239 | self.assertTrue('JAMBDataPlugin: Installed JAMB data root.' in log) |
---|
240 | return |
---|
241 | |
---|
242 | def test_update_new(self): |
---|
243 | # Run update on a site without applicants root. |
---|
244 | site = FakeSite() |
---|
245 | plugin = JAMBDataPlugin() |
---|
246 | plugin.update(site, 'blah', self.logger) |
---|
247 | self.assertTrue('jambdata' in site.keys()) |
---|
248 | log = self.get_log() |
---|
249 | self.assertTrue('Updating site at <Unnamed Site>' in log) |
---|
250 | self.assertTrue('Installed JAMB data root.' in log) |
---|
251 | return |
---|
252 | |
---|
253 | def test_update_outdated(self): |
---|
254 | # Run update on a site with outdated applicants root. |
---|
255 | site = FakeSite() |
---|
256 | root = object() # # This is not a proper applicants root |
---|
257 | site['jambdata'] = root |
---|
258 | plugin = JAMBDataPlugin() |
---|
259 | plugin.update(site, 'blah', self.logger) |
---|
260 | self.assertTrue(site['jambdata'] is not root) |
---|
261 | self.assertTrue(isinstance(site['jambdata'], JAMBDataRoot)) |
---|
262 | log = self.get_log() |
---|
263 | self.assertTrue('Outdated JAMB data folder detected' in log) |
---|
264 | self.assertTrue('Updating site at <Unnamed Site>' in log) |
---|
265 | self.assertTrue('Installed JAMB data root.' in log) |
---|
266 | return |
---|
267 | |
---|
268 | def test_update_uptodate(self): |
---|
269 | # Run update on a site with proper applicants root. |
---|
270 | site = FakeSite() |
---|
271 | root = JAMBDataRoot() |
---|
272 | site['jambdata'] = root |
---|
273 | plugin = JAMBDataPlugin() |
---|
274 | plugin.update(site, 'blah', self.logger) |
---|
275 | self.assertTrue(site['jambdata'] is root) |
---|
276 | log = self.get_log() |
---|
277 | self.assertTrue('Updating site at <Unnamed Site>' in log) |
---|
278 | self.assertTrue('Nothing to do' in log) |
---|
279 | return |
---|
280 | |
---|
281 | def test_update_log(self): |
---|
282 | # Check that sitename is used in log messages on updates. |
---|
283 | site = FakeSite() |
---|
284 | site.__name__ = 'my_site' |
---|
285 | plugin = JAMBDataPlugin() |
---|
286 | plugin.update(site, 'blah', self.logger) |
---|
287 | log = self.get_log() |
---|
288 | self.assertTrue('Updating site at my_site.' in log) |
---|
289 | return |
---|
290 | |
---|
291 | |
---|
292 | def test_suite(): |
---|
293 | suite = unittest.TestSuite() |
---|
294 | for testcase in [ |
---|
295 | JAMBTableTestCase, |
---|
296 | JAMBTableFunctionalTestCase, |
---|
297 | JAMBDataPluginTestCase, |
---|
298 | ]: |
---|
299 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
300 | testcase |
---|
301 | ) |
---|
302 | ) |
---|
303 | return suite |
---|