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 os |
---|
27 | import shutil |
---|
28 | import tempfile |
---|
29 | import unittest |
---|
30 | |
---|
31 | import grok |
---|
32 | |
---|
33 | from datetime import datetime |
---|
34 | |
---|
35 | from zope.site import LocalSiteManager |
---|
36 | from zope.site.hooks import setSite, clearSite |
---|
37 | |
---|
38 | from waeup.sirp.app import University |
---|
39 | from waeup.sirp.jambtables import JAMBDataTable |
---|
40 | from waeup.sirp.datacenter import DataCenter |
---|
41 | |
---|
42 | class JAMBTableTestCaseBase(unittest.TestCase): |
---|
43 | |
---|
44 | def setUp(self): |
---|
45 | self.tempdir = tempfile.mkdtemp() |
---|
46 | self.datafile = os.path.join( |
---|
47 | self.tempdir, 'jambsample.csv' |
---|
48 | ) |
---|
49 | open(self.datafile, 'wb').write( |
---|
50 | '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' + |
---|
51 | '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' + |
---|
52 | '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' + |
---|
53 | '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' |
---|
54 | ) |
---|
55 | setSite() |
---|
56 | return |
---|
57 | |
---|
58 | def tearDown(self): |
---|
59 | shutil.rmtree(self.tempdir) |
---|
60 | return |
---|
61 | |
---|
62 | class JAMBTableTestCase(JAMBTableTestCaseBase): |
---|
63 | def test_createJAMBTable(self): |
---|
64 | table = JAMBDataTable() |
---|
65 | self.assertEqual(table._data_len, 0) |
---|
66 | self.assertEqual(table._temporary, False) |
---|
67 | self.assertEqual(table._datafile_path, None) |
---|
68 | return |
---|
69 | |
---|
70 | def test_iterator(self): |
---|
71 | table = JAMBDataTable() |
---|
72 | self.assertEqual(len(list(table)), 0) |
---|
73 | table.importFromCSV(self.datafile) |
---|
74 | self.assertEqual(len(list(table)), 3) |
---|
75 | |
---|
76 | def test_import(self): |
---|
77 | table = JAMBDataTable() |
---|
78 | self.assertEqual(table.import_datetime, None) |
---|
79 | table.importFromCSV(self.datafile) |
---|
80 | self.assertNotEqual(table.import_datetime, None) |
---|
81 | |
---|
82 | def test_clear(self): |
---|
83 | table = JAMBDataTable() |
---|
84 | table.importFromCSV(self.datafile) |
---|
85 | path = table._datafile_path |
---|
86 | table.clear() |
---|
87 | exists = os.path.exists(path) |
---|
88 | self.assertEqual(exists, False) |
---|
89 | self.assertEqual(0, len(list(table))) |
---|
90 | |
---|
91 | def test_import_datetime(self): |
---|
92 | table = JAMBDataTable() |
---|
93 | self.assertTrue(table.import_datetime is None) |
---|
94 | table.importFromCSV(self.datafile) |
---|
95 | now = datetime.now() |
---|
96 | diff = now - table.import_datetime |
---|
97 | self.assertTrue(diff.seconds < 5) |
---|
98 | table.clear() |
---|
99 | self.assertTrue(table.import_datetime is None) |
---|
100 | |
---|
101 | def test_keys(self): |
---|
102 | table = JAMBDataTable() |
---|
103 | self.assertFalse('91100546DD' in table.keys()) |
---|
104 | table.importFromCSV(self.datafile) |
---|
105 | self.assertTrue('91100546DD' in table.keys()) |
---|
106 | self.assertFalse('91100546DE' in table.keys()) |
---|
107 | |
---|
108 | def test_items(self): |
---|
109 | table = JAMBDataTable() |
---|
110 | self.assertEqual([], list(table.items())) |
---|
111 | table.importFromCSV(self.datafile) |
---|
112 | self.assertNotEqual([], list(table.items())) |
---|
113 | |
---|
114 | class JAMBTableFunctionalTestCase(JAMBTableTestCaseBase): |
---|
115 | """A test case where we check JAMB tables inside a site. |
---|
116 | """ |
---|
117 | |
---|
118 | def setUp(self): |
---|
119 | super(JAMBTableFunctionalTestCase, self).setUp() |
---|
120 | |
---|
121 | # We need at least these to register factories for a University. |
---|
122 | grok.testing.grok('waeup.sirp.student') |
---|
123 | grok.testing.grok('waeup.sirp.hostel') |
---|
124 | grok.testing.grok('waeup.sirp.university') |
---|
125 | |
---|
126 | self.site = University() |
---|
127 | sm = LocalSiteManager(self.site) |
---|
128 | self.site.setSiteManager(sm) |
---|
129 | self.datacenter_dir = tempfile.mkdtemp() |
---|
130 | self.datacenter = self.site['datacenter'] |
---|
131 | self.datacenter.setStoragePath(self.datacenter_dir) |
---|
132 | # If the old datacenter already had a jambtables dir, remove it... |
---|
133 | if os.path.isdir( |
---|
134 | os.path.join(self.datacenter_dir, 'jambdata') |
---|
135 | ): |
---|
136 | shutil.rmtree(os.path.join(self.datacenter_dir, 'jambdata')) |
---|
137 | return |
---|
138 | |
---|
139 | def tearDown(self): |
---|
140 | shutil.rmtree(self.tempdir) |
---|
141 | shutil.rmtree(self.datacenter_dir) |
---|
142 | # Unregister site... |
---|
143 | setSite() |
---|
144 | return |
---|
145 | |
---|
146 | def test_createJAMBTableInSite(self): |
---|
147 | table = JAMBDataTable() |
---|
148 | self.assertEqual(table._data_len, 0) |
---|
149 | self.assertEqual(table._temporary, False) |
---|
150 | self.assertEqual(table._datafile_path, None) |
---|
151 | table.importFromCSV(self.datafile) |
---|
152 | return |
---|
153 | |
---|
154 | def test_createJAMBTableOutsideSite(self): |
---|
155 | clearSite(self.site) |
---|
156 | table = JAMBDataTable() |
---|
157 | self.assertEqual(table._data_len, 0) |
---|
158 | self.assertEqual(table._temporary, False) |
---|
159 | self.assertEqual(table._datafile_path, None) |
---|
160 | table.importFromCSV(self.datafile) |
---|
161 | return |
---|
162 | |
---|
163 | |
---|
164 | def test_suite(): |
---|
165 | suite = unittest.TestSuite() |
---|
166 | for testcase in [ |
---|
167 | JAMBTableTestCase, |
---|
168 | JAMBTableFunctionalTestCase, |
---|
169 | ]: |
---|
170 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
171 | testcase |
---|
172 | ) |
---|
173 | ) |
---|
174 | return suite |
---|