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

Last change on this file since 7736 was 7731, checked in by uli, 13 years ago

Add tests for department exporter and reflect changes in ICSVExporter
interface.

File size: 6.5 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 FacultiesContainer, Faculty, Department
10from waeup.sirp.university.export import FacultyExporter, DepartmentExporter
11
12class FacultyExporterTest(unittest.TestCase):
13
14    layer = SIRPUnitTestLayer
15
16    def setUp(self):
17        self.workdir = tempfile.mkdtemp()
18        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
19        return
20
21    def tearDown(self):
22        shutil.rmtree(self.workdir)
23        return
24
25    def test_ifaces(self):
26        # make sure we fullfill interface contracts
27        obj = FacultyExporter()
28        verifyObject(ICSVExporter, obj)
29        verifyClass(ICSVExporter, FacultyExporter)
30        return
31
32    def test_get_as_utility(self):
33        # we can get a faculty exporter as utility
34        result = queryUtility(ICSVExporter, name="faculties")
35        self.assertTrue(result is not None)
36        return
37
38    def test_export(self):
39        # we can export a set of faculties
40        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
41        exporter = FacultyExporter()
42        exporter.export([fac], self.outfile)
43        result = open(self.outfile, 'rb').read()
44        self.assertEqual(
45            result,
46            'code,title,title_prefix\r\n'
47            'F1,Faculty of Cheese,faculty\r\n'
48            )
49        return
50
51    def test_export_to_string(self):
52        # we can export a set of faculties to a string.
53        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
54        exporter = FacultyExporter()
55        result = exporter.export([fac], filepath=None)
56        self.assertEqual(
57            result,
58            'code,title,title_prefix\r\n'
59            'F1,Faculty of Cheese,faculty\r\n'
60            )
61        return
62
63    def test_export_all(self):
64        # we can export all faculties in a site
65        container = FacultiesContainer()
66        site = {'faculties':container}
67        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
68        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
69        container.addFaculty(fac1)
70        container.addFaculty(fac2)
71        exporter = FacultyExporter()
72        exporter.export_all(site, self.outfile)
73        result = open(self.outfile, 'rb').read()
74        self.assertEqual(
75            result,
76            'code,title,title_prefix\r\n'
77            'F1,Faculty of Cheese,faculty\r\n'
78            'F2,Centre of Onion,centre\r\n'
79            )
80        return
81
82    def test_export_all_to_string(self):
83        # we can export all faculties in a site to a string
84        container = FacultiesContainer()
85        site = {'faculties':container}
86        fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
87        fac2 = Faculty('Centre of Onion', 'centre', 'F2')
88        container.addFaculty(fac1)
89        container.addFaculty(fac2)
90        exporter = FacultyExporter()
91        result = exporter.export_all(site, filepath=None)
92        self.assertEqual(
93            result,
94            'code,title,title_prefix\r\n'
95            'F1,Faculty of Cheese,faculty\r\n'
96            'F2,Centre of Onion,centre\r\n'
97            )
98        return
99
100class DepartmentExporterTest(unittest.TestCase):
101    # Tests for DepartmentExporter
102
103    layer = SIRPUnitTestLayer
104
105    def setUp(self):
106        self.workdir = tempfile.mkdtemp()
107        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
108        # create some departments in a fake site
109        container = FacultiesContainer()
110        self.site = {'faculties':container}
111        self.fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
112        self.fac2 = Faculty('Centre of Onion', 'centre', 'F2')
113        container.addFaculty(self.fac1)
114        container.addFaculty(self.fac2)
115        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
116        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
117        self.dept3 = Department('Department of Rings', 'department', 'D3')
118        self.fac1.addDepartment(self.dept1)
119        self.fac1.addDepartment(self.dept2)
120        self.fac2.addDepartment(self.dept3)
121        return
122
123    def tearDown(self):
124        shutil.rmtree(self.workdir)
125        return
126
127    def test_ifaces(self):
128        # make sure we fullfill interface contracts
129        obj = DepartmentExporter()
130        verifyObject(ICSVExporter, obj)
131        verifyClass(ICSVExporter, DepartmentExporter)
132        return
133
134    def test_get_as_utility(self):
135        # we can get a faculty exporter as utility
136        result = queryUtility(ICSVExporter, name="departments")
137        self.assertTrue(result is not None)
138        return
139
140    def test_export(self):
141        # we can export an iterable of departments
142        exporter = DepartmentExporter()
143        exporter.export([self.dept1], self.outfile)
144        result = open(self.outfile, 'rb').read()
145        self.assertEqual(
146            result,
147            'code,faculty,title,title_prefix\r\n'
148            'D1,F1,Department of Cheddar,department\r\n'
149            )
150        return
151
152    def test_export_to_string(self):
153        # we can export an iterable of departments to a string.
154        exporter = DepartmentExporter()
155        result = exporter.export([self.dept1, self.dept2], filepath=None)
156        self.assertEqual(
157            result,
158            'code,faculty,title,title_prefix\r\n'
159            'D1,F1,Department of Cheddar,department\r\n'
160            'D2,F1,Institue of Gouda,institute\r\n'
161            )
162        return
163
164    def test_export_all(self):
165        # we can export all depts in a site
166        exporter = DepartmentExporter()
167        exporter.export_all(self.site, self.outfile)
168        result = open(self.outfile, 'rb').read()
169        self.assertEqual(
170            result,
171            'code,faculty,title,title_prefix\r\n'
172            'D1,F1,Department of Cheddar,department\r\n'
173            'D2,F1,Institue of Gouda,institute\r\n'
174            'D3,F2,Department of Rings,department\r\n'
175            )
176        return
177
178    def test_export_all_to_string(self):
179        # we can export all depts in a site to a string
180        exporter = DepartmentExporter()
181        result = exporter.export_all(self.site, filepath=None)
182        self.assertEqual(
183            result,
184            'code,faculty,title,title_prefix\r\n'
185            'D1,F1,Department of Cheddar,department\r\n'
186            'D2,F1,Institue of Gouda,institute\r\n'
187            'D3,F2,Department of Rings,department\r\n'
188            )
189        return
Note: See TracBrowser for help on using the repository browser.