Ignore:
Timestamp:
10 Feb 2012, 12:08:47 (13 years ago)
Author:
Henrik Bettermann
Message:

Implement first version of payment importer.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/students/tests
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_batching.py

    r7548 r7623  
    2323import unittest
    2424import datetime
     25from time import time
    2526from zope.component import createObject
    2627from zope.component.hooks import setSite, clearSite
     
    3233from waeup.sirp.students.batching import (
    3334    StudentProcessor, StudentStudyCourseProcessor,
    34     StudentStudyLevelProcessor, CourseTicketProcessor)
     35    StudentStudyLevelProcessor, CourseTicketProcessor,
     36    StudentOnlinePaymentProcessor)
    3537from waeup.sirp.students.student import Student
    3638from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
     
    8486
    8587COURSETICKET_HEADER_FIELDS = COURSETICKET_SAMPLE_DATA.split(
     88    '\n')[0].split(',')
     89
     90PAYMENT_SAMPLE_DATA = open(
     91    os.path.join(os.path.dirname(__file__), 'sample_payment_data.csv'),
     92    'rb').read()
     93
     94PAYMENT_HEADER_FIELDS = PAYMENT_SAMPLE_DATA.split(
    8695    '\n')[0].split(',')
    8796
     
    524533        shutil.rmtree(os.path.dirname(fin_file))
    525534
     535class PaymentImporterTest(FunctionalTestCase):
     536
     537    layer = FunctionalLayer
     538
     539    def setUp(self):
     540        super(PaymentImporterTest, self).setUp()
     541        self.dc_root = tempfile.mkdtemp()
     542        self.workdir = tempfile.mkdtemp()
     543        app = University()
     544        app['datacenter'].setStoragePath(self.dc_root)
     545        self.getRootFolder()['app'] = app
     546        self.app = self.getRootFolder()['app']
     547        setSite(app)
     548
     549        # Add student with payment
     550        student = Student()
     551        student.firstname = u'Anna'
     552        student.lastname = u'Tester'
     553        student.reg_number = u'123'
     554        student.matric_number = u'234'
     555        self.app['students'].addStudent(student)
     556        self.student = self.app['students'][student.student_id]
     557        payment = createObject(u'waeup.StudentOnlinePayment')
     558        payment.p_id = 'p123'
     559        self.student['payments'][payment.p_id] = payment
     560
     561        # Import students with subobjects
     562        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
     563        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
     564        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
     565            student_file, STUDENT_HEADER_FIELDS)
     566        shutil.rmtree(os.path.dirname(fin_file))
     567
     568        self.importer = StudentOnlinePaymentProcessor()
     569        self.csv_file = os.path.join(
     570            self.workdir, 'sample_payment_data.csv')
     571        open(self.csv_file, 'wb').write(PAYMENT_SAMPLE_DATA)
     572
     573    def tearDown(self):
     574        super(PaymentImporterTest, self).tearDown()
     575        shutil.rmtree(self.workdir)
     576        shutil.rmtree(self.dc_root)
     577        clearSite()
     578        return
     579
     580    def test_interface(self):
     581        # Make sure we fulfill the interface contracts.
     582        assert verifyObject(IBatchProcessor, self.importer) is True
     583        assert verifyClass(
     584            IBatchProcessor, StudentOnlinePaymentProcessor) is True
     585
     586    def test_getEntry(self):
     587        assert self.importer.getEntry(
     588            dict(student_id='ID_NONE', p_id='nonsense'), self.app) is None
     589        assert self.importer.getEntry(
     590            dict(student_id=self.student.student_id, p_id='p123'),
     591            self.app) is self.student['payments']['p123']
     592        assert self.importer.getEntry(
     593            dict(student_id=self.student.student_id, p_id='XXXXXX123'),
     594            self.app) is self.student['payments']['p123']
     595
     596    def test_addEntry(self):
     597        self.assertEqual(len(self.student['payments'].keys()),1)
     598        payment1 = createObject(u'waeup.StudentOnlinePayment')
     599        payment1.p_id = 'p234'
     600        self.importer.addEntry(
     601            payment1, dict(student_id=self.student.student_id, p_id='p234'),
     602            self.app)
     603        self.assertEqual(len(self.student['payments'].keys()),2)
     604        self.assertEqual(self.student['payments']['p234'].p_id, 'p234')
     605        payment2 = createObject(u'waeup.StudentOnlinePayment')
     606        payment1.p_id = 'nonsense'
     607        # payment1.p_id will be replaced if p_id doesn't start with 'p'
     608        self.importer.addEntry(
     609            payment2, dict(student_id=self.student.student_id, p_id='XXXXXX456'),
     610            self.app)
     611        self.assertEqual(len(self.student['payments'].keys()),3)
     612        self.assertEqual(self.student['payments']['p456'].p_id, 'p456')
     613
     614    def test_checkConversion(self):
     615        errs, inv_errs, conv_dict = self.importer.checkConversion(
     616            dict(reg_number='1', p_id='3816951266236341955'))
     617        self.assertEqual(len(errs),0)
     618        errs, inv_errs, conv_dict = self.importer.checkConversion(
     619            dict(reg_number='1', p_id='p1266236341955'))
     620        self.assertEqual(len(errs),0)
     621        errs, inv_errs, conv_dict = self.importer.checkConversion(
     622            dict(reg_number='1', p_id='nonsense'))
     623        self.assertEqual(len(errs),1)
     624        timestamp = "%d" % int(time()*1000)
     625        p_id = "p%s" % timestamp
     626        errs, inv_errs, conv_dict = self.importer.checkConversion(
     627            dict(reg_number='1', p_id=p_id))
     628        self.assertEqual(len(errs),0)
     629
     630    def test_import(self):
     631        num, num_warns, fin_file, fail_file = self.importer.doImport(
     632            self.csv_file, PAYMENT_HEADER_FIELDS,'create')
     633        self.assertEqual(num_warns,0)
     634        payment = self.importer.getEntry(dict(reg_number='1',
     635            p_id='p1290797973744'), self.app)
     636        self.assertEqual(payment.p_id, 'p1290797973744')
     637        cdate = payment.creation_date.strftime("%Y-%m-%d %H:%M:%S")
     638        self.assertEqual(cdate, "2010-11-26 19:59:33")
     639        shutil.rmtree(os.path.dirname(fin_file))
    526640
    527641def test_suite():
     
    529643    for testcase in [
    530644        StudentImporterTest,StudentStudyCourseImporterTest,
    531         StudentStudyLevelImporterTest,CourseTicketImporterTest,]:
     645        StudentStudyLevelImporterTest,CourseTicketImporterTest,
     646        PaymentImporterTest,]:
    532647        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
    533648                testcase
Note: See TracChangeset for help on using the changeset viewer.