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

Last change on this file since 15278 was 14511, checked in by Henrik Bettermann, 8 years ago

Add officer name fields do IDepartment and IFaculty. Plugins must be updated!

  • Property svn:keywords set to Id
File size: 22.6 KB
Line 
1## $Id: test_export.py 14511 2017-02-07 08:33:05Z 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,'
70            'officer_1,officer_2\r\n'
71            'F1,Faculty of Cheese,faculty,[],,\r\n'
72            )
73        return
74
75    def test_export_to_string(self):
76        # we can export a set of faculties to a string.
77        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
78        exporter = FacultyExporter()
79        result = exporter.export([fac], filepath=None)
80        self.assertEqual(
81            result,
82            'code,title,title_prefix,users_with_local_roles,'
83            'officer_1,officer_2\r\n'
84            'F1,Faculty of Cheese,faculty,[],,\r\n'
85            )
86        return
87
88    def test_export_all(self):
89        # we can export all faculties in a site
90        container = FacultiesContainer()
91        site = {'faculties':container}
92        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
93        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
94        container.addFaculty(fac1)
95        container.addFaculty(fac2)
96        exporter = FacultyExporter()
97        exporter.export_all(site, self.outfile)
98        result = open(self.outfile, 'rb').read()
99        self.assertEqual(
100            result,
101            'code,title,title_prefix,users_with_local_roles,'
102            'officer_1,officer_2\r\n'
103            'F1,Faculty of Cheese,faculty,[],,\r\n'
104            'F2,Centre of Onion,centre,[],,\r\n'
105            )
106        return
107
108    def test_export_all_to_string(self):
109        # we can export all faculties in a site to a string
110        container = FacultiesContainer()
111        site = {'faculties':container}
112        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
113        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
114        container.addFaculty(fac1)
115        container.addFaculty(fac2)
116        exporter = FacultyExporter()
117        result = exporter.export_all(site, filepath=None)
118        self.assertEqual(
119            result,
120            'code,title,title_prefix,users_with_local_roles,'
121            'officer_1,officer_2\r\n'
122            'F1,Faculty of Cheese,faculty,[],,\r\n'
123            'F2,Centre of Onion,centre,[],,\r\n'
124            )
125        return
126
127class DepartmentExporterTest(unittest.TestCase):
128    # Tests for DepartmentExporter
129
130    layer = FunctionalLayer
131
132    def setUp(self):
133        self.workdir = tempfile.mkdtemp()
134        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
135        # create some departments in a fake site
136        container = FacultiesContainer()
137        self.site = {'faculties':container}
138        self.fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
139        self.fac2 = Faculty('Centre of Onion', 'centre', 'F2')
140        container.addFaculty(self.fac1)
141        container.addFaculty(self.fac2)
142        self.dept1 = Department(
143            'Department of Cheddar', 'department', 'D1', 'HoCheese')
144        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
145        self.dept3 = Department('Department of Rings', 'department', 'D3')
146        self.fac1.addDepartment(self.dept1)
147        self.fac1.addDepartment(self.dept2)
148        self.fac2.addDepartment(self.dept3)
149        role_manager = IPrincipalRoleManager(self.dept1)
150        role_manager.assignRoleToPrincipal(u'bobsrole', u'bob')
151        return
152
153    def tearDown(self):
154        shutil.rmtree(self.workdir)
155        return
156
157    def test_ifaces(self):
158        # make sure we fullfill interface contracts
159        obj = DepartmentExporter()
160        verifyObject(ICSVExporter, obj)
161        verifyClass(ICSVExporter, DepartmentExporter)
162        return
163
164    def test_get_as_utility(self):
165        # we can get a department exporter as utility
166        result = queryUtility(ICSVExporter, name="departments")
167        self.assertTrue(result is not None)
168        return
169
170    def test_export(self):
171        # we can export an iterable of departments
172        exporter = DepartmentExporter()
173        exporter.export([self.dept1], self.outfile)
174        result = open(self.outfile, 'rb').read()
175        self.assertEqual(
176            result,
177            'code,faculty_code,title,title_prefix,users_with_local_roles,'
178            'officer_1,officer_2,officer_3,officer_4\r\n'
179            'D1,F1,Department of Cheddar,department,'
180            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]",'
181            'HoCheese,,,\r\n'
182            )
183        return
184
185    def test_export_to_string(self):
186        # we can export an iterable of departments to a string.
187        exporter = DepartmentExporter()
188        result = exporter.export([self.dept1, self.dept2], filepath=None)
189        self.assertEqual(
190            result,
191            'code,faculty_code,title,title_prefix,users_with_local_roles,'
192            'officer_1,officer_2,officer_3,officer_4\r\n'
193            'D1,F1,Department of Cheddar,department,'
194            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]",'
195            'HoCheese,,,\r\n'
196            'D2,F1,Institue of Gouda,institute,[],,,,\r\n'
197            )
198        return
199
200    def test_export_all(self):
201        # we can export all depts in a site
202        exporter = DepartmentExporter()
203        exporter.export_all(self.site, self.outfile)
204        result = open(self.outfile, 'rb').read()
205        self.assertEqual(
206            result,
207            'code,faculty_code,title,title_prefix,users_with_local_roles,'
208            'officer_1,officer_2,officer_3,officer_4\r\n'
209            'D1,F1,Department of Cheddar,department,'
210            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]",'
211            'HoCheese,,,\r\n'
212            'D2,F1,Institue of Gouda,institute,[],,,,\r\n'
213            'D3,F2,Department of Rings,department,[],,,,\r\n'
214            )
215        return
216
217    def test_export_all_to_string(self):
218        # we can export all depts in a site to a string
219        exporter = DepartmentExporter()
220        result = exporter.export_all(self.site, filepath=None)
221        self.assertEqual(
222            result,
223            'code,faculty_code,title,title_prefix,users_with_local_roles,'
224            'officer_1,officer_2,officer_3,officer_4\r\n'
225            'D1,F1,Department of Cheddar,department,'
226            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]",'
227            'HoCheese,,,\r\n'
228            'D2,F1,Institue of Gouda,institute,[],,,,\r\n'
229            'D3,F2,Department of Rings,department,[],,,,\r\n'
230            )
231        return
232
233class CourseExporterTest(unittest.TestCase):
234    # Tests for CourseExporter
235
236    layer = FunctionalLayer
237
238    def setUp(self):
239        self.workdir = tempfile.mkdtemp()
240        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
241        # create some departments and courses in a fake site
242        container = FacultiesContainer()
243        self.site = {'faculties':container}
244        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
245        container.addFaculty(self.fac)
246        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
247        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
248        self.fac.addDepartment(self.dept1)
249        self.fac.addDepartment(self.dept2)
250        self.course1 = Course('Cheese Basics', 'C1')
251        self.course2 = Course('Advanced Cheese Making', 'C2')
252        self.course3 = Course('Selling Cheese', 'C3')
253        self.dept1.courses.addCourse(self.course1)
254        self.dept1.courses.addCourse(self.course2)
255        self.dept2.courses.addCourse(self.course3)
256        return
257
258    def tearDown(self):
259        shutil.rmtree(self.workdir)
260        return
261
262    def test_ifaces(self):
263        # make sure we fullfill interface contracts
264        obj = CourseExporter()
265        verifyObject(ICSVExporter, obj)
266        verifyClass(ICSVExporter, CourseExporter)
267        return
268
269    def test_get_as_utility(self):
270        # we can get a course exporter as utility
271        result = queryUtility(ICSVExporter, name="courses")
272        self.assertTrue(result is not None)
273        return
274
275    def test_export(self):
276        # we can export an iterable of courses
277        exporter = CourseExporter()
278        exporter.export([self.course1], self.outfile)
279        result = open(self.outfile, 'rb').read()
280        self.assertEqual(
281            result,
282            'code,faculty_code,department_code,title,credits,'
283            'passmark,semester,users_with_local_roles,former_course\r\n'
284            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
285            )
286        return
287
288    def test_export_to_string(self):
289        # we can export an iterable of courses to a string.
290        exporter = CourseExporter()
291        result = exporter.export([self.course1, self.course2], filepath=None)
292        self.assertEqual(
293            result,
294            'code,faculty_code,department_code,title,credits,passmark,'
295            'semester,users_with_local_roles,former_course\r\n'
296            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
297            'C2,F1,D1,Advanced Cheese Making,0,40,1,[],0\r\n'
298            )
299        return
300
301    def test_export_all(self):
302        # we can export all courses in a site
303        exporter = CourseExporter()
304        exporter.export_all(self.site, self.outfile)
305        result = open(self.outfile, 'rb').read()
306        self.assertEqual(
307            result,
308            'code,faculty_code,department_code,title,credits,passmark,'
309            'semester,users_with_local_roles,former_course\r\n'
310            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
311            'C2,F1,D1,Advanced Cheese Making,0,40,1,[],0\r\n'
312            'C3,F1,D2,Selling Cheese,0,40,1,[],0\r\n'
313            )
314        return
315
316    def test_export_all_to_string(self):
317        # we can export all courses in a site to a string
318        exporter = CourseExporter()
319        result = exporter.export_all(self.site, filepath=None)
320        self.assertEqual(
321            result,
322            'code,faculty_code,department_code,title,credits,passmark,'
323            'semester,users_with_local_roles,former_course\r\n'
324            'C1,F1,D1,Cheese Basics,0,40,1,[],0\r\n'
325            'C2,F1,D1,Advanced Cheese Making,0,40,1,[],0\r\n'
326            'C3,F1,D2,Selling Cheese,0,40,1,[],0\r\n'
327            )
328        return
329
330class CertificateExporterTest(unittest.TestCase):
331    # Tests for CertificateExporter
332
333    layer = FunctionalLayer
334
335    def setUp(self):
336        self.workdir = tempfile.mkdtemp()
337        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
338        # create some departments and courses in a fake site
339        container = FacultiesContainer()
340        self.site = {'faculties':container}
341        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
342        container.addFaculty(self.fac)
343        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
344        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
345        self.fac.addDepartment(self.dept1)
346        self.fac.addDepartment(self.dept2)
347        self.course1 = Course('Cheese Basics', 'C1')
348        self.course2 = Course('Advanced Cheese Making', 'C2')
349        self.course3 = Course('Selling Cheese', 'C3')
350        self.dept1.courses.addCourse(self.course1)
351        self.dept1.courses.addCourse(self.course2)
352        self.dept2.courses.addCourse(self.course3)
353        self.cert1 = Certificate(
354            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
355            end_level=300, application_category='basic')
356        self.cert2 = Certificate(
357            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
358            end_level=700, application_category='cest')
359        self.cert3 = Certificate(
360            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
361            end_level=200, application_category='no')
362        self.dept1.certificates.addCertificate(self.cert1)
363        self.dept1.certificates.addCertificate(self.cert2)
364        self.dept2.certificates.addCertificate(self.cert3)
365        role_manager = IPrincipalRoleManager(self.cert1)
366        role_manager.assignRoleToPrincipal(u'bobsrole', u'bob')
367        return
368
369    def tearDown(self):
370        shutil.rmtree(self.workdir)
371        return
372
373    def test_ifaces(self):
374        # make sure we fullfill interface contracts
375        obj = CertificateExporter()
376        verifyObject(ICSVExporter, obj)
377        verifyClass(ICSVExporter, CertificateExporter)
378        return
379
380    def test_get_as_utility(self):
381        # we can get a certificate exporter as utility
382        result = queryUtility(ICSVExporter, name="certificates")
383        self.assertTrue(result is not None)
384        return
385
386    def test_export(self):
387        # we can export an iterable of certificates
388        exporter = CertificateExporter()
389        exporter.export([self.cert1], self.outfile)
390        result = open(self.outfile, 'rb').read()
391        self.assertEqual(
392            result,
393            'code,faculty_code,department_code,title,study_mode,'
394            'degree,start_level,'
395            'end_level,application_category,ratio,school_fee_1,'
396            'school_fee_2,school_fee_3,school_fee_4,'
397            'custom_textline_1,custom_textline_2,'
398            'custom_float_1,custom_float_2,'
399            'users_with_local_roles\r\n'
400            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
401            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
402            )
403        return
404
405    def test_export_to_string(self):
406        # we can export an iterable of certificates to a string.
407        exporter = CertificateExporter()
408        result = exporter.export([self.cert1, self.cert2], filepath=None)
409        self.assertEqual(
410            result,
411            'code,faculty_code,department_code,title,study_mode,'
412            'degree,start_level,'
413            'end_level,application_category,ratio,school_fee_1,'
414            'school_fee_2,school_fee_3,school_fee_4,'
415            'custom_textline_1,custom_textline_2,'
416            'custom_float_1,custom_float_2,'
417            'users_with_local_roles\r\n'
418            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
419            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
420            'CERT2,F1,D1,Master of Cheddar,ct_ft,,400,700,cest,,,,,,,,,,[]\r\n'
421            )
422        return
423
424    def test_export_all(self):
425        # we can export all certificates in a site
426        exporter = CertificateExporter()
427        exporter.export_all(self.site, self.outfile)
428        result = open(self.outfile, 'rb').read()
429        self.assertEqual(
430            result,
431            'code,faculty_code,department_code,title,study_mode,'
432            'degree,start_level,'
433            'end_level,application_category,ratio,'
434            'school_fee_1,school_fee_2,school_fee_3,school_fee_4,'
435            'custom_textline_1,custom_textline_2,'
436            'custom_float_1,custom_float_2,'
437            'users_with_local_roles\r\n'
438            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
439            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
440            'CERT2,F1,D1,Master of Cheddar,ct_ft,,400,700,cest,,,,,,,,,,[]\r\n'
441            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,,100,200,no,,,,,,,,,,[]\r\n'
442            )
443        return
444
445    def test_export_all_to_string(self):
446        # we can export all certificates in a site to a string
447        exporter = CertificateExporter()
448        result = exporter.export_all(self.site, filepath=None)
449        self.assertEqual(
450            result,
451            'code,faculty_code,department_code,title,study_mode,'
452            'degree,start_level,'
453            'end_level,application_category,ratio,'
454            'school_fee_1,school_fee_2,school_fee_3,school_fee_4,'
455            'custom_textline_1,custom_textline_2,'
456            'custom_float_1,custom_float_2,'
457            'users_with_local_roles\r\n'
458            'CERT1,F1,D1,Master of Cheese,ct_ft,,100,300,basic,,,,,,,,,,'
459            '"[{\'user_name\': u\'bob\', \'local_role\': u\'bobsrole\'}]"\r\n'
460            'CERT2,F1,D1,Master of Cheddar,ct_ft,,400,700,cest,,,,,,,,,,[]\r\n'
461            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,,100,200,no,,,,,,,,,,[]\r\n'
462            )
463        return
464
465class CertificateCourseExporterTest(unittest.TestCase):
466    # Tests for CertificateCourseExporter
467
468    layer = KofaUnitTestLayer
469
470    def setUp(self):
471        self.workdir = tempfile.mkdtemp()
472        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
473        # create some departments and courses in a fake site
474        container = FacultiesContainer()
475        self.site = {'faculties':container}
476        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
477        container.addFaculty(self.fac)
478        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
479        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
480        self.fac.addDepartment(self.dept1)
481        self.fac.addDepartment(self.dept2)
482        self.course1 = Course('Cheese Basics', 'C1')
483        self.course2 = Course('Advanced Cheese Making', 'C2')
484        self.course3 = Course('Selling Cheese', 'C3')
485        self.dept1.courses.addCourse(self.course1)
486        self.dept1.courses.addCourse(self.course2)
487        self.dept2.courses.addCourse(self.course3)
488        self.cert1 = Certificate(
489            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
490            end_level=300, application_category='basic')
491        self.cert2 = Certificate(
492            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
493            end_level=700, application_category='cest')
494        self.cert3 = Certificate(
495            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
496            end_level=200, application_category='no')
497        self.dept1.certificates.addCertificate(self.cert1)
498        self.dept1.certificates.addCertificate(self.cert2)
499        self.dept2.certificates.addCertificate(self.cert3)
500        self.cert1.addCertCourse(self.course1, 100, True)
501        self.cert1.addCertCourse(self.course2, 400, False)
502        self.cert3.addCertCourse(self.course3, 100, False)
503        self.certcourse1 = self.cert1['C1_100']
504        self.certcourse2 = self.cert1['C2_400']
505        self.certcourse3 = self.cert3['C3_100']
506        return
507
508    def tearDown(self):
509        shutil.rmtree(self.workdir)
510        return
511
512    def test_ifaces(self):
513        # make sure we fullfill interface contracts
514        obj = CertificateCourseExporter()
515        verifyObject(ICSVExporter, obj)
516        verifyClass(ICSVExporter, CertificateCourseExporter)
517        return
518
519    def test_get_as_utility(self):
520        # we can get a certificate exporter as utility
521        result = queryUtility(ICSVExporter, name="certificate_courses")
522        self.assertTrue(result is not None)
523        return
524
525    def test_export(self):
526        # we can export an iterable of certificates
527        exporter = CertificateCourseExporter()
528        exporter.export([self.certcourse1], self.outfile)
529        result = open(self.outfile, 'rb').read()
530        self.assertEqual(
531            result,
532            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
533            'C1,F1,D1,CERT1,100,1\r\n'
534            )
535        return
536
537    def test_export_to_string(self):
538        # we can export an iterable of certificates to a string.
539        exporter = CertificateCourseExporter()
540        result = exporter.export(
541            [self.certcourse1, self.certcourse2], filepath=None)
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            )
548        return
549
550    def test_export_all(self):
551        # we can export all certificates in a site
552        exporter = CertificateCourseExporter()
553        exporter.export_all(self.site, self.outfile)
554        result = open(self.outfile, 'rb').read()
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
563
564    def test_export_all_to_string(self):
565        # we can export all certificates in a site to a string
566        exporter = CertificateCourseExporter()
567        result = exporter.export_all(self.site, filepath=None)
568        self.assertEqual(
569            result,
570            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
571            'C1,F1,D1,CERT1,100,1\r\n'
572            'C2,F1,D1,CERT1,400,0\r\n'
573            'C3,F1,D2,CERT3,100,0\r\n'
574            )
575        return
Note: See TracBrowser for help on using the repository browser.