source: main/waeup.sirp/trunk/src/waeup/sirp/university/tests/test_export.py @ 7741

Last change on this file since 7741 was 7739, checked in by uli, 13 years ago

Add course exporter tests.

File size: 9.9 KB
Line 
1import os
2import shutil
3import tempfile
4import unittest
5from zope.component import queryUtility
6from zope.interface.verify import verifyObject, verifyClass
7from waeup.sirp.interfaces import ICSVExporter
8from waeup.sirp.testing import SIRPUnitTestLayer
9from waeup.sirp.university import (
10    FacultiesContainer, Faculty, Department, Course,
11    )
12from waeup.sirp.university.export import (
13    FacultyExporter, DepartmentExporter, CourseExporter,
14    )
15
16class FacultyExporterTest(unittest.TestCase):
17
18    layer = SIRPUnitTestLayer
19
20    def setUp(self):
21        self.workdir = tempfile.mkdtemp()
22        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
23        return
24
25    def tearDown(self):
26        shutil.rmtree(self.workdir)
27        return
28
29    def test_ifaces(self):
30        # make sure we fullfill interface contracts
31        obj = FacultyExporter()
32        verifyObject(ICSVExporter, obj)
33        verifyClass(ICSVExporter, FacultyExporter)
34        return
35
36    def test_get_as_utility(self):
37        # we can get a faculty exporter as utility
38        result = queryUtility(ICSVExporter, name="faculties")
39        self.assertTrue(result is not None)
40        return
41
42    def test_export(self):
43        # we can export a set of faculties
44        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
45        exporter = FacultyExporter()
46        exporter.export([fac], self.outfile)
47        result = open(self.outfile, 'rb').read()
48        self.assertEqual(
49            result,
50            'code,title,title_prefix\r\n'
51            'F1,Faculty of Cheese,faculty\r\n'
52            )
53        return
54
55    def test_export_to_string(self):
56        # we can export a set of faculties to a string.
57        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
58        exporter = FacultyExporter()
59        result = exporter.export([fac], filepath=None)
60        self.assertEqual(
61            result,
62            'code,title,title_prefix\r\n'
63            'F1,Faculty of Cheese,faculty\r\n'
64            )
65        return
66
67    def test_export_all(self):
68        # we can export all faculties in a site
69        container = FacultiesContainer()
70        site = {'faculties':container}
71        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
72        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
73        container.addFaculty(fac1)
74        container.addFaculty(fac2)
75        exporter = FacultyExporter()
76        exporter.export_all(site, self.outfile)
77        result = open(self.outfile, 'rb').read()
78        self.assertEqual(
79            result,
80            'code,title,title_prefix\r\n'
81            'F1,Faculty of Cheese,faculty\r\n'
82            'F2,Centre of Onion,centre\r\n'
83            )
84        return
85
86    def test_export_all_to_string(self):
87        # we can export all faculties in a site to a string
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        result = exporter.export_all(site, filepath=None)
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
104class DepartmentExporterTest(unittest.TestCase):
105    # Tests for DepartmentExporter
106
107    layer = SIRPUnitTestLayer
108
109    def setUp(self):
110        self.workdir = tempfile.mkdtemp()
111        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
112        # create some departments in a fake site
113        container = FacultiesContainer()
114        self.site = {'faculties':container}
115        self.fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
116        self.fac2 = Faculty('Centre of Onion', 'centre', 'F2')
117        container.addFaculty(self.fac1)
118        container.addFaculty(self.fac2)
119        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
120        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
121        self.dept3 = Department('Department of Rings', 'department', 'D3')
122        self.fac1.addDepartment(self.dept1)
123        self.fac1.addDepartment(self.dept2)
124        self.fac2.addDepartment(self.dept3)
125        return
126
127    def tearDown(self):
128        shutil.rmtree(self.workdir)
129        return
130
131    def test_ifaces(self):
132        # make sure we fullfill interface contracts
133        obj = DepartmentExporter()
134        verifyObject(ICSVExporter, obj)
135        verifyClass(ICSVExporter, DepartmentExporter)
136        return
137
138    def test_get_as_utility(self):
139        # we can get a department exporter as utility
140        result = queryUtility(ICSVExporter, name="departments")
141        self.assertTrue(result is not None)
142        return
143
144    def test_export(self):
145        # we can export an iterable of departments
146        exporter = DepartmentExporter()
147        exporter.export([self.dept1], self.outfile)
148        result = open(self.outfile, 'rb').read()
149        self.assertEqual(
150            result,
151            'code,faculty,title,title_prefix\r\n'
152            'D1,F1,Department of Cheddar,department\r\n'
153            )
154        return
155
156    def test_export_to_string(self):
157        # we can export an iterable of departments to a string.
158        exporter = DepartmentExporter()
159        result = exporter.export([self.dept1, self.dept2], filepath=None)
160        self.assertEqual(
161            result,
162            'code,faculty,title,title_prefix\r\n'
163            'D1,F1,Department of Cheddar,department\r\n'
164            'D2,F1,Institue of Gouda,institute\r\n'
165            )
166        return
167
168    def test_export_all(self):
169        # we can export all depts in a site
170        exporter = DepartmentExporter()
171        exporter.export_all(self.site, self.outfile)
172        result = open(self.outfile, 'rb').read()
173        self.assertEqual(
174            result,
175            'code,faculty,title,title_prefix\r\n'
176            'D1,F1,Department of Cheddar,department\r\n'
177            'D2,F1,Institue of Gouda,institute\r\n'
178            'D3,F2,Department of Rings,department\r\n'
179            )
180        return
181
182    def test_export_all_to_string(self):
183        # we can export all depts in a site to a string
184        exporter = DepartmentExporter()
185        result = exporter.export_all(self.site, filepath=None)
186        self.assertEqual(
187            result,
188            'code,faculty,title,title_prefix\r\n'
189            'D1,F1,Department of Cheddar,department\r\n'
190            'D2,F1,Institue of Gouda,institute\r\n'
191            'D3,F2,Department of Rings,department\r\n'
192            )
193        return
194
195class CourseExporterTest(unittest.TestCase):
196    # Tests for CourseExporter
197
198    layer = SIRPUnitTestLayer
199
200    def setUp(self):
201        self.workdir = tempfile.mkdtemp()
202        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
203        # create some departments and courses in a fake site
204        container = FacultiesContainer()
205        self.site = {'faculties':container}
206        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
207        container.addFaculty(self.fac)
208        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
209        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
210        self.fac.addDepartment(self.dept1)
211        self.fac.addDepartment(self.dept2)
212        self.course1 = Course('Cheese Basics', 'C1')
213        self.course2 = Course('Advanced Cheese Making', 'C2')
214        self.course3 = Course('Selling Cheese', 'C3')
215        self.dept1.courses[u'C1'] = self.course1
216        self.dept1.courses['C2'] = self.course2
217        self.dept2.courses[u'C3'] = self.course3
218        return
219
220    def tearDown(self):
221        shutil.rmtree(self.workdir)
222        return
223
224    def test_ifaces(self):
225        # make sure we fullfill interface contracts
226        obj = CourseExporter()
227        verifyObject(ICSVExporter, obj)
228        verifyClass(ICSVExporter, CourseExporter)
229        return
230
231    def test_get_as_utility(self):
232        # we can get a course exporter as utility
233        result = queryUtility(ICSVExporter, name="courses")
234        self.assertTrue(result is not None)
235        return
236
237    def test_export(self):
238        # we can export an iterable of courses
239        exporter = CourseExporter()
240        exporter.export([self.course1], self.outfile)
241        result = open(self.outfile, 'rb').read()
242        self.assertEqual(
243            result,
244            'code,faculty,department,title,credits,passmark,semester\r\n'
245            'C1,F1,,Cheese Basics,0,40,1\r\n'
246            )
247        return
248
249    def test_export_to_string(self):
250        # we can export an iterable of courses to a string.
251        exporter = CourseExporter()
252        result = exporter.export([self.course1, self.course2], filepath=None)
253        self.assertEqual(
254            result,
255            'code,faculty,department,title,credits,passmark,semester\r\n'
256            'C1,F1,,Cheese Basics,0,40,1\r\n'
257            'C2,F1,,Advanced Cheese Making,0,40,1\r\n'
258            )
259        return
260
261    def test_export_all(self):
262        # we can export all courses in a site
263        exporter = CourseExporter()
264        exporter.export_all(self.site, self.outfile)
265        result = open(self.outfile, 'rb').read()
266        self.assertEqual(
267            result,
268            'code,faculty,department,title,credits,passmark,semester\r\n'
269            'C1,F1,,Cheese Basics,0,40,1\r\n'
270            'C2,F1,,Advanced Cheese Making,0,40,1\r\n'
271            'C3,F1,,Selling Cheese,0,40,1\r\n'
272            )
273        return
274
275    def test_export_all_to_string(self):
276        # we can export all courses in a site to a string
277        exporter = CourseExporter()
278        result = exporter.export_all(self.site, filepath=None)
279        self.assertEqual(
280            result,
281            'code,faculty,department,title,credits,passmark,semester\r\n'
282            'C1,F1,,Cheese Basics,0,40,1\r\n'
283            'C2,F1,,Advanced Cheese Making,0,40,1\r\n'
284            'C3,F1,,Selling Cheese,0,40,1\r\n'
285            )
286        return
Note: See TracBrowser for help on using the repository browser.