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

Last change on this file since 10466 was 10458, checked in by Henrik Bettermann, 11 years ago

Add pages and buttons for transcript request processing. Work in progress, tests will follow!

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