source: main/waeup.kofa/backup-trunk/src/waeup/kofa/university/tests/test_export.py @ 10009

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

Fix test.

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