1 | import os |
---|
2 | import shutil |
---|
3 | import tempfile |
---|
4 | import unittest |
---|
5 | from zope.component import queryUtility |
---|
6 | from zope.interface.verify import verifyObject, verifyClass |
---|
7 | from waeup.sirp.interfaces import ICSVExporter |
---|
8 | from waeup.sirp.testing import SIRPUnitTestLayer |
---|
9 | from waeup.sirp.university import FacultiesContainer, Faculty |
---|
10 | from waeup.sirp.university.export import FacultyExporter |
---|
11 | |
---|
12 | class 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="Faculty Exporter") |
---|
35 | self.assertTrue(result is not None) |
---|
36 | return |
---|
37 | |
---|
38 | def test_export(self): |
---|
39 | # we can export single faculties (for what it's worth) |
---|
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 single 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 |
---|