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 | JAMBDataPlugin, JAMBDataRoot, |
---|
45 | ) |
---|
46 | |
---|
47 | class FakeSite(dict): |
---|
48 | pass |
---|
49 | |
---|
50 | class JAMBTableTestCaseBase(unittest.TestCase): |
---|
51 | |
---|
52 | def setUp(self): |
---|
53 | self.tempdir = tempfile.mkdtemp() |
---|
54 | self.datafile = os.path.join( |
---|
55 | self.tempdir, 'jambsample.csv' |
---|
56 | ) |
---|
57 | open(self.datafile, 'wb').write( |
---|
58 | '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' + |
---|
59 | '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' + |
---|
60 | '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' + |
---|
61 | '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' |
---|
62 | ) |
---|
63 | setSite() |
---|
64 | return |
---|
65 | |
---|
66 | def tearDown(self): |
---|
67 | shutil.rmtree(self.tempdir) |
---|
68 | return |
---|
69 | |
---|
70 | |
---|
71 | class JAMBDataPluginTestCase(unittest.TestCase): |
---|
72 | def create_logger(self): |
---|
73 | # create a logger suitable for local tests. |
---|
74 | test_logger = logging.getLogger('waeup.sirp.jambdata.testlogger') |
---|
75 | log = StringIO() |
---|
76 | handler = logging.StreamHandler(log) |
---|
77 | handler.setLevel(logging.DEBUG) |
---|
78 | test_logger.addHandler(handler) |
---|
79 | test_logger.setLevel(logging.DEBUG) |
---|
80 | self.logger = test_logger |
---|
81 | self.log = log |
---|
82 | self.handler = handler |
---|
83 | return self.logger |
---|
84 | |
---|
85 | def remove_logger(self): |
---|
86 | del self.handler |
---|
87 | del self.logger |
---|
88 | del self.log |
---|
89 | pass |
---|
90 | |
---|
91 | def get_log(self): |
---|
92 | self.log.seek(0) |
---|
93 | return self.log.read() |
---|
94 | |
---|
95 | def setUp(self): |
---|
96 | self.create_logger() |
---|
97 | return |
---|
98 | |
---|
99 | def tearDown(self): |
---|
100 | self.remove_logger() |
---|
101 | return |
---|
102 | |
---|
103 | # Real tests start here... |
---|
104 | def test_pluginsetup(self): |
---|
105 | # Make sure we can add ApplicantsRoot to sites. |
---|
106 | site = FakeSite() |
---|
107 | plugin = JAMBDataPlugin() |
---|
108 | plugin.setup(site, 'blah', self.logger) |
---|
109 | self.assertTrue('jambdata' in site.keys()) |
---|
110 | log = self.get_log() |
---|
111 | self.assertTrue('JAMBDataPlugin: Installed JAMB data root.' in log) |
---|
112 | return |
---|
113 | |
---|
114 | def test_update_new(self): |
---|
115 | # Run update on a site without applicants root. |
---|
116 | site = FakeSite() |
---|
117 | plugin = JAMBDataPlugin() |
---|
118 | plugin.update(site, 'blah', self.logger) |
---|
119 | self.assertTrue('jambdata' in site.keys()) |
---|
120 | log = self.get_log() |
---|
121 | self.assertTrue('Updating site at <Unnamed Site>' in log) |
---|
122 | self.assertTrue('Installed JAMB data root.' in log) |
---|
123 | return |
---|
124 | |
---|
125 | def test_update_outdated(self): |
---|
126 | # Run update on a site with outdated applicants root. |
---|
127 | site = FakeSite() |
---|
128 | root = object() # # This is not a proper applicants root |
---|
129 | site['jambdata'] = root |
---|
130 | plugin = JAMBDataPlugin() |
---|
131 | plugin.update(site, 'blah', self.logger) |
---|
132 | self.assertTrue(site['jambdata'] is not root) |
---|
133 | self.assertTrue(isinstance(site['jambdata'], JAMBDataRoot)) |
---|
134 | log = self.get_log() |
---|
135 | self.assertTrue('Outdated JAMB data folder detected' in log) |
---|
136 | self.assertTrue('Updating site at <Unnamed Site>' in log) |
---|
137 | self.assertTrue('Installed JAMB data root.' in log) |
---|
138 | return |
---|
139 | |
---|
140 | def test_update_uptodate(self): |
---|
141 | # Run update on a site with proper applicants root. |
---|
142 | site = FakeSite() |
---|
143 | root = JAMBDataRoot() |
---|
144 | site['jambdata'] = root |
---|
145 | plugin = JAMBDataPlugin() |
---|
146 | plugin.update(site, 'blah', self.logger) |
---|
147 | self.assertTrue(site['jambdata'] is root) |
---|
148 | log = self.get_log() |
---|
149 | self.assertTrue('Updating site at <Unnamed Site>' in log) |
---|
150 | self.assertTrue('Nothing to do' in log) |
---|
151 | return |
---|
152 | |
---|
153 | def test_update_log(self): |
---|
154 | # Check that sitename is used in log messages on updates. |
---|
155 | site = FakeSite() |
---|
156 | site.__name__ = 'my_site' |
---|
157 | plugin = JAMBDataPlugin() |
---|
158 | plugin.update(site, 'blah', self.logger) |
---|
159 | log = self.get_log() |
---|
160 | self.assertTrue('Updating site at my_site.' in log) |
---|
161 | return |
---|
162 | |
---|
163 | |
---|
164 | def test_suite(): |
---|
165 | suite = unittest.TestSuite() |
---|
166 | for testcase in [ |
---|
167 | JAMBDataPluginTestCase, |
---|
168 | ]: |
---|
169 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
170 | testcase |
---|
171 | ) |
---|
172 | ) |
---|
173 | return suite |
---|