## $Id: test_export.py 12289 2014-12-21 22:17:06Z 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
##
"""
Test the customer exporter.
"""

import os
import grok
import datetime
from cStringIO import StringIO
from zope.component import queryUtility, getUtility
from zope.event import notify
from zope.interface.verify import verifyObject, verifyClass
from waeup.ikoba.interfaces import (
    ICSVExporter, IExtFileStore, IFileStoreNameChooser)
from waeup.ikoba.customers.catalog import CustomersQuery
from waeup.ikoba.customers.export import (
    CustomerExporter, CustomerSampleDocumentExporter, SampleContractExporter,
    get_customers)
from waeup.ikoba.customers.interfaces import ICSVCustomerExporter
from waeup.ikoba.customers.customer import Customer
from waeup.ikoba.customers.documents import CustomerSampleDocument
from waeup.ikoba.customers.contracts import SampleContract
from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
from waeup.ikoba.testing import FunctionalLayer

curr_year = datetime.datetime.now().year
year_range = range(curr_year - 9, curr_year + 1)
year_range_str = ','.join([str(i) for i in year_range])

class ExportHelperTests(CustomerImportExportSetup):
    layer = FunctionalLayer
    def setUp(self):
        super(ExportHelperTests, self).setUp()
        customer = Customer()
        self.app['customers'].addCustomer(customer)
        customer = self.setup_customer(customer)
        notify(grok.ObjectModifiedEvent(customer))
        self.customer = self.app['customers'][customer.customer_id]
        return

    def test_get_customers_plain(self):
        # without a filter we get all customers
        result = get_customers(self.app)
        self.assertEqual(len(list(result)), 1)
        return

class CustomerExporterTest(CustomerImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(CustomerExporterTest, self).setUp()
        self.setup_for_export()
        return

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

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

    def test_export(self):
        # we can really export customers
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = CustomerExporter()
        exporter.export([self.customer], self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(
            'customer_id,email,firstname,lastname,middlename,phone,reg_number,'
            'sex,suspended,suspended_comment,password,state,history\r\n'
            'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,123,,0,,,'
            'created,[u\'2014-12-04 11:25:35 UTC - Customer created '
            'by system\']\r\n',
            result
            )
        return

    def test_export_all(self):
        # we can really export customers
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = CustomerExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue(
            'customer_id,email,firstname,lastname,middlename,phone,'
            'reg_number,sex,suspended,suspended_comment,password,state,history\r\n'
            'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,'
            '123,,0,,,created'
            in result
            )
        return

    def test_export_customer(self):
        # we can export a single customer
        self.setup_customer(self.customer)
        exporter = CustomerExporter()
        exporter.export_customer(self.customer, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue(
            'customer_id,email,firstname,lastname,middlename,phone,reg_number,'
            'sex,suspended,suspended_comment,password,state,history\r\n'
            'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,'
            '123,,0,,,created,'
            in result
            )
        return

class CustomerSampleDocumentExporterTest(CustomerImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(CustomerSampleDocumentExporterTest, self).setUp()
        self.setup_for_export()
        return

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

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

    def test_export_empty(self):
        # we can export a nearly empty document
        document = CustomerSampleDocument()
        document.document_id = u'DOC1'
        exporter = CustomerSampleDocumentExporter()
        exporter.export([document], self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertEqual(
            result,
            'class_name,document_id,history,state,title,user_id\r\n'
            'CustomerSampleDocument,DOC1,[],,,\r\n'
            )
        return

    def test_export(self):
        # we can really export customer documents.
        # set values we can expect in export file
        self.setup_customer(self.customer)
        document = self.customer['documents']['DOC1']
        exporter = CustomerSampleDocumentExporter()
        exporter.export([document], self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue(
            'class_name,document_id,history,state,title,user_id\r\n'
            in result
            )
        self.assertMatches(
            '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - '
            'Document created by system\'],'
            'verified,My Document,A111111...',
            result
            )
        return

    def test_export_all(self):
        # we can really export all documents
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = CustomerSampleDocumentExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue(
            'class_name,document_id,history,state,title,user_id\r\n'
            in result)
        self.assertMatches(
            '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - '
            'Document created by system\'],'
            'verified,My Document,A111111...',
            result
            )
        return

    def test_export_customer_document(self):
        # we can really export all documents of a certain customer
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = CustomerSampleDocumentExporter()
        exporter.export_customer(self.customer, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue(
            'class_name,document_id,history,state,title,user_id\r\n'
            in result)
        self.assertMatches(
            '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - '
            'Document created by system\'],'
            'verified,My Document,A111111...',
            result
            )
        return


class SampleContractExporterTest(CustomerImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(SampleContractExporterTest, self).setUp()
        self.setup_for_export()
        return

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

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

    def test_export_empty(self):
        # we can export a nearly empty contract
        contract = SampleContract()
        contract.contract_id = u'CON1'
        exporter = SampleContractExporter()
        exporter.export([contract], self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertEqual(
            result,
            'class_name,contract_category,contract_id,document_object,'
            'history,last_product_id,'
            'product_object,state,title,user_id\r\n'

            'SampleContract,sample,CON1,,[],,,,,\r\n'
            )
        return

    def test_export(self):
        # we can really export customer contracts.
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = SampleContractExporter()
        exporter.export([self.contract], self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(
            'class_name,contract_category,contract_id,document_object,'
            'history,last_product_id,'
            'product_object,state,title,user_id\r\n'

            'SampleContract,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
            'Contract created by system\'],,'
            'SAM,created,My Contract,A111111\r\n',
            result
            )
        return

    def test_export_all(self):
        # we can really export all contracts
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = SampleContractExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(
            'class_name,contract_category,contract_id,document_object,'
            'history,last_product_id,'
            'product_object,state,title,user_id\r\n'

            'SampleContract,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
            'Contract created by system\'],,'
            'SAM,created,My Contract,A111111\r\n',
            result
            )
        return

    def test_export_contract(self):
        # we can really export all contracts of a certain customer
        # set values we can expect in export file
        self.setup_customer(self.customer)
        exporter = SampleContractExporter()
        exporter.export_customer(self.customer, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(
            'class_name,contract_category,contract_id,document_object,'
            'history,last_product_id,'
            'product_object,state,title,user_id\r\n'

            'SampleContract,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
            'Contract created by system\'],,'
            'SAM,created,My Contract,A111111\r\n',
            result
            )
        return

