source: main/waeup.kofa/trunk/src/waeup/kofa/university/tests/test_export.py @ 11150

Last change on this file since 11150 was 10185, checked in by Henrik Bettermann, 12 years ago

Add custom fields to certificates (not used in base package).

  • Property svn:keywords set to Id
File size: 21.9 KB
Line 
1## $Id: test_export.py 10185 2013-05-22 06:45:17Z henrik $
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18import os
19import shutil
20import tempfile
21import unittest
22from zope.component import queryUtility
23from zope.interface.verify import verifyObject, verifyClass
24from zope.securitypolicy.interfaces import IPrincipalRoleManager
25from waeup.kofa.interfaces import ICSVExporter
26from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer
27from waeup.kofa.university import (
28    FacultiesContainer, Faculty, Department, Course, Certificate,
29    )
30from waeup.kofa.university.export import (
31    FacultyExporter, DepartmentExporter, CourseExporter,
32    CertificateExporter, CertificateCourseExporter,
33    )
34
35class FacultyExporterTest(unittest.TestCase):
36
37    layer = FunctionalLayer
38
39    def setUp(self):
40        self.workdir = tempfile.mkdtemp()
41        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
42        return
43
44    def tearDown(self):
45        shutil.rmtree(self.workdir)
46        return
47
48    def test_ifaces(self):
49        # make sure we fullfill interface contracts
50        obj = FacultyExporter()
51        verifyObject(ICSVExporter, obj)
52        verifyClass(ICSVExporter, FacultyExporter)
53        return
54
55    def test_get_as_utility(self):
56        # we can get a faculty exporter as utility
57        result = queryUtility(ICSVExporter, name="faculties")
58        self.assertTrue(result is not None)
59        return
60
61    def test_export(self):
62        # we can export a set of faculties
63        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
64        exporter = FacultyExporter()
65        exporter.export([fac], self.outfile)
66        result = open(self.outfile, 'rb').read()
67        self.assertEqual(
68            result,
69            'code,title,title_prefix,users_with_local_roles\r\n'
70            'F1,Faculty of Cheese,faculty,[]\r\n'
71            )
72        return
73
74    def test_export_to_string(self):
75        # we can export a set of faculties to a string.
76        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
77        exporter = FacultyExporter()
78        result = exporter.export([fac], filepath=None)
79        self.assertEqual(
80            result,
81            'code,title,title_prefix,users_with_local_roles\r\n'
82            'F1,Faculty of Cheese,faculty,[]\r\n'
83            )
84        return
85
86    def test_export_all(self):
87        # we can export all faculties in a site
88        container = FacultiesContainer()
89        site = {'faculties':container}
90        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
91        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
92        container.addFaculty(fac1)
93        container.addFaculty(fac2)
94        exporter = FacultyExporter()
95        exporter.export_all(site, self.outfile)
96        result = open(self.outfile, 'rb').read()
97        self.assertEqual(
98            result,
99            'code,title,title_prefix,users_with_local_roles\r\n'
100            'F1,Faculty of Cheese,faculty,[]\r\n'
101            'F2,Centre of Onion,centre,[]\r\n'
102            )
103        return
104
105    def test_export_all_to_string(self):
106        # we can export all faculties in a site to a string
107        container = FacultiesContainer()
108        site = {'faculties':container}
109        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
110        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
111        container.addFaculty(fac1)
112        container.addFaculty(fac2)
113        exporter = FacultyExporter()
114        result = exporter.export_all(site, filepath=None)
115        self.assertEqual(
116            result,
117            'code,title,title_prefix,users_with_local_roles\r\n'
118            'F1,Faculty of Cheese,faculty,[]\r\n'
119            'F2,Centre of Onion,centre,[]\r\n'
120            )
121        return
122
123class DepartmentExporterTest(unittest.TestCase):
124    # Tests for DepartmentExporter
125
126    layer = FunctionalLayer
127
128    def setUp(self):
129        self.workdir = tempfile.mkdtemp()
130        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
131        # create some departments in a fake site
132        container = FacultiesContainer()
133        self.site = {'faculties':container}
134        self.fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
135        self.fac2 = Faculty('Centre of Onion', 'centre', 'F2')
136        container.addFaculty(self.fac1)
137        container.addFaculty(self.fac2)
138        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
139        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
140        self.dept3 = Department('Department of Rings', 'department', 'D3')
141        self.fac1.addDepartment(self.dept1)
142        self.fac1.addDepartment(self.dept2)
143        self.fac2.addDepartment(self.dept3)
144        role_manager = IPrincipalRoleManager(self.dept1)
145        role_manager.assignRoleToPrincipal(u'bobsrole', u'bob')
146        return
147
148    def tearDown(self):
149        shutil.rmtree(self.workdir)
150        return
151
152    def test_ifaces(self):
153        # make sure we fullfill interface contracts
154        obj = DepartmentExporter()
155        verifyObject(ICSVExporter, obj)
156        verifyClass(ICSVExporter, DepartmentExporter)
157        return
158
159    def test_get_as_utility(self):
160        # we can get a department exporter as utility
161        result = queryUtility(ICSVExporter, name="departments")
162        self.assertTrue(result is not None)
163        return
164
165    def test_export(self):
166        # we can export an iterable of departments
167        exporter = DepartmentExporter()
168        exporter.export([self.dept1], self.outfile)
169        result = open(self.outfile, 'rb').read()
170        self.assertEqual(
171            result,
172            'code,faculty_code,title,title_prefix,users_with_local_roles\r\n'
173            'D1,F1,Department of Cheddar,department,'
174            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
175            )
176        return
177
178    def test_export_to_string(self):
179        # we can export an iterable of departments to a string.
180        exporter = DepartmentExporter()
181        result = exporter.export([self.dept1, self.dept2], filepath=None)
182        self.assertEqual(
183            result,
184            'code,faculty_code,title,title_prefix,users_with_local_roles\r\n'
185            'D1,F1,Department of Cheddar,department,'
186            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
187            'D2,F1,Institue of Gouda,institute,[]\r\n'
188            )
189        return
190
191    def test_export_all(self):
192        # we can export all depts in a site
193        exporter = DepartmentExporter()
194        exporter.export_all(self.site, self.outfile)
195        result = open(self.outfile, 'rb').read()
196        self.assertEqual(
197            result,
198            'code,faculty_code,title,title_prefix,users_with_local_roles\r\n'
199            'D1,F1,Department of Cheddar,department,'
200            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
201            'D2,F1,Institue of Gouda,institute,[]\r\n'
202            'D3,F2,Department of Rings,department,[]\r\n'
203            )
204        return
205
206    def test_export_all_to_string(self):
207        # we can export all depts in a site to a string
208        exporter = DepartmentExporter()
209        result = exporter.export_all(self.site, filepath=None)
210        self.assertEqual(
211            result,
212            'code,faculty_code,title,title_prefix,users_with_local_roles\r\n'
213            'D1,F1,Department of Cheddar,department,'
214            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
215            'D2,F1,Institue of Gouda,institute,[]\r\n'
216            'D3,F2,Department of Rings,department,[]\r\n'
217            )
218        return
219
220class CourseExporterTest(unittest.TestCase):
221    # Tests for CourseExporter
222
223    layer = FunctionalLayer
224
225    def setUp(self):
226        self.workdir = tempfile.mkdtemp()
227        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
228        # create some departments and courses in a fake site
229        container = FacultiesContainer()
230        self.site = {'faculties':container}
231        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
232        container.addFaculty(self.fac)
233        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
234        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
235        self.fac.addDepartment(self.dept1)
236        self.fac.addDepartment(self.dept2)
237        self.course1 = Course('Cheese Basics', 'C1')
238        self.course2 = Course('Advanced Cheese Making', 'C2')
239        self.course3 = Course('Selling Cheese', 'C3')
240        self.dept1.courses.addCourse(self.course1)
241        self.dept1.courses.addCourse(self.course2)
242        self.dept2.courses.addCourse(self.course3)
243        return
244
245    def tearDown(self):
246        shutil.rmtree(self.workdir)
247        return
248
249    def test_ifaces(self):
250        # make sure we fullfill interface contracts
251        obj = CourseExporter()
252        verifyObject(ICSVExporter, obj)
253        verifyClass(ICSVExporter, CourseExporter)
254        return
255
256    def test_get_as_utility(self):
257        # we can get a course exporter as utility
258        result = queryUtility(ICSVExporter, name="courses")
259        self.assertTrue(result is not None)
260        return
261
262    def test_export(self):
263        # we can export an iterable of courses
264        exporter = CourseExporter()
265        exporter.export([self.course1], self.outfile)
266        result = open(self.outfile, 'rb').read()
267        self.assertEqual(
268            result,
269            'code,faculty_code,department_code,title,credits,'
270            'passmark,semester,users_with_local_roles,former_course\r\n'
271            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
272            )
273        return
274
275    def test_export_to_string(self):
276        # we can export an iterable of courses to a string.
277        exporter = CourseExporter()
278        result = exporter.export([self.course1, self.course2], filepath=None)
279        self.assertEqual(
280            result,
281            'code,faculty_code,department_code,title,credits,passmark,'
282            'semester,users_with_local_roles,former_course\r\n'
283            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
284            'C2,F1,D1,Advanced Cheese Making,0,40,1,[],0\r\n'
285            )
286        return
287
288    def test_export_all(self):
289        # we can export all courses in a site
290        exporter = CourseExporter()
291        exporter.export_all(self.site, self.outfile)
292        result = open(self.outfile, 'rb').read()
293        self.assertEqual(
294            result,
295            'code,faculty_code,department_code,title,credits,passmark,'
296            'semester,users_with_local_roles,former_course\r\n'
297            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
298            'C2,F1,D1,Advanced Cheese Making,0,40,1,[],0\r\n'
299            'C3,F1,D2,Selling Cheese,0,40,1,[],0\r\n'
300            )
301        return
302
303    def test_export_all_to_string(self):
304        # we can export all courses in a site to a string
305        exporter = CourseExporter()
306        result = exporter.export_all(self.site, filepath=None)
307        self.assertEqual(
308            result,
309            'code,faculty_code,department_code,title,credits,passmark,'
310            'semester,users_with_local_roles,former_course\r\n'
311            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
312            'C2,F1,D1,Advanced Cheese Making,0,40,1,[],0\r\n'
313            'C3,F1,D2,Selling Cheese,0,40,1,[],0\r\n'
314            )
315        return
316
317class CertificateExporterTest(unittest.TestCase):
318    # Tests for CertificateExporter
319
320    layer = FunctionalLayer
321
322    def setUp(self):
323        self.workdir = tempfile.mkdtemp()
324        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
325        # create some departments and courses in a fake site
326        container = FacultiesContainer()
327        self.site = {'faculties':container}
328        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
329        container.addFaculty(self.fac)
330        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
331        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
332        self.fac.addDepartment(self.dept1)
333        self.fac.addDepartment(self.dept2)
334        self.course1 = Course('Cheese Basics', 'C1')
335        self.course2 = Course('Advanced Cheese Making', 'C2')
336        self.course3 = Course('Selling Cheese', 'C3')
337        self.dept1.courses.addCourse(self.course1)
338        self.dept1.courses.addCourse(self.course2)
339        self.dept2.courses.addCourse(self.course3)
340        self.cert1 = Certificate(
341            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
342            end_level=300, application_category='basic')
343        self.cert2 = Certificate(
344            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
345            end_level=700, application_category='cest')
346        self.cert3 = Certificate(
347            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
348            end_level=200, application_category='no')
349        self.dept1.certificates.addCertificate(self.cert1)
350        self.dept1.certificates.addCertificate(self.cert2)
351        self.dept2.certificates.addCertificate(self.cert3)
352        role_manager = IPrincipalRoleManager(self.cert1)
353        role_manager.assignRoleToPrincipal(u'bobsrole', u'bob')
354        return
355
356    def tearDown(self):
357        shutil.rmtree(self.workdir)
358        return
359
360    def test_ifaces(self):
361        # make sure we fullfill interface contracts
362        obj = CertificateExporter()
363        verifyObject(ICSVExporter, obj)
364        verifyClass(ICSVExporter, CertificateExporter)
365        return
366
367    def test_get_as_utility(self):
368        # we can get a certificate exporter as utility
369        result = queryUtility(ICSVExporter, name="certificates")
370        self.assertTrue(result is not None)
371        return
372
373    def test_export(self):
374        # we can export an iterable of certificates
375        exporter = CertificateExporter()
376        exporter.export([self.cert1], self.outfile)
377        result = open(self.outfile, 'rb').read()
378        self.assertEqual(
379            result,
380            'code,faculty_code,department_code,title,study_mode,start_level,'
381            'end_level,application_category,ratio,school_fee_1,'
382            'school_fee_2,school_fee_3,school_fee_4,'
383            'custom_textline_1,custom_textline_2,'
384            'custom_float_1,custom_float_2,'
385            'users_with_local_roles\r\n'
386            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic,,,,,,,,,,'
387            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
388            )
389        return
390
391    def test_export_to_string(self):
392        # we can export an iterable of certificates to a string.
393        exporter = CertificateExporter()
394        result = exporter.export([self.cert1, self.cert2], filepath=None)
395        self.assertEqual(
396            result,
397            'code,faculty_code,department_code,title,study_mode,start_level,'
398            'end_level,application_category,ratio,school_fee_1,'
399            'school_fee_2,school_fee_3,school_fee_4,'
400            'custom_textline_1,custom_textline_2,'
401            'custom_float_1,custom_float_2,'
402            'users_with_local_roles\r\n'
403            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic,,,,,,,,,,'
404            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
405            'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest,,,,,,,,,,[]\r\n'
406            )
407        return
408
409    def test_export_all(self):
410        # we can export all certificates in a site
411        exporter = CertificateExporter()
412        exporter.export_all(self.site, self.outfile)
413        result = open(self.outfile, 'rb').read()
414        self.assertEqual(
415            result,
416            'code,faculty_code,department_code,title,study_mode,start_level,'
417            'end_level,application_category,ratio,'
418            'school_fee_1,school_fee_2,school_fee_3,school_fee_4,'
419            'custom_textline_1,custom_textline_2,'
420            'custom_float_1,custom_float_2,'
421            'users_with_local_roles\r\n'
422            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic,,,,,,,,,,'
423            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
424            'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest,,,,,,,,,,[]\r\n'
425            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,100,200,no,,,,,,,,,,[]\r\n'
426            )
427        return
428
429    def test_export_all_to_string(self):
430        # we can export all certificates in a site to a string
431        exporter = CertificateExporter()
432        result = exporter.export_all(self.site, filepath=None)
433        self.assertEqual(
434            result,
435            'code,faculty_code,department_code,title,study_mode,start_level,'
436            'end_level,application_category,ratio,'
437            'school_fee_1,school_fee_2,school_fee_3,school_fee_4,'
438            'custom_textline_1,custom_textline_2,'
439            'custom_float_1,custom_float_2,'
440            'users_with_local_roles\r\n'
441            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic,,,,,,,,,,'
442            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
443            'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest,,,,,,,,,,[]\r\n'
444            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,100,200,no,,,,,,,,,,[]\r\n'
445            )
446        return
447
448class CertificateCourseExporterTest(unittest.TestCase):
449    # Tests for CertificateCourseExporter
450
451    layer = KofaUnitTestLayer
452
453    def setUp(self):
454        self.workdir = tempfile.mkdtemp()
455        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
456        # create some departments and courses in a fake site
457        container = FacultiesContainer()
458        self.site = {'faculties':container}
459        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
460        container.addFaculty(self.fac)
461        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
462        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
463        self.fac.addDepartment(self.dept1)
464        self.fac.addDepartment(self.dept2)
465        self.course1 = Course('Cheese Basics', 'C1')
466        self.course2 = Course('Advanced Cheese Making', 'C2')
467        self.course3 = Course('Selling Cheese', 'C3')
468        self.dept1.courses.addCourse(self.course1)
469        self.dept1.courses.addCourse(self.course2)
470        self.dept2.courses.addCourse(self.course3)
471        self.cert1 = Certificate(
472            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
473            end_level=300, application_category='basic')
474        self.cert2 = Certificate(
475            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
476            end_level=700, application_category='cest')
477        self.cert3 = Certificate(
478            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
479            end_level=200, application_category='no')
480        self.dept1.certificates.addCertificate(self.cert1)
481        self.dept1.certificates.addCertificate(self.cert2)
482        self.dept2.certificates.addCertificate(self.cert3)
483        self.cert1.addCertCourse(self.course1, 100, True)
484        self.cert1.addCertCourse(self.course2, 400, False)
485        self.cert3.addCertCourse(self.course3, 100, False)
486        self.certcourse1 = self.cert1['C1_100']
487        self.certcourse2 = self.cert1['C2_400']
488        self.certcourse3 = self.cert3['C3_100']
489        return
490
491    def tearDown(self):
492        shutil.rmtree(self.workdir)
493        return
494
495    def test_ifaces(self):
496        # make sure we fullfill interface contracts
497        obj = CertificateCourseExporter()
498        verifyObject(ICSVExporter, obj)
499        verifyClass(ICSVExporter, CertificateCourseExporter)
500        return
501
502    def test_get_as_utility(self):
503        # we can get a certificate exporter as utility
504        result = queryUtility(ICSVExporter, name="certificate_courses")
505        self.assertTrue(result is not None)
506        return
507
508    def test_export(self):
509        # we can export an iterable of certificates
510        exporter = CertificateCourseExporter()
511        exporter.export([self.certcourse1], self.outfile)
512        result = open(self.outfile, 'rb').read()
513        self.assertEqual(
514            result,
515            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
516            'C1,F1,D1,CERT1,100,1\r\n'
517            )
518        return
519
520    def test_export_to_string(self):
521        # we can export an iterable of certificates to a string.
522        exporter = CertificateCourseExporter()
523        result = exporter.export(
524            [self.certcourse1, self.certcourse2], filepath=None)
525        self.assertEqual(
526            result,
527            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
528            'C1,F1,D1,CERT1,100,1\r\n'
529            'C2,F1,D1,CERT1,400,0\r\n'
530            )
531        return
532
533    def test_export_all(self):
534        # we can export all certificates in a site
535        exporter = CertificateCourseExporter()
536        exporter.export_all(self.site, self.outfile)
537        result = open(self.outfile, 'rb').read()
538        self.assertEqual(
539            result,
540            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
541            'C1,F1,D1,CERT1,100,1\r\n'
542            'C2,F1,D1,CERT1,400,0\r\n'
543            'C3,F1,D2,CERT3,100,0\r\n'
544            )
545        return
546
547    def test_export_all_to_string(self):
548        # we can export all certificates in a site to a string
549        exporter = CertificateCourseExporter()
550        result = exporter.export_all(self.site, filepath=None)
551        self.assertEqual(
552            result,
553            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
554            'C1,F1,D1,CERT1,100,1\r\n'
555            'C2,F1,D1,CERT1,400,0\r\n'
556            'C3,F1,D2,CERT3,100,0\r\n'
557            )
558        return
Note: See TracBrowser for help on using the repository browser.