source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_export.py @ 18021

Last change on this file since 18021 was 18021, checked in by Henrik Bettermann, 2 days ago

Implement payment ticket exporter with date range filter.

  • Property svn:keywords set to Id
File size: 18.2 KB
Line 
1import datetime
2import os
3import pytz
4import shutil
5import tempfile
6import unittest
7from zope.catalog.interfaces import ICatalog
8from zope.component import queryUtility, getUtility, createObject
9from zope.interface.verify import verifyObject, verifyClass
10from zope.intid.interfaces import IIntIds
11from waeup.kofa.applicants import ApplicantsContainer
12from waeup.kofa.applicants.export import (
13    ApplicantsContainerExporter, ApplicantExporter,
14    ApplicantPaymentExporter,  ApplicantRefereeReportExporter)
15from waeup.kofa.applicants.interfaces import (
16    AppCatSource, ApplicationTypeSource)
17from waeup.kofa.applicants.tests.test_batching import (
18    ApplicantImportExportSetup)
19from waeup.kofa.interfaces import ICSVExporter
20from waeup.kofa.schoolgrades import ResultEntry
21from waeup.kofa.refereeentries import RefereeEntry
22from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer
23from waeup.kofa.utils.utils import KofaUtils
24
25curr_year = datetime.datetime.now().year
26
27class ApplicantsContainersExporterTest(unittest.TestCase):
28
29    layer = KofaUnitTestLayer
30
31    def setUp(self):
32        self.workdir = tempfile.mkdtemp()
33        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
34        return
35
36    def tearDown(self):
37        shutil.rmtree(self.workdir)
38        return
39
40    def test_ifaces(self):
41        # make sure we fullfill interface contracts
42        obj = ApplicantsContainerExporter()
43        verifyObject(ICSVExporter, obj)
44        verifyClass(ICSVExporter, ApplicantsContainerExporter)
45        return
46
47    def test_get_as_utility(self):
48        # we can get a faculty exporter as utility
49        result = queryUtility(ICSVExporter, name="applicantscontainers")
50        self.assertTrue(result is not None)
51        return
52
53    def setup_container(self, container):
54        # set all attributes of a container
55        container.code = u'dp2017'
56        container.title = u'General Studies'
57        container.prefix = list(ApplicationTypeSource()(container))[0]
58        container.year = 2017
59        container.application_category = list(AppCatSource()(container))[0]
60        container.description = u'Some Description\nwith linebreak\n'
61        container.description += u'<<de>>man spriht deutsh'
62        container.startdate = datetime.datetime(
63            2017, 1, 1, 12, 0, 0, tzinfo=pytz.utc)
64        container.enddate = datetime.datetime(
65            2017, 1, 31, 23, 0, 0, tzinfo=pytz.utc)
66        return container
67
68    def test_export(self):
69        # we can export a set of applicants containers (w/o applicants)
70        container = ApplicantsContainer()
71        container = self.setup_container(container)
72        exporter = ApplicantsContainerExporter()
73        exporter.export([container], self.outfile)
74        result = open(self.outfile, 'rb').read()
75        self.assertEqual(
76            result,
77            'application_category,application_fee,application_slip_notice,code,'
78            'description,enddate,hidden,mode,prefix,send_email,startdate,'
79            'strict_deadline,title,with_picture,year\r\n'
80
81            'basic,0.0,,dp2017,'
82            '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",'
83            '2017-01-31 23:00:00+00:00#,0,,app,0,2017-01-01 12:00:00+00:00#,1,'
84            'General Studies,1,2017\r\n'
85            )
86        return
87
88class ApplicantExporterTest(ApplicantImportExportSetup):
89
90    layer = FunctionalLayer
91
92    def setUp(self):
93        super(ApplicantExporterTest, self).setUp()
94        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
95        self.cat = getUtility(ICatalog, name='applicants_catalog')
96        self.intids = getUtility(IIntIds)
97        return
98
99    def test_ifaces(self):
100        # make sure we fullfill interface contracts
101        obj = ApplicantExporter()
102        verifyObject(ICSVExporter, obj)
103        verifyClass(ICSVExporter, ApplicantExporter)
104        return
105
106    def test_get_as_utility(self):
107        # we can get an applicant exporter as utility
108        result = queryUtility(ICSVExporter, name="applicants")
109        self.assertTrue(result is not None)
110        return
111
112    def setup_applicant(self, applicant):
113        # set predictable values for `applicant`
114        applicant.reg_number = u'123456'
115        applicant.applicant_id = u'dp2011_654321'
116        applicant.firstname = u'Anna'
117        applicant.lastname = u'Tester'
118        applicant.middlename = u'M.'
119        applicant.date_of_birth = datetime.date(1981, 2, 4)
120        applicant.sex = 'f'
121        applicant.email = 'anna@sample.com'
122        applicant.phone = u'+234-123-12345'
123        applicant.course1 = self.certificate
124        applicant.course2 = self.certificate
125        applicant.course_admitted = self.certificate
126        applicant.notice = u'Some notice\nin lines.'
127        applicant.password = 'any password'
128        result_entry = ResultEntry(
129            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
130            KofaUtils.EXAM_GRADES[0][0]
131            )
132        applicant.school_grades = [result_entry]
133        referee_entry = RefereeEntry(u'John Doe', 'john@aa.aa')
134        applicant.referees = [referee_entry]
135        return applicant
136
137    def test_export_emtpy(self):
138        # we can export nearly empty applicants
139        self.applicant.applicant_id = u'dp2011_654321'
140        exporter = ApplicantExporter()
141        exporter.export([self.applicant], self.outfile)
142        result = open(self.outfile, 'rb').read()
143        # The exported records do contain a real date in their
144        # history dict. We skip the date and split the comparison
145        # into two parts.
146        self.assertTrue(
147            'applicant_id,course1,course2,course_admitted,date_of_birth,'
148            'email,firstname,lastname,locked,middlename,notice,phone,'
149            'referees,reg_number,school_grades,sex,special_application,'
150            'student_id,suspended,password,state,history,container_code,'
151            'application_number,display_fullname,application_date\r\n'
152            'dp2011_654321,,,,,,Anna,Tester'
153            in result)
154        self.assertTrue(
155            'Application initialized by system\'],dp2011,654321,Anna Tester'
156            in result)
157        return
158
159    def test_export(self):
160        # we can really export applicants
161        # set values we can expect in export file
162        applicant = self.setup_applicant(self.applicant)
163        exporter = ApplicantExporter()
164        exporter.export([applicant], self.outfile)
165        result = open(self.outfile, 'rb').read()
166        # The exported records do contain a real date in their
167        # history dict. We skip the date and split the comparison
168        # into two parts.
169        self.assertTrue(
170            'applicant_id,course1,course2,course_admitted,date_of_birth,'
171            'email,firstname,lastname,locked,middlename,notice,phone,'
172            'referees,reg_number,school_grades,sex,special_application,'
173            'student_id,suspended,password,state,history,container_code,'
174            'application_number,display_fullname,application_date\r\n'
175            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,'
176            'anna@sample.com,Anna,Tester,'
177            in result)
178        self.assertTrue(
179            'Application initialized by system\'],dp2011,654321,'
180            'Anna M. Tester,\r\n'
181            in result)
182
183        return
184
185    def test_export_all(self):
186        # we can export all applicants in a portal
187        # set values we can expect in export file
188        self.applicant = self.setup_applicant(self.applicant)
189        exporter = ApplicantExporter()
190        exporter.export_all(self.app, self.outfile)
191        result = open(self.outfile, 'rb').read()
192        self.assertTrue(
193            'applicant_id,course1,course2,course_admitted,date_of_birth,'
194            'email,firstname,lastname,locked,middlename,notice,phone,'
195            'referees,reg_number,school_grades,sex,special_application,'
196            'student_id,suspended,password,state,history,container_code,'
197            'application_number,display_fullname,application_date\r\n'
198            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,'
199            'anna@sample.com,Anna,Tester,'
200            in result)
201        self.assertTrue(
202            'Application initialized by system\'],dp2011,654321,'
203            'Anna M. Tester,\r\n'
204            in result)
205        self.assertTrue(
206            '[(\'computer_science\', \'A\')]'
207            in result)
208        self.assertTrue(
209            '[(u\'John Doe\', \'john@aa.aa\', False)]'
210            in result)
211        return
212
213    def test_export_filtered(self):
214        self.applicant = self.setup_applicant(self.applicant)
215        exporter = ApplicantExporter()
216        exporter.export_filtered(
217            self.app, self.outfile, container=self.container.code)
218        result = open(self.outfile, 'rb').read()
219        self.assertTrue(
220            'applicant_id,course1,course2,course_admitted,date_of_birth,'
221            'email,firstname,lastname,locked,middlename,notice,phone,'
222            'referees,reg_number,school_grades,sex,special_application,'
223            'student_id,suspended,password,state,history,container_code,'
224            'application_number,display_fullname,application_date\r\n'
225            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,'
226            'anna@sample.com,Anna,Tester,'
227            in result)
228        self.assertTrue(
229            'Application initialized by system\'],dp2011,654321,'
230            'Anna M. Tester,\r\n'
231            in result)
232        # In empty container no applicants are exported
233        container = ApplicantsContainer()
234        container.code = u'anything'
235        self.app['applicants']['anything'] = self.container
236        exporter.export_filtered(
237            self.app, self.outfile, container=container.code)
238        result = open(self.outfile, 'rb').read()
239        self.assertTrue(
240            'applicant_id,course1,course2,course_admitted,date_of_birth,'
241            'email,firstname,lastname,locked,middlename,notice,phone,'
242            'referees,reg_number,school_grades,sex,special_application,'
243            'student_id,suspended,password,state,history,container_code,'
244            'application_number,display_fullname,application_date\r\n'
245            in result)
246        return
247
248
249class ApplicantPaymentExporterTest(ApplicantImportExportSetup):
250
251    layer = FunctionalLayer
252
253    def setUp(self):
254        super(ApplicantPaymentExporterTest, self).setUp()
255        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
256        self.cat = getUtility(ICatalog, name='applicants_catalog')
257        self.intids = getUtility(IIntIds)
258        return
259
260    def test_ifaces(self):
261        # make sure we fullfill interface contracts
262        obj = ApplicantPaymentExporter()
263        verifyObject(ICSVExporter, obj)
264        verifyClass(ICSVExporter, ApplicantPaymentExporter)
265        return
266
267    def test_get_as_utility(self):
268        # we can get an applicant exporter as utility
269        result = queryUtility(ICSVExporter, name="applicantpayments")
270        self.assertTrue(result is not None)
271        return
272
273    def setup_applicant(self, applicant):
274        # set predictable values for `applicant`
275        applicant.reg_number = u'123456'
276        applicant.applicant_id = u'dp2011_654321'
277        applicant.firstname = u'Anna'
278        applicant.lastname = u'Tester'
279        applicant.middlename = u'M.'
280        applicant.date_of_birth = datetime.date(1981, 2, 4)
281        applicant.sex = 'f'
282        applicant.email = 'anna@sample.com'
283        applicant.phone = u'+234-123-12345'
284        # Add payment
285        payment = createObject(u'waeup.ApplicantOnlinePayment')
286        payment.p_id = 'p120'
287        payment.p_session = 2012
288        payment.p_category = 'application'
289        payment.p_state = 'paid'
290        applicant['p120'] = payment
291        return applicant
292
293    def test_export(self):
294        applicant = self.setup_applicant(self.applicant)
295        exporter = ApplicantPaymentExporter()
296        exporter.export(applicant.payments, self.outfile)
297        result = open(self.outfile, 'rb').read()
298        cdate = str('%s#' % self.applicant['p120'].creation_date)
299        self.assertEqual(
300            'ac,amount_auth,creation_date,p_category,p_combi,p_id,'
301            'p_item,p_session,p_state,payment_date,r_amount_approved,'
302            'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n'
303            ',0.0,%s,application,[],p120,,2012,paid,,0.0,,,dp2011_654321,'
304            '123456,Anna M. Tester\r\n' % cdate, result)
305        return
306
307    def test_export_all(self):
308        self.applicant = self.setup_applicant(self.applicant)
309        exporter = ApplicantPaymentExporter()
310        exporter.export_all(self.app, self.outfile)
311        result = open(self.outfile, 'rb').read()
312        cdate = str('%s#' % self.applicant['p120'].creation_date)
313        self.assertEqual(
314            'ac,amount_auth,creation_date,p_category,p_combi,p_id,'
315            'p_item,p_session,p_state,payment_date,r_amount_approved,'
316            'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n'
317            ',0.0,%s,application,[],p120,,2012,paid,,0.0,,,dp2011_654321,'
318            '123456,Anna M. Tester\r\n' % cdate, result)
319        return
320
321    def test_export_filtered(self):
322        self.applicant = self.setup_applicant(self.applicant)
323        cdate = str('%s#' % self.applicant['p120'].creation_date)
324        self.applicant['p120'].payment_date = datetime.datetime(
325            curr_year-6, 4, 1, 14, 12, 1)
326        exporter = ApplicantPaymentExporter()
327        exporter.export_filtered(
328            self.app, self.outfile, container=self.container.code)
329        result = open(self.outfile, 'rb').read()
330        self.assertEqual(
331            'ac,amount_auth,creation_date,p_category,p_combi,p_id,'
332            'p_item,p_session,p_state,payment_date,r_amount_approved,'
333            'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n'
334            ',0.0,%s,application,[],p120,,2012,paid,2019-04-01 14:12:01#,0.0,'
335            ',,dp2011_654321,'
336            '123456,Anna M. Tester\r\n' % cdate, result)
337        exporter.export_filtered(
338            self.app, self.outfile, container=self.container.code,
339            p_start='03/02/2025', p_end='04/02/2025')
340        result = open(self.outfile, 'rb').read()
341        self.assertEqual(
342            'ac,amount_auth,creation_date,p_category,p_combi,p_id,'
343            'p_item,p_session,p_state,payment_date,r_amount_approved,'
344            'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n',
345            result)
346        exporter.export_filtered(
347            self.app, self.outfile, container=self.container.code,
348            p_start='03/02/2019', p_end='04/05/2019')
349        result = open(self.outfile, 'rb').read()
350        self.assertEqual(
351            'ac,amount_auth,creation_date,p_category,p_combi,p_id,'
352            'p_item,p_session,p_state,payment_date,r_amount_approved,'
353            'r_code,r_desc,applicant_id,reg_number,display_fullname\r\n'
354            ',0.0,%s,application,[],p120,,2012,paid,2019-04-01 14:12:01#,0.0,'
355            ',,dp2011_654321,'
356            '123456,Anna M. Tester\r\n' % cdate, result)
357        return
358
359class ApplicantRefereeReportExporterTest(ApplicantImportExportSetup):
360
361    layer = FunctionalLayer
362
363    def setUp(self):
364        super(ApplicantRefereeReportExporterTest, self).setUp()
365        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
366        self.cat = getUtility(ICatalog, name='applicants_catalog')
367        self.intids = getUtility(IIntIds)
368        return
369
370    def test_ifaces(self):
371        # make sure we fullfill interface contracts
372        obj = ApplicantRefereeReportExporter()
373        verifyObject(ICSVExporter, obj)
374        verifyClass(ICSVExporter, ApplicantRefereeReportExporter)
375        return
376
377    def test_get_as_utility(self):
378        # we can get an applicant exporter as utility
379        result = queryUtility(ICSVExporter, name="applicantrefereereports")
380        self.assertTrue(result is not None)
381        return
382
383    def setup_applicant(self, applicant):
384        # set predictable values for `applicant`
385        applicant.reg_number = u'123456'
386        applicant.applicant_id = u'dp2011_654321'
387        applicant.firstname = u'Anna'
388        applicant.lastname = u'Tester'
389        applicant.middlename = u'M.'
390        applicant.date_of_birth = datetime.date(1981, 2, 4)
391        applicant.sex = 'f'
392        applicant.email = 'anna@sample.com'
393        applicant.phone = u'+234-123-12345'
394        # Add payment
395        report = createObject(u'waeup.ApplicantRefereeReport')
396        report.r_id = 'r120'
397        report.name = u'John Doe'
398        report.email = 'xx@xx.xx'
399        applicant['r120'] = report
400        return applicant
401
402    def test_export(self):
403        applicant = self.setup_applicant(self.applicant)
404        exporter = ApplicantRefereeReportExporter()
405        exporter.export(applicant.refereereports, self.outfile)
406        result = open(self.outfile, 'rb').read()
407        cdate = str('%s#' % self.applicant['r120'].creation_date)
408        self.assertEqual(
409            'creation_date,email,email_pref,name,phone,r_id,report,'
410            'applicant_id,reg_number,display_fullname\r\n'
411            '%s,xx@xx.xx,aa@aa.aa,John Doe,,r120,,dp2011_654321,'
412            '123456,Anna M. Tester\r\n' % cdate, result)
413        return
414
415    def test_export_all(self):
416        self.applicant = self.setup_applicant(self.applicant)
417        exporter = ApplicantRefereeReportExporter()
418        exporter.export_all(self.app, self.outfile)
419        result = open(self.outfile, 'rb').read()
420        cdate = str('%s#' % self.applicant['r120'].creation_date)
421        self.assertEqual(
422            'creation_date,email,email_pref,name,phone,r_id,report,'
423            'applicant_id,reg_number,display_fullname\r\n'
424            '%s,xx@xx.xx,aa@aa.aa,John Doe,,r120,,dp2011_654321,'
425            '123456,Anna M. Tester\r\n' % cdate, result)
426        return
427
428    def test_export_filtered(self):
429        self.applicant = self.setup_applicant(self.applicant)
430        exporter = ApplicantRefereeReportExporter()
431        exporter.export_filtered(
432            self.app, self.outfile, container=self.container.code)
433        result = open(self.outfile, 'rb').read()
434        cdate = str('%s#' % self.applicant['r120'].creation_date)
435        self.assertEqual(
436            'creation_date,email,email_pref,name,phone,r_id,report,'
437            'applicant_id,reg_number,display_fullname\r\n'
438            '%s,xx@xx.xx,aa@aa.aa,John Doe,,r120,,dp2011_654321,'
439            '123456,Anna M. Tester\r\n' % cdate, result)
440        return
441
Note: See TracBrowser for help on using the repository browser.