Ignore:
Timestamp:
4 May 2012, 11:52:16 (13 years ago)
Author:
uli
Message:
  • Avoid repetitive code using methods from ImportExportSetup?.
  • Add tests for course ticket exporter.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py

    r8070 r8341  
    1 import datetime
    2 import os
    3 from zope.catalog.interfaces import ICatalog
    4 from zope.component import queryUtility, getUtility
     1from zope.component import queryUtility
    52from zope.interface.verify import verifyObject, verifyClass
    6 from zope.intid.interfaces import IIntIds
    73from waeup.kofa.interfaces import ICSVExporter
    84from waeup.kofa.students.export import (
    9     StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,)
    10 from waeup.kofa.students.student import Student, StudentStudyCourse
    11 from waeup.kofa.students.studylevel import StudentStudyLevel
     5    StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,
     6    CourseTicketExporter)
     7from waeup.kofa.students.student import StudentStudyCourse
     8from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
    129from waeup.kofa.students.tests.test_batching import StudentImportExportSetup
    1310from waeup.kofa.testing import FunctionalLayer
     
    1916    def setUp(self):
    2017        super(StudentsExporterTest, self).setUp()
    21         student = Student()
    22         student.student_id = u'A111111'
    23         self.app['students'][student.student_id] = self.student = student
    24         self.outfile = os.path.join(self.workdir, 'myoutput.csv')
    25         self.cat = getUtility(ICatalog, name='students_catalog')
    26         self.intids = getUtility(IIntIds)
     18        self.setup_for_export()
    2719        return
    2820
     
    3931        self.assertTrue(result is not None)
    4032        return
    41 
    42     def setup_student(self, student):
    43         # set predictable values for `student`
    44         student.matric_number = u'M123456'
    45         student.adm_code = u'my adm code'
    46         student.clearance_locked = False
    47         student.clr_code = u'my clr code'
    48         student.perm_address = u'Studentroad 21\nLagos 123456\n'
    49         student.reg_number = u'123456'
    50         student.student_id = u'A111111'
    51         student.firstname = u'Anna'
    52         student.lastname = u'Tester'
    53         student.middlename = u'M.'
    54         student.date_of_birth = datetime.date(1981, 2, 4)
    55         student.sex = 'f'
    56         student.email = 'anna@sample.com'
    57         student.phone = u'+234-123-12345'
    58         student.notice = u'Some notice\nin lines.'
    59         student.nationality = u'NG'
    60         return student
    6133
    6234    def test_export(self):
     
    10476    def setUp(self):
    10577        super(StudentStudyCourseExporterTest, self).setUp()
    106         student = Student()
    107         student.student_id = u'A111111'
    108         self.app['students'][student.student_id] = self.student = student
    109         self.outfile = os.path.join(self.workdir, 'myoutput.csv')
    110         self.cat = getUtility(ICatalog, name='students_catalog')
    111         self.intids = getUtility(IIntIds)
    112         #self.study_course = StudyCourse()
     78        self.setup_for_export()
    11379        return
    11480
     
    12591        self.assertTrue(result is not None)
    12692        return
    127 
    128     def setup_student(self, student):
    129         # set predictable values for `student`
    130         student.matric_number = u'M123456'
    131         student.adm_code = u'my adm code'
    132         student.clearance_locked = False
    133         student.clr_code = u'my clr code'
    134         student.perm_address = u'Studentroad 21\nLagos 123456\n'
    135         student.reg_number = u'123456'
    136         student.student_id = u'A111111'
    137         student.firstname = u'Anna'
    138         student.lastname = u'Tester'
    139         student.middlename = u'M.'
    140         student.date_of_birth = datetime.date(1981, 2, 4)
    141         student.sex = 'f'
    142         student.email = 'anna@sample.com'
    143         student.phone = u'+234-123-12345'
    144         student.notice = u'Some notice\nin lines.'
    145 
    146         study_course = student.get('studycourse')
    147         student['studycourse'].certificate = self.certificate
    148         student['studycourse'].entry_mode = 'ug_ft'
    149         student['studycourse'].entry_session = 2010
    150         student['studycourse'].current_session = 2012
    151         student['studycourse'].current_level = int(self.certificate.start_level)
    152         return student
    15393
    15494    def test_export_empty(self):
     
    206146    def setUp(self):
    207147        super(StudentStudyLevelExporterTest, self).setUp()
    208         student = Student()
    209         student.student_id = u'A111111'
    210         self.app['students'][student.student_id] = self.student = student
    211         self.outfile = os.path.join(self.workdir, 'myoutput.csv')
    212         self.cat = getUtility(ICatalog, name='students_catalog')
    213         self.intids = getUtility(IIntIds)
    214         #self.study_level = StudyLevel()
     148        self.setup_for_export()
    215149        return
    216150
     
    227161        self.assertTrue(result is not None)
    228162        return
    229 
    230     def setup_student(self, student):
    231         # set predictable values for `student`
    232         student.matric_number = u'M123456'
    233         student.adm_code = u'my adm code'
    234         student.clearance_locked = False
    235         student.clr_code = u'my clr code'
    236         student.perm_address = u'Studentroad 21\nLagos 123456\n'
    237         student.reg_number = u'123456'
    238         student.student_id = u'A111111'
    239         student.firstname = u'Anna'
    240         student.lastname = u'Tester'
    241         student.middlename = u'M.'
    242         student.date_of_birth = datetime.date(1981, 2, 4)
    243         student.sex = 'f'
    244         student.email = 'anna@sample.com'
    245         student.phone = u'+234-123-12345'
    246         student.notice = u'Some notice\nin lines.'
    247 
    248         study_course = student.get('studycourse')
    249         student['studycourse'].certificate = self.certificate
    250         student['studycourse'].entry_mode = 'ug_ft'
    251         student['studycourse'].entry_session = 2010
    252         student['studycourse'].current_session = 2012
    253         student['studycourse'].current_level = int(self.certificate.start_level)
    254 
    255         study_level = StudentStudyLevel()
    256         study_level.level_session = 2012
    257         study_level.level_verdict = "A"
    258         study_level.level = 100
    259         student['studycourse'].addStudentStudyLevel(
    260             self.certificate, study_level)
    261         return student
    262163
    263164    def test_export_empty(self):
     
    303204            )
    304205        return
     206
     207class CourseTicketExporterTest(StudentImportExportSetup):
     208
     209    layer = FunctionalLayer
     210
     211    def setUp(self):
     212        super(CourseTicketExporterTest, self).setUp()
     213        self.setup_for_export()
     214        return
     215
     216    def test_ifaces(self):
     217        # make sure we fullfill interface contracts
     218        obj = CourseTicketExporter()
     219        verifyObject(ICSVExporter, obj)
     220        verifyClass(ICSVExporter, CourseTicketExporter)
     221        return
     222
     223    def test_get_as_utility(self):
     224        # we can get an student exporter as utility
     225        result = queryUtility(ICSVExporter, name="coursetickets")
     226        self.assertTrue(result is not None)
     227        return
     228
     229    def test_export_empty(self):
     230        # we can export a nearly empty course ticket
     231        ticket = CourseTicket()
     232        exporter = CourseTicketExporter()
     233        exporter.export([ticket], self.outfile)
     234        result = open(self.outfile, 'rb').read()
     235        self.assertEqual(
     236            result,
     237            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
     238            'matric_number,passmark,reg_number,score,semester,title\r\n'
     239            '0,0,,,,,,0,,,,0,,\r\n'
     240            )
     241        return
     242
     243    def test_export(self):
     244        # we can really export course tickets.
     245        # set values we can expect in export file
     246        self.setup_student(self.student)
     247        study_course = self.student.get('studycourse')
     248        study_level = study_course[study_course.keys()[0]]
     249        ticket = study_level['CRS1']
     250        exporter = CourseTicketExporter()
     251        exporter.export([ticket], self.outfile)
     252        result = open(self.outfile, 'rb').read()
     253        self.assertEqual(
     254            result,
     255            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
     256            'matric_number,passmark,reg_number,score,semester,title\r\n'
     257            '1,1,CRS1,100,DEP1,FAC1,100,0,M123456,100,123456,0,2,Course 1\r\n'
     258            )
     259        return
     260
     261    def test_export_all(self):
     262        # we can really export all course tickets
     263        # set values we can expect in export file
     264        self.setup_student(self.student)
     265        exporter = CourseTicketExporter()
     266        exporter.export_all(self.app, self.outfile)
     267        result = open(self.outfile, 'rb').read()
     268        self.assertEqual(
     269            result,
     270            'automatic,carry_over,code,credits,dcode,fcode,level,mandatory,'
     271            'matric_number,passmark,reg_number,score,semester,title\r\n'
     272            '1,1,CRS1,100,DEP1,FAC1,100,0,M123456,100,123456,0,2,Course 1\r\n'
     273            )
     274        return
Note: See TracChangeset for help on using the changeset viewer.