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, getUtilitiesFor |
---|
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, ApplicantsExporter) |
---|
14 | from waeup.kofa.applicants.interfaces import ( |
---|
15 | AppCatSource, ApplicationTypeSource) |
---|
16 | from waeup.kofa.applicants.tests.test_batching import ( |
---|
17 | ApplicantImportExportSetup) |
---|
18 | from waeup.kofa.interfaces import ICSVExporter |
---|
19 | from waeup.kofa.schoolgrades import ResultEntry |
---|
20 | from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer |
---|
21 | from waeup.kofa.utils.utils import KofaUtils |
---|
22 | |
---|
23 | class ApplicantsContainersExporterTest(unittest.TestCase): |
---|
24 | |
---|
25 | layer = KofaUnitTestLayer |
---|
26 | |
---|
27 | def setUp(self): |
---|
28 | self.workdir = tempfile.mkdtemp() |
---|
29 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
30 | return |
---|
31 | |
---|
32 | def tearDown(self): |
---|
33 | shutil.rmtree(self.workdir) |
---|
34 | return |
---|
35 | |
---|
36 | def test_ifaces(self): |
---|
37 | # make sure we fullfill interface contracts |
---|
38 | obj = ApplicantsContainerExporter() |
---|
39 | verifyObject(ICSVExporter, obj) |
---|
40 | verifyClass(ICSVExporter, ApplicantsContainerExporter) |
---|
41 | return |
---|
42 | |
---|
43 | def test_get_as_utility(self): |
---|
44 | # we can get a faculty exporter as utility |
---|
45 | result = queryUtility(ICSVExporter, name="applicantscontainers") |
---|
46 | self.assertTrue(result is not None) |
---|
47 | return |
---|
48 | |
---|
49 | def setup_container(self, container): |
---|
50 | # set all attributes of a container |
---|
51 | container.code = u'dp2012' |
---|
52 | container.title = u'General Studies 2012/13' |
---|
53 | container.prefix = list(ApplicationTypeSource()(container))[0] |
---|
54 | container.year = 2012 |
---|
55 | container.application_category = list(AppCatSource()(container))[0] |
---|
56 | container.description = u'Some Description\nwith linebreak\n' |
---|
57 | container.description += u'<<de>>man spriht deutsh' |
---|
58 | container.startdate = datetime.datetime( |
---|
59 | 2012, 1, 1, 12, 0, 0, tzinfo=pytz.utc) |
---|
60 | container.enddate = datetime.datetime( |
---|
61 | 2012, 1, 31, 23, 0, 0, tzinfo=pytz.utc) |
---|
62 | return container |
---|
63 | |
---|
64 | def test_export(self): |
---|
65 | # we can export a set of applicants containers (w/o applicants) |
---|
66 | container = ApplicantsContainer() |
---|
67 | container = self.setup_container(container) |
---|
68 | exporter = ApplicantsContainerExporter() |
---|
69 | exporter.export([container], self.outfile) |
---|
70 | result = open(self.outfile, 'rb').read() |
---|
71 | self.assertEqual( |
---|
72 | result, |
---|
73 | 'code,title,prefix,year,application_category,' |
---|
74 | 'description,startdate,enddate,strict_deadline\r\n' |
---|
75 | |
---|
76 | 'dp2012,General Studies 2012/13,app,2012,basic,' |
---|
77 | '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",' |
---|
78 | '2012-01-01 12:00:00+00:00,2012-01-31 23:00:00+00:00,1\r\n' |
---|
79 | ) |
---|
80 | return |
---|
81 | |
---|
82 | class ApplicantsExporterTest(ApplicantImportExportSetup): |
---|
83 | |
---|
84 | layer = FunctionalLayer |
---|
85 | |
---|
86 | def setUp(self): |
---|
87 | super(ApplicantsExporterTest, self).setUp() |
---|
88 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
89 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
90 | self.intids = getUtility(IIntIds) |
---|
91 | return |
---|
92 | |
---|
93 | def test_ifaces(self): |
---|
94 | # make sure we fullfill interface contracts |
---|
95 | obj = ApplicantsExporter() |
---|
96 | verifyObject(ICSVExporter, obj) |
---|
97 | verifyClass(ICSVExporter, ApplicantsExporter) |
---|
98 | return |
---|
99 | |
---|
100 | def test_get_as_utility(self): |
---|
101 | # we can get an applicant exporter as utility |
---|
102 | result = queryUtility(ICSVExporter, name="applicants") |
---|
103 | self.assertTrue(result is not None) |
---|
104 | return |
---|
105 | |
---|
106 | def setup_applicant(self, applicant): |
---|
107 | # set predictable values for `applicant` |
---|
108 | applicant.reg_number = u'123456' |
---|
109 | applicant.applicant_id = u'dp2011_654321' |
---|
110 | applicant.firstname = u'Anna' |
---|
111 | applicant.lastname = u'Tester' |
---|
112 | applicant.middlename = u'M.' |
---|
113 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
114 | applicant.sex = 'f' |
---|
115 | applicant.email = 'anna@sample.com' |
---|
116 | applicant.phone = u'+234-123-12345' |
---|
117 | applicant.course1 = self.certificate |
---|
118 | applicant.course2 = self.certificate |
---|
119 | applicant.course_admitted = self.certificate |
---|
120 | applicant.notice = u'Some notice\nin lines.' |
---|
121 | applicant.screening_score = 98 |
---|
122 | applicant.screening_venue = u'Exam Room' |
---|
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 = ApplicantsExporter() |
---|
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,application_date,application_number,course1,course2,' |
---|
143 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
144 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
145 | 'reg_number,screening_score,screening_venue,sex,state,' |
---|
146 | 'student_id,container_code\r\n' |
---|
147 | 'dp2011_654321,,654321,,,,,Anna Tester,,Anna,' |
---|
148 | in result) |
---|
149 | self.assertTrue( |
---|
150 | 'Application initialized by system\'],Tester,' |
---|
151 | '0,,,,,,,,,initialized,,dp2011\r\n' |
---|
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 = ApplicantsExporter() |
---|
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,application_date,application_number,course1,course2,' |
---|
167 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
168 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
169 | 'reg_number,screening_score,screening_venue,sex,state,' |
---|
170 | 'student_id,container_code\r\n' |
---|
171 | 'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
172 | 'Anna M. Tester,anna@sample.com,Anna,' |
---|
173 | in result) |
---|
174 | self.assertTrue( |
---|
175 | 'Application initialized by system\'],' |
---|
176 | 'Tester,0,M.,"Some notice\nin lines.",any password,' |
---|
177 | '+234-123-12345,123456,98,Exam Room,f,initialized,,dp2011\r\n' |
---|
178 | in result) |
---|
179 | |
---|
180 | return |
---|
181 | |
---|
182 | def test_export_all(self): |
---|
183 | # we can export all applicants in a portal |
---|
184 | # set values we can expect in export file |
---|
185 | self.applicant = self.setup_applicant(self.applicant) |
---|
186 | exporter = ApplicantsExporter() |
---|
187 | exporter.export_all(self.app, self.outfile) |
---|
188 | result = open(self.outfile, 'rb').read() |
---|
189 | self.assertTrue( |
---|
190 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
191 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
192 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
193 | 'reg_number,screening_score,screening_venue,sex,state,' |
---|
194 | 'student_id,container_code\r\n' |
---|
195 | 'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
196 | 'Anna M. Tester,anna@sample.com,Anna,' |
---|
197 | in result) |
---|
198 | self.assertTrue( |
---|
199 | 'Application initialized by system\'],' |
---|
200 | 'Tester,0,M.,"Some notice\nin lines.",any password,' |
---|
201 | '+234-123-12345,123456,98,Exam Room,f,initialized,,dp2011\r\n' |
---|
202 | in result) |
---|
203 | return |
---|