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

Last change on this file since 10267 was 10245, checked in by Henrik Bettermann, 12 years ago

Export entry_mode correctly.

  • Property svn:keywords set to Id
File size: 38.4 KB
Line 
1import os
2import grok
3import datetime
4from cStringIO import StringIO
5from zope.component import queryUtility, getUtility
6from zope.event import notify
7from zope.interface.verify import verifyObject, verifyClass
8from waeup.kofa.interfaces import (
9    ICSVExporter, IExtFileStore, IFileStoreNameChooser)
10from waeup.kofa.students.catalog import StudentsQuery
11from waeup.kofa.students.export import (
12    StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,
13    CourseTicketExporter, StudentPaymentsExporter, BedTicketsExporter,
14    StudentPaymentsOverviewExporter, StudentStudyLevelsOverviewExporter,
15    ComboCardDataExporter, DataForBursaryExporter,
16    get_students,)
17from waeup.kofa.students.accommodation import BedTicket
18from waeup.kofa.students.interfaces import ICSVStudentExporter
19from waeup.kofa.students.payments import StudentOnlinePayment
20from waeup.kofa.students.student import Student
21from waeup.kofa.students.studycourse import StudentStudyCourse
22from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
23from waeup.kofa.students.tests.test_batching import StudentImportExportSetup
24from waeup.kofa.testing import FunctionalLayer
25
26curr_year = datetime.datetime.now().year
27year_range = range(curr_year - 9, curr_year + 1)
28year_range_str = ','.join([str(i) for i in year_range])
29
30class ExportHelperTests(StudentImportExportSetup):
31    layer = FunctionalLayer
32    def setUp(self):
33        super(ExportHelperTests, self).setUp()
34        student = Student()
35        self.app['students'].addStudent(student)
36        student = self.setup_student(student)
37        notify(grok.ObjectModifiedEvent(student))
38        self.student = self.app['students'][student.student_id]
39        return
40
41    def test_get_students_plain(self):
42        # without a filter we get all students
43        result = get_students(self.app)
44        self.assertEqual(len(list(result)), 1)
45        return
46
47    def test_get_students_by_session(self):
48        # we can filter out students of a certain session
49        my_filter1 = StudentsQuery(current_session=2012)
50        result = get_students(self.app, stud_filter=my_filter1)
51        self.assertEqual(len(list(result)), 1)
52
53        my_filter2 = StudentsQuery(current_session=1964)
54        result = get_students(self.app, stud_filter=my_filter2)
55        self.assertEqual(len(list(result)), 0)
56        return
57
58    def test_get_students_by_level(self):
59        # we can filter out students of a certain level
60        my_filter1 = StudentsQuery(current_level=200)
61        result = get_students(self.app, stud_filter=my_filter1)
62        self.assertEqual(len(list(result)), 1)
63
64        my_filter2 = StudentsQuery(current_level=300)
65        result = get_students(self.app, stud_filter=my_filter2)
66        self.assertEqual(len(list(result)), 0)
67        return
68
69    def test_get_students_by_deptcode(self):
70        # we can filter out students of a certain dept.
71        my_filter1 = StudentsQuery(depcode='NA')
72        result = get_students(self.app, stud_filter=my_filter1)
73        self.assertEqual(len(list(result)), 1)
74
75        my_filter2 = StudentsQuery(depcode='NOTEXISTING')
76        result = get_students(self.app, stud_filter=my_filter2)
77        self.assertEqual(len(list(result)), 0)
78        return
79
80    def test_get_students_by_faccode(self):
81        # we can filter out students of a certain faculty.
82        my_filter1 = StudentsQuery(faccode='NA')
83        result = get_students(self.app, stud_filter=my_filter1)
84        self.assertEqual(len(list(result)), 1)
85
86        my_filter2 = StudentsQuery(faccode='NOTEXISTING')
87        result = get_students(self.app, stud_filter=my_filter2)
88        self.assertEqual(len(list(result)), 0)
89        return
90
91    def test_get_students_by_current_mode(self):
92        # we can filter out students in a certain mode.
93        my_filter1 = StudentsQuery(current_mode='ug_ft')
94        result = get_students(self.app, stud_filter=my_filter1)
95        self.assertEqual(len(list(result)), 1)
96
97        my_filter2 = StudentsQuery(current_mode='NOTEXISTING')
98        result = get_students(self.app, stud_filter=my_filter2)
99        self.assertEqual(len(list(result)), 0)
100        return
101
102
103class StudentsExporterTest(StudentImportExportSetup):
104
105    layer = FunctionalLayer
106
107    std_csv_entry = (
108        'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
109        'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
110        '+234-123-12345#,123,f,A111111,0,,,created'
111        )
112
113    def setUp(self):
114        super(StudentsExporterTest, self).setUp()
115        self.setup_for_export()
116        return
117
118    def test_ifaces(self):
119        # make sure we fullfill interface contracts
120        obj = StudentsExporter()
121        verifyObject(ICSVStudentExporter, obj)
122        verifyClass(ICSVStudentExporter, StudentsExporter)
123        return
124
125    def test_get_as_utility(self):
126        # we can get an student exporter as utility
127        result = queryUtility(ICSVExporter, name="students")
128        self.assertTrue(result is not None)
129        return
130
131    def test_export(self):
132        # we can really export students
133        # set values we can expect in export file
134        self.setup_student(self.student)
135        exporter = StudentsExporter()
136        exporter.export([self.student], self.outfile)
137        result = open(self.outfile, 'rb').read()
138        self.assertTrue(
139            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
140            'firstname,lastname,matric_number,middlename,nationality,officer_comment,'
141            'perm_address,personal_updated,'
142            'phone,reg_number,sex,student_id,suspended,suspended_comment,password,'
143            'state,history,certcode,is_postgrad,current_level,current_session\r\n'
144
145            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
146            'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
147            '+234-123-12345#,123,f,A111111,0,,,created'
148            in result
149            )
150        return
151
152    def test_export_all(self):
153        # we can really export students
154        # set values we can expect in export file
155        self.setup_student(self.student)
156        exporter = StudentsExporter()
157        exporter.export_all(self.app, self.outfile)
158        result = open(self.outfile, 'rb').read()
159        self.assertTrue(
160            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
161            'firstname,lastname,matric_number,middlename,nationality,officer_comment,'
162            'perm_address,personal_updated'
163            ',phone,reg_number,sex,student_id,suspended,suspended_comment,password,'
164            'state,history,certcode,is_postgrad,current_level,current_session\r\n'
165
166            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
167            'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
168            '+234-123-12345#,123,f,A111111,0,,,created'
169            in result
170            )
171        return
172
173    def test_export_student(self):
174        # we can export a single student
175        self.setup_student(self.student)
176        exporter = StudentsExporter()
177        exporter.export_student(self.student, self.outfile)
178        result = open(self.outfile, 'rb').read()
179        self.assertTrue(
180            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
181            'firstname,lastname,matric_number,middlename,nationality,officer_comment,'
182            'perm_address,personal_updated'
183            ',phone,reg_number,sex,student_id,suspended,suspended_comment,password,'
184            'state,history,certcode,is_postgrad,current_level,current_session\r\n'
185
186            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
187            'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
188            '+234-123-12345#,123,f,A111111,0,,,created'
189            in result
190            )
191        return
192
193    def test_export_filtered(self):
194        # we can export a filtered set of students (filtered by session/level)
195        self.setup_student(self.student)
196        self.app['students'].addStudent(self.student)
197        notify(grok.ObjectModifiedEvent(self.student))
198        exporter = StudentsExporter()
199
200        exporter.export_filtered(
201            self.app, self.outfile,
202            current_session=None, current_level=None)
203        result1 = open(self.outfile, 'rb').read()
204        exporter.export_filtered(
205            self.app, self.outfile,
206            current_session=2012, current_level=None)
207        result2 = open(self.outfile, 'rb').read()
208        # current_level can be both a string ...
209        exporter.export_filtered(
210            self.app, self.outfile,
211            current_session=None, current_level=u'200')
212        result3 = open(self.outfile, 'rb').read()
213        exporter.export_filtered(
214            self.app, self.outfile,
215            current_session=2011, current_level=None)
216        result4 = open(self.outfile, 'rb').read()
217        # ... and an integer
218        exporter.export_filtered(
219            self.app, self.outfile,
220            current_session=None, current_level=100)
221        result5 = open(self.outfile, 'rb').read()
222        self.assertTrue(self.std_csv_entry in result1)
223        self.assertTrue(self.std_csv_entry in result2)
224        self.assertTrue(self.std_csv_entry in result3)
225        self.assertTrue(self.std_csv_entry not in result4)
226        self.assertTrue(self.std_csv_entry not in result5)
227        return
228
229    def test_export_filtered_by_dept(self):
230        # we can export a set of students filtered by department
231        self.setup_student(self.student)
232        self.app['students'].addStudent(self.student)
233        notify(grok.ObjectModifiedEvent(self.student))
234        exporter = StudentsExporter()
235        # current_session can be both a string ...
236        exporter.export_filtered(
237            self.app, self.outfile,
238            current_session='2012', current_level=u'200', depcode='NA')
239        result1 = open(self.outfile, 'rb').read()
240        # ... and an integer
241        exporter.export_filtered(
242            self.app, self.outfile,
243            current_session=2012, current_level=200, depcode='NODEPT')
244        result2 = open(self.outfile, 'rb').read()
245        self.assertTrue(self.std_csv_entry in result1)
246        self.assertTrue(self.std_csv_entry not in result2)
247        return
248
249    def test_export_filtered_by_faculty(self):
250        # we can export a set of students filtered by faculty
251        self.setup_student(self.student)
252        self.app['students'].addStudent(self.student)
253        notify(grok.ObjectModifiedEvent(self.student))
254        exporter = StudentsExporter()
255
256        exporter.export_filtered(
257            self.app, self.outfile,
258            current_session=2012, current_level='200', faccode='NA')
259        result1 = open(self.outfile, 'rb').read()
260        exporter.export_filtered(
261            self.app, self.outfile,
262            current_session=2012, current_level=200, faccode='NOFAC')
263        result2 = open(self.outfile, 'rb').read()
264        self.assertTrue(self.std_csv_entry in result1)
265        self.assertTrue(self.std_csv_entry not in result2)
266        return
267
268class StudentStudyCourseExporterTest(StudentImportExportSetup):
269
270    layer = FunctionalLayer
271
272    def setUp(self):
273        super(StudentStudyCourseExporterTest, self).setUp()
274        self.setup_for_export()
275        return
276
277    def test_ifaces(self):
278        # make sure we fullfill interface contracts
279        obj = StudentStudyCourseExporter()
280        verifyObject(ICSVStudentExporter, obj)
281        verifyClass(ICSVStudentExporter, StudentStudyCourseExporter)
282        return
283
284    def test_get_as_utility(self):
285        # we can get an student exporter as utility
286        result = queryUtility(ICSVExporter, name="studentstudycourses")
287        self.assertTrue(result is not None)
288        return
289
290    def test_export_empty(self):
291        # we can export a nearly empty study course
292        study_course = StudentStudyCourse()
293        exporter = StudentStudyCourseExporter()
294        exporter.export([study_course], self.outfile)
295        result = open(self.outfile, 'rb').read()
296        self.assertEqual(
297            result,
298            'certificate,current_level,current_session,current_verdict,'
299            'entry_mode,entry_session,previous_verdict,student_id\r\n'
300
301            ',,,0,,,0,\r\n'
302            )
303        return
304
305    def test_export(self):
306        # we can really export study courses.
307        # set values we can expect in export file
308        self.setup_student(self.student)
309        study_course = self.student.get('studycourse')
310        exporter = StudentStudyCourseExporter()
311        exporter.export([study_course], self.outfile)
312        result = open(self.outfile, 'rb').read()
313        self.assertEqual(
314            result,
315            'certificate,current_level,current_session,current_verdict,'
316            'entry_mode,entry_session,previous_verdict,student_id\r\n'
317
318            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
319            )
320        return
321
322    def test_export_all(self):
323        # we can really export students
324        # set values we can expect in export file
325        self.setup_student(self.student)
326        exporter = StudentStudyCourseExporter()
327        exporter.export_all(self.app, self.outfile)
328        result = open(self.outfile, 'rb').read()
329        self.assertEqual(
330            result,
331            'certificate,current_level,current_session,current_verdict,'
332            'entry_mode,entry_session,previous_verdict,student_id\r\n'
333
334            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
335            )
336        return
337
338    def test_export_student(self):
339        # we can export studycourse of a certain student
340        self.setup_student(self.student)
341        exporter = StudentStudyCourseExporter()
342        exporter.export_student(self.student, self.outfile)
343        result = open(self.outfile, 'rb').read()
344        self.assertEqual(
345            result,
346            'certificate,current_level,current_session,current_verdict,'
347            'entry_mode,entry_session,previous_verdict,student_id\r\n'
348
349            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
350            )
351        return
352
353    def test_export_filtered(self):
354        # we can export studycourses of a filtered set of students
355        self.setup_student(self.student)
356        self.app['students'].addStudent(self.student)
357        notify(grok.ObjectModifiedEvent(self.student))
358
359        exporter = StudentStudyCourseExporter()
360        exporter.export_filtered(
361            self.student, self.outfile, current_session=2012)
362        result = open(self.outfile, 'rb').read()
363        self.assertEqual(
364            result,
365            'certificate,current_level,current_session,current_verdict,'
366            'entry_mode,entry_session,previous_verdict,student_id\r\n'
367
368            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
369            )
370        return
371
372
373
374class StudentStudyLevelExporterTest(StudentImportExportSetup):
375
376    layer = FunctionalLayer
377
378    def setUp(self):
379        super(StudentStudyLevelExporterTest, self).setUp()
380        self.setup_for_export()
381        return
382
383    def test_ifaces(self):
384        # make sure we fullfill interface contracts
385        obj = StudentStudyLevelExporter()
386        verifyObject(ICSVStudentExporter, obj)
387        verifyClass(ICSVStudentExporter, StudentStudyLevelExporter)
388        return
389
390    def test_get_as_utility(self):
391        # we can get an student exporter as utility
392        result = queryUtility(ICSVExporter, name="studentstudylevels")
393        self.assertTrue(result is not None)
394        return
395
396    def test_export_empty(self):
397        # we can export a nearly empty study level
398        study_level = StudentStudyLevel()
399        exporter = StudentStudyLevelExporter()
400        exporter.export([study_level], self.outfile)
401        result = open(self.outfile, 'rb').read()
402        self.assertEqual(
403            result,
404            'gpa,level,level_session,level_verdict,total_credits,'
405            'validated_by,validation_date,'
406            'student_id,number_of_tickets,certcode\r\n'
407            '0.0,,,0,0,,,,0,\r\n'
408            )
409        return
410
411    def test_export(self):
412        # we can really export study levels.
413        # set values we can expect in export file
414        self.setup_student(self.student)
415        study_course = self.student.get('studycourse')
416        study_level = study_course[study_course.keys()[0]]
417        exporter = StudentStudyLevelExporter()
418        exporter.export([study_level], self.outfile)
419        result = open(self.outfile, 'rb').read()
420        self.assertEqual(
421            result,
422            'gpa,level,level_session,level_verdict,total_credits,'
423            'validated_by,validation_date,'
424            'student_id,number_of_tickets,certcode\r\n'
425            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
426            )
427        return
428
429    def test_export_all(self):
430        # we can really export study levels
431        # set values we can expect in export file
432        self.setup_student(self.student)
433        exporter = StudentStudyLevelExporter()
434        exporter.export_all(self.app, self.outfile)
435        result = open(self.outfile, 'rb').read()
436        self.assertEqual(
437            result,
438            'gpa,level,level_session,level_verdict,total_credits,'
439            'validated_by,validation_date,'
440            'student_id,number_of_tickets,certcode\r\n'
441            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
442            )
443        return
444
445    def test_export_student(self):
446        # we can really export study levels of a certain student
447        self.setup_student(self.student)
448        exporter = StudentStudyLevelExporter()
449        exporter.export_student(self.student, self.outfile)
450        result = open(self.outfile, 'rb').read()
451        self.assertEqual(
452            result,
453            'gpa,level,level_session,level_verdict,total_credits,'
454            'validated_by,validation_date,'
455            'student_id,number_of_tickets,certcode\r\n'
456            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
457            )
458        return
459
460    def test_export_filtered(self):
461        # we can export studylevels of a filtered set of students
462        self.setup_student(self.student)
463        self.app['students'].addStudent(self.student)
464        notify(grok.ObjectModifiedEvent(self.student))
465
466        exporter = StudentStudyLevelExporter()
467        exporter.export_filtered(
468            self.student, self.outfile, current_level=200)
469        result = open(self.outfile, 'rb').read()
470        self.assertEqual(
471            result,
472            'gpa,level,level_session,level_verdict,total_credits,'
473            'validated_by,validation_date,'
474            'student_id,number_of_tickets,certcode\r\n'
475            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
476            )
477        return
478
479class CourseTicketExporterTest(StudentImportExportSetup):
480
481    layer = FunctionalLayer
482
483    def setUp(self):
484        super(CourseTicketExporterTest, self).setUp()
485        self.setup_for_export()
486        return
487
488    def test_ifaces(self):
489        # make sure we fullfill interface contracts
490        obj = CourseTicketExporter()
491        verifyObject(ICSVStudentExporter, obj)
492        verifyClass(ICSVStudentExporter, CourseTicketExporter)
493        return
494
495    def test_get_as_utility(self):
496        # we can get an student exporter as utility
497        result = queryUtility(ICSVExporter, name="coursetickets")
498        self.assertTrue(result is not None)
499        return
500
501    def test_export_empty(self):
502        # we can export a nearly empty course ticket
503        ticket = CourseTicket()
504        exporter = CourseTicketExporter()
505        exporter.export([ticket], self.outfile)
506        result = open(self.outfile, 'rb').read()
507        self.assertEqual(
508            result,
509            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
510            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
511            '0,0,,,,,,,0,,,,,,\r\n'
512            )
513        return
514
515    def test_export(self):
516        # we can really export course tickets.
517        # set values we can expect in export file
518        self.setup_student(self.student)
519        study_course = self.student.get('studycourse')
520        study_level = study_course[study_course.keys()[0]]
521        ticket = study_level['CRS1']
522        exporter = CourseTicketExporter()
523        exporter.export([ticket], self.outfile)
524        result = open(self.outfile, 'rb').read()
525        self.assertEqual(
526            result,
527            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
528            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
529            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1\r\n'
530            )
531        return
532
533    def test_export_all(self):
534        # we can really export all course tickets
535        # set values we can expect in export file
536        self.setup_student(self.student)
537        exporter = CourseTicketExporter()
538        exporter.export_all(self.app, self.outfile)
539        result = open(self.outfile, 'rb').read()
540        self.assertEqual(
541            result,
542            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
543            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
544            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1\r\n'
545            )
546        return
547
548    def test_export_student(self):
549        # we can really export all course tickets of a certain student
550        self.setup_student(self.student)
551        exporter = CourseTicketExporter()
552        exporter.export_student(self.student, self.outfile)
553        result = open(self.outfile, 'rb').read()
554        self.assertEqual(
555            result,
556            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
557            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
558            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1\r\n'
559            )
560        return
561
562    def test_export_filtered(self):
563        # we can export course tickets of a filtered set of students
564        self.setup_student(self.student)
565        self.app['students'].addStudent(self.student)
566        notify(grok.ObjectModifiedEvent(self.student))
567
568        exporter = CourseTicketExporter()
569        exporter.export_filtered(
570            self.student, self.outfile, current_level=200)
571        result = open(self.outfile, 'rb').read()
572        self.assertEqual(
573            result,
574            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
575            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
576            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1\r\n'
577            )
578        # if the coursetickets catalog is used to filter students
579        # and (course) code is not None
580        # only course tickets which belong to this course are exported
581        exporter.export_filtered(
582            self.student, self.outfile, catalog='coursetickets', code='CRS1')
583        result = open(self.outfile, 'rb').read()
584        self.assertEqual(
585            result,
586            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
587            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
588            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1\r\n'
589            )
590        exporter.export_filtered(
591            self.student, self.outfile, catalog='coursetickets', code='any code')
592        result = open(self.outfile, 'rb').read()
593        self.assertEqual(
594            result,
595            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
596            'mandatory,passmark,score,semester,title,student_id,certcode\r\n'
597            )
598        return
599
600class StudentPaymentsExporterTest(StudentImportExportSetup):
601
602    layer = FunctionalLayer
603
604    def setUp(self):
605        super(StudentPaymentsExporterTest, self).setUp()
606        self.setup_for_export()
607        return
608
609    def test_ifaces(self):
610        # make sure we fullfill interface contracts
611        obj = StudentPaymentsExporter()
612        verifyObject(ICSVStudentExporter, obj)
613        verifyClass(ICSVStudentExporter, StudentPaymentsExporter)
614        return
615
616    def test_get_as_utility(self):
617        # we can get a payments exporter as utility
618        result = queryUtility(ICSVExporter, name="studentpayments")
619        self.assertTrue(result is not None)
620        return
621
622    def test_export_empty(self):
623        # we can export a nearly empty payment
624        payment = StudentOnlinePayment()
625        payment.creation_date = datetime.datetime(2012, 4, 1, 13, 12, 1)
626        exporter = StudentPaymentsExporter()
627        exporter.export([payment], self.outfile)
628        result = open(self.outfile, 'rb').read()
629        self.assertEqual(
630            result,
631            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
632            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
633            'r_code,r_desc,student_id,state,current_session\r\n'
634
635            ',0.0,2012-04-01 13:12:01,schoolfee,1,,,,,unpaid,,0.0,,,,,\r\n'
636            )
637        return
638
639    def test_export(self):
640        # we can really export student payments.
641        # set values we can expect in export file
642        self.setup_student(self.student)
643        payment = self.student['payments']['my-payment']
644        exporter = StudentPaymentsExporter()
645        exporter.export([payment], self.outfile)
646        result = open(self.outfile, 'rb').read()
647        self.assertEqual(
648            result,
649            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
650            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
651            'r_code,r_desc,student_id,state,current_session\r\n'
652
653            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
654            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
655            'r-code,,A111111,created,2012\r\n'
656            )
657        return
658
659    def test_export_all(self):
660        # we can really export all payments
661        # set values we can expect in export file
662        self.setup_student(self.student)
663        exporter = StudentPaymentsExporter()
664        exporter.export_all(self.app, self.outfile)
665        result = open(self.outfile, 'rb').read()
666        self.assertEqual(
667            result,
668            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
669            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
670            'r_code,r_desc,student_id,state,current_session\r\n'
671
672            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
673            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
674            'r-code,,A111111,created,2012\r\n'
675            )
676        return
677
678    def test_export_student(self):
679        # we can really export all payments of a certain student
680        # set values we can expect in export file
681        self.setup_student(self.student)
682        exporter = StudentPaymentsExporter()
683        exporter.export_student(self.student, self.outfile)
684        result = open(self.outfile, 'rb').read()
685        self.assertEqual(
686            result,
687            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
688            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
689            'r_code,r_desc,student_id,state,current_session\r\n'
690
691            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
692            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
693            'r-code,,A111111,created,2012\r\n'
694            )
695        return
696
697    def test_export_filtered(self):
698        # we can export payments of a filtered set of students
699        self.setup_student(self.student)
700        self.app['students'].addStudent(self.student)
701        notify(grok.ObjectModifiedEvent(self.student))
702
703        exporter = StudentPaymentsExporter()
704        exporter.export_filtered(
705            self.student, self.outfile, current_level=200)
706        result = open(self.outfile, 'rb').read()
707        self.assertEqual(
708            result,
709            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
710            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
711            'r_code,r_desc,student_id,state,current_session\r\n'
712
713            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
714            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
715            'r-code,,A111111,created,2012\r\n'
716            )
717        return
718
719class BursaryDataExporterTest(StudentImportExportSetup):
720
721    layer = FunctionalLayer
722
723    def setUp(self):
724        super(BursaryDataExporterTest, self).setUp()
725        self.setup_for_export()
726        return
727
728    def test_export_all(self):
729        # we can really export all payments
730        # set values we can expect in export file
731        self.setup_student(self.student)
732        exporter = DataForBursaryExporter()
733        exporter.export_all(self.app, self.outfile)
734        result = open(self.outfile, 'rb').read()
735        self.assertEqual(
736            result,
737            'ac,amount_auth,creation_date,p_category,p_current,p_id,p_item,'
738            'p_level,p_session,p_state,payment_date,r_amount_approved,r_code,'
739            'r_desc,student_id,matric_number,firstname,middlename,lastname,'
740            'state,current_session,entry_session,entry_mode,faccode,depcode\r\n'
741
742            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,p-item,100,2012,'
743            'unpaid,2012-04-01 14:12:01,12.12,r-code,,A111111,234,'
744            'Anna,M.,Tester,created,2012,2010,ug_ft,NA,NA\r\n'
745            )
746        return
747
748class BedTicketsExporterTest(StudentImportExportSetup):
749
750    layer = FunctionalLayer
751
752    def setUp(self):
753        super(BedTicketsExporterTest, self).setUp()
754        self.setup_for_export()
755        return
756
757    def test_ifaces(self):
758        # make sure we fullfill interface contracts
759        obj = BedTicketsExporter()
760        verifyObject(ICSVStudentExporter, obj)
761        verifyClass(ICSVStudentExporter, BedTicketsExporter)
762        return
763
764    def test_get_as_utility(self):
765        # we can get a bedtickets exporter as utility
766        result = queryUtility(ICSVExporter, name="bedtickets")
767        self.assertTrue(result is not None)
768        return
769
770    def test_export_empty(self):
771        # we can export a nearly empty bedticket
772        bedticket = BedTicket()
773        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
774        bedticket.bed = bed
775        exporter = BedTicketsExporter()
776        exporter.export([bedticket], self.outfile)
777        result = open(self.outfile, 'rb').read()
778        self.assertMatches(
779            result,
780            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
781            'booking_session,student_id,actual_bed_type\r\n'
782            'hall-1_A_101_A,,,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,,,regular_male_fr\r\n'
783            )
784        return
785
786    def test_export(self):
787        # we can really export student bedtickets.
788        # set values we can expect in export file
789        self.setup_student(self.student)
790        bedticket = self.student['accommodation']['2004']
791        exporter = BedTicketsExporter()
792        exporter.export([bedticket], self.outfile)
793        result = open(self.outfile, 'rb').read()
794        self.assertMatches(
795            result,
796            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
797            'booking_session,student_id,actual_bed_type\r\n'
798            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
799            'A111111,regular_male_fr\r\n'
800            )
801        return
802
803    def test_export_all(self):
804        # we can really export all bedtickets
805        # set values we can expect in export file
806        self.setup_student(self.student)
807        exporter = BedTicketsExporter()
808        exporter.export_all(self.app, self.outfile)
809        result = open(self.outfile, 'rb').read()
810        self.assertMatches(
811            result,
812            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
813            'booking_session,student_id,actual_bed_type\r\n'
814            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
815            'A111111,regular_male_fr\r\n'
816            )
817        return
818
819    def test_export_student(self):
820        # we can really export all bedtickets of a certain student
821        # set values we can expect in export file
822        self.setup_student(self.student)
823        exporter = BedTicketsExporter()
824        exporter.export_student(self.student, self.outfile)
825        result = open(self.outfile, 'rb').read()
826        self.assertMatches(
827            result,
828            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
829            'booking_session,student_id,actual_bed_type\r\n'
830            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
831            'A111111,regular_male_fr\r\n'
832            )
833        return
834
835    def test_export_filtered(self):
836        # we can export payments of a filtered set of students
837        self.setup_student(self.student)
838        self.app['students'].addStudent(self.student)
839        notify(grok.ObjectModifiedEvent(self.student))
840
841        exporter = BedTicketsExporter()
842        exporter.export_filtered(
843            self.student, self.outfile, current_level=200)
844        result = open(self.outfile, 'rb').read()
845        self.assertMatches(
846            result,
847            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
848            'booking_session,student_id,actual_bed_type\r\n'
849            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,'
850            '2004,A111111,regular_male_fr\r\n')
851        return
852
853
854class StudentPaymentsOverviewExporterTest(StudentImportExportSetup):
855
856    layer = FunctionalLayer
857
858    def setUp(self):
859        super(StudentPaymentsOverviewExporterTest, self).setUp()
860        self.setup_for_export()
861        return
862
863    def test_ifaces(self):
864        # make sure we fullfill interface contracts
865        obj = StudentPaymentsOverviewExporter()
866        verifyObject(ICSVStudentExporter, obj)
867        verifyClass(ICSVStudentExporter, StudentPaymentsOverviewExporter)
868        return
869
870    def test_get_as_utility(self):
871        # we can get a payments exporter as utility
872        result = queryUtility(ICSVExporter, name="paymentsoverview")
873        self.assertTrue(result is not None)
874        return
875
876    def test_export(self):
877        self.setup_student(self.student)
878        exporter = StudentPaymentsOverviewExporter()
879        exporter.export([self.student], self.outfile)
880        result = open(self.outfile, 'rb').read()
881        self.assertEqual(
882            'student_id,matric_number,display_fullname,state,certcode,'
883            'faccode,depcode,is_postgrad,'
884            'current_level,current_session,current_mode,'
885            '%s\r\n'
886            'A111111,234,Anna M. Tester,created,CERT1,NA,NA,0,200,2012,ug_ft,'
887            ',,,,,,,,,\r\n' % year_range_str,
888            result
889            )
890        return
891
892    def test_export_all(self):
893        # we can really export students
894        # set values we can expect in export file
895        self.setup_student(self.student)
896        # We add successful payments
897        payment = StudentOnlinePayment()
898        payment.p_id = 'my-id'
899        payment.p_session = 2009
900        payment.amount_auth = 12.12
901        payment.p_state = u'paid'
902        self.student['payments']['my-2ndpayment'] = payment
903        exporter = StudentPaymentsOverviewExporter()
904        exporter.export_all(self.app, self.outfile)
905        result = open(self.outfile, 'rb').read()
906        self.assertEqual(
907            'student_id,matric_number,display_fullname,state,'
908            'certcode,faccode,depcode,is_postgrad,'
909            'current_level,current_session,current_mode,'
910            '%s\r\nA111111,234,Anna M. Tester,created,CERT1,NA,NA,0,'
911            '200,2012,ug_ft,,,,,,12.12,,,,\r\n' % year_range_str,
912            result
913            )
914        return
915
916class StudentStudyLevelsOverviewExporterTest(StudentImportExportSetup):
917
918    layer = FunctionalLayer
919
920    def setUp(self):
921        super(StudentStudyLevelsOverviewExporterTest, self).setUp()
922        self.setup_for_export()
923        return
924
925    def test_ifaces(self):
926        obj = StudentStudyLevelsOverviewExporter()
927        verifyObject(ICSVStudentExporter, obj)
928        verifyClass(ICSVStudentExporter, StudentStudyLevelsOverviewExporter)
929        return
930
931    def test_get_as_utility(self):
932        result = queryUtility(ICSVExporter, name="studylevelsoverview")
933        self.assertTrue(result is not None)
934        return
935
936    def test_export(self):
937        self.setup_student(self.student)
938        exporter = StudentStudyLevelsOverviewExporter()
939        exporter.export([self.student], self.outfile)
940        result = open(self.outfile, 'rb').read()
941        self.assertEqual(
942             'student_id,state,certcode,faccode,depcode,is_postgrad,'
943             'entry_session,current_level,current_session,'
944             '10,100,110,120,200,210,220,300,310,320,400,410,420,500,'
945             '510,520,600,610,620,700,710,720,800,810,820,900,910,920,999\r\n'
946             'A111111,created,CERT1,NA,NA,0,2010,200,2012,,2012'
947             ',,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n',
948            result
949            )
950        return
951
952    def test_export_all(self):
953        self.setup_student(self.student)
954        exporter = StudentStudyLevelsOverviewExporter()
955        exporter.export_all(self.app, self.outfile)
956        result = open(self.outfile, 'rb').read()
957        self.assertEqual(
958            'student_id,state,certcode,faccode,depcode,is_postgrad,'
959            'entry_session,current_level,current_session,'
960            '10,100,110,120,200,210,220,300,310,320,400,410,420,500,'
961            '510,520,600,610,620,700,710,720,800,810,820,900,910,920,999\r\n'
962            'A111111,created,CERT1,NA,NA,0,2010,200,2012,,2012'
963            ',,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n',
964            result
965            )
966        return
967
968class ComboCardExporterTest(StudentImportExportSetup):
969
970    layer = FunctionalLayer
971
972    def setUp(self):
973        super(ComboCardExporterTest, self).setUp()
974        self.setup_for_export()
975        return
976
977    def create_passport_img(self, student):
978        # create some passport file for `student`
979        storage = getUtility(IExtFileStore)
980        image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
981        self.image_contents = open(image_path, 'rb').read()
982        file_id = IFileStoreNameChooser(student).chooseName(
983            attr='passport.jpg')
984        storage.createFile(file_id, StringIO(self.image_contents))
985
986    def test_export_all(self):
987        self.setup_student(self.student)
988        self.create_passport_img(self.student)
989        exporter = ComboCardDataExporter()
990        exporter.export_all(self.app, self.outfile)
991        result = open(self.outfile, 'rb').read()
992        self.assertTrue(
993            'display_fullname,student_id,matric_number,certificate,faculty,'
994            'department,passport_path\r\nAnna M. Tester,A111111,234,'
995            'Unnamed Certificate,Faculty of Unnamed Faculty (NA),'
996            'Department of Unnamed Department (NA),'
997            'students/00110/A111111/passport_A111111.jpg\r\n'
998            in result
999            )
1000        return
Note: See TracBrowser for help on using the repository browser.