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

Last change on this file since 15057 was 13968, checked in by Henrik Bettermann, 8 years ago

Make provision against storing other objects than applicant payments in applicant containers.

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