1 | import datetime |
---|
2 | import os |
---|
3 | from zope.catalog.interfaces import ICatalog |
---|
4 | from zope.component import queryUtility, getUtility |
---|
5 | from zope.interface.verify import verifyObject, verifyClass |
---|
6 | from zope.intid.interfaces import IIntIds |
---|
7 | from waeup.kofa.interfaces import ICSVExporter |
---|
8 | from waeup.kofa.students.export import ( |
---|
9 | StudentsExporter, StudentStudyCourseExporter) |
---|
10 | from waeup.kofa.students.student import Student, StudentStudyCourse |
---|
11 | from waeup.kofa.students.tests.test_batching import StudentImportExportSetup |
---|
12 | from waeup.kofa.testing import FunctionalLayer |
---|
13 | |
---|
14 | class StudentsExporterTest(StudentImportExportSetup): |
---|
15 | |
---|
16 | layer = FunctionalLayer |
---|
17 | |
---|
18 | def setUp(self): |
---|
19 | super(StudentsExporterTest, self).setUp() |
---|
20 | student = Student() |
---|
21 | student.student_id = u'A111111' |
---|
22 | self.app['students'][student.student_id] = self.student = student |
---|
23 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
24 | self.cat = getUtility(ICatalog, name='students_catalog') |
---|
25 | self.intids = getUtility(IIntIds) |
---|
26 | return |
---|
27 | |
---|
28 | def test_ifaces(self): |
---|
29 | # make sure we fullfill interface contracts |
---|
30 | obj = StudentsExporter() |
---|
31 | verifyObject(ICSVExporter, obj) |
---|
32 | verifyClass(ICSVExporter, StudentsExporter) |
---|
33 | return |
---|
34 | |
---|
35 | def test_get_as_utility(self): |
---|
36 | # we can get an student exporter as utility |
---|
37 | result = queryUtility(ICSVExporter, name="students") |
---|
38 | self.assertTrue(result is not None) |
---|
39 | return |
---|
40 | |
---|
41 | def setup_student(self, student): |
---|
42 | # set predictable values for `student` |
---|
43 | student.matric_number = u'M123456' |
---|
44 | student.adm_code = u'my adm code' |
---|
45 | student.clearance_locked = False |
---|
46 | student.clr_code = u'my clr code' |
---|
47 | student.perm_address = u'Studentroad 21\nLagos 123456\n' |
---|
48 | student.reg_number = u'123456' |
---|
49 | student.student_id = u'A111111' |
---|
50 | student.firstname = u'Anna' |
---|
51 | student.lastname = u'Tester' |
---|
52 | student.middlename = u'M.' |
---|
53 | student.date_of_birth = datetime.date(1981, 2, 4) |
---|
54 | student.sex = 'f' |
---|
55 | student.email = 'anna@sample.com' |
---|
56 | student.phone = u'+234-123-12345' |
---|
57 | student.notice = u'Some notice\nin lines.' |
---|
58 | return student |
---|
59 | |
---|
60 | def test_export(self): |
---|
61 | # we can really export students |
---|
62 | # set values we can expect in export file |
---|
63 | self.setup_student(self.student) |
---|
64 | exporter = StudentsExporter() |
---|
65 | exporter.export([self.student], self.outfile) |
---|
66 | result = open(self.outfile, 'rb').read() |
---|
67 | self.assertEqual( |
---|
68 | result, |
---|
69 | 'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,' |
---|
70 | 'firstname,lastname,matric_number,middlename,nationality,' |
---|
71 | 'perm_address,phone,reg_number,sex,student_id\r\n' |
---|
72 | |
---|
73 | 'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,Anna,' |
---|
74 | 'Tester,M123456,M.,nigeria,"Studentroad 21\nLagos 123456\n",' |
---|
75 | '+234-123-12345,123456,f,A111111\r\n' |
---|
76 | ) |
---|
77 | return |
---|
78 | |
---|
79 | def test_export_all(self): |
---|
80 | # we can really export students |
---|
81 | # set values we can expect in export file |
---|
82 | self.setup_student(self.student) |
---|
83 | exporter = StudentsExporter() |
---|
84 | exporter.export_all(self.app, self.outfile) |
---|
85 | result = open(self.outfile, 'rb').read() |
---|
86 | self.assertEqual( |
---|
87 | result, |
---|
88 | 'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,' |
---|
89 | 'firstname,lastname,matric_number,middlename,nationality,' |
---|
90 | 'perm_address,phone,reg_number,sex,student_id\r\n' |
---|
91 | |
---|
92 | 'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,Anna,' |
---|
93 | 'Tester,M123456,M.,nigeria,"Studentroad 21\nLagos 123456\n",' |
---|
94 | '+234-123-12345,123456,f,A111111\r\n' |
---|
95 | ) |
---|
96 | return |
---|
97 | |
---|
98 | class StudentStudyCourseExporterTest(StudentImportExportSetup): |
---|
99 | |
---|
100 | layer = FunctionalLayer |
---|
101 | |
---|
102 | def setUp(self): |
---|
103 | super(StudentStudyCourseExporterTest, self).setUp() |
---|
104 | student = Student() |
---|
105 | student.student_id = u'A111111' |
---|
106 | self.app['students'][student.student_id] = self.student = student |
---|
107 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
108 | self.cat = getUtility(ICatalog, name='students_catalog') |
---|
109 | self.intids = getUtility(IIntIds) |
---|
110 | #self.study_course = StudyCourse() |
---|
111 | return |
---|
112 | |
---|
113 | def test_ifaces(self): |
---|
114 | # make sure we fullfill interface contracts |
---|
115 | obj = StudentStudyCourseExporter() |
---|
116 | verifyObject(ICSVExporter, obj) |
---|
117 | verifyClass(ICSVExporter, StudentStudyCourseExporter) |
---|
118 | return |
---|
119 | |
---|
120 | def test_get_as_utility(self): |
---|
121 | # we can get an student exporter as utility |
---|
122 | result = queryUtility(ICSVExporter, name="studentstudycourses") |
---|
123 | self.assertTrue(result is not None) |
---|
124 | return |
---|
125 | |
---|
126 | def setup_student(self, student): |
---|
127 | # set predictable values for `student` |
---|
128 | student.matric_number = u'M123456' |
---|
129 | student.adm_code = u'my adm code' |
---|
130 | student.clearance_locked = False |
---|
131 | student.clr_code = u'my clr code' |
---|
132 | student.perm_address = u'Studentroad 21\nLagos 123456\n' |
---|
133 | student.reg_number = u'123456' |
---|
134 | student.student_id = u'A111111' |
---|
135 | student.firstname = u'Anna' |
---|
136 | student.lastname = u'Tester' |
---|
137 | student.middlename = u'M.' |
---|
138 | student.date_of_birth = datetime.date(1981, 2, 4) |
---|
139 | student.sex = 'f' |
---|
140 | student.email = 'anna@sample.com' |
---|
141 | student.phone = u'+234-123-12345' |
---|
142 | student.notice = u'Some notice\nin lines.' |
---|
143 | |
---|
144 | study_course = student.get('studycourse') |
---|
145 | student['studycourse'].certificate = self.certificate |
---|
146 | student['studycourse'].entry_mode = 'ug_ft' |
---|
147 | student['studycourse'].entry_session = 2010 |
---|
148 | student['studycourse'].current_session = 2012 |
---|
149 | student['studycourse'].current_level = int(self.certificate.start_level) |
---|
150 | return student |
---|
151 | |
---|
152 | def test_export_empty(self): |
---|
153 | # we can export a nearly empty study course |
---|
154 | study_course = StudentStudyCourse() |
---|
155 | exporter = StudentStudyCourseExporter() |
---|
156 | exporter.export([study_course], self.outfile) |
---|
157 | result = open(self.outfile, 'rb').read() |
---|
158 | self.assertEqual( |
---|
159 | result, |
---|
160 | 'certificate,current_level,current_session,current_verdict,' |
---|
161 | 'entry_mode,entry_session,previous_verdict\r\n' |
---|
162 | |
---|
163 | ',,,0,,,0\r\n' |
---|
164 | ) |
---|
165 | return |
---|
166 | |
---|
167 | def test_export(self): |
---|
168 | # we can really export study courses. |
---|
169 | # set values we can expect in export file |
---|
170 | self.setup_student(self.student) |
---|
171 | study_course = self.student.get('studycourse') |
---|
172 | exporter = StudentStudyCourseExporter() |
---|
173 | exporter.export([study_course], self.outfile) |
---|
174 | result = open(self.outfile, 'rb').read() |
---|
175 | self.assertEqual( |
---|
176 | result, |
---|
177 | 'certificate,current_level,current_session,current_verdict,' |
---|
178 | 'entry_mode,entry_session,previous_verdict\r\n' |
---|
179 | |
---|
180 | 'CERT1,200,2012,0,ug_ft,2010,0\r\n' |
---|
181 | ) |
---|
182 | return |
---|
183 | |
---|
184 | def test_export_all(self): |
---|
185 | # we can really export students |
---|
186 | # set values we can expect in export file |
---|
187 | self.setup_student(self.student) |
---|
188 | exporter = StudentStudyCourseExporter() |
---|
189 | exporter.export_all(self.app, self.outfile) |
---|
190 | result = open(self.outfile, 'rb').read() |
---|
191 | self.assertEqual( |
---|
192 | result, |
---|
193 | 'certificate,current_level,current_session,current_verdict,' |
---|
194 | 'entry_mode,entry_session,previous_verdict\r\n' |
---|
195 | |
---|
196 | 'CERT1,200,2012,0,ug_ft,2010,0\r\n' |
---|
197 | ) |
---|
198 | return |
---|