import datetime
import os
import shutil
import tempfile
import unittest
from zope.component import queryUtility
from zope.interface.verify import verifyObject, verifyClass
from waeup.kofa.interfaces import ICSVExporter
from waeup.kofa.testing import KofaUnitTestLayer
from waeup.kofa.applicants import ApplicantsContainer
from waeup.kofa.applicants.export import ApplicantsContainerExporter

class ApplicantsContainersExporterTest(unittest.TestCase):

    layer = KofaUnitTestLayer

    def setUp(self):
        self.workdir = tempfile.mkdtemp()
        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
        return

    def tearDown(self):
        shutil.rmtree(self.workdir)
        return

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

    def test_get_as_utility(self):
        # we can get a faculty exporter as utility
        result = queryUtility(ICSVExporter, name="applicantscontainers")
        self.assertTrue(result is not None)
        return

    def test_export(self):
        # we can export a set of applicants containers (w/o applicants)
        container = ApplicantsContainer()
        container.code = u'dp2012'
        container.startdate = datetime.date(2012, 1, 1)
        exporter = ApplicantsContainerExporter()
        exporter.export([container], self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertEqual(
            result,
            'code,title,prefix,entry_level,year,provider,application_category,'
            'description,description_dict,startdate,enddate,strict_deadline\r\n'
            'dp2012,-,,100,,,,"This text can been seen by anonymous users.\nHere we put mult-lingual information about the study courses provided, the application procedure and deadlines.\n>>de<<\nDieser Text kann von anonymen Benutzern gelesen werden.\nHier koennen mehrsprachige Informationen fuer Antragsteller hinterlegt werden.",{},2012-01-01,,1\r\n'
            )
        return
