## $Id: test_batching.py 7665 2012-02-17 12:06:10Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""Unit tests for students-related data importers.
"""
import os
import shutil
import tempfile
import unittest
import datetime
from time import time
from zope.component import createObject
from zope.component.hooks import setSite, clearSite
from zope.interface.verify import verifyClass, verifyObject

from waeup.sirp.app import University
from waeup.sirp.university.faculty import Faculty
from waeup.sirp.university.department import Department
from waeup.sirp.students.batching import (
    StudentProcessor, StudentStudyCourseProcessor,
    StudentStudyLevelProcessor, CourseTicketProcessor,
    StudentOnlinePaymentProcessor)
from waeup.sirp.students.student import Student
from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
from waeup.sirp.interfaces import IBatchProcessor

STUDENT_SAMPLE_DATA = open(
    os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'),
    'rb').read()

STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split(
    '\n')[0].split(',')

STUDENT_SAMPLE_DATA_UPDATE = open(
    os.path.join(os.path.dirname(__file__), 'sample_student_data_update.csv'),
    'rb').read()

STUDENT_HEADER_FIELDS_UPDATE = STUDENT_SAMPLE_DATA_UPDATE.split(
    '\n')[0].split(',')

STUDENT_SAMPLE_DATA_UPDATE2 = open(
    os.path.join(os.path.dirname(__file__), 'sample_student_data_update2.csv'),
    'rb').read()

STUDENT_HEADER_FIELDS_UPDATE2 = STUDENT_SAMPLE_DATA_UPDATE2.split(
    '\n')[0].split(',')

STUDYCOURSE_SAMPLE_DATA = open(
    os.path.join(os.path.dirname(__file__), 'sample_studycourse_data.csv'),
    'rb').read()

STUDYCOURSE_HEADER_FIELDS = STUDYCOURSE_SAMPLE_DATA.split(
    '\n')[0].split(',')

STUDENT_SAMPLE_DATA_MIGRATION = open(
    os.path.join(os.path.dirname(__file__), 'sample_student_data_migration.csv'),
    'rb').read()

STUDENT_HEADER_FIELDS_MIGRATION = STUDENT_SAMPLE_DATA_MIGRATION.split(
    '\n')[0].split(',')

STUDYLEVEL_SAMPLE_DATA = open(
    os.path.join(os.path.dirname(__file__), 'sample_studylevel_data.csv'),
    'rb').read()

STUDYLEVEL_HEADER_FIELDS = STUDYLEVEL_SAMPLE_DATA.split(
    '\n')[0].split(',')

COURSETICKET_SAMPLE_DATA = open(
    os.path.join(os.path.dirname(__file__), 'sample_courseticket_data.csv'),
    'rb').read()

COURSETICKET_HEADER_FIELDS = COURSETICKET_SAMPLE_DATA.split(
    '\n')[0].split(',')

PAYMENT_SAMPLE_DATA = open(
    os.path.join(os.path.dirname(__file__), 'sample_payment_data.csv'),
    'rb').read()

PAYMENT_HEADER_FIELDS = PAYMENT_SAMPLE_DATA.split(
    '\n')[0].split(',')

class StudentImporterTest(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(StudentImporterTest, self).setUp()
        # Setup a sample site for each test
        app = University()
        self.dc_root = tempfile.mkdtemp()
        app['datacenter'].setStoragePath(self.dc_root)

        # Prepopulate the ZODB...
        self.getRootFolder()['app'] = app
        # we add the site immediately after creation to the
        # ZODB. Catalogs and other local utilities are not setup
        # before that step.
        self.app = self.getRootFolder()['app']
        # Set site here. Some of the following setup code might need
        # to access grok.getSite() and should get our new app then
        setSite(app)

        # Add student with subobjects
        student = Student()
        student.firstname = u'Anna'
        student.lastname = u'Tester'
        student.reg_number = u'123'
        student.matric_number = u'234'
        self.app['students'].addStudent(student)
        self.student = self.app['students'][student.student_id]
        self.importer = StudentProcessor()
        self.workdir = tempfile.mkdtemp()
        self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv')
        self.csv_file_update = os.path.join(
            self.workdir, 'sample_student_data_update.csv')
        self.csv_file_update2 = os.path.join(
            self.workdir, 'sample_student_data_update2.csv')
        self.csv_file_migration = os.path.join(
            self.workdir, 'sample_student_data_migration.csv')
        open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA)
        open(self.csv_file_update, 'wb').write(STUDENT_SAMPLE_DATA_UPDATE)
        open(self.csv_file_update2, 'wb').write(STUDENT_SAMPLE_DATA_UPDATE2)
        open(self.csv_file_migration, 'wb').write(STUDENT_SAMPLE_DATA_MIGRATION)

    def tearDown(self):
        super(StudentImporterTest, self).tearDown()
        shutil.rmtree(self.workdir)
        shutil.rmtree(self.dc_root)
        clearSite()
        return

    def test_interface(self):
        # Make sure we fulfill the interface contracts.
        assert verifyObject(IBatchProcessor, self.importer) is True
        assert verifyClass(
            IBatchProcessor, StudentProcessor) is True

    def test_parentsExist(self):
        self.assertFalse(self.importer.parentsExist(None, dict()))
        self.assertTrue(self.importer.parentsExist(None, self.app))

    def test_entryExists(self):
        assert self.importer.entryExists(
            dict(student_id='ID_NONE'), self.app) is False
        assert self.importer.entryExists(
            dict(reg_number='123'), self.app) is True

    def test_getParent(self):
        parent = self.importer.getParent(None, self.app)
        assert parent is self.app['students']

    def test_getEntry(self):
        assert self.importer.getEntry(
            dict(student_id='ID_NONE'), self.app) is None
        assert self.importer.getEntry(
            dict(student_id=self.student.student_id), self.app) is self.student

    def test_addEntry(self):
        new_student = Student()
        self.importer.addEntry(
            new_student, dict(), self.app)
        assert len(self.app['students'].keys()) == 2

    def test_checkConversion(self):
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', reg_state='admitted'))
        self.assertEqual(len(errs),0)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', reg_state=''))
        self.assertEqual(len(errs),1)
        self.assertTrue(('reg_state', 'no value provided') in errs)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', reg_state='nonsense'))
        self.assertEqual(len(errs),1)
        self.assertTrue(('reg_state', 'not allowed') in errs)

    def test_delEntry(self):
        assert self.student.student_id in self.app['students'].keys()
        self.importer.delEntry(
            dict(reg_number=self.student.reg_number), self.app)
        assert self.student.student_id not in self.app['students'].keys()

    def test_import(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, STUDENT_HEADER_FIELDS)
        self.assertEqual(num_warns,0)
        assert len(self.app['students'].keys()) == 4
        self.assertEqual(self.app['students']['X666666'].reg_number,'1')
        shutil.rmtree(os.path.dirname(fin_file))

    def test_import_update(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file_update, STUDENT_HEADER_FIELDS_UPDATE, 'update')
        self.assertEqual(num_warns,0)
        shutil.rmtree(os.path.dirname(fin_file))

    def test_import_update2(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file_update2, STUDENT_HEADER_FIELDS_UPDATE2, 'update')
        self.assertEqual(num_warns,0)
        shutil.rmtree(os.path.dirname(fin_file))

    def test_import_remove(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file_update, STUDENT_HEADER_FIELDS_UPDATE, 'remove')
        self.assertEqual(num_warns,0)
        shutil.rmtree(os.path.dirname(fin_file))

    def test_import_migration_data(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file_migration, STUDENT_HEADER_FIELDS_MIGRATION)
        self.assertEqual(num_warns,2)
        assert len(self.app['students'].keys()) == 4
        self.assertTrue('A123456' in self.app['students'].keys())
        self.assertEqual(self.app['students']['A123456'].state,'clearance started')
        self.assertEqual(self.app['students']['A123456'].date_of_birth,
            datetime.date(1990, 1, 2))
        self.assertFalse(self.app['students']['A123456'].clearance_locked)
        self.assertEqual(self.app['students']['B123456'].state,'cleared')
        self.assertEqual(self.app['students']['B123456'].date_of_birth,
            datetime.date(1990, 1, 3))
        self.assertTrue(self.app['students']['B123456'].clearance_locked)
        history = ' '.join(self.app['students']['A123456'].history.messages)
        self.assertTrue(
            "State 'clearance started' set by system" in history)
        shutil.rmtree(os.path.dirname(fin_file))


class StudentStudyCourseImporterTest(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(StudentStudyCourseImporterTest, self).setUp()
        self.dc_root = tempfile.mkdtemp()
        self.workdir = tempfile.mkdtemp()
        app = University()
        app['datacenter'].setStoragePath(self.dc_root)
        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(app)

        # Import students with subobjects
        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
            student_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))

        # Populate university
        self.certificate = createObject('waeup.Certificate')
        self.certificate.code = 'CERT1'
        self.certificate.application_category = 'basic'
        self.certificate.start_level = 200
        self.certificate.end_level = 500
        self.app['faculties']['fac1'] = Faculty()
        self.app['faculties']['fac1']['dep1'] = Department()
        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
            self.certificate)

        self.importer = StudentStudyCourseProcessor()
        self.csv_file = os.path.join(
            self.workdir, 'sample_studycourse_data.csv')
        open(self.csv_file, 'wb').write(STUDYCOURSE_SAMPLE_DATA)
        return

    def tearDown(self):
        super(StudentStudyCourseImporterTest, self).tearDown()
        shutil.rmtree(self.workdir)
        shutil.rmtree(self.dc_root)
        clearSite()
        return

    def test_interface(self):
        # Make sure we fulfill the interface contracts.
        assert verifyObject(IBatchProcessor, self.importer) is True
        assert verifyClass(
            IBatchProcessor, StudentStudyCourseProcessor) is True

    def test_entryExists(self):
        assert self.importer.entryExists(
            dict(reg_number='REG_NONE'), self.app) is False
        assert self.importer.entryExists(
            dict(reg_number='1'), self.app) is True

    def test_getEntry(self):
        student = self.importer.getEntry(
            dict(reg_number='1'), self.app).__parent__
        self.assertEqual(student.reg_number,'1')

    def test_checkConversion(self):
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', certificate='CERT1', current_level='200'))
        self.assertEqual(len(errs),0)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', certificate='CERT999'))
        self.assertEqual(len(errs),1)
        self.assertTrue(('certificate', u'Invalid value') in errs)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', certificate='CERT1', current_level='100'))
        self.assertEqual(len(errs),1)
        self.assertTrue(('current_level','not in range') in errs)
        # If we import only current_level, no conversion checking is done.
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', current_level='100'))
        self.assertEqual(len(errs),0)

    def test_import(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, STUDYCOURSE_HEADER_FIELDS,'update')
        self.assertEqual(num_warns,1)
        studycourse = self.importer.getEntry(dict(reg_number='1'), self.app)
        self.assertEqual(studycourse.certificate.code, u'CERT1')
        shutil.rmtree(os.path.dirname(fin_file))

class StudentStudyLevelImporterTest(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(StudentStudyLevelImporterTest, self).setUp()
        self.dc_root = tempfile.mkdtemp()
        self.workdir = tempfile.mkdtemp()
        app = University()
        app['datacenter'].setStoragePath(self.dc_root)
        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(app)

        # Import students with subobjects
        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
            student_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))

        # Populate university
        self.certificate = createObject('waeup.Certificate')
        self.certificate.code = 'CERT1'
        self.certificate.application_category = 'basic'
        self.certificate.start_level = 200
        self.certificate.end_level = 500
        self.app['faculties']['fac1'] = Faculty()
        self.app['faculties']['fac1']['dep1'] = Department()
        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
            self.certificate)

        # Update study courses
        studycourse_file = os.path.join(
            self.workdir, 'sample_studycourse_data.csv')
        open(studycourse_file, 'wb').write(STUDYCOURSE_SAMPLE_DATA)
        importer = StudentStudyCourseProcessor()
        num, num_warns, fin_file, fail_file = importer.doImport(
            studycourse_file, STUDYCOURSE_HEADER_FIELDS,'update')
        shutil.rmtree(os.path.dirname(fin_file))

        self.importer = StudentStudyLevelProcessor()
        self.csv_file = os.path.join(
            self.workdir, 'sample_studylevel_data.csv')
        open(self.csv_file, 'wb').write(STUDYLEVEL_SAMPLE_DATA)

    def tearDown(self):
        super(StudentStudyLevelImporterTest, self).tearDown()
        shutil.rmtree(self.workdir)
        shutil.rmtree(self.dc_root)
        clearSite()
        return

    def test_interface(self):
        # Make sure we fulfill the interface contracts.
        assert verifyObject(IBatchProcessor, self.importer) is True
        assert verifyClass(
            IBatchProcessor, StudentStudyLevelProcessor) is True

    def test_checkConversion(self):
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', level='220'))
        self.assertEqual(len(errs),0)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', level='900'))
        self.assertEqual(len(errs),1)
        self.assertTrue(('level','no valid integer') in errs)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', level='xyz'))
        self.assertEqual(len(errs),1)
        self.assertTrue(('level','no integer') in errs)

    def test_import(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, STUDYLEVEL_HEADER_FIELDS,'create')
        self.assertEqual(num_warns,2)
        assert self.importer.entryExists(
            dict(reg_number='1', level='100'), self.app) is True
        studylevel = self.importer.getEntry(
            dict(reg_number='1', level='100'), self.app)
        self.assertEqual(studylevel.__parent__.certificate.code, u'CERT1')
        self.assertEqual(studylevel.level_session, 2008)
        self.assertEqual(studylevel.level_verdict, 'A')
        self.assertEqual(studylevel.level, 100)
        shutil.rmtree(os.path.dirname(fin_file))
        

class CourseTicketImporterTest(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(CourseTicketImporterTest, self).setUp()
        self.dc_root = tempfile.mkdtemp()
        self.workdir = tempfile.mkdtemp()
        app = University()
        app['datacenter'].setStoragePath(self.dc_root)
        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(app)

        # Import students with subobjects
        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
            student_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))

        # Populate university
        self.certificate = createObject('waeup.Certificate')
        self.certificate.code = 'CERT1'
        self.certificate.application_category = 'basic'
        self.certificate.start_level = 200
        self.certificate.end_level = 500
        self.app['faculties']['fac1'] = Faculty()
        self.app['faculties']['fac1']['dep1'] = Department()
        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
            self.certificate)
        self.course = createObject('waeup.Course')
        self.course.code = 'COURSE1'
        self.course.semester = 1
        self.course.credits = 10
        self.course.passmark = 40
        self.app['faculties']['fac1']['dep1'].courses.addCourse(
            self.course)
        self.app['faculties']['fac1']['dep1'].certificates['CERT1'].addCourseRef(
            self.course, level=100)

        # Update study courses
        studycourse_file = os.path.join(
            self.workdir, 'sample_studycourse_data.csv')
        open(studycourse_file, 'wb').write(STUDYCOURSE_SAMPLE_DATA)
        importer = StudentStudyCourseProcessor()
        num, num_warns, fin_file, fail_file = importer.doImport(
            studycourse_file, STUDYCOURSE_HEADER_FIELDS,'update')
        shutil.rmtree(os.path.dirname(fin_file))

        # Import study levels
        importer = StudentStudyLevelProcessor()
        studylevel_file = os.path.join(
            self.workdir, 'sample_studylevel_data.csv')
        open(studylevel_file, 'wb').write(STUDYLEVEL_SAMPLE_DATA)
        num, num_warns, fin_file, fail_file = importer.doImport(
            studylevel_file, STUDYLEVEL_HEADER_FIELDS,'create')
        shutil.rmtree(os.path.dirname(fin_file))

        self.importer = CourseTicketProcessor()
        self.csv_file = os.path.join(
            self.workdir, 'sample_courseticket_data.csv')
        open(self.csv_file, 'wb').write(COURSETICKET_SAMPLE_DATA)

    def tearDown(self):
        super(CourseTicketImporterTest, self).tearDown()
        shutil.rmtree(self.workdir)
        shutil.rmtree(self.dc_root)
        clearSite()
        return

    def test_interface(self):
        # Make sure we fulfill the interface contracts.
        assert verifyObject(IBatchProcessor, self.importer) is True
        assert verifyClass(
            IBatchProcessor, CourseTicketProcessor) is True

    def test_checkConversion(self):
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', code='COURSE1', level='220'))
        self.assertEqual(len(errs),0)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', code='COURSE2', level='220'))
        self.assertEqual(len(errs),1)
        self.assertTrue(('code','non-existent') in errs)

    def test_import(self):

        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, COURSETICKET_HEADER_FIELDS,'create')

        self.assertEqual(num_warns,2)
        assert self.importer.entryExists(
            dict(reg_number='1', level='100', code='COURSE1'), self.app) is True
        courseticket = self.importer.getEntry(
            dict(reg_number='1', level='100', code='COURSE1'), self.app)
        self.assertEqual(courseticket.__parent__.__parent__.certificate.code, u'CERT1')
        self.assertEqual(courseticket.score, 1)
        self.assertEqual(courseticket.mandatory, True)
        self.assertEqual(courseticket.fcode, 'NA')
        self.assertEqual(courseticket.dcode, 'NA')
        self.assertEqual(courseticket.code, 'COURSE1')
        self.assertEqual(courseticket.title, 'Unnamed Course')
        self.assertEqual(courseticket.credits, 10)
        self.assertEqual(courseticket.passmark, 40)
        self.assertEqual(courseticket.semester, 1)
        shutil.rmtree(os.path.dirname(fin_file))

class PaymentImporterTest(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(PaymentImporterTest, self).setUp()
        self.dc_root = tempfile.mkdtemp()
        self.workdir = tempfile.mkdtemp()
        app = University()
        app['datacenter'].setStoragePath(self.dc_root)
        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(app)

        # Add student with payment
        student = Student()
        student.firstname = u'Anna'
        student.lastname = u'Tester'
        student.reg_number = u'123'
        student.matric_number = u'234'
        self.app['students'].addStudent(student)
        self.student = self.app['students'][student.student_id]
        payment = createObject(u'waeup.StudentOnlinePayment')
        payment.p_id = 'p123'
        self.student['payments'][payment.p_id] = payment

        # Import students with subobjects
        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
            student_file, STUDENT_HEADER_FIELDS)
        shutil.rmtree(os.path.dirname(fin_file))

        self.importer = StudentOnlinePaymentProcessor()
        self.csv_file = os.path.join(
            self.workdir, 'sample_payment_data.csv')
        open(self.csv_file, 'wb').write(PAYMENT_SAMPLE_DATA)

    def tearDown(self):
        super(PaymentImporterTest, self).tearDown()
        shutil.rmtree(self.workdir)
        shutil.rmtree(self.dc_root)
        clearSite()
        return

    def test_interface(self):
        # Make sure we fulfill the interface contracts.
        assert verifyObject(IBatchProcessor, self.importer) is True
        assert verifyClass(
            IBatchProcessor, StudentOnlinePaymentProcessor) is True

    def test_getEntry(self):
        assert self.importer.getEntry(
            dict(student_id='ID_NONE', p_id='nonsense'), self.app) is None
        assert self.importer.getEntry(
            dict(student_id=self.student.student_id, p_id='p123'),
            self.app) is self.student['payments']['p123']
        assert self.importer.getEntry(
            dict(student_id=self.student.student_id, p_id='XXXXXX123'),
            self.app) is self.student['payments']['p123']

    def test_addEntry(self):
        self.assertEqual(len(self.student['payments'].keys()),1)
        payment1 = createObject(u'waeup.StudentOnlinePayment')
        payment1.p_id = 'p234'
        self.importer.addEntry(
            payment1, dict(student_id=self.student.student_id, p_id='p234'),
            self.app)
        self.assertEqual(len(self.student['payments'].keys()),2)
        self.assertEqual(self.student['payments']['p234'].p_id, 'p234')
        payment2 = createObject(u'waeup.StudentOnlinePayment')
        payment1.p_id = 'nonsense'
        # payment1.p_id will be replaced if p_id doesn't start with 'p'
        self.importer.addEntry(
            payment2, dict(student_id=self.student.student_id, p_id='XXXXXX456'),
            self.app)
        self.assertEqual(len(self.student['payments'].keys()),3)
        self.assertEqual(self.student['payments']['p456'].p_id, 'p456')

    def test_checkConversion(self):
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', p_id='3816951266236341955'))
        self.assertEqual(len(errs),0)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', p_id='p1266236341955'))
        self.assertEqual(len(errs),0)
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', p_id='nonsense'))
        self.assertEqual(len(errs),1)
        timestamp = "%d" % int(time()*1000)
        p_id = "p%s" % timestamp
        errs, inv_errs, conv_dict = self.importer.checkConversion(
            dict(reg_number='1', p_id=p_id))
        self.assertEqual(len(errs),0)

    def test_import(self):
        num, num_warns, fin_file, fail_file = self.importer.doImport(
            self.csv_file, PAYMENT_HEADER_FIELDS,'create')
        self.assertEqual(num_warns,0)
        payment = self.importer.getEntry(dict(reg_number='1',
            p_id='p1290797973744'), self.app)
        self.assertEqual(payment.p_id, 'p1290797973744')
        cdate = payment.creation_date.strftime("%Y-%m-%d %H:%M:%S")
        self.assertEqual(cdate, "2010-11-26 19:59:33")
        shutil.rmtree(os.path.dirname(fin_file))

def test_suite():
    suite = unittest.TestSuite()
    for testcase in [
        StudentImporterTest,StudentStudyCourseImporterTest,
        StudentStudyLevelImporterTest,CourseTicketImporterTest,
        PaymentImporterTest,]:
        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
                testcase
                )
        )
    return suite


