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

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

Remove print statement.

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