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

Last change on this file since 11756 was 11756, checked in by Henrik Bettermann, 10 years ago

Catch error if payment_date and payment is None.

  • Property svn:keywords set to Id
File size: 43.8 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 and current_session can be both a string ...
210        exporter.export_filtered(
211            self.app, self.outfile,
212            current_session='2012', 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        # Also students at probating levels are being exported ...
224        self.student['studycourse'].current_level = 210
225        notify(grok.ObjectModifiedEvent(self.student))
226        exporter.export_filtered(
227            self.app, self.outfile,
228            current_session=None, current_level=200)
229        result6 = open(self.outfile, 'rb').read()
230        # ... but not in the wrong level range.
231        self.student['studycourse'].current_level = 310
232        notify(grok.ObjectModifiedEvent(self.student))
233        exporter.export_filtered(
234            self.app, self.outfile,
235            current_session=None, current_level=200)
236        result7 = open(self.outfile, 'rb').read()
237        self.assertTrue(self.std_csv_entry in result1)
238        self.assertTrue(self.std_csv_entry in result2)
239        self.assertTrue(self.std_csv_entry in result3)
240        self.assertFalse(self.std_csv_entry in result4)
241        self.assertFalse(self.std_csv_entry in result5)
242        self.assertTrue(self.std_csv_entry in result6)
243        self.assertFalse(self.std_csv_entry in result7)
244        return
245
246    def test_export_filtered_by_dept(self):
247        # we can export a set of students filtered by department
248        self.setup_student(self.student)
249        self.app['students'].addStudent(self.student)
250        notify(grok.ObjectModifiedEvent(self.student))
251        exporter = StudentsExporter()
252        # current_session can be both a string ...
253        exporter.export_filtered(
254            self.app, self.outfile,
255            current_session='2012', current_level=u'200', depcode='NA')
256        result1 = open(self.outfile, 'rb').read()
257        # ... and an integer
258        exporter.export_filtered(
259            self.app, self.outfile,
260            current_session=2012, current_level=200, depcode='NODEPT')
261        result2 = open(self.outfile, 'rb').read()
262        self.assertTrue(self.std_csv_entry in result1)
263        self.assertTrue(self.std_csv_entry not in result2)
264        return
265
266    def test_export_filtered_by_faculty(self):
267        # we can export a set of students filtered by faculty
268        self.setup_student(self.student)
269        self.app['students'].addStudent(self.student)
270        notify(grok.ObjectModifiedEvent(self.student))
271        exporter = StudentsExporter()
272
273        exporter.export_filtered(
274            self.app, self.outfile,
275            current_session=2012, current_level='200', faccode='NA')
276        result1 = open(self.outfile, 'rb').read()
277        exporter.export_filtered(
278            self.app, self.outfile,
279            current_session=2012, current_level=200, faccode='NOFAC')
280        result2 = open(self.outfile, 'rb').read()
281        self.assertTrue(self.std_csv_entry in result1)
282        self.assertTrue(self.std_csv_entry not in result2)
283        return
284
285class StudentStudyCourseExporterTest(StudentImportExportSetup):
286
287    layer = FunctionalLayer
288
289    def setUp(self):
290        super(StudentStudyCourseExporterTest, self).setUp()
291        self.setup_for_export()
292        return
293
294    def test_ifaces(self):
295        # make sure we fullfill interface contracts
296        obj = StudentStudyCourseExporter()
297        verifyObject(ICSVStudentExporter, obj)
298        verifyClass(ICSVStudentExporter, StudentStudyCourseExporter)
299        return
300
301    def test_get_as_utility(self):
302        # we can get an student exporter as utility
303        result = queryUtility(ICSVExporter, name="studentstudycourses")
304        self.assertTrue(result is not None)
305        return
306
307    def test_export_empty(self):
308        # we can export a nearly empty study course
309        study_course = StudentStudyCourse()
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            ',,,0,,,0,\r\n'
319            )
320        return
321
322    def test_export(self):
323        # we can really export study courses.
324        # set values we can expect in export file
325        self.setup_student(self.student)
326        study_course = self.student.get('studycourse')
327        exporter = StudentStudyCourseExporter()
328        exporter.export([study_course], 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_all(self):
340        # we can really export students
341        # set values we can expect in export file
342        self.setup_student(self.student)
343        exporter = StudentStudyCourseExporter()
344        exporter.export_all(self.app, self.outfile)
345        result = open(self.outfile, 'rb').read()
346        self.assertEqual(
347            result,
348            'certificate,current_level,current_session,current_verdict,'
349            'entry_mode,entry_session,previous_verdict,student_id\r\n'
350
351            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
352            )
353        return
354
355    def test_export_student(self):
356        # we can export studycourse of a certain student
357        self.setup_student(self.student)
358        exporter = StudentStudyCourseExporter()
359        exporter.export_student(self.student, self.outfile)
360        result = open(self.outfile, 'rb').read()
361        self.assertEqual(
362            result,
363            'certificate,current_level,current_session,current_verdict,'
364            'entry_mode,entry_session,previous_verdict,student_id\r\n'
365
366            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
367            )
368        return
369
370    def test_export_filtered(self):
371        # we can export studycourses of a filtered set of students
372        self.setup_student(self.student)
373        self.app['students'].addStudent(self.student)
374        notify(grok.ObjectModifiedEvent(self.student))
375
376        exporter = StudentStudyCourseExporter()
377        exporter.export_filtered(
378            self.student, self.outfile, current_session=2012)
379        result = open(self.outfile, 'rb').read()
380        self.assertEqual(
381            result,
382            'certificate,current_level,current_session,current_verdict,'
383            'entry_mode,entry_session,previous_verdict,student_id\r\n'
384
385            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
386            )
387        return
388
389
390
391class StudentStudyLevelExporterTest(StudentImportExportSetup):
392
393    layer = FunctionalLayer
394
395    def setUp(self):
396        super(StudentStudyLevelExporterTest, self).setUp()
397        self.setup_for_export()
398        return
399
400    def test_ifaces(self):
401        # make sure we fullfill interface contracts
402        obj = StudentStudyLevelExporter()
403        verifyObject(ICSVStudentExporter, obj)
404        verifyClass(ICSVStudentExporter, StudentStudyLevelExporter)
405        return
406
407    def test_get_as_utility(self):
408        # we can get an student exporter as utility
409        result = queryUtility(ICSVExporter, name="studentstudylevels")
410        self.assertTrue(result is not None)
411        return
412
413    def test_export_empty(self):
414        # we can export a nearly empty study level
415        study_level = StudentStudyLevel()
416        exporter = StudentStudyLevelExporter()
417        exporter.export([study_level], self.outfile)
418        result = open(self.outfile, 'rb').read()
419        self.assertEqual(
420            result,
421            'gpa,level,level_session,level_verdict,total_credits,'
422            'validated_by,validation_date,'
423            'student_id,number_of_tickets,certcode\r\n'
424            '0.0,,,0,0,,,,0,\r\n'
425            )
426        return
427
428    def test_export(self):
429        # we can really export study levels.
430        # set values we can expect in export file
431        self.setup_student(self.student)
432        study_course = self.student.get('studycourse')
433        study_level = study_course[study_course.keys()[0]]
434        exporter = StudentStudyLevelExporter()
435        exporter.export([study_level], self.outfile)
436        result = open(self.outfile, 'rb').read()
437        self.assertEqual(
438            result,
439            'gpa,level,level_session,level_verdict,total_credits,'
440            'validated_by,validation_date,'
441            'student_id,number_of_tickets,certcode\r\n'
442            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
443            )
444        return
445
446    def test_export_all(self):
447        # we can really export study levels
448        # set values we can expect in export file
449        self.setup_student(self.student)
450        exporter = StudentStudyLevelExporter()
451        exporter.export_all(self.app, self.outfile)
452        result = open(self.outfile, 'rb').read()
453        self.assertEqual(
454            result,
455            'gpa,level,level_session,level_verdict,total_credits,'
456            'validated_by,validation_date,'
457            'student_id,number_of_tickets,certcode\r\n'
458            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
459            )
460        return
461
462    def test_export_student(self):
463        # we can really export study levels of a certain student
464        self.setup_student(self.student)
465        exporter = StudentStudyLevelExporter()
466        exporter.export_student(self.student, self.outfile)
467        result = open(self.outfile, 'rb').read()
468        self.assertEqual(
469            result,
470            'gpa,level,level_session,level_verdict,total_credits,'
471            'validated_by,validation_date,'
472            'student_id,number_of_tickets,certcode\r\n'
473            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
474            )
475        return
476
477    def test_export_filtered(self):
478        # we can export studylevels of a filtered set of students
479        self.setup_student(self.student)
480        self.app['students'].addStudent(self.student)
481        notify(grok.ObjectModifiedEvent(self.student))
482
483        exporter = StudentStudyLevelExporter()
484        exporter.export_filtered(
485            self.student, self.outfile)
486        result = open(self.outfile, 'rb').read()
487        self.assertEqual(
488            result,
489            'gpa,level,level_session,level_verdict,total_credits,'
490            'validated_by,validation_date,'
491            'student_id,number_of_tickets,certcode\r\n'
492            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
493            )
494        return
495
496class CourseTicketExporterTest(StudentImportExportSetup):
497
498    layer = FunctionalLayer
499
500    def setUp(self):
501        super(CourseTicketExporterTest, self).setUp()
502        self.setup_for_export()
503        return
504
505    def test_ifaces(self):
506        # make sure we fullfill interface contracts
507        obj = CourseTicketExporter()
508        verifyObject(ICSVStudentExporter, obj)
509        verifyClass(ICSVStudentExporter, CourseTicketExporter)
510        return
511
512    def test_get_as_utility(self):
513        # we can get an student exporter as utility
514        result = queryUtility(ICSVExporter, name="coursetickets")
515        self.assertTrue(result is not None)
516        return
517
518    def test_export_empty(self):
519        # we can export a nearly empty course ticket
520        ticket = CourseTicket()
521        exporter = CourseTicketExporter()
522        exporter.export([ticket], self.outfile)
523        result = open(self.outfile, 'rb').read()
524        self.assertEqual(
525            result,
526            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
527            'mandatory,passmark,score,semester,title,student_id,certcode,'
528            'display_fullname\r\n'
529            '0,0,,,,,,,0,,,,,,,\r\n'
530            )
531        return
532
533    def test_export(self):
534        # we can really export course tickets.
535        # set values we can expect in export file
536        self.setup_student(self.student)
537        study_course = self.student.get('studycourse')
538        study_level = study_course[study_course.keys()[0]]
539        ticket = study_level['CRS1']
540        exporter = CourseTicketExporter()
541        exporter.export([ticket], self.outfile)
542        result = open(self.outfile, 'rb').read()
543        self.assertEqual(
544            result,
545            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
546            'mandatory,passmark,score,semester,title,student_id,certcode,'
547            'display_fullname\r\n'
548            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1,'
549            'Anna M. Tester\r\n'
550            )
551        return
552
553    def test_export_all(self):
554        # we can really export all course tickets
555        # set values we can expect in export file
556        self.setup_student(self.student)
557        exporter = CourseTicketExporter()
558        exporter.export_all(self.app, self.outfile)
559        result = open(self.outfile, 'rb').read()
560        self.assertEqual(
561            result,
562            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
563            'mandatory,passmark,score,semester,title,student_id,certcode,'
564            'display_fullname\r\n'
565            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1,'
566            'Anna M. Tester\r\n'
567            )
568        return
569
570    def test_export_student(self):
571        # we can really export all course tickets of a certain student
572        self.setup_student(self.student)
573        exporter = CourseTicketExporter()
574        exporter.export_student(self.student, self.outfile)
575        result = open(self.outfile, 'rb').read()
576        self.assertEqual(
577            result,
578            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
579            'mandatory,passmark,score,semester,title,student_id,certcode,'
580            'display_fullname\r\n'
581            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1,'
582            'Anna M. Tester\r\n'
583            )
584        return
585
586    def test_export_filtered(self):
587        # we can export course tickets of a filtered set of students
588        self.setup_student(self.student)
589        self.app['students'].addStudent(self.student)
590        notify(grok.ObjectModifiedEvent(self.student))
591
592        exporter = CourseTicketExporter()
593        exporter.export_filtered(
594            self.student, self.outfile)
595        result = open(self.outfile, 'rb').read()
596        self.assertEqual(
597            result,
598            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
599            'mandatory,passmark,score,semester,title,student_id,certcode,'
600            'display_fullname\r\n'
601            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1,'
602            'Anna M. Tester\r\n'
603            )
604        # if the coursetickets catalog is used to filter students
605        # and (course) code is not None
606        # only course tickets which belong to this course are exported
607        exporter.export_filtered(
608            self.student, self.outfile, catalog='coursetickets', code='CRS1')
609        result = open(self.outfile, 'rb').read()
610        self.assertEqual(
611            result,
612            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
613            'mandatory,passmark,score,semester,title,student_id,certcode,'
614            'display_fullname\r\n'
615            '1,1,CRS1,100,DEP1,FAC1,100,2012,0,100,,2,Course 1,A111111,CERT1,'
616            'Anna M. Tester\r\n'
617            )
618        exporter.export_filtered(
619            self.student, self.outfile, catalog='coursetickets', code='any code')
620        result = open(self.outfile, 'rb').read()
621        self.assertEqual(
622            result,
623            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
624            'mandatory,passmark,score,semester,title,student_id,certcode,'
625            'display_fullname\r\n'
626            )
627        # Also tickets in probating levels are exported. Therefore
628        # we change the level attribute to fake a 110 level.
629        self.student['studycourse']['100'].level = 110
630        notify(grok.ObjectModifiedEvent(self.student['studycourse']['100']['CRS1']))
631        exporter.export_filtered(
632            self.student, self.outfile, catalog='coursetickets', code='CRS1', level='100')
633        result = open(self.outfile, 'rb').read()
634        self.assertEqual(
635            result,
636            'automatic,carry_over,code,credits,dcode,fcode,level,level_session,'
637            'mandatory,passmark,score,semester,title,student_id,certcode,'
638            'display_fullname\r\n'
639            '1,1,CRS1,100,DEP1,FAC1,110,2012,0,100,,2,Course 1,A111111,CERT1,'
640            'Anna M. Tester\r\n'
641            )
642        return
643
644class StudentPaymentsExporterTest(StudentImportExportSetup):
645
646    layer = FunctionalLayer
647
648    def setUp(self):
649        super(StudentPaymentsExporterTest, self).setUp()
650        self.setup_for_export()
651        return
652
653    def test_ifaces(self):
654        # make sure we fullfill interface contracts
655        obj = StudentPaymentsExporter()
656        verifyObject(ICSVStudentExporter, obj)
657        verifyClass(ICSVStudentExporter, StudentPaymentsExporter)
658        return
659
660    def test_get_as_utility(self):
661        # we can get a payments exporter as utility
662        result = queryUtility(ICSVExporter, name="studentpayments")
663        self.assertTrue(result is not None)
664        return
665
666    def test_export_empty(self):
667        # we can export a nearly empty payment
668        payment = StudentOnlinePayment()
669        payment.creation_date = datetime.datetime(2012, 4, 1, 13, 12, 1)
670        exporter = StudentPaymentsExporter()
671        exporter.export([payment], self.outfile)
672        result = open(self.outfile, 'rb').read()
673        self.assertEqual(
674            result,
675            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
676            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
677            'r_code,r_desc,student_id,state,current_session\r\n'
678
679            ',0.0,2012-04-01 13:12:01#,,1,,,,,unpaid,,0.0,,,,,\r\n'
680            )
681        return
682
683    def test_export(self):
684        # we can really export student payments.
685        # set values we can expect in export file
686        self.setup_student(self.student)
687        payment = self.student['payments']['my-payment']
688        exporter = StudentPaymentsExporter()
689        exporter.export([payment], self.outfile)
690        result = open(self.outfile, 'rb').read()
691        self.assertEqual(
692            result,
693            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
694            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
695            'r_code,r_desc,student_id,state,current_session\r\n'
696
697            '666,12.12,2012-04-01 13:12:01#,schoolfee,1,my-id,'
698            'p-item,100,2012,paid,2012-04-01 14:12:01#,12.12,'
699            'r-code,,A111111,created,2012\r\n'
700            )
701        return
702
703    def test_export_all(self):
704        # we can really export all payments
705        # set values we can expect in export file
706        self.setup_student(self.student)
707        exporter = StudentPaymentsExporter()
708        exporter.export_all(self.app, self.outfile)
709        result = open(self.outfile, 'rb').read()
710        self.assertEqual(
711            result,
712            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
713            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
714            'r_code,r_desc,student_id,state,current_session\r\n'
715
716            '666,12.12,2012-04-01 13:12:01#,schoolfee,1,my-id,'
717            'p-item,100,2012,paid,2012-04-01 14:12:01#,12.12,'
718            'r-code,,A111111,created,2012\r\n'
719            )
720        return
721
722    def test_export_student(self):
723        # we can really export all payments of a certain student
724        # set values we can expect in export file
725        self.setup_student(self.student)
726        exporter = StudentPaymentsExporter()
727        exporter.export_student(self.student, self.outfile)
728        result = open(self.outfile, 'rb').read()
729        self.assertEqual(
730            result,
731            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
732            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
733            'r_code,r_desc,student_id,state,current_session\r\n'
734
735            '666,12.12,2012-04-01 13:12:01#,schoolfee,1,my-id,'
736            'p-item,100,2012,paid,2012-04-01 14:12:01#,12.12,'
737            'r-code,,A111111,created,2012\r\n'
738            )
739        return
740
741    def test_export_filtered(self):
742        # we can export payments of a filtered set of students
743        self.setup_student(self.student)
744        self.app['students'].addStudent(self.student)
745        notify(grok.ObjectModifiedEvent(self.student))
746
747        exporter = StudentPaymentsExporter()
748        exporter.export_filtered(
749            self.student, self.outfile, current_level=200)
750        result = open(self.outfile, 'rb').read()
751        self.assertEqual(
752            result,
753            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
754            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
755            'r_code,r_desc,student_id,state,current_session\r\n'
756
757            '666,12.12,2012-04-01 13:12:01#,schoolfee,1,my-id,'
758            'p-item,100,2012,paid,2012-04-01 14:12:01#,12.12,'
759            'r-code,,A111111,created,2012\r\n'
760            )
761        return
762
763    def test_export_filtered_by_date(self):
764        # payments_start and payments_end are being ignored
765        self.setup_student(self.student)
766        self.app['students'].addStudent(self.student)
767        notify(grok.ObjectModifiedEvent(self.student))
768        exporter = StudentPaymentsExporter()
769        # A key xxx does not exist
770        self.assertRaises(
771            KeyError, exporter.export_filtered, self.app, self.outfile,
772            current_session=None,
773            current_level=None, xxx='nonsense')
774        # payments_start and payments_end do exist but must match format '%Y-%m-%d'
775        self.assertRaises(
776            ValueError, exporter.export_filtered, self.app, self.outfile,
777            current_session=None, current_level=None,
778            payments_start='nonsense', payments_end='nonsense')
779        # If they match the format they are ignored by get_filtered and the
780        # exporter works properly
781        exporter.export_filtered(
782            self.app, self.outfile,
783            current_session=None, current_level=None,
784            payments_start='01/04/2012', payments_end='02/04/2012')
785        result = open(self.outfile, 'rb').read()
786        self.assertEqual(
787            result,
788            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
789            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
790            'r_code,r_desc,student_id,state,current_session\r\n'
791
792            '666,12.12,2012-04-01 13:12:01#,schoolfee,1,my-id,'
793            'p-item,100,2012,paid,2012-04-01 14:12:01#,12.12,'
794            'r-code,,A111111,created,2012\r\n'
795            )
796        # no results if payment_date is outside the given period
797        exporter.export_filtered(
798            self.app, self.outfile,
799            current_session=None, current_level=None,
800            payments_start='31/03/2012', payments_end='01/04/2012')
801        result = open(self.outfile, 'rb').read()
802        self.assertEqual(
803            result,
804            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
805            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
806            'r_code,r_desc,student_id,state,current_session\r\n'
807            )
808        exporter.export_filtered(
809            self.app, self.outfile,
810            current_session=None, current_level=None,
811            payments_start='02/04/2012', payments_end='03/04/2012')
812        result = open(self.outfile, 'rb').read()
813        self.assertEqual(
814            result,
815            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
816            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
817            'r_code,r_desc,student_id,state,current_session\r\n'
818            )
819        # no results if payment_date is not set
820        self.payment.payment_date = None
821        exporter.export_filtered(
822            self.app, self.outfile,
823            current_session=None, current_level=None,
824            payments_start='01/04/2012', payments_end='02/04/2012')
825        result = open(self.outfile, 'rb').read()
826       
827        return
828
829class BursaryDataExporterTest(StudentImportExportSetup):
830
831    layer = FunctionalLayer
832
833    def setUp(self):
834        super(BursaryDataExporterTest, self).setUp()
835        self.setup_for_export()
836        return
837
838    def test_export_all(self):
839        # we can really export all payments
840        # set values we can expect in export file
841        self.setup_student(self.student)
842        exporter = DataForBursaryExporter()
843        exporter.export_all(self.app, self.outfile)
844        result = open(self.outfile, 'rb').read()
845        self.assertEqual(
846            result,
847            'ac,amount_auth,creation_date,p_category,p_current,p_id,p_item,'
848            'p_level,p_session,p_state,payment_date,r_amount_approved,r_code,'
849            'r_desc,student_id,matric_number,reg_number,firstname,middlename,lastname,'
850            'state,current_session,entry_session,entry_mode,faccode,depcode,certcode\r\n'
851
852            '666,12.12,2012-04-01 13:12:01#,schoolfee,1,my-id,p-item,100,2012,'
853            'paid,2012-04-01 14:12:01#,12.12,r-code,,A111111,234,123,'
854            'Anna,M.,Tester,created,2012,2010,ug_ft,NA,NA,CERT1\r\n'
855            )
856        return
857
858class BedTicketsExporterTest(StudentImportExportSetup):
859
860    layer = FunctionalLayer
861
862    def setUp(self):
863        super(BedTicketsExporterTest, self).setUp()
864        self.setup_for_export()
865        return
866
867    def test_ifaces(self):
868        # make sure we fullfill interface contracts
869        obj = BedTicketsExporter()
870        verifyObject(ICSVStudentExporter, obj)
871        verifyClass(ICSVStudentExporter, BedTicketsExporter)
872        return
873
874    def test_get_as_utility(self):
875        # we can get a bedtickets exporter as utility
876        result = queryUtility(ICSVExporter, name="bedtickets")
877        self.assertTrue(result is not None)
878        return
879
880    def test_export_empty(self):
881        # we can export a nearly empty bedticket
882        bedticket = BedTicket()
883        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
884        bedticket.bed = bed
885        exporter = BedTicketsExporter()
886        exporter.export([bedticket], self.outfile)
887        result = open(self.outfile, 'rb').read()
888        self.assertMatches(
889            result,
890            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
891            'booking_session,student_id,actual_bed_type\r\n'
892            'hall-1_A_101_A,,,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>#,,,regular_male_fr\r\n'
893            )
894        return
895
896    def test_export(self):
897        # we can really export student bedtickets.
898        # set values we can expect in export file
899        self.setup_student(self.student)
900        bedticket = self.student['accommodation']['2004']
901        exporter = BedTicketsExporter()
902        exporter.export([bedticket], self.outfile)
903        result = open(self.outfile, 'rb').read()
904        self.assertMatches(
905            result,
906            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
907            'booking_session,student_id,actual_bed_type\r\n'
908            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>#,2004,'
909            'A111111,regular_male_fr\r\n'
910            )
911        return
912
913    def test_export_all(self):
914        # we can really export all bedtickets
915        # set values we can expect in export file
916        self.setup_student(self.student)
917        exporter = BedTicketsExporter()
918        exporter.export_all(self.app, self.outfile)
919        result = open(self.outfile, 'rb').read()
920        self.assertMatches(
921            result,
922            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
923            'booking_session,student_id,actual_bed_type\r\n'
924            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>#,2004,'
925            'A111111,regular_male_fr\r\n'
926            )
927        return
928
929    def test_export_student(self):
930        # we can really export all bedtickets of a certain student
931        # set values we can expect in export file
932        self.setup_student(self.student)
933        exporter = BedTicketsExporter()
934        exporter.export_student(self.student, self.outfile)
935        result = open(self.outfile, 'rb').read()
936        self.assertMatches(
937            result,
938            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
939            'booking_session,student_id,actual_bed_type\r\n'
940            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>#,2004,'
941            'A111111,regular_male_fr\r\n'
942            )
943        return
944
945    def test_export_filtered(self):
946        # we can export payments of a filtered set of students
947        self.setup_student(self.student)
948        self.app['students'].addStudent(self.student)
949        notify(grok.ObjectModifiedEvent(self.student))
950
951        exporter = BedTicketsExporter()
952        exporter.export_filtered(
953            self.student, self.outfile, current_level=200)
954        result = open(self.outfile, 'rb').read()
955        self.assertMatches(
956            result,
957            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
958            'booking_session,student_id,actual_bed_type\r\n'
959            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>#,'
960            '2004,A111111,regular_male_fr\r\n')
961        return
962
963
964class StudentPaymentsOverviewExporterTest(StudentImportExportSetup):
965
966    layer = FunctionalLayer
967
968    def setUp(self):
969        super(StudentPaymentsOverviewExporterTest, self).setUp()
970        self.setup_for_export()
971        return
972
973    def test_ifaces(self):
974        # make sure we fullfill interface contracts
975        obj = StudentPaymentsOverviewExporter()
976        verifyObject(ICSVStudentExporter, obj)
977        verifyClass(ICSVStudentExporter, StudentPaymentsOverviewExporter)
978        return
979
980    def test_get_as_utility(self):
981        # we can get a payments exporter as utility
982        result = queryUtility(ICSVExporter, name="paymentsoverview")
983        self.assertTrue(result is not None)
984        return
985
986    def test_export(self):
987        self.setup_student(self.student)
988        exporter = StudentPaymentsOverviewExporter()
989        exporter.export([self.student], self.outfile)
990        result = open(self.outfile, 'rb').read()
991        self.assertTrue(
992            'student_id,matric_number,display_fullname,state,certcode,'
993            'faccode,depcode,is_postgrad,'
994            'current_level,current_session,current_mode,'
995            '%s\r\n'
996            'A111111,234,Anna M. Tester,created,CERT1,NA,NA,0,200,2012,ug_ft,'
997            % year_range_str in result
998            )
999        return
1000
1001    def test_export_all(self):
1002        # we can really export students
1003        # set values we can expect in export file
1004        self.setup_student(self.student)
1005        # We add successful payments.
1006        payment_2 = StudentOnlinePayment()
1007        payment_2.p_id = 'my-id'
1008        payment_2.p_session = curr_year - 5
1009        payment_2.amount_auth = 13.13
1010        payment_2.p_state = u'paid'
1011        payment_2.p_category = u'schoolfee'
1012        self.student['payments']['my-2ndpayment'] = payment_2
1013        # This one could be a balance payment.
1014        # The amount is being added.
1015        payment_3 = StudentOnlinePayment()
1016        payment_3.p_id = 'my-id_2'
1017        payment_3.p_session = curr_year - 5
1018        payment_3.amount_auth = 1.01
1019        payment_3.p_state = u'paid'
1020        payment_3.p_category = u'schoolfee'
1021        self.student['payments']['my-3rdpayment'] = payment_3
1022        exporter = StudentPaymentsOverviewExporter()
1023        exporter.export_all(self.app, self.outfile)
1024        result = open(self.outfile, 'rb').read()
1025        self.assertTrue(
1026            'student_id,matric_number,display_fullname,state,'
1027            'certcode,faccode,depcode,is_postgrad,'
1028            'current_level,current_session,current_mode,'
1029            '%s\r\nA111111,234,Anna M. Tester,created,CERT1,NA,NA,0,'
1030            '200,2012,ug_ft,,,,,14.14,,,12.12,,\r\n'
1031            % year_range_str in result
1032            )
1033        return
1034
1035class StudentStudyLevelsOverviewExporterTest(StudentImportExportSetup):
1036
1037    layer = FunctionalLayer
1038
1039    def setUp(self):
1040        super(StudentStudyLevelsOverviewExporterTest, self).setUp()
1041        self.setup_for_export()
1042        return
1043
1044    def test_ifaces(self):
1045        obj = StudentStudyLevelsOverviewExporter()
1046        verifyObject(ICSVStudentExporter, obj)
1047        verifyClass(ICSVStudentExporter, StudentStudyLevelsOverviewExporter)
1048        return
1049
1050    def test_get_as_utility(self):
1051        result = queryUtility(ICSVExporter, name="studylevelsoverview")
1052        self.assertTrue(result is not None)
1053        return
1054
1055    def test_export(self):
1056        self.setup_student(self.student)
1057        exporter = StudentStudyLevelsOverviewExporter()
1058        exporter.export([self.student], self.outfile)
1059        result = open(self.outfile, 'rb').read()
1060        self.assertEqual(
1061             'student_id,state,certcode,faccode,depcode,is_postgrad,'
1062             'entry_session,current_level,current_session,'
1063             '10,100,110,120,200,210,220,300,310,320,400,410,420,500,'
1064             '510,520,600,610,620,700,710,720,800,810,820,900,910,920,999\r\n'
1065             'A111111,created,CERT1,NA,NA,0,2010,200,2012,,2012'
1066             ',,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n',
1067            result
1068            )
1069        return
1070
1071    def test_export_all(self):
1072        self.setup_student(self.student)
1073        exporter = StudentStudyLevelsOverviewExporter()
1074        exporter.export_all(self.app, self.outfile)
1075        result = open(self.outfile, 'rb').read()
1076        self.assertEqual(
1077            'student_id,state,certcode,faccode,depcode,is_postgrad,'
1078            'entry_session,current_level,current_session,'
1079            '10,100,110,120,200,210,220,300,310,320,400,410,420,500,'
1080            '510,520,600,610,620,700,710,720,800,810,820,900,910,920,999\r\n'
1081            'A111111,created,CERT1,NA,NA,0,2010,200,2012,,2012'
1082            ',,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n',
1083            result
1084            )
1085        return
1086
1087class ComboCardExporterTest(StudentImportExportSetup):
1088
1089    layer = FunctionalLayer
1090
1091    def setUp(self):
1092        super(ComboCardExporterTest, self).setUp()
1093        self.setup_for_export()
1094        return
1095
1096    def create_passport_img(self, student):
1097        # create some passport file for `student`
1098        storage = getUtility(IExtFileStore)
1099        image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
1100        self.image_contents = open(image_path, 'rb').read()
1101        file_id = IFileStoreNameChooser(student).chooseName(
1102            attr='passport.jpg')
1103        storage.createFile(file_id, StringIO(self.image_contents))
1104
1105    def test_export_all(self):
1106        self.setup_student(self.student)
1107        self.create_passport_img(self.student)
1108        exporter = ComboCardDataExporter()
1109        exporter.export_all(self.app, self.outfile)
1110        result = open(self.outfile, 'rb').read()
1111        self.assertTrue(
1112            'display_fullname,student_id,matric_number,certificate,faculty,'
1113            'department,passport_path\r\nAnna M. Tester,A111111,234,'
1114            'Unnamed Certificate,Faculty of Unnamed Faculty (NA),'
1115            'Department of Unnamed Department (NA),'
1116            'students/00110/A111111/passport_A111111.jpg\r\n'
1117            in result
1118            )
1119        return
Note: See TracBrowser for help on using the repository browser.