# -*- coding: utf-8 -*-
## $Id: test_interfaces.py 12102 2014-12-01 06:46:13Z 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 interfaces and included components.
import grok
import unittest
import tempfile
import shutil
from zope.component.hooks import setSite
from zope.component import queryUtility, createObject
from hurry.workflow.interfaces import IWorkflowState
from waeup.ikoba.app import Company
from waeup.ikoba.interfaces import SUBMITTED
from waeup.ikoba.customers.vocabularies import (
    ConCatProductSource, CustomerDocumentSource)
from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase


class SourceTests(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(SourceTests, self).setUp()

        # Prepopulate ZODB
        app = Company()
        self.dc_root = tempfile.mkdtemp()
        app['datacenter'].setStoragePath(self.dc_root)

        # Prepopulate the ZODB...
        self.getRootFolder()['app'] = app
        self.app = self.getRootFolder()['app']
        setSite(self.app)

        # Create customer with subobjects
        customer = createObject('waeup.Customer')
        customer.firstname = u'Bob'
        customer.lastname = u'Tester'
        customer.matric_number = u'1234'
        self.app['customers'].addCustomer(customer)
        self.customer_id = customer.customer_id
        self.customer = self.app['customers'][self.customer_id]

        product = createObject('waeup.Product')
        product.product_id = u'SAM'
        product.title = u'Our Sample Product'
        product.contract_category = u'sample'
        self.app['products'].addProduct(product)
        self.product_id = product.product_id
        self.product = self.app['products'][self.product_id]

        contract = createObject('waeup.SampleContract')
        contract.title = u'My Samle Contract'
        contract.product = product
        contract.last_product_id = product.product_id
        self.customer['contracts'].addContract(contract)
        self.contract_id = contract.contract_id
        self.contract = self.customer['contracts'][self.contract_id]

        document = createObject('waeup.CustomerSampleDocument')
        document.title = u'My Samle Document'
        self.customer['documents'].addDocument(document)
        self.document_id = document.document_id
        self.document = self.customer['documents'][self.document_id]
        return

    def test_getValues_ConCatProductSource(self):
        source = ConCatProductSource()
        self.product.contract_category = u'sample'
        result = source.factory.getValues(self.contract)
        self.assertTrue(self.product in result)
        self.product.contract_category = u'license'
        result = source.factory.getValues(self.contract)
        self.assertEqual(len(result), 0)

    def test_getTitle_ConCatProductSource(self):
        source = ConCatProductSource()
        result = source.factory.getTitle(object(), self.product)
        self.assertEqual(result, u'SAM - Our Sample Product')

    def test_getValues_CustomerDocumentSource(self):
        source = CustomerDocumentSource()
        result = source.factory.getValues(self.contract)
        self.assertFalse(self.document in result)
        IWorkflowState(self.document).setState(SUBMITTED)
        result = source.factory.getValues(self.contract)
        self.assertTrue(self.document in result)

    def test_getTitle_CustomerDocumentSource(self):
        source = CustomerDocumentSource()
        result = source.factory.getTitle(object(), self.document)
        self.assertEqual(result, u'd101 - My Samle Document')

