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 |
---|
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 | 'application_category,application_fee,code,description,' |
---|
74 | 'enddate,mode,prefix,startdate,strict_deadline,title,year\r\n' |
---|
75 | |
---|
76 | 'basic,0.0,dp2012,' |
---|
77 | '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",' |
---|
78 | '2012-01-31 23:00:00+00:00,,app,2012-01-01 12:00:00+00:00,1,' |
---|
79 | 'General Studies 2012/13,2012\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.password = 'any password' |
---|
123 | result_entry = ResultEntry( |
---|
124 | KofaUtils.EXAM_SUBJECTS_DICT.keys()[0], |
---|
125 | KofaUtils.EXAM_GRADES[0][0] |
---|
126 | ) |
---|
127 | applicant.school_grades = [ |
---|
128 | result_entry] |
---|
129 | return applicant |
---|
130 | |
---|
131 | def test_export_emtpy(self): |
---|
132 | # we can export nearly empty applicants |
---|
133 | self.applicant.applicant_id = u'dp2011_654321' |
---|
134 | exporter = ApplicantsExporter() |
---|
135 | exporter.export([self.applicant], self.outfile) |
---|
136 | result = open(self.outfile, 'rb').read() |
---|
137 | # The exported records do contain a real date in their |
---|
138 | # history dict. We skip the date and split the comparison |
---|
139 | # into two parts. |
---|
140 | self.assertTrue( |
---|
141 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
142 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
143 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
144 | 'reg_number,sex,state,' |
---|
145 | 'student_id,suspended,container_code\r\n' |
---|
146 | 'dp2011_654321,,654321,,,,,Anna Tester,,Anna,' |
---|
147 | in result) |
---|
148 | self.assertTrue( |
---|
149 | 'Application initialized by system\'],Tester,' |
---|
150 | '0,,,,,,,initialized,,0,dp2011\r\n' |
---|
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 = ApplicantsExporter() |
---|
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,application_date,application_number,course1,course2,' |
---|
166 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
167 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
168 | 'reg_number,sex,state,' |
---|
169 | 'student_id,suspended,container_code\r\n' |
---|
170 | 'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
171 | 'Anna M. Tester,anna@sample.com,Anna,' |
---|
172 | in result) |
---|
173 | self.assertTrue( |
---|
174 | 'Application initialized by system\'],' |
---|
175 | 'Tester,0,M.,"Some notice\nin lines.",any password,' |
---|
176 | '+234-123-12345#,123456,f,initialized,,0,dp2011\r\n' |
---|
177 | in result) |
---|
178 | |
---|
179 | return |
---|
180 | |
---|
181 | def test_export_all(self): |
---|
182 | # we can export all applicants in a portal |
---|
183 | # set values we can expect in export file |
---|
184 | self.applicant = self.setup_applicant(self.applicant) |
---|
185 | exporter = ApplicantsExporter() |
---|
186 | exporter.export_all(self.app, self.outfile) |
---|
187 | result = open(self.outfile, 'rb').read() |
---|
188 | self.assertTrue( |
---|
189 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
190 | 'course_admitted,date_of_birth,display_fullname,email,firstname,' |
---|
191 | 'history,lastname,locked,middlename,notice,password,phone,' |
---|
192 | 'reg_number,sex,state,' |
---|
193 | 'student_id,suspended,container_code\r\n' |
---|
194 | 'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04#,' |
---|
195 | 'Anna M. Tester,anna@sample.com,Anna,' |
---|
196 | in result) |
---|
197 | self.assertTrue( |
---|
198 | 'Application initialized by system\'],' |
---|
199 | 'Tester,0,M.,"Some notice\nin lines.",any password,' |
---|
200 | '+234-123-12345#,123456,f,initialized,,0,dp2011\r\n' |
---|
201 | in result) |
---|
202 | return |
---|