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

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

Export users with locale roles in faculties, departments, courses and certificates.

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