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.entry_level = 100 |
---|
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 | 2012, 1, 1, 12, 0, 0, tzinfo=pytz.utc) |
---|
61 | container.enddate = datetime.datetime( |
---|
62 | 2012, 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 | 'code,title,prefix,entry_level,year,application_category,' |
---|
75 | 'description,startdate,enddate,strict_deadline\r\n' |
---|
76 | |
---|
77 | 'dp2012,General Studies 2012/13,app,100,2012,basic,' |
---|
78 | '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",' |
---|
79 | '2012-01-01 12:00:00+00:00,2012-01-31 23:00:00+00:00,1\r\n' |
---|
80 | ) |
---|
81 | return |
---|
82 | |
---|
83 | class ApplicantsExporterTest(ApplicantImportExportSetup): |
---|
84 | |
---|
85 | layer = FunctionalLayer |
---|
86 | |
---|
87 | def setUp(self): |
---|
88 | super(ApplicantsExporterTest, self).setUp() |
---|
89 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
90 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
91 | self.intids = getUtility(IIntIds) |
---|
92 | return |
---|
93 | |
---|
94 | def test_ifaces(self): |
---|
95 | # make sure we fullfill interface contracts |
---|
96 | obj = ApplicantsExporter() |
---|
97 | verifyObject(ICSVExporter, obj) |
---|
98 | verifyClass(ICSVExporter, ApplicantsExporter) |
---|
99 | return |
---|
100 | |
---|
101 | def test_get_as_utility(self): |
---|
102 | # we can get an applicant exporter as utility |
---|
103 | result = queryUtility(ICSVExporter, name="applicants") |
---|
104 | self.assertTrue(result is not None) |
---|
105 | return |
---|
106 | |
---|
107 | def setup_applicant(self, applicant): |
---|
108 | # set predictable values for `applicant` |
---|
109 | applicant.reg_number = u'123456' |
---|
110 | applicant.applicant_id = u'dp2011_654321' |
---|
111 | applicant.firstname = u'Anna' |
---|
112 | applicant.lastname = u'Tester' |
---|
113 | applicant.middlename = u'M.' |
---|
114 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
115 | applicant.sex = 'f' |
---|
116 | applicant.email = 'anna@sample.com' |
---|
117 | applicant.phone = u'+234-123-12345' |
---|
118 | applicant.course1 = self.certificate |
---|
119 | applicant.course2 = self.certificate |
---|
120 | applicant.course_admitted = self.certificate |
---|
121 | applicant.notice = u'Some notice\nin lines.' |
---|
122 | applicant.screening_score = 98 |
---|
123 | applicant.screening_venue = u'Exam Room' |
---|
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 = ApplicantsExporter() |
---|
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,application_date,application_number,course1,course2,' |
---|
144 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
145 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
146 | 'reg_number,screening_score,screening_venue,sex,state,' |
---|
147 | 'student_id\r\n' |
---|
148 | 'dp2011_654321,,654321,,,,,Anna Tester,,Anna,' |
---|
149 | in result) |
---|
150 | self.assertTrue( |
---|
151 | 'Application initialized by system\'],Tester,' |
---|
152 | '0,,,,,,,,,initialized,\r\n' |
---|
153 | in result) |
---|
154 | return |
---|
155 | |
---|
156 | def test_export(self): |
---|
157 | # we can really export applicants |
---|
158 | # set values we can expect in export file |
---|
159 | applicant = self.setup_applicant(self.applicant) |
---|
160 | exporter = ApplicantsExporter() |
---|
161 | exporter.export([applicant], self.outfile) |
---|
162 | result = open(self.outfile, 'rb').read() |
---|
163 | # The exported records do contain a real date in their |
---|
164 | # history dict. We skip the date and split the comparison |
---|
165 | # into two parts. |
---|
166 | self.assertTrue( |
---|
167 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
168 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
169 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
170 | 'reg_number,screening_score,screening_venue,sex,state,' |
---|
171 | 'student_id\r\n' |
---|
172 | 'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04,' |
---|
173 | 'Anna M. Tester,anna@sample.com,Anna,' |
---|
174 | in result) |
---|
175 | self.assertTrue( |
---|
176 | 'Application initialized by system\'],' |
---|
177 | 'Tester,0,M.,"Some notice\nin lines.",any password,' |
---|
178 | '+234-123-12345,123456,98,Exam Room,f,initialized,\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 = ApplicantsExporter() |
---|
188 | exporter.export_all(self.app, self.outfile) |
---|
189 | result = open(self.outfile, 'rb').read() |
---|
190 | self.assertTrue( |
---|
191 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
192 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
193 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
194 | 'reg_number,screening_score,screening_venue,sex,state,' |
---|
195 | 'student_id\r\n' |
---|
196 | 'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04,' |
---|
197 | 'Anna M. Tester,anna@sample.com,Anna,' |
---|
198 | in result) |
---|
199 | self.assertTrue( |
---|
200 | 'Application initialized by system\'],' |
---|
201 | 'Tester,0,M.,"Some notice\nin lines.",any password,' |
---|
202 | '+234-123-12345,123456,98,Exam Room,f,initialized,\r\n' |
---|
203 | in result) |
---|
204 | return |
---|