1 | import os |
---|
2 | import shutil |
---|
3 | import tempfile |
---|
4 | import unittest |
---|
5 | from zope.component import queryUtility |
---|
6 | from zope.interface.verify import verifyObject, verifyClass |
---|
7 | from waeup.sirp.interfaces import ICSVExporter |
---|
8 | from waeup.sirp.testing import SIRPUnitTestLayer |
---|
9 | from waeup.sirp.university import ( |
---|
10 | FacultiesContainer, Faculty, Department, Course, Certificate, |
---|
11 | ) |
---|
12 | from waeup.sirp.university.export import ( |
---|
13 | FacultyExporter, DepartmentExporter, CourseExporter, |
---|
14 | CertificateExporter, |
---|
15 | ) |
---|
16 | |
---|
17 | class FacultyExporterTest(unittest.TestCase): |
---|
18 | |
---|
19 | layer = SIRPUnitTestLayer |
---|
20 | |
---|
21 | def setUp(self): |
---|
22 | self.workdir = tempfile.mkdtemp() |
---|
23 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
24 | return |
---|
25 | |
---|
26 | def tearDown(self): |
---|
27 | shutil.rmtree(self.workdir) |
---|
28 | return |
---|
29 | |
---|
30 | def test_ifaces(self): |
---|
31 | # make sure we fullfill interface contracts |
---|
32 | obj = FacultyExporter() |
---|
33 | verifyObject(ICSVExporter, obj) |
---|
34 | verifyClass(ICSVExporter, FacultyExporter) |
---|
35 | return |
---|
36 | |
---|
37 | def test_get_as_utility(self): |
---|
38 | # we can get a faculty exporter as utility |
---|
39 | result = queryUtility(ICSVExporter, name="faculties") |
---|
40 | self.assertTrue(result is not None) |
---|
41 | return |
---|
42 | |
---|
43 | def test_export(self): |
---|
44 | # we can export a set of faculties |
---|
45 | fac = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
46 | exporter = FacultyExporter() |
---|
47 | exporter.export([fac], self.outfile) |
---|
48 | result = open(self.outfile, 'rb').read() |
---|
49 | self.assertEqual( |
---|
50 | result, |
---|
51 | 'code,title,title_prefix\r\n' |
---|
52 | 'F1,Faculty of Cheese,faculty\r\n' |
---|
53 | ) |
---|
54 | return |
---|
55 | |
---|
56 | def test_export_to_string(self): |
---|
57 | # we can export a set of faculties to a string. |
---|
58 | fac = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
59 | exporter = FacultyExporter() |
---|
60 | result = exporter.export([fac], filepath=None) |
---|
61 | self.assertEqual( |
---|
62 | result, |
---|
63 | 'code,title,title_prefix\r\n' |
---|
64 | 'F1,Faculty of Cheese,faculty\r\n' |
---|
65 | ) |
---|
66 | return |
---|
67 | |
---|
68 | def test_export_all(self): |
---|
69 | # we can export all faculties in a site |
---|
70 | container = FacultiesContainer() |
---|
71 | site = {'faculties':container} |
---|
72 | fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
73 | fac2 = Faculty('Centre of Onion', 'centre', 'F2') |
---|
74 | container.addFaculty(fac1) |
---|
75 | container.addFaculty(fac2) |
---|
76 | exporter = FacultyExporter() |
---|
77 | exporter.export_all(site, self.outfile) |
---|
78 | result = open(self.outfile, 'rb').read() |
---|
79 | self.assertEqual( |
---|
80 | result, |
---|
81 | 'code,title,title_prefix\r\n' |
---|
82 | 'F1,Faculty of Cheese,faculty\r\n' |
---|
83 | 'F2,Centre of Onion,centre\r\n' |
---|
84 | ) |
---|
85 | return |
---|
86 | |
---|
87 | def test_export_all_to_string(self): |
---|
88 | # we can export all faculties in a site to a string |
---|
89 | container = FacultiesContainer() |
---|
90 | site = {'faculties':container} |
---|
91 | fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
92 | fac2 = Faculty('Centre of Onion', 'centre', 'F2') |
---|
93 | container.addFaculty(fac1) |
---|
94 | container.addFaculty(fac2) |
---|
95 | exporter = FacultyExporter() |
---|
96 | result = exporter.export_all(site, filepath=None) |
---|
97 | self.assertEqual( |
---|
98 | result, |
---|
99 | 'code,title,title_prefix\r\n' |
---|
100 | 'F1,Faculty of Cheese,faculty\r\n' |
---|
101 | 'F2,Centre of Onion,centre\r\n' |
---|
102 | ) |
---|
103 | return |
---|
104 | |
---|
105 | class DepartmentExporterTest(unittest.TestCase): |
---|
106 | # Tests for DepartmentExporter |
---|
107 | |
---|
108 | layer = SIRPUnitTestLayer |
---|
109 | |
---|
110 | def setUp(self): |
---|
111 | self.workdir = tempfile.mkdtemp() |
---|
112 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
113 | # create some departments in a fake site |
---|
114 | container = FacultiesContainer() |
---|
115 | self.site = {'faculties':container} |
---|
116 | self.fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
117 | self.fac2 = Faculty('Centre of Onion', 'centre', 'F2') |
---|
118 | container.addFaculty(self.fac1) |
---|
119 | container.addFaculty(self.fac2) |
---|
120 | self.dept1 = Department('Department of Cheddar', 'department', 'D1') |
---|
121 | self.dept2 = Department('Institue of Gouda', 'institute', 'D2') |
---|
122 | self.dept3 = Department('Department of Rings', 'department', 'D3') |
---|
123 | self.fac1.addDepartment(self.dept1) |
---|
124 | self.fac1.addDepartment(self.dept2) |
---|
125 | self.fac2.addDepartment(self.dept3) |
---|
126 | return |
---|
127 | |
---|
128 | def tearDown(self): |
---|
129 | shutil.rmtree(self.workdir) |
---|
130 | return |
---|
131 | |
---|
132 | def test_ifaces(self): |
---|
133 | # make sure we fullfill interface contracts |
---|
134 | obj = DepartmentExporter() |
---|
135 | verifyObject(ICSVExporter, obj) |
---|
136 | verifyClass(ICSVExporter, DepartmentExporter) |
---|
137 | return |
---|
138 | |
---|
139 | def test_get_as_utility(self): |
---|
140 | # we can get a department exporter as utility |
---|
141 | result = queryUtility(ICSVExporter, name="departments") |
---|
142 | self.assertTrue(result is not None) |
---|
143 | return |
---|
144 | |
---|
145 | def test_export(self): |
---|
146 | # we can export an iterable of departments |
---|
147 | exporter = DepartmentExporter() |
---|
148 | exporter.export([self.dept1], self.outfile) |
---|
149 | result = open(self.outfile, 'rb').read() |
---|
150 | self.assertEqual( |
---|
151 | result, |
---|
152 | 'code,faculty_code,title,title_prefix\r\n' |
---|
153 | 'D1,F1,Department of Cheddar,department\r\n' |
---|
154 | ) |
---|
155 | return |
---|
156 | |
---|
157 | def test_export_to_string(self): |
---|
158 | # we can export an iterable of departments to a string. |
---|
159 | exporter = DepartmentExporter() |
---|
160 | result = exporter.export([self.dept1, self.dept2], filepath=None) |
---|
161 | self.assertEqual( |
---|
162 | result, |
---|
163 | 'code,faculty_code,title,title_prefix\r\n' |
---|
164 | 'D1,F1,Department of Cheddar,department\r\n' |
---|
165 | 'D2,F1,Institue of Gouda,institute\r\n' |
---|
166 | ) |
---|
167 | return |
---|
168 | |
---|
169 | def test_export_all(self): |
---|
170 | # we can export all depts in a site |
---|
171 | exporter = DepartmentExporter() |
---|
172 | exporter.export_all(self.site, self.outfile) |
---|
173 | result = open(self.outfile, 'rb').read() |
---|
174 | self.assertEqual( |
---|
175 | result, |
---|
176 | 'code,faculty_code,title,title_prefix\r\n' |
---|
177 | 'D1,F1,Department of Cheddar,department\r\n' |
---|
178 | 'D2,F1,Institue of Gouda,institute\r\n' |
---|
179 | 'D3,F2,Department of Rings,department\r\n' |
---|
180 | ) |
---|
181 | return |
---|
182 | |
---|
183 | def test_export_all_to_string(self): |
---|
184 | # we can export all depts in a site to a string |
---|
185 | exporter = DepartmentExporter() |
---|
186 | result = exporter.export_all(self.site, filepath=None) |
---|
187 | self.assertEqual( |
---|
188 | result, |
---|
189 | 'code,faculty_code,title,title_prefix\r\n' |
---|
190 | 'D1,F1,Department of Cheddar,department\r\n' |
---|
191 | 'D2,F1,Institue of Gouda,institute\r\n' |
---|
192 | 'D3,F2,Department of Rings,department\r\n' |
---|
193 | ) |
---|
194 | return |
---|
195 | |
---|
196 | class CourseExporterTest(unittest.TestCase): |
---|
197 | # Tests for CourseExporter |
---|
198 | |
---|
199 | layer = SIRPUnitTestLayer |
---|
200 | |
---|
201 | def setUp(self): |
---|
202 | self.workdir = tempfile.mkdtemp() |
---|
203 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
204 | # create some departments and courses in a fake site |
---|
205 | container = FacultiesContainer() |
---|
206 | self.site = {'faculties':container} |
---|
207 | self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
208 | container.addFaculty(self.fac) |
---|
209 | self.dept1 = Department('Department of Cheddar', 'department', 'D1') |
---|
210 | self.dept2 = Department('Institue of Gouda', 'institute', 'D2') |
---|
211 | self.fac.addDepartment(self.dept1) |
---|
212 | self.fac.addDepartment(self.dept2) |
---|
213 | self.course1 = Course('Cheese Basics', 'C1') |
---|
214 | self.course2 = Course('Advanced Cheese Making', 'C2') |
---|
215 | self.course3 = Course('Selling Cheese', 'C3') |
---|
216 | self.dept1.courses.addCourse(self.course1) |
---|
217 | self.dept1.courses.addCourse(self.course2) |
---|
218 | self.dept2.courses.addCourse(self.course3) |
---|
219 | return |
---|
220 | |
---|
221 | def tearDown(self): |
---|
222 | shutil.rmtree(self.workdir) |
---|
223 | return |
---|
224 | |
---|
225 | def test_ifaces(self): |
---|
226 | # make sure we fullfill interface contracts |
---|
227 | obj = CourseExporter() |
---|
228 | verifyObject(ICSVExporter, obj) |
---|
229 | verifyClass(ICSVExporter, CourseExporter) |
---|
230 | return |
---|
231 | |
---|
232 | def test_get_as_utility(self): |
---|
233 | # we can get a course exporter as utility |
---|
234 | result = queryUtility(ICSVExporter, name="courses") |
---|
235 | self.assertTrue(result is not None) |
---|
236 | return |
---|
237 | |
---|
238 | def test_export(self): |
---|
239 | # we can export an iterable of courses |
---|
240 | exporter = CourseExporter() |
---|
241 | exporter.export([self.course1], self.outfile) |
---|
242 | result = open(self.outfile, 'rb').read() |
---|
243 | self.assertEqual( |
---|
244 | result, |
---|
245 | 'code,faculty_code,department_code,title,credits,passmark,semester\r\n' |
---|
246 | 'C1,F1,D1,Cheese Basics,0,40,1\r\n' |
---|
247 | ) |
---|
248 | return |
---|
249 | |
---|
250 | def test_export_to_string(self): |
---|
251 | # we can export an iterable of courses to a string. |
---|
252 | exporter = CourseExporter() |
---|
253 | result = exporter.export([self.course1, self.course2], filepath=None) |
---|
254 | self.assertEqual( |
---|
255 | result, |
---|
256 | 'code,faculty_code,department_code,title,credits,passmark,semester\r\n' |
---|
257 | 'C1,F1,D1,Cheese Basics,0,40,1\r\n' |
---|
258 | 'C2,F1,D1,Advanced Cheese Making,0,40,1\r\n' |
---|
259 | ) |
---|
260 | return |
---|
261 | |
---|
262 | def test_export_all(self): |
---|
263 | # we can export all courses in a site |
---|
264 | exporter = CourseExporter() |
---|
265 | exporter.export_all(self.site, self.outfile) |
---|
266 | result = open(self.outfile, 'rb').read() |
---|
267 | self.assertEqual( |
---|
268 | result, |
---|
269 | 'code,faculty_code,department_code,title,credits,passmark,semester\r\n' |
---|
270 | 'C1,F1,D1,Cheese Basics,0,40,1\r\n' |
---|
271 | 'C2,F1,D1,Advanced Cheese Making,0,40,1\r\n' |
---|
272 | 'C3,F1,D2,Selling Cheese,0,40,1\r\n' |
---|
273 | ) |
---|
274 | return |
---|
275 | |
---|
276 | def test_export_all_to_string(self): |
---|
277 | # we can export all courses in a site to a string |
---|
278 | exporter = CourseExporter() |
---|
279 | result = exporter.export_all(self.site, filepath=None) |
---|
280 | self.assertEqual( |
---|
281 | result, |
---|
282 | 'code,faculty_code,department_code,title,credits,passmark,semester\r\n' |
---|
283 | 'C1,F1,D1,Cheese Basics,0,40,1\r\n' |
---|
284 | 'C2,F1,D1,Advanced Cheese Making,0,40,1\r\n' |
---|
285 | 'C3,F1,D2,Selling Cheese,0,40,1\r\n' |
---|
286 | ) |
---|
287 | return |
---|
288 | |
---|
289 | class CertificateExporterTest(unittest.TestCase): |
---|
290 | # Tests for CertificateExporter |
---|
291 | |
---|
292 | layer = SIRPUnitTestLayer |
---|
293 | |
---|
294 | def setUp(self): |
---|
295 | self.workdir = tempfile.mkdtemp() |
---|
296 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
297 | # create some departments and courses in a fake site |
---|
298 | container = FacultiesContainer() |
---|
299 | self.site = {'faculties':container} |
---|
300 | self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1') |
---|
301 | container.addFaculty(self.fac) |
---|
302 | self.dept1 = Department('Department of Cheddar', 'department', 'D1') |
---|
303 | self.dept2 = Department('Institue of Gouda', 'institute', 'D2') |
---|
304 | self.fac.addDepartment(self.dept1) |
---|
305 | self.fac.addDepartment(self.dept2) |
---|
306 | self.course1 = Course('Cheese Basics', 'C1') |
---|
307 | self.course2 = Course('Advanced Cheese Making', 'C2') |
---|
308 | self.course3 = Course('Selling Cheese', 'C3') |
---|
309 | self.dept1.courses.addCourse(self.course1) |
---|
310 | self.dept1.courses.addCourse(self.course2) |
---|
311 | self.dept2.courses.addCourse(self.course3) |
---|
312 | self.cert1 = Certificate( |
---|
313 | 'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100, |
---|
314 | end_level=300, application_category='basic') |
---|
315 | self.cert2 = Certificate( |
---|
316 | 'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400, |
---|
317 | end_level=700, application_category='cest') |
---|
318 | self.cert3 = Certificate( |
---|
319 | 'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100, |
---|
320 | end_level=200, application_category='no') |
---|
321 | self.dept1.certificates.addCertificate(self.cert1) |
---|
322 | self.dept1.certificates.addCertificate(self.cert2) |
---|
323 | self.dept2.certificates.addCertificate(self.cert3) |
---|
324 | return |
---|
325 | |
---|
326 | def tearDown(self): |
---|
327 | shutil.rmtree(self.workdir) |
---|
328 | return |
---|
329 | |
---|
330 | def test_ifaces(self): |
---|
331 | # make sure we fullfill interface contracts |
---|
332 | obj = CertificateExporter() |
---|
333 | verifyObject(ICSVExporter, obj) |
---|
334 | verifyClass(ICSVExporter, CertificateExporter) |
---|
335 | return |
---|
336 | |
---|
337 | def test_get_as_utility(self): |
---|
338 | # we can get a certificate exporter as utility |
---|
339 | result = queryUtility(ICSVExporter, name="certificates") |
---|
340 | self.assertTrue(result is not None) |
---|
341 | return |
---|
342 | |
---|
343 | def test_export(self): |
---|
344 | # we can export an iterable of certificates |
---|
345 | exporter = CertificateExporter() |
---|
346 | exporter.export([self.cert1], self.outfile) |
---|
347 | result = open(self.outfile, 'rb').read() |
---|
348 | self.assertEqual( |
---|
349 | result, |
---|
350 | 'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n' |
---|
351 | 'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n' |
---|
352 | ) |
---|
353 | return |
---|
354 | |
---|
355 | def test_export_to_string(self): |
---|
356 | # we can export an iterable of certificates to a string. |
---|
357 | exporter = CertificateExporter() |
---|
358 | result = exporter.export([self.cert1, self.cert2], filepath=None) |
---|
359 | self.assertEqual( |
---|
360 | result, |
---|
361 | 'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n' |
---|
362 | 'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n' |
---|
363 | 'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest\r\n' |
---|
364 | ) |
---|
365 | return |
---|
366 | |
---|
367 | def test_export_all(self): |
---|
368 | # we can export all certificates in a site |
---|
369 | exporter = CertificateExporter() |
---|
370 | exporter.export_all(self.site, self.outfile) |
---|
371 | result = open(self.outfile, 'rb').read() |
---|
372 | self.assertEqual( |
---|
373 | result, |
---|
374 | 'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n' |
---|
375 | 'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n' |
---|
376 | 'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest\r\n' |
---|
377 | 'CERT3,F1,D2,Cert. of Rubbish,dp_pt,100,200,no\r\n' |
---|
378 | ) |
---|
379 | return |
---|
380 | |
---|
381 | def test_export_all_to_string(self): |
---|
382 | # we can export all certificates in a site to a string |
---|
383 | exporter = CertificateExporter() |
---|
384 | result = exporter.export_all(self.site, filepath=None) |
---|
385 | self.assertEqual( |
---|
386 | result, |
---|
387 | 'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n' |
---|
388 | 'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n' |
---|
389 | 'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest\r\n' |
---|
390 | 'CERT3,F1,D2,Cert. of Rubbish,dp_pt,100,200,no\r\n' |
---|
391 | ) |
---|
392 | return |
---|