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

Last change on this file since 17930 was 17766, checked in by Henrik Bettermann, 6 months ago

Add ApplicantRefereeReportExporterTest?.

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