## $Id: test_contract.py 12221 2014-12-14 06:14:39Z 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 contracts.
"""
from zope.interface.verify import verifyClass, verifyObject
from zope.component import createObject
from hurry.workflow.interfaces import (
    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
from waeup.ikoba.customers.interfaces import (
    IContractsContainer, IContract)
from waeup.ikoba.interfaces import IObjectHistory
from waeup.ikoba.customers.contracts import (
    ContractsContainer, SampleContract)
from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)

class ContractsContainerTestCase(FunctionalTestCase):

    layer = FunctionalLayer

    def test_interfaces(self):
        # Make sure the correct interfaces are implemented.
        self.assertTrue(
            verifyClass(
                IContractsContainer, ContractsContainer)
            )
        self.assertTrue(
            verifyObject(
                IContractsContainer, ContractsContainer())
            )
        self.assertTrue(
            verifyClass(
                IContract, SampleContract)
            )
        self.assertTrue(
            verifyObject(
                IContract, SampleContract())
            )
        return

    def test_addContract(self):
        container = ContractsContainer()
        contract = createObject(u'waeup.SampleContract')
        id = contract.contract_id
        container.addContract(contract)
        self.assertEqual(container[id], contract)
        self.assertRaises(TypeError, container.addContract, object())
        self.assertEqual(contract.class_name, 'SampleContract')
        self.assertEqual(id, 'c999')
        return

    def test_contract_workflow(self):
        contract = createObject(u'waeup.SampleContract')
        IWorkflowInfo(contract).fireTransition('create')
        self.assertEqual(IWorkflowState(contract).getState(), 'created')
        IWorkflowInfo(contract).fireTransition('submit')
        self.assertEqual(IWorkflowState(contract).getState(), 'submitted')
        IWorkflowInfo(contract).fireTransition('approve')
        self.assertEqual(IWorkflowState(contract).getState(), 'approved')
        IWorkflowInfo(contract).fireTransition('expire')
        self.assertEqual(IWorkflowState(contract).getState(), 'expired')
        IWorkflowInfo(contract).fireTransition('reset4')
        self.assertEqual(IWorkflowState(contract).getState(), 'created')
        self.assertRaises(InvalidTransitionError,
            IWorkflowInfo(contract).fireTransition, 'approve')
        IWorkflowInfo(contract).fireTransition('submit')
        IWorkflowInfo(contract).fireTransition('reset3')
        self.assertEqual(IWorkflowState(contract).getState(), 'created')
        return

    def test_contract_history(self):
        doc = createObject(u'waeup.SampleContract')
        IWorkflowInfo(doc).fireTransition('create')
        messages = ' '.join(doc.history.messages)
        self.assertTrue('Contract created by system' in messages)
        return
