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

Last change on this file since 14212 was 13654, checked in by Henrik Bettermann, 9 years ago

Export degree field.

  • Property svn:keywords set to Id
File size: 22.0 KB
Line 
1## $Id: test_export.py 13654 2016-02-07 07:57:36Z 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,'
381            'degree,start_level,'
382            'end_level,application_category,ratio,school_fee_1,'
383            'school_fee_2,school_fee_3,school_fee_4,'
384            'custom_textline_1,custom_textline_2,'
385            'custom_float_1,custom_float_2,'
386            'users_with_local_roles\r\n'
387            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
388            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
389            )
390        return
391
392    def test_export_to_string(self):
393        # we can export an iterable of certificates to a string.
394        exporter = CertificateExporter()
395        result = exporter.export([self.cert1, self.cert2], filepath=None)
396        self.assertEqual(
397            result,
398            'code,faculty_code,department_code,title,study_mode,'
399            'degree,start_level,'
400            'end_level,application_category,ratio,school_fee_1,'
401            'school_fee_2,school_fee_3,school_fee_4,'
402            'custom_textline_1,custom_textline_2,'
403            'custom_float_1,custom_float_2,'
404            'users_with_local_roles\r\n'
405            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
406            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
407            'CERT2,F1,D1,Master of Cheddar,ct_ft,,400,700,cest,,,,,,,,,,[]\r\n'
408            )
409        return
410
411    def test_export_all(self):
412        # we can export all certificates in a site
413        exporter = CertificateExporter()
414        exporter.export_all(self.site, self.outfile)
415        result = open(self.outfile, 'rb').read()
416        self.assertEqual(
417            result,
418            'code,faculty_code,department_code,title,study_mode,'
419            'degree,start_level,'
420            'end_level,application_category,ratio,'
421            'school_fee_1,school_fee_2,school_fee_3,school_fee_4,'
422            'custom_textline_1,custom_textline_2,'
423            'custom_float_1,custom_float_2,'
424            'users_with_local_roles\r\n'
425            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
426            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
427            'CERT2,F1,D1,Master of Cheddar,ct_ft,,400,700,cest,,,,,,,,,,[]\r\n'
428            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,,100,200,no,,,,,,,,,,[]\r\n'
429            )
430        return
431
432    def test_export_all_to_string(self):
433        # we can export all certificates in a site to a string
434        exporter = CertificateExporter()
435        result = exporter.export_all(self.site, filepath=None)
436        self.assertEqual(
437            result,
438            'code,faculty_code,department_code,title,study_mode,'
439            'degree,start_level,'
440            'end_level,application_category,ratio,'
441            'school_fee_1,school_fee_2,school_fee_3,school_fee_4,'
442            'custom_textline_1,custom_textline_2,'
443            'custom_float_1,custom_float_2,'
444            'users_with_local_roles\r\n'
445            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
446            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
447            'CERT2,F1,D1,Master of Cheddar,ct_ft,,400,700,cest,,,,,,,,,,[]\r\n'
448            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,,100,200,no,,,,,,,,,,[]\r\n'
449            )
450        return
451
452class CertificateCourseExporterTest(unittest.TestCase):
453    # Tests for CertificateCourseExporter
454
455    layer = KofaUnitTestLayer
456
457    def setUp(self):
458        self.workdir = tempfile.mkdtemp()
459        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
460        # create some departments and courses in a fake site
461        container = FacultiesContainer()
462        self.site = {'faculties':container}
463        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
464        container.addFaculty(self.fac)
465        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
466        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
467        self.fac.addDepartment(self.dept1)
468        self.fac.addDepartment(self.dept2)
469        self.course1 = Course('Cheese Basics', 'C1')
470        self.course2 = Course('Advanced Cheese Making', 'C2')
471        self.course3 = Course('Selling Cheese', 'C3')
472        self.dept1.courses.addCourse(self.course1)
473        self.dept1.courses.addCourse(self.course2)
474        self.dept2.courses.addCourse(self.course3)
475        self.cert1 = Certificate(
476            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
477            end_level=300, application_category='basic')
478        self.cert2 = Certificate(
479            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
480            end_level=700, application_category='cest')
481        self.cert3 = Certificate(
482            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
483            end_level=200, application_category='no')
484        self.dept1.certificates.addCertificate(self.cert1)
485        self.dept1.certificates.addCertificate(self.cert2)
486        self.dept2.certificates.addCertificate(self.cert3)
487        self.cert1.addCertCourse(self.course1, 100, True)
488        self.cert1.addCertCourse(self.course2, 400, False)
489        self.cert3.addCertCourse(self.course3, 100, False)
490        self.certcourse1 = self.cert1['C1_100']
491        self.certcourse2 = self.cert1['C2_400']
492        self.certcourse3 = self.cert3['C3_100']
493        return
494
495    def tearDown(self):
496        shutil.rmtree(self.workdir)
497        return
498
499    def test_ifaces(self):
500        # make sure we fullfill interface contracts
501        obj = CertificateCourseExporter()
502        verifyObject(ICSVExporter, obj)
503        verifyClass(ICSVExporter, CertificateCourseExporter)
504        return
505
506    def test_get_as_utility(self):
507        # we can get a certificate exporter as utility
508        result = queryUtility(ICSVExporter, name="certificate_courses")
509        self.assertTrue(result is not None)
510        return
511
512    def test_export(self):
513        # we can export an iterable of certificates
514        exporter = CertificateCourseExporter()
515        exporter.export([self.certcourse1], self.outfile)
516        result = open(self.outfile, 'rb').read()
517        self.assertEqual(
518            result,
519            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
520            'C1,F1,D1,CERT1,100,1\r\n'
521            )
522        return
523
524    def test_export_to_string(self):
525        # we can export an iterable of certificates to a string.
526        exporter = CertificateCourseExporter()
527        result = exporter.export(
528            [self.certcourse1, self.certcourse2], filepath=None)
529        self.assertEqual(
530            result,
531            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
532            'C1,F1,D1,CERT1,100,1\r\n'
533            'C2,F1,D1,CERT1,400,0\r\n'
534            )
535        return
536
537    def test_export_all(self):
538        # we can export all certificates in a site
539        exporter = CertificateCourseExporter()
540        exporter.export_all(self.site, self.outfile)
541        result = open(self.outfile, 'rb').read()
542        self.assertEqual(
543            result,
544            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
545            'C1,F1,D1,CERT1,100,1\r\n'
546            'C2,F1,D1,CERT1,400,0\r\n'
547            'C3,F1,D2,CERT3,100,0\r\n'
548            )
549        return
550
551    def test_export_all_to_string(self):
552        # we can export all certificates in a site to a string
553        exporter = CertificateCourseExporter()
554        result = exporter.export_all(self.site, filepath=None)
555        self.assertEqual(
556            result,
557            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
558            'C1,F1,D1,CERT1,100,1\r\n'
559            'C2,F1,D1,CERT1,400,0\r\n'
560            'C3,F1,D2,CERT3,100,0\r\n'
561            )
562        return
Note: See TracBrowser for help on using the repository browser.