source: main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py @ 8202

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

Turn autocomplition off.

Fix test.

  • Property svn:keywords set to Id
File size: 11.4 KB
Line 
1import datetime
2import os
3from zope.catalog.interfaces import ICatalog
4from zope.component import queryUtility, getUtility
5from zope.interface.verify import verifyObject, verifyClass
6from zope.intid.interfaces import IIntIds
7from waeup.kofa.interfaces import ICSVExporter
8from waeup.kofa.students.export import (
9    StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,)
10from waeup.kofa.students.student import Student, StudentStudyCourse
11from waeup.kofa.students.studylevel import StudentStudyLevel
12from waeup.kofa.students.tests.test_batching import StudentImportExportSetup
13from waeup.kofa.testing import FunctionalLayer
14
15class StudentsExporterTest(StudentImportExportSetup):
16
17    layer = FunctionalLayer
18
19    def setUp(self):
20        super(StudentsExporterTest, self).setUp()
21        student = Student()
22        student.student_id = u'A111111'
23        self.app['students'][student.student_id] = self.student = student
24        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
25        self.cat = getUtility(ICatalog, name='students_catalog')
26        self.intids = getUtility(IIntIds)
27        return
28
29    def test_ifaces(self):
30        # make sure we fullfill interface contracts
31        obj = StudentsExporter()
32        verifyObject(ICSVExporter, obj)
33        verifyClass(ICSVExporter, StudentsExporter)
34        return
35
36    def test_get_as_utility(self):
37        # we can get an student exporter as utility
38        result = queryUtility(ICSVExporter, name="students")
39        self.assertTrue(result is not None)
40        return
41
42    def setup_student(self, student):
43        # set predictable values for `student`
44        student.matric_number = u'M123456'
45        student.adm_code = u'my adm code'
46        student.clearance_locked = False
47        student.clr_code = u'my clr code'
48        student.perm_address = u'Studentroad 21\nLagos 123456\n'
49        student.reg_number = u'123456'
50        student.student_id = u'A111111'
51        student.firstname = u'Anna'
52        student.lastname = u'Tester'
53        student.middlename = u'M.'
54        student.date_of_birth = datetime.date(1981, 2, 4)
55        student.sex = 'f'
56        student.email = 'anna@sample.com'
57        student.phone = u'+234-123-12345'
58        student.notice = u'Some notice\nin lines.'
59        student.nationality = u'NG'
60        return student
61
62    def test_export(self):
63        # we can really export students
64        # set values we can expect in export file
65        self.setup_student(self.student)
66        exporter = StudentsExporter()
67        exporter.export([self.student], self.outfile)
68        result = open(self.outfile, 'rb').read()
69        self.assertEqual(
70            result,
71            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
72            'firstname,lastname,matric_number,middlename,nationality,'
73            'perm_address,phone,reg_number,sex,student_id\r\n'
74
75            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,Anna,'
76            'Tester,M123456,M.,NG,"Studentroad 21\nLagos 123456\n",'
77            '+234-123-12345,123456,f,A111111\r\n'
78            )
79        return
80
81    def test_export_all(self):
82        # we can really export students
83        # set values we can expect in export file
84        self.setup_student(self.student)
85        exporter = StudentsExporter()
86        exporter.export_all(self.app, self.outfile)
87        result = open(self.outfile, 'rb').read()
88        self.assertEqual(
89            result,
90            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
91            'firstname,lastname,matric_number,middlename,nationality,'
92            'perm_address,phone,reg_number,sex,student_id\r\n'
93
94            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,Anna,'
95            'Tester,M123456,M.,NG,"Studentroad 21\nLagos 123456\n",'
96            '+234-123-12345,123456,f,A111111\r\n'
97            )
98        return
99
100class StudentStudyCourseExporterTest(StudentImportExportSetup):
101
102    layer = FunctionalLayer
103
104    def setUp(self):
105        super(StudentStudyCourseExporterTest, self).setUp()
106        student = Student()
107        student.student_id = u'A111111'
108        self.app['students'][student.student_id] = self.student = student
109        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
110        self.cat = getUtility(ICatalog, name='students_catalog')
111        self.intids = getUtility(IIntIds)
112        #self.study_course = StudyCourse()
113        return
114
115    def test_ifaces(self):
116        # make sure we fullfill interface contracts
117        obj = StudentStudyCourseExporter()
118        verifyObject(ICSVExporter, obj)
119        verifyClass(ICSVExporter, StudentStudyCourseExporter)
120        return
121
122    def test_get_as_utility(self):
123        # we can get an student exporter as utility
124        result = queryUtility(ICSVExporter, name="studentstudycourses")
125        self.assertTrue(result is not None)
126        return
127
128    def setup_student(self, student):
129        # set predictable values for `student`
130        student.matric_number = u'M123456'
131        student.adm_code = u'my adm code'
132        student.clearance_locked = False
133        student.clr_code = u'my clr code'
134        student.perm_address = u'Studentroad 21\nLagos 123456\n'
135        student.reg_number = u'123456'
136        student.student_id = u'A111111'
137        student.firstname = u'Anna'
138        student.lastname = u'Tester'
139        student.middlename = u'M.'
140        student.date_of_birth = datetime.date(1981, 2, 4)
141        student.sex = 'f'
142        student.email = 'anna@sample.com'
143        student.phone = u'+234-123-12345'
144        student.notice = u'Some notice\nin lines.'
145
146        study_course = student.get('studycourse')
147        student['studycourse'].certificate = self.certificate
148        student['studycourse'].entry_mode = 'ug_ft'
149        student['studycourse'].entry_session = 2010
150        student['studycourse'].current_session = 2012
151        student['studycourse'].current_level = int(self.certificate.start_level)
152        return student
153
154    def test_export_empty(self):
155        # we can export a nearly empty study course
156        study_course = StudentStudyCourse()
157        exporter = StudentStudyCourseExporter()
158        exporter.export([study_course], self.outfile)
159        result = open(self.outfile, 'rb').read()
160        self.assertEqual(
161            result,
162            'certificate,current_level,current_session,current_verdict,'
163            'entry_mode,entry_session,previous_verdict\r\n'
164
165            ',,,0,,,0\r\n'
166            )
167        return
168
169    def test_export(self):
170        # we can really export study courses.
171        # set values we can expect in export file
172        self.setup_student(self.student)
173        study_course = self.student.get('studycourse')
174        exporter = StudentStudyCourseExporter()
175        exporter.export([study_course], self.outfile)
176        result = open(self.outfile, 'rb').read()
177        self.assertEqual(
178            result,
179            'certificate,current_level,current_session,current_verdict,'
180            'entry_mode,entry_session,previous_verdict\r\n'
181
182            'CERT1,200,2012,0,ug_ft,2010,0\r\n'
183            )
184        return
185
186    def test_export_all(self):
187        # we can really export students
188        # set values we can expect in export file
189        self.setup_student(self.student)
190        exporter = StudentStudyCourseExporter()
191        exporter.export_all(self.app, self.outfile)
192        result = open(self.outfile, 'rb').read()
193        self.assertEqual(
194            result,
195            'certificate,current_level,current_session,current_verdict,'
196            'entry_mode,entry_session,previous_verdict\r\n'
197
198            'CERT1,200,2012,0,ug_ft,2010,0\r\n'
199            )
200        return
201
202class StudentStudyLevelExporterTest(StudentImportExportSetup):
203
204    layer = FunctionalLayer
205
206    def setUp(self):
207        super(StudentStudyLevelExporterTest, self).setUp()
208        student = Student()
209        student.student_id = u'A111111'
210        self.app['students'][student.student_id] = self.student = student
211        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
212        self.cat = getUtility(ICatalog, name='students_catalog')
213        self.intids = getUtility(IIntIds)
214        #self.study_level = StudyLevel()
215        return
216
217    def test_ifaces(self):
218        # make sure we fullfill interface contracts
219        obj = StudentStudyLevelExporter()
220        verifyObject(ICSVExporter, obj)
221        verifyClass(ICSVExporter, StudentStudyLevelExporter)
222        return
223
224    def test_get_as_utility(self):
225        # we can get an student exporter as utility
226        result = queryUtility(ICSVExporter, name="studentstudylevels")
227        self.assertTrue(result is not None)
228        return
229
230    def setup_student(self, student):
231        # set predictable values for `student`
232        student.matric_number = u'M123456'
233        student.adm_code = u'my adm code'
234        student.clearance_locked = False
235        student.clr_code = u'my clr code'
236        student.perm_address = u'Studentroad 21\nLagos 123456\n'
237        student.reg_number = u'123456'
238        student.student_id = u'A111111'
239        student.firstname = u'Anna'
240        student.lastname = u'Tester'
241        student.middlename = u'M.'
242        student.date_of_birth = datetime.date(1981, 2, 4)
243        student.sex = 'f'
244        student.email = 'anna@sample.com'
245        student.phone = u'+234-123-12345'
246        student.notice = u'Some notice\nin lines.'
247
248        study_course = student.get('studycourse')
249        student['studycourse'].certificate = self.certificate
250        student['studycourse'].entry_mode = 'ug_ft'
251        student['studycourse'].entry_session = 2010
252        student['studycourse'].current_session = 2012
253        student['studycourse'].current_level = int(self.certificate.start_level)
254
255        study_level = StudentStudyLevel()
256        study_level.level_session = 2012
257        study_level.level_verdict = "A"
258        study_level.level = 100
259        student['studycourse'].addStudentStudyLevel(
260            self.certificate, study_level)
261        return student
262
263    def test_export_empty(self):
264        # we can export a nearly empty study level
265        study_level = StudentStudyLevel()
266        exporter = StudentStudyLevelExporter()
267        exporter.export([study_level], self.outfile)
268        result = open(self.outfile, 'rb').read()
269        self.assertEqual(
270            result,
271            'level,level_session,level_verdict,matric_number,reg_number\r\n'
272            ',,0,,\r\n'
273            )
274        return
275
276    def test_export(self):
277        # we can really export study levels.
278        # set values we can expect in export file
279        self.setup_student(self.student)
280        study_course = self.student.get('studycourse')
281        study_level = study_course[study_course.keys()[0]]
282        exporter = StudentStudyLevelExporter()
283        exporter.export([study_level], self.outfile)
284        result = open(self.outfile, 'rb').read()
285        self.assertEqual(
286            result,
287            'level,level_session,level_verdict,matric_number,reg_number\r\n'
288            '100,2012,A,M123456,123456\r\n'
289            )
290        return
291
292    def test_export_all(self):
293        # we can really export study levels
294        # set values we can expect in export file
295        self.setup_student(self.student)
296        exporter = StudentStudyLevelExporter()
297        exporter.export_all(self.app, self.outfile)
298        result = open(self.outfile, 'rb').read()
299        self.assertEqual(
300            result,
301            'level,level_session,level_verdict,matric_number,reg_number\r\n'
302            '100,2012,A,M123456,123456\r\n'
303            )
304        return
Note: See TracBrowser for help on using the repository browser.