1 | import datetime |
---|
2 | import os |
---|
3 | import pytz |
---|
4 | import shutil |
---|
5 | import tempfile |
---|
6 | import unittest |
---|
7 | from zope.catalog.interfaces import ICatalog |
---|
8 | from zope.component import queryUtility, getUtility, createObject |
---|
9 | from zope.interface.verify import verifyObject, verifyClass |
---|
10 | from zope.intid.interfaces import IIntIds |
---|
11 | from waeup.kofa.applicants import ApplicantsContainer |
---|
12 | from waeup.kofa.applicants.export import ( |
---|
13 | ApplicantsContainerExporter, ApplicantExporter, |
---|
14 | ApplicantPaymentExporter) |
---|
15 | from waeup.kofa.applicants.interfaces import ( |
---|
16 | AppCatSource, ApplicationTypeSource) |
---|
17 | from waeup.kofa.applicants.tests.test_batching import ( |
---|
18 | ApplicantImportExportSetup) |
---|
19 | from waeup.kofa.interfaces import ICSVExporter |
---|
20 | from waeup.kofa.schoolgrades import ResultEntry |
---|
21 | from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer |
---|
22 | from waeup.kofa.utils.utils import KofaUtils |
---|
23 | |
---|
24 | class ApplicantsContainersExporterTest(unittest.TestCase): |
---|
25 | |
---|
26 | layer = KofaUnitTestLayer |
---|
27 | |
---|
28 | def setUp(self): |
---|
29 | self.workdir = tempfile.mkdtemp() |
---|
30 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
31 | return |
---|
32 | |
---|
33 | def tearDown(self): |
---|
34 | shutil.rmtree(self.workdir) |
---|
35 | return |
---|
36 | |
---|
37 | def test_ifaces(self): |
---|
38 | # make sure we fullfill interface contracts |
---|
39 | obj = ApplicantsContainerExporter() |
---|
40 | verifyObject(ICSVExporter, obj) |
---|
41 | verifyClass(ICSVExporter, ApplicantsContainerExporter) |
---|
42 | return |
---|
43 | |
---|
44 | def test_get_as_utility(self): |
---|
45 | # we can get a faculty exporter as utility |
---|
46 | result = queryUtility(ICSVExporter, name="applicantscontainers") |
---|
47 | self.assertTrue(result is not None) |
---|
48 | return |
---|
49 | |
---|
50 | def setup_container(self, container): |
---|
51 | # set all attributes of a container |
---|
52 | container.code = u'dp2015' |
---|
53 | container.title = u'General Studies' |
---|
54 | container.prefix = list(ApplicationTypeSource()(container))[0] |
---|
55 | container.year = 2015 |
---|
56 | container.application_category = list(AppCatSource()(container))[0] |
---|
57 | container.description = u'Some Description\nwith linebreak\n' |
---|
58 | container.description += u'<<de>>man spriht deutsh' |
---|
59 | container.startdate = datetime.datetime( |
---|
60 | 2015, 1, 1, 12, 0, 0, tzinfo=pytz.utc) |
---|
61 | container.enddate = datetime.datetime( |
---|
62 | 2015, 1, 31, 23, 0, 0, tzinfo=pytz.utc) |
---|
63 | return container |
---|
64 | |
---|
65 | def test_export(self): |
---|
66 | # we can export a set of applicants containers (w/o applicants) |
---|
67 | container = ApplicantsContainer() |
---|
68 | container = self.setup_container(container) |
---|
69 | exporter = ApplicantsContainerExporter() |
---|
70 | exporter.export([container], self.outfile) |
---|
71 | result = open(self.outfile, 'rb').read() |
---|
72 | self.assertEqual( |
---|
73 | result, |
---|
74 | 'application_category,application_fee,application_slip_notice,code,' |
---|
75 | 'description,enddate,hidden,mode,prefix,startdate,' |
---|
76 | 'strict_deadline,title,with_picture,year\r\n' |
---|
77 | |
---|
78 | 'basic,0.0,,dp2015,' |
---|
79 | '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",' |
---|
80 | '2015-01-31 23:00:00+00:00#,0,,app,2015-01-01 12:00:00+00:00#,1,' |
---|
81 | 'General Studies,1,2015\r\n' |
---|
82 | ) |
---|
83 | return |
---|
84 | |
---|
85 | class ApplicantExporterTest(ApplicantImportExportSetup): |
---|
86 | |
---|
87 | layer = FunctionalLayer |
---|
88 | |
---|
89 | def setUp(self): |
---|
90 | super(ApplicantExporterTest, self).setUp() |
---|
91 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
92 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
93 | self.intids = getUtility(IIntIds) |
---|
94 | return |
---|
95 | |
---|
96 | def test_ifaces(self): |
---|
97 | # make sure we fullfill interface contracts |
---|
98 | obj = ApplicantExporter() |
---|
99 | verifyObject(ICSVExporter, obj) |
---|
100 | verifyClass(ICSVExporter, ApplicantExporter) |
---|
101 | return |
---|
102 | |
---|
103 | def test_get_as_utility(self): |
---|
104 | # we can get an applicant exporter as utility |
---|
105 | result = queryUtility(ICSVExporter, name="applicants") |
---|
106 | self.assertTrue(result is not None) |
---|
107 | return |
---|
108 | |
---|
109 | def setup_applicant(self, applicant): |
---|
110 | # set predictable values for `applicant` |
---|
111 | applicant.reg_number = u'123456' |
---|
112 | applicant.applicant_id = u'dp2011_654321' |
---|
113 | applicant.firstname = u'Anna' |
---|
114 | applicant.lastname = u'Tester' |
---|
115 | applicant.middlename = u'M.' |
---|
116 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
117 | applicant.sex = 'f' |
---|
118 | applicant.email = 'anna@sample.com' |
---|
119 | applicant.phone = u'+234-123-12345' |
---|
120 | applicant.course1 = self.certificate |
---|
121 | applicant.course2 = self.certificate |
---|
122 | applicant.course_admitted = self.certificate |
---|
123 | applicant.notice = u'Some notice\nin lines.' |
---|
124 | applicant.password = 'any password' |
---|
125 | result_entry = ResultEntry( |
---|
126 | KofaUtils.EXAM_SUBJECTS_DICT.keys()[0], |
---|
127 | KofaUtils.EXAM_GRADES[0][0] |
---|
128 | ) |
---|
129 | applicant.school_grades = [ |
---|
130 | result_entry] |
---|
131 | return applicant |
---|
132 | |
---|
133 | def test_export_emtpy(self): |
---|
134 | # we can export nearly empty applicants |
---|
135 | self.applicant.applicant_id = u'dp2011_654321' |
---|
136 | exporter = ApplicantExporter() |
---|
137 | exporter.export([self.applicant], self.outfile) |
---|
138 | result = open(self.outfile, 'rb').read() |
---|
139 | # The exported records do contain a real date in their |
---|
140 | # history dict. We skip the date and split the comparison |
---|
141 | # into two parts. |
---|
142 | self.assertTrue( |
---|
143 | 'applicant_id,course1,course2,course_admitted,date_of_birth,' |
---|
144 | 'email,firstname,lastname,locked,middlename,notice,phone,' |
---|
145 | 'reg_number,sex,special_application,student_id,suspended,' |
---|
146 | 'password,state,history,container_code,application_number,' |
---|
147 | 'display_fullname,application_date\r\n' |
---|
148 | 'dp2011_654321,,,,,,Anna,Tester,0' |
---|
149 | in result) |
---|
150 | self.assertTrue( |
---|
151 | 'Application initialized by system\'],dp2011,654321,Anna Tester' |
---|
152 | in result) |
---|
153 | return |
---|
154 | |
---|
155 | def test_export(self): |
---|
156 | # we can really export applicants |
---|
157 | # set values we can expect in export file |
---|
158 | applicant = self.setup_applicant(self.applicant) |
---|
159 | exporter = ApplicantExporter() |
---|
160 | exporter.export([applicant], self.outfile) |
---|
161 | result = open(self.outfile, 'rb').read() |
---|
162 | # The exported records do contain a real date in their |
---|
163 | # history dict. We skip the date and split the comparison |
---|
164 | # into two parts. |
---|
165 | self.assertTrue( |
---|
166 | 'applicant_id,course1,course2,course_admitted,date_of_birth,' |
---|
167 | 'email,firstname,lastname,locked,middlename,notice,phone,' |
---|
168 | 'reg_number,sex,special_application,student_id,suspended,' |
---|
169 | 'password,state,history,container_code,application_number,' |
---|
170 | 'display_fullname,application_date\r\n' |
---|
171 | 'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
172 | 'anna@sample.com,Anna,Tester,' |
---|
173 | in result) |
---|
174 | self.assertTrue( |
---|
175 | 'Application initialized by system\'],dp2011,654321,' |
---|
176 | 'Anna M. Tester,\r\n' |
---|
177 | in result) |
---|
178 | |
---|
179 | return |
---|
180 | |
---|
181 | def test_export_all(self): |
---|
182 | # we can export all applicants in a portal |
---|
183 | # set values we can expect in export file |
---|
184 | self.applicant = self.setup_applicant(self.applicant) |
---|
185 | exporter = ApplicantExporter() |
---|
186 | exporter.export_all(self.app, self.outfile) |
---|
187 | result = open(self.outfile, 'rb').read() |
---|
188 | self.assertTrue( |
---|
189 | 'applicant_id,course1,course2,course_admitted,date_of_birth,' |
---|
190 | 'email,firstname,lastname,locked,middlename,notice,phone,' |
---|
191 | 'reg_number,sex,special_application,student_id,suspended,' |
---|
192 | 'password,state,history,container_code,application_number,' |
---|
193 | 'display_fullname,application_date\r\n' |
---|
194 | 'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
195 | 'anna@sample.com,Anna,Tester,' |
---|
196 | in result) |
---|
197 | self.assertTrue( |
---|
198 | 'Application initialized by system\'],dp2011,654321,' |
---|
199 | 'Anna M. Tester,\r\n' |
---|
200 | in result) |
---|
201 | return |
---|
202 | |
---|
203 | def test_export_filtered(self): |
---|
204 | self.applicant = self.setup_applicant(self.applicant) |
---|
205 | exporter = ApplicantExporter() |
---|
206 | exporter.export_filtered( |
---|
207 | self.app, self.outfile, container=self.container.code) |
---|
208 | result = open(self.outfile, 'rb').read() |
---|
209 | self.assertTrue( |
---|
210 | 'applicant_id,course1,course2,course_admitted,date_of_birth,' |
---|
211 | 'email,firstname,lastname,locked,middlename,notice,phone,' |
---|
212 | 'reg_number,sex,special_application,student_id,suspended,' |
---|
213 | 'password,state,history,container_code,application_number,' |
---|
214 | 'display_fullname,application_date\r\n' |
---|
215 | 'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
216 | 'anna@sample.com,Anna,Tester,' |
---|
217 | in result) |
---|
218 | self.assertTrue( |
---|
219 | 'Application initialized by system\'],dp2011,654321,' |
---|
220 | 'Anna M. Tester,\r\n' |
---|
221 | in result) |
---|
222 | # In empty container no applicants are exported |
---|
223 | container = ApplicantsContainer() |
---|
224 | container.code = u'anything' |
---|
225 | self.app['applicants']['anything'] = self.container |
---|
226 | exporter.export_filtered( |
---|
227 | self.app, self.outfile, container=container.code) |
---|
228 | result = open(self.outfile, 'rb').read() |
---|
229 | self.assertTrue( |
---|
230 | 'applicant_id,course1,course2,course_admitted,date_of_birth,' |
---|
231 | 'email,firstname,lastname,locked,middlename,notice,phone,' |
---|
232 | 'reg_number,sex,special_application,student_id,suspended,' |
---|
233 | 'password,state,history,container_code,application_number,' |
---|
234 | 'display_fullname,application_date\r\n' |
---|
235 | in result) |
---|
236 | return |
---|
237 | |
---|
238 | |
---|
239 | class ApplicantPaymentExporterTest(ApplicantImportExportSetup): |
---|
240 | |
---|
241 | layer = FunctionalLayer |
---|
242 | |
---|
243 | def setUp(self): |
---|
244 | super(ApplicantPaymentExporterTest, self).setUp() |
---|
245 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
246 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
247 | self.intids = getUtility(IIntIds) |
---|
248 | return |
---|
249 | |
---|
250 | def test_ifaces(self): |
---|
251 | # make sure we fullfill interface contracts |
---|
252 | obj = ApplicantPaymentExporter() |
---|
253 | verifyObject(ICSVExporter, obj) |
---|
254 | verifyClass(ICSVExporter, ApplicantPaymentExporter) |
---|
255 | return |
---|
256 | |
---|
257 | def test_get_as_utility(self): |
---|
258 | # we can get an applicant exporter as utility |
---|
259 | result = queryUtility(ICSVExporter, name="applicantpayments") |
---|
260 | self.assertTrue(result is not None) |
---|
261 | return |
---|
262 | |
---|
263 | def setup_applicant(self, applicant): |
---|
264 | # set predictable values for `applicant` |
---|
265 | applicant.reg_number = u'123456' |
---|
266 | applicant.applicant_id = u'dp2011_654321' |
---|
267 | applicant.firstname = u'Anna' |
---|
268 | applicant.lastname = u'Tester' |
---|
269 | applicant.middlename = u'M.' |
---|
270 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
271 | applicant.sex = 'f' |
---|
272 | applicant.email = 'anna@sample.com' |
---|
273 | applicant.phone = u'+234-123-12345' |
---|
274 | # Add payment |
---|
275 | payment = createObject(u'waeup.ApplicantOnlinePayment') |
---|
276 | payment.p_id = 'p120' |
---|
277 | payment.p_session = 2012 |
---|
278 | payment.p_category = 'application' |
---|
279 | payment.p_state = 'paid' |
---|
280 | applicant['p120'] = payment |
---|
281 | return applicant |
---|
282 | |
---|
283 | def test_export(self): |
---|
284 | applicant = self.setup_applicant(self.applicant) |
---|
285 | exporter = ApplicantPaymentExporter() |
---|
286 | exporter.export(applicant.payments, self.outfile) |
---|
287 | result = open(self.outfile, 'rb').read() |
---|
288 | cdate = str('%s#' % self.applicant['p120'].creation_date) |
---|
289 | self.assertEqual( |
---|
290 | 'ac,amount_auth,creation_date,p_category,p_combi,p_id,' |
---|
291 | 'p_item,p_session,p_state,payment_date,r_amount_approved,' |
---|
292 | 'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n' |
---|
293 | ',0.0,%s,application,[],p120,,2012,paid,,0.0,,,dp2011_654321,' |
---|
294 | '123456,Anna M. Tester\r\n' % cdate, result) |
---|
295 | return |
---|
296 | |
---|
297 | def test_export_all(self): |
---|
298 | self.applicant = self.setup_applicant(self.applicant) |
---|
299 | exporter = ApplicantPaymentExporter() |
---|
300 | exporter.export_all(self.app, self.outfile) |
---|
301 | result = open(self.outfile, 'rb').read() |
---|
302 | cdate = str('%s#' % self.applicant['p120'].creation_date) |
---|
303 | self.assertEqual( |
---|
304 | 'ac,amount_auth,creation_date,p_category,p_combi,p_id,' |
---|
305 | 'p_item,p_session,p_state,payment_date,r_amount_approved,' |
---|
306 | 'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n' |
---|
307 | ',0.0,%s,application,[],p120,,2012,paid,,0.0,,,dp2011_654321,' |
---|
308 | '123456,Anna M. Tester\r\n' % cdate, result) |
---|
309 | return |
---|
310 | |
---|
311 | def test_export_filtered(self): |
---|
312 | self.applicant = self.setup_applicant(self.applicant) |
---|
313 | exporter = ApplicantPaymentExporter() |
---|
314 | exporter.export_filtered( |
---|
315 | self.app, self.outfile, container=self.container.code) |
---|
316 | result = open(self.outfile, 'rb').read() |
---|
317 | cdate = str('%s#' % self.applicant['p120'].creation_date) |
---|
318 | self.assertEqual( |
---|
319 | 'ac,amount_auth,creation_date,p_category,p_combi,p_id,' |
---|
320 | 'p_item,p_session,p_state,payment_date,r_amount_approved,' |
---|
321 | 'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n' |
---|
322 | ',0.0,%s,application,[],p120,,2012,paid,,0.0,,,dp2011_654321,' |
---|
323 | '123456,Anna M. Tester\r\n' % cdate, result) |
---|
324 | return |
---|