##
## test_applicants.py
## Login : <uli@pu.smp.net>
## Started on  Tue Aug 24 04:36:11 2010 Uli Fouquet
## $Id$
## 
## Copyright (C) 2010 Uli Fouquet
## 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
##
"""Tests for applicants and related.
"""
import logging
import unittest
from StringIO import StringIO
from zope.component.interfaces import IFactory
from zope.interface import verify
from waeup.sirp.interfaces import IWAeUPSIRPPluggable
from waeup.sirp.jambtables import (
    ResultEntry, Applicant, ApplicantFactory, ApplicantContainer,
    ApplicantsPlugin,
    )
from waeup.sirp.jambtables.interfaces import (
    IResultEntry, IApplicant, IApplicantContainer,
    )

class ResultEntryTest(unittest.TestCase):

    def setUp(self):
        self.result_entry = ResultEntry()
        return

    def tearDown(self):
        pass

    def test_interfaces(self):
        verify.verifyClass(IResultEntry, ResultEntry)
        verify.verifyObject(IResultEntry, self.result_entry)

    def test_resultentry(self):
        entry = ResultEntry('Some subject', 3.7)
        assert entry.subject == 'Some subject'
        assert entry.score == 3.7

class ApplicantTest(unittest.TestCase):

    def setUp(self):
        self.applicant = Applicant()

    def tearDown(self):
        pass

    def test_interfaces(self):
        verify.verifyClass(IApplicant, Applicant)
        verify.verifyObject(IApplicant, self.applicant)

class ApplicantFactoryTest(unittest.TestCase):

    def setUp(self):
        self.factory = ApplicantFactory()

    def tearDown(self):
        pass

    def test_interfaces(self):
        verify.verifyClass(IFactory, ApplicantFactory)
        verify.verifyObject(IFactory, self.factory)

    def test_factory(self):
        obj = self.factory()
        assert isinstance(obj, Applicant)

    def test_getInterfaces(self):
        implemented_by = self.factory.getInterfaces()
        assert implemented_by.isOrExtends(IApplicant)

class ApplicantContainerTest(unittest.TestCase):

    def setUp(self):
        self.container = ApplicantContainer()
        return

    def tearDown(self):
        pass

    def test_interfaces(self):
        verify.verifyClass(IApplicantContainer, ApplicantContainer)
        verify.verifyObject(IApplicantContainer, self.container)

class ApplicantsPluginTest(unittest.TestCase):

    def setUp(self):
        self.plugin = ApplicantsPlugin()
        self.site1 = dict()
        self.site2 = dict(applications = True)
        self.log_stream = StringIO()#.open('w')
        handler = logging.StreamHandler(self.log_stream)
        self.logger = logging.getLogger('waeup.sirp.testing')
        self.logger.addHandler(handler)
        self.logger.propagate = False
        self.logger.setLevel(logging.DEBUG)
        
    def tearDown(self):
        handlers = self.logger.handlers
        for handler in handlers:
            del handler
        pass

    def notest_interfaces(self):
        verify.verifyClass(IWAeUPSIRPPluggable, ApplicantsPlugin)
        verify.verifyObject(IWAeUPSIRPPluggable, self.plugin)

    def test_setup(self):
        self.plugin.setup(self.site1, 'some.site', self.logger)
        assert 'Installed application container' in self.log_stream.getvalue()
        assert 'applications' in self.site1.keys()

    def test_update_empty_site(self):
        self.plugin.update(self.site1, 'some.site', self.logger)
        assert 'Updating site' in self.log_stream.getvalue()
        assert 'Installing' in self.log_stream.getvalue()

    def test_update_filled_site(self):
        self.plugin.update(self.site2, 'some.site', self.logger)
        assert 'Nothing to do' in self.log_stream.getvalue()
        

def test_suite():
    suite = unittest.TestSuite()
    for testcase in [
        ResultEntryTest, ApplicantTest, ApplicantFactoryTest,
        ApplicantContainerTest, ApplicantsPluginTest,
        ]:
        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
                testcase
                )
        )
    return suite
