## $Id: test_container.py 12090 2014-11-29 07:57:51Z henrik $
##
## Copyright (C) 2014 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
##
"""
Tests for customers containers.
"""
import tempfile
import shutil
from zope.interface.verify import verifyClass, verifyObject
from zope.component.hooks import setSite, clearSite
from waeup.ikoba.app import Company
from waeup.ikoba.customers.interfaces import (
    ICustomersContainer,
    )
from waeup.ikoba.customers.container import (
    CustomersContainer,
    )
from waeup.ikoba.testing import (
    FunctionalLayer, FunctionalTestCase, remove_logger)

class CustomersContainerTestCase(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        remove_logger('waeup.ikoba.app.customers')
        super(CustomersContainerTestCase, self).setUp()
        # Setup a sample site for each test
        # Prepopulate the ZODB...
        app = Company()
        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(self.app)
        self.dc_root = tempfile.mkdtemp()
        app['datacenter'].setStoragePath(self.dc_root)
        return

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

    def test_interfaces(self):
        # Make sure the correct interfaces are implemented.
        self.assertTrue(
            verifyClass(
                ICustomersContainer, CustomersContainer)
            )
        self.assertTrue(
            verifyObject(
                ICustomersContainer, CustomersContainer())
            )
        return

    def test_logger(self):
        # We can get a logger from root
        logger = self.app['customers'].logger
        assert logger is not None
        assert logger.name == 'waeup.ikoba.app.customers'
        handlers = logger.handlers
        assert len(handlers) == 1
        filename = logger.handlers[0].baseFilename
        assert filename.endswith('customers.log')
        assert filename.startswith(self.dc_root)

    def test_logger_multiple(self):
        # Make sure the logger is still working after 2nd call
        # First time we call it, it might be registered
        logger = self.app['customers'].logger
        # At second call the already registered logger should be returned
        logger = self.app['customers'].logger
        assert logger is not None
        assert logger.name == 'waeup.ikoba.app.customers'
        handlers = logger.handlers
        assert len(handlers) == 1
        filename = logger.handlers[0].baseFilename
        assert filename.endswith('customers.log')
        assert filename.startswith(self.dc_root)
