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

Last change on this file since 8367 was 8303, checked in by Henrik Bettermann, 13 years ago

Fix test.

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