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

Last change on this file since 9736 was 9734, checked in by uli, 12 years ago

Try to make clearer where we head to regarding filtered exports.

  • Property svn:keywords set to Id
File size: 24.0 KB
Line 
1import grok
2import datetime
3from zope.component import queryUtility
4from zope.event import notify
5from zope.interface.verify import verifyObject, verifyClass
6from waeup.kofa.interfaces import ICSVExporter
7from waeup.kofa.students.export import (
8    StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,
9    CourseTicketExporter, PaymentsExporter, BedTicketsExporter,
10    StudentPaymentsOverviewExporter, get_students, StudentsExportFilter)
11from waeup.kofa.students.accommodation import BedTicket
12from waeup.kofa.students.interfaces import ICSVStudentExporter
13from waeup.kofa.students.payments import StudentOnlinePayment
14from waeup.kofa.students.student import Student
15from waeup.kofa.students.studycourse import StudentStudyCourse
16from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
17from waeup.kofa.students.tests.test_batching import StudentImportExportSetup
18from waeup.kofa.testing import FunctionalLayer
19
20curr_year = datetime.datetime.now().year
21year_range = range(curr_year - 9, curr_year + 1)
22year_range_str = ','.join([str(i) for i in year_range])
23
24class ExportHelperTests(StudentImportExportSetup):
25    layer = FunctionalLayer
26    def setUp(self):
27        super(ExportHelperTests, self).setUp()
28        student = Student()
29        self.app['students'].addStudent(student)
30        student = self.setup_student(student)
31        notify(grok.ObjectModifiedEvent(student))
32        self.student = self.app['students'][student.student_id]
33        return
34
35    def test_get_students_plain(self):
36        # without a filter we get all students
37        result = get_students(self.app)
38        self.assertEqual(len(list(result)), 1)
39        return
40
41    def test_get_students_by_session(self):
42        # we can filter out students of a certain session
43        my_filter1 = StudentsExportFilter(session=2012)
44        result = get_students(self.app, stud_filter=my_filter1)
45        self.assertEqual(len(list(result)), 1)
46
47        my_filter2 = StudentsExportFilter(session=1964)
48        result = get_students(self.app, stud_filter=my_filter2)
49        self.assertEqual(len(list(result)), 0)
50        return
51
52    def test_get_students_by_level(self):
53        # we can filter out students of a certain level
54        my_filter1 = StudentsExportFilter(level=200)
55        result = get_students(self.app, stud_filter=my_filter1)
56        self.assertEqual(len(list(result)), 1)
57
58        my_filter2 = StudentsExportFilter(level=300)
59        result = get_students(self.app, stud_filter=my_filter2)
60        self.assertEqual(len(list(result)), 0)
61        return
62
63class StudentsExporterTest(StudentImportExportSetup):
64
65    layer = FunctionalLayer
66
67    def setUp(self):
68        super(StudentsExporterTest, self).setUp()
69        self.setup_for_export()
70        return
71
72    def test_ifaces(self):
73        # make sure we fullfill interface contracts
74        obj = StudentsExporter()
75        verifyObject(ICSVStudentExporter, obj)
76        verifyClass(ICSVStudentExporter, StudentsExporter)
77        return
78
79    def test_get_as_utility(self):
80        # we can get an student exporter as utility
81        result = queryUtility(ICSVExporter, name="students")
82        self.assertTrue(result is not None)
83        return
84
85    def test_export(self):
86        # we can really export students
87        # set values we can expect in export file
88        self.setup_student(self.student)
89        exporter = StudentsExporter()
90        exporter.export([self.student], self.outfile)
91        result = open(self.outfile, 'rb').read()
92        self.assertTrue(
93            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
94            'firstname,lastname,matric_number,middlename,nationality,officer_comment,'
95            'perm_address,personal_updated,'
96            'phone,reg_number,sex,student_id,suspended,suspended_comment,password,'
97            'state,history,certcode,is_postgrad,current_level,current_session\r\n'
98
99            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
100            'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
101            '+234-123-12345#,123,f,A111111,0,,,created'
102            in result
103            )
104        return
105
106    def test_export_all(self):
107        # we can really export students
108        # set values we can expect in export file
109        self.setup_student(self.student)
110        exporter = StudentsExporter()
111        exporter.export_all(self.app, self.outfile)
112        result = open(self.outfile, 'rb').read()
113        self.assertTrue(
114            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
115            'firstname,lastname,matric_number,middlename,nationality,officer_comment,'
116            'perm_address,personal_updated'
117            ',phone,reg_number,sex,student_id,suspended,suspended_comment,password,'
118            'state,history,certcode,is_postgrad,current_level,current_session\r\n'
119
120            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
121            'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
122            '+234-123-12345#,123,f,A111111,0,,,created'
123            in result
124            )
125        return
126
127    def test_export_student(self):
128        # we can export a single student
129        self.setup_student(self.student)
130        exporter = StudentsExporter()
131        exporter.export_student(self.student, self.outfile)
132        result = open(self.outfile, 'rb').read()
133        self.assertTrue(
134            'adm_code,clearance_locked,clr_code,date_of_birth,email,employer,'
135            'firstname,lastname,matric_number,middlename,nationality,officer_comment,'
136            'perm_address,personal_updated'
137            ',phone,reg_number,sex,student_id,suspended,suspended_comment,password,'
138            'state,history,certcode,is_postgrad,current_level,current_session\r\n'
139
140            'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
141            'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
142            '+234-123-12345#,123,f,A111111,0,,,created'
143            in result
144            )
145        return
146
147
148class StudentStudyCourseExporterTest(StudentImportExportSetup):
149
150    layer = FunctionalLayer
151
152    def setUp(self):
153        super(StudentStudyCourseExporterTest, self).setUp()
154        self.setup_for_export()
155        return
156
157    def test_ifaces(self):
158        # make sure we fullfill interface contracts
159        obj = StudentStudyCourseExporter()
160        verifyObject(ICSVExporter, obj)
161        verifyClass(ICSVExporter, StudentStudyCourseExporter)
162        return
163
164    def test_get_as_utility(self):
165        # we can get an student exporter as utility
166        result = queryUtility(ICSVExporter, name="studentstudycourses")
167        self.assertTrue(result is not None)
168        return
169
170    def test_export_empty(self):
171        # we can export a nearly empty study course
172        study_course = StudentStudyCourse()
173        exporter = StudentStudyCourseExporter()
174        exporter.export([study_course], self.outfile)
175        result = open(self.outfile, 'rb').read()
176        self.assertEqual(
177            result,
178            'certificate,current_level,current_session,current_verdict,'
179            'entry_mode,entry_session,previous_verdict,student_id\r\n'
180
181            ',,,0,,,0,\r\n'
182            )
183        return
184
185    def test_export(self):
186        # we can really export study courses.
187        # set values we can expect in export file
188        self.setup_student(self.student)
189        study_course = self.student.get('studycourse')
190        exporter = StudentStudyCourseExporter()
191        exporter.export([study_course], self.outfile)
192        result = open(self.outfile, 'rb').read()
193        self.assertEqual(
194            result,
195            'certificate,current_level,current_session,current_verdict,'
196            'entry_mode,entry_session,previous_verdict,student_id\r\n'
197
198            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
199            )
200        return
201
202    def test_export_all(self):
203        # we can really export students
204        # set values we can expect in export file
205        self.setup_student(self.student)
206        exporter = StudentStudyCourseExporter()
207        exporter.export_all(self.app, self.outfile)
208        result = open(self.outfile, 'rb').read()
209        self.assertEqual(
210            result,
211            'certificate,current_level,current_session,current_verdict,'
212            'entry_mode,entry_session,previous_verdict,student_id\r\n'
213
214            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
215            )
216        return
217
218    def test_export_student(self):
219        # we can export studycourse of a certain student
220        self.setup_student(self.student)
221        exporter = StudentStudyCourseExporter()
222        exporter.export_student(self.student, self.outfile)
223        result = open(self.outfile, 'rb').read()
224        self.assertEqual(
225            result,
226            'certificate,current_level,current_session,current_verdict,'
227            'entry_mode,entry_session,previous_verdict,student_id\r\n'
228
229            'CERT1,200,2012,0,ug_ft,2010,0,A111111\r\n'
230            )
231        return
232
233class StudentStudyLevelExporterTest(StudentImportExportSetup):
234
235    layer = FunctionalLayer
236
237    def setUp(self):
238        super(StudentStudyLevelExporterTest, self).setUp()
239        self.setup_for_export()
240        return
241
242    def test_ifaces(self):
243        # make sure we fullfill interface contracts
244        obj = StudentStudyLevelExporter()
245        verifyObject(ICSVExporter, obj)
246        verifyClass(ICSVExporter, StudentStudyLevelExporter)
247        return
248
249    def test_get_as_utility(self):
250        # we can get an student exporter as utility
251        result = queryUtility(ICSVExporter, name="studentstudylevels")
252        self.assertTrue(result is not None)
253        return
254
255    def test_export_empty(self):
256        # we can export a nearly empty study level
257        study_level = StudentStudyLevel()
258        exporter = StudentStudyLevelExporter()
259        exporter.export([study_level], self.outfile)
260        result = open(self.outfile, 'rb').read()
261        self.assertEqual(
262            result,
263            'gpa,level,level_session,level_verdict,total_credits,'
264            'validated_by,validation_date,'
265            'student_id,number_of_tickets,certcode\r\n'
266            '0.0,,,0,0,,,,0,\r\n'
267            )
268        return
269
270    def test_export(self):
271        # we can really export study levels.
272        # set values we can expect in export file
273        self.setup_student(self.student)
274        study_course = self.student.get('studycourse')
275        study_level = study_course[study_course.keys()[0]]
276        exporter = StudentStudyLevelExporter()
277        exporter.export([study_level], self.outfile)
278        result = open(self.outfile, 'rb').read()
279        self.assertEqual(
280            result,
281            'gpa,level,level_session,level_verdict,total_credits,'
282            'validated_by,validation_date,'
283            'student_id,number_of_tickets,certcode\r\n'
284            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
285            )
286        return
287
288    def test_export_all(self):
289        # we can really export study levels
290        # set values we can expect in export file
291        self.setup_student(self.student)
292        exporter = StudentStudyLevelExporter()
293        exporter.export_all(self.app, self.outfile)
294        result = open(self.outfile, 'rb').read()
295        self.assertEqual(
296            result,
297            'gpa,level,level_session,level_verdict,total_credits,'
298            'validated_by,validation_date,'
299            'student_id,number_of_tickets,certcode\r\n'
300            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
301            )
302        return
303
304    def test_export_student(self):
305        # we can really export study levels of a certain student
306        self.setup_student(self.student)
307        exporter = StudentStudyLevelExporter()
308        exporter.export_student(self.student, self.outfile)
309        result = open(self.outfile, 'rb').read()
310        self.assertEqual(
311            result,
312            'gpa,level,level_session,level_verdict,total_credits,'
313            'validated_by,validation_date,'
314            'student_id,number_of_tickets,certcode\r\n'
315            '0.0,100,2012,A,100,,,A111111,1,CERT1\r\n'
316            )
317        return
318
319class CourseTicketExporterTest(StudentImportExportSetup):
320
321    layer = FunctionalLayer
322
323    def setUp(self):
324        super(CourseTicketExporterTest, self).setUp()
325        self.setup_for_export()
326        return
327
328    def test_ifaces(self):
329        # make sure we fullfill interface contracts
330        obj = CourseTicketExporter()
331        verifyObject(ICSVExporter, obj)
332        verifyClass(ICSVExporter, CourseTicketExporter)
333        return
334
335    def test_get_as_utility(self):
336        # we can get an student exporter as utility
337        result = queryUtility(ICSVExporter, name="coursetickets")
338        self.assertTrue(result is not None)
339        return
340
341    def test_export_empty(self):
342        # we can export a nearly empty course ticket
343        ticket = CourseTicket()
344        exporter = CourseTicketExporter()
345        exporter.export([ticket], self.outfile)
346        result = open(self.outfile, 'rb').read()
347        self.assertEqual(
348            result,
349            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
350            'passmark,score,semester,title,student_id,certcode\r\n'
351            '0,0,,,,,,0,,,,,,\r\n'
352            )
353        return
354
355    def test_export(self):
356        # we can really export course tickets.
357        # set values we can expect in export file
358        self.setup_student(self.student)
359        study_course = self.student.get('studycourse')
360        study_level = study_course[study_course.keys()[0]]
361        ticket = study_level['CRS1']
362        exporter = CourseTicketExporter()
363        exporter.export([ticket], self.outfile)
364        result = open(self.outfile, 'rb').read()
365        self.assertEqual(
366            result,
367            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
368            'passmark,score,semester,title,student_id,certcode\r\n'
369            '1,1,CRS1,100,DEP1,FAC1,100,0,100,,2,Course 1,A111111,CERT1\r\n'
370            )
371        return
372
373    def test_export_all(self):
374        # we can really export all course tickets
375        # set values we can expect in export file
376        self.setup_student(self.student)
377        exporter = CourseTicketExporter()
378        exporter.export_all(self.app, self.outfile)
379        result = open(self.outfile, 'rb').read()
380        self.assertEqual(
381            result,
382            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
383            'passmark,score,semester,title,student_id,certcode\r\n'
384            '1,1,CRS1,100,DEP1,FAC1,100,0,100,,2,Course 1,A111111,CERT1\r\n'
385            )
386        return
387
388    def test_export_student(self):
389        # we can really export all course tickets of a certain student
390        self.setup_student(self.student)
391        exporter = CourseTicketExporter()
392        exporter.export_student(self.student, self.outfile)
393        result = open(self.outfile, 'rb').read()
394        self.assertEqual(
395            result,
396            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
397            'passmark,score,semester,title,student_id,certcode\r\n'
398            '1,1,CRS1,100,DEP1,FAC1,100,0,100,,2,Course 1,A111111,CERT1\r\n'
399            )
400        return
401
402class PaymentsExporterTest(StudentImportExportSetup):
403
404    layer = FunctionalLayer
405
406    def setUp(self):
407        super(PaymentsExporterTest, self).setUp()
408        self.setup_for_export()
409        return
410
411    def test_ifaces(self):
412        # make sure we fullfill interface contracts
413        obj = PaymentsExporter()
414        verifyObject(ICSVExporter, obj)
415        verifyClass(ICSVExporter, PaymentsExporter)
416        return
417
418    def test_get_as_utility(self):
419        # we can get a payments exporter as utility
420        result = queryUtility(ICSVExporter, name="studentpayments")
421        self.assertTrue(result is not None)
422        return
423
424    def test_export_empty(self):
425        # we can export a nearly empty payment
426        payment = StudentOnlinePayment()
427        payment.creation_date = datetime.datetime(2012, 4, 1, 13, 12, 1)
428        exporter = PaymentsExporter()
429        exporter.export([payment], self.outfile)
430        result = open(self.outfile, 'rb').read()
431        self.assertEqual(
432            result,
433            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
434            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
435            'r_code,r_desc,student_id,student_state,current_session\r\n'
436
437            ',0.0,2012-04-01 13:12:01,schoolfee,1,,,,,unpaid,,0.0,,,,,\r\n'
438            )
439        return
440
441    def test_export(self):
442        # we can really export student payments.
443        # set values we can expect in export file
444        self.setup_student(self.student)
445        payment = self.student['payments']['my-payment']
446        exporter = PaymentsExporter()
447        exporter.export([payment], self.outfile)
448        result = open(self.outfile, 'rb').read()
449        self.assertEqual(
450            result,
451            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
452            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
453            'r_code,r_desc,student_id,student_state,current_session\r\n'
454
455            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
456            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
457            'r-code,,A111111,created,2012\r\n'
458            )
459        return
460
461    def test_export_all(self):
462        # we can really export all payments
463        # set values we can expect in export file
464        self.setup_student(self.student)
465        exporter = PaymentsExporter()
466        exporter.export_all(self.app, self.outfile)
467        result = open(self.outfile, 'rb').read()
468        self.assertEqual(
469            result,
470            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
471            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
472            'r_code,r_desc,student_id,student_state,current_session\r\n'
473
474            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
475            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
476            'r-code,,A111111,created,2012\r\n'
477            )
478        return
479
480    def test_export_student(self):
481        # we can really export all payments of a certain student
482        # set values we can expect in export file
483        self.setup_student(self.student)
484        exporter = PaymentsExporter()
485        exporter.export_student(self.student, self.outfile)
486        result = open(self.outfile, 'rb').read()
487        self.assertEqual(
488            result,
489            'ac,amount_auth,creation_date,p_category,p_current,p_id,'
490            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
491            'r_code,r_desc,student_id,student_state,current_session\r\n'
492
493            '666,0.0,2012-04-01 13:12:01,schoolfee,1,my-id,'
494            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
495            'r-code,,A111111,created,2012\r\n'
496            )
497        return
498
499class BedTicketsExporterTest(StudentImportExportSetup):
500
501    layer = FunctionalLayer
502
503    def setUp(self):
504        super(BedTicketsExporterTest, self).setUp()
505        self.setup_for_export()
506        return
507
508    def test_ifaces(self):
509        # make sure we fullfill interface contracts
510        obj = BedTicketsExporter()
511        verifyObject(ICSVExporter, obj)
512        verifyClass(ICSVExporter, BedTicketsExporter)
513        return
514
515    def test_get_as_utility(self):
516        # we can get a bedtickets exporter as utility
517        result = queryUtility(ICSVExporter, name="bedtickets")
518        self.assertTrue(result is not None)
519        return
520
521    def test_export_empty(self):
522        # we can export a nearly empty bedticket
523        bedticket = BedTicket()
524        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
525        bedticket.bed = bed
526        exporter = BedTicketsExporter()
527        exporter.export([bedticket], self.outfile)
528        result = open(self.outfile, 'rb').read()
529        self.assertMatches(
530            result,
531            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
532            'booking_session,student_id,actual_bed_type\r\n'
533            'hall-1_A_101_A,,,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,,,regular_male_fr\r\n'
534            )
535        return
536
537    def test_export(self):
538        # we can really export student bedtickets.
539        # set values we can expect in export file
540        self.setup_student(self.student)
541        bedticket = self.student['accommodation']['2004']
542        exporter = BedTicketsExporter()
543        exporter.export([bedticket], self.outfile)
544        result = open(self.outfile, 'rb').read()
545        self.assertMatches(
546            result,
547            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
548            'booking_session,student_id,actual_bed_type\r\n'
549            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
550            'A111111,regular_male_fr\r\n'
551            )
552        return
553
554    def test_export_all(self):
555        # we can really export all bedtickets
556        # set values we can expect in export file
557        self.setup_student(self.student)
558        exporter = BedTicketsExporter()
559        exporter.export_all(self.app, self.outfile)
560        result = open(self.outfile, 'rb').read()
561        self.assertMatches(
562            result,
563            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
564            'booking_session,student_id,actual_bed_type\r\n'
565            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
566            'A111111,regular_male_fr\r\n'
567            )
568        return
569
570    def test_export_student(self):
571        # we can really export all bedtickets of a certain student
572        # set values we can expect in export file
573        self.setup_student(self.student)
574        exporter = BedTicketsExporter()
575        exporter.export_student(self.student, self.outfile)
576        result = open(self.outfile, 'rb').read()
577        self.assertMatches(
578            result,
579            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
580            'booking_session,student_id,actual_bed_type\r\n'
581            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
582            'A111111,regular_male_fr\r\n'
583            )
584        return
585
586class StudentPaymentsOverviewExporterTest(StudentImportExportSetup):
587
588    layer = FunctionalLayer
589
590    def setUp(self):
591        super(StudentPaymentsOverviewExporterTest, self).setUp()
592        self.setup_for_export()
593        return
594
595    def test_ifaces(self):
596        # make sure we fullfill interface contracts
597        obj = StudentPaymentsOverviewExporter()
598        verifyObject(ICSVExporter, obj)
599        verifyClass(ICSVExporter, StudentPaymentsOverviewExporter)
600        return
601
602    def test_get_as_utility(self):
603        # we can get a payments exporter as utility
604        result = queryUtility(ICSVExporter, name="paymentsoverview")
605        self.assertTrue(result is not None)
606        return
607
608    def test_export(self):
609        self.setup_student(self.student)
610        exporter = StudentPaymentsOverviewExporter()
611        exporter.export([self.student], self.outfile)
612        result = open(self.outfile, 'rb').read()
613        self.assertEqual(
614            'student_id,state,certcode,faccode,depcode,is_postgrad,'
615            'current_level,current_session,'
616            '%s\r\n'
617            'A111111,created,CERT1,NA,NA,0,200,2012,'
618            ',,,,,,,,,\r\n' % year_range_str,
619            result
620            )
621        return
622
623    def test_export_all(self):
624        # we can really export students
625        # set values we can expect in export file
626        self.setup_student(self.student)
627        # We add successful payments
628        payment = StudentOnlinePayment()
629        payment.p_id = 'my-id'
630        payment.p_session = 2009
631        payment.amount_auth = 12.12
632        payment.p_state = u'paid'
633        self.student['payments']['my-2ndpayment'] = payment
634        exporter = StudentPaymentsOverviewExporter()
635        exporter.export_all(self.app, self.outfile)
636        result = open(self.outfile, 'rb').read()
637        self.assertEqual(
638            'student_id,state,certcode,faccode,depcode,is_postgrad,'
639            'current_level,current_session,'
640            '%s\r\nA111111,created,CERT1,NA,NA,0,'
641            '200,2012,,,,,,,12.12,,,\r\n' % year_range_str,
642            result
643            )
644        return
Note: See TracBrowser for help on using the repository browser.