import datetime
from zope.component import queryUtility
from zope.interface.verify import verifyObject, verifyClass
from waeup.kofa.interfaces import ICSVExporter
from waeup.kofa.schoolgrades import ResultEntry
from waeup.kofa.students.tests.test_batching import (
    StudentImportExportSetup, curr_year)
from kofacustom.iuokada.utils.utils import CustomKofaUtils
from kofacustom.iuokada.students.export import (
    CustomStudentExporter, CustomStudentPaymentExporter)
from kofacustom.iuokada.testing import FunctionalLayer


class CustomStudentExporterTest(StudentImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(CustomStudentExporterTest, self).setUp()
        self.setup_for_export()
        result_entry = ResultEntry(
            CustomKofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
            CustomKofaUtils.EXAM_GRADES[0][0]
            )
        self.student.alr_results = [
            result_entry]
        self.student.fst_sit_results = [
            result_entry]
        self.student.scd_sit_results = [
            result_entry]
        return

    def test_ifaces(self):
        # make sure we fullfill interface contracts
        obj = CustomStudentExporter()
        verifyObject(ICSVExporter, obj)
        verifyClass(ICSVExporter, CustomStudentExporter)
        return


    def test_export_all(self):
        # we can really export students
        # set values we can expect in export file
        self.setup_student(self.student)
        exporter = CustomStudentExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue('entry_session' in result)
        return

class CustomStudentPaymentExporterTest(StudentImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(CustomStudentPaymentExporterTest, self).setUp()
        self.setup_for_export()
        return

    def test_ifaces(self):
        # make sure we fullfill interface contracts
        obj = CustomStudentPaymentExporter()
        verifyObject(ICSVExporter, obj)
        verifyClass(ICSVExporter, CustomStudentPaymentExporter)
        return

    def test_export_all(self):
        # we can really export students
        # set values we can expect in export file
        self.setup_student(self.student)
        self.student['payments']['my-payment'].r_company = 'interswitch'
        self.student['payments']['my-payment'].p_option = 'first'
        self.student['payments']['my-payment'].r_card_num = '789'
        exporter = CustomStudentPaymentExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(
            'ac,amount_auth,creation_date,gateway_amt,net_amt,p_category,p_combi,p_current,'
            'p_id,p_item,p_level,p_option,p_session,p_state,payment_date,provider_amt,'
            'r_amount_approved,r_card_num,r_code,r_company,r_desc,'
            'r_pay_reference,thirdparty_amt,student_id,state,'
            'current_session,entry_session\r\n'
            '666,12.12,%s-04-01 13:12:01#,,,schoolfee,[],1,my-id,p-item,'
            '100,first,%s,paid,%s-04-01 14:12:01#,,12.12,'
            '789,r-code,interswitch,,,,A111111,created,2012,2010\r\n'
            % (curr_year-6, curr_year-6, curr_year-6), result
            )
        return
