## $Id: test_batching.py 7357 2011-12-16 06:40:31Z 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
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)
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(',')

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):
        self.assertFalse(self.importer.entryExists(
            dict(student_id='ID_NONE'), self.app))
        student = self.importer.getEntry(
            dict(student_id=self.student.student_id), self.app)
        self.assertEqual(student.reg_number, u'123')

    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_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
        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,0)
        assert len(self.app['students'].keys()) == 4
        self.assertTrue('A123456' in self.app['students'].keys())
        shutil.rmtree(os.path.dirname(fin_file))

class StudentStudyCourseImporterTest(FunctionalTestCase):

    layer = FunctionalLayer

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

        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(app)

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

        # 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 = 100
        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)
        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 None
        student = self.importer.entryExists(dict(reg_number='1'), self.app)
        self.assertEqual(student.reg_number, u'1')

    def test_getEntry(self):
        studycourse = self.importer.getEntry(dict(reg_number='1'), self.app)
        student = studycourse.__parent__
        s_id = student.student_id
        assert studycourse is self.app['students'][s_id]['studycourse']

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

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