Ignore:
Timestamp:
21 Dec 2014, 22:43:47 (10 years ago)
Author:
Henrik Bettermann
Message:

Add two more export-import tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/ikobacustom.skeleton/trunk/src/ikobacustom/skeleton/customers/tests/test_browser.py

    r12286 r12291  
    2626from zope.event import notify
    2727from zope.interface.verify import verifyObject, verifyClass
     28from zope.testbrowser.testing import Browser
     29from hurry.workflow.interfaces import IWorkflowState
    2830from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
    2931from ikobacustom.skeleton.customers.documents import SkeletonCustomerDocument
    30 from ikobacustom.skeleton.customers.export import SkeletonCustomerDocumentExporter
    31 from ikobacustom.skeleton.customers.batching import SkeletonCustomerDocumentProcessor
     32from ikobacustom.skeleton.customers.export import (
     33    SkeletonCustomerExporter,
     34    SkeletonCustomerDocumentExporter,
     35    SkeletonContractExporter)
     36from ikobacustom.skeleton.customers.batching import (
     37    SkeletonCustomerProcessor,
     38    SkeletonCustomerDocumentProcessor,
     39    SkeletonContractProcessor)
    3240from ikobacustom.skeleton.testing import FunctionalLayer
    3341
     
    3947    def setup_for_export(self):
    4048        customer = createObject(u'waeup.Customer')
     49        customer.firstname = u'Beate'
     50        customer.lastname = u'Mueller'
     51        customer.reg_number = u'123'
     52        customer.sex = u'f'
     53        IWorkflowState(customer).setState('started')
    4154        self.app['customers'].addCustomer(customer)
    4255        document = createObject(u'waeup.SkeletonCustomerDocument')
    4356        document.title = u'My first document'
    4457        customer['documents'].addDocument(document)
     58        contract = createObject(u'waeup.SkeletonContract')
     59        contract .title = u'My first contract'
     60        customer['contracts'].addContract(contract)
    4561        self.customer = customer
    4662        self.document = document
     63        self.contract = contract
    4764        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
    4865        return
    4966
    50     def test_export_reimport_all(self):
     67    def test_export_reimport_customers(self):
     68        # we can export all customers in a portal
     69        # set values we can expect in export file
     70        self.setup_for_export()
     71        exporter = SkeletonCustomerExporter()
     72        exporter.export_all(self.app, self.outfile)
     73        result = open(self.outfile, 'rb').read()
     74        self.assertEqual(result,
     75            'customer_id,email,firstname,lastname,middlename,phone,'
     76            'reg_number,sex,suspended,suspended_comment,password,state,history\r\n'
     77            'K1000000,,Beate,Mueller,,,123,f,0,,,started,[]\r\n')
     78        # We can reimport the file ...
     79        processor = SkeletonCustomerProcessor()
     80        result = processor.doImport(
     81            self.outfile,
     82            ['customer_id','email','firstname','lastname','middlename','phone',
     83            'reg_number','sex','suspended','suspended_comment','password','state'],
     84            mode='create')
     85        num, num_fail, finished_path, failed_path = result
     86        self.assertEqual(num_fail,1)
     87        # ... if we remove the original customer.
     88        del self.app['customers']['K1000000']
     89        result = processor.doImport(
     90            self.outfile,
     91            ['customer_id','email','firstname','lastname','middlename','phone',
     92            'reg_number','sex','suspended','suspended_comment','password','state'],
     93            mode='create')
     94        num_succ, num_fail, finished_path, failed_path = result
     95        self.assertEqual(num_fail,0)
     96        # We can import the same file in update mode if we ignore the reg_number.
     97        result = processor.doImport(
     98            self.outfile,
     99            ['customer_id','email','firstname','lastname','middlename','phone',
     100            'xx_reg_number','sex','suspended','suspended_comment','password','state'],
     101            mode='update')
     102        num_succ, num_fail, finished_path, failed_path = result
     103        self.assertEqual(num_succ,1)
     104        self.assertEqual(num_fail,0)
     105        return
     106
     107    def test_export_reimport_documents(self):
    51108        # we can export all documents in a portal
    52109        # set values we can expect in export file
     
    61118            'created,My first document,K1000000\r\n'
    62119            % self.document.document_id)
    63         # We can import the file if we change the header (user_id -> customer_id)
     120        # We can reimport the file if we change the header (user_id -> customer_id)
    64121        processor = SkeletonCustomerDocumentProcessor()
    65122        open(self.outfile, 'wb').write(
     
    72129            mode='create')
    73130        num, num_fail, finished_path, failed_path = result
    74         #content = open(failed_path).read()
    75131        # The object exists.
    76132        self.assertEqual(num_fail,1)
     
    84140        self.assertEqual(num_fail,0)
    85141        # We can import the same file in update mode.
    86         processor = SkeletonCustomerDocumentProcessor()
    87142        result = processor.doImport(
    88143            self.outfile,
     
    93148        self.assertEqual(num_fail,0)
    94149        return
     150
     151    def test_export_reimport_contracts(self):
     152        # we can export all contracts in a portal
     153        # set values we can expect in export file
     154        self.setup_for_export()
     155        exporter = SkeletonContractExporter()
     156        exporter.export_all(self.app, self.outfile)
     157        result = open(self.outfile, 'rb').read()
     158        self.assertMatches(result,
     159            'class_name,contract_category,contract_id,document_object,'
     160            'history,last_product_id,product_object,state,title,user_id\r\n'
     161            'SkeletonContract,sample,%s,,'
     162            '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']'
     163            ',,,created,My first contract,K1000000\r\n'
     164            % self.contract.contract_id)
     165        # We can reimport the file if we change the header (user_id -> customer_id)
     166        processor = SkeletonContractProcessor()
     167        open(self.outfile, 'wb').write(
     168            'class_name,contract_category,contract_id,document_object,'
     169            'history,last_product_id,product_object,state,title,customer_id\r\n'
     170            'SkeletonContract,sample,%s,,'
     171            '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']'
     172            ',,,created,My first contract,K1000000\r\n'
     173            % self.contract.contract_id)
     174        result = processor.doImport(
     175            self.outfile,
     176            ['class_name','contract_category','contract_id','document_object',
     177            'history','last_product_id','product_object','state','title','customer_id'],
     178            mode='create')
     179        num, num_fail, finished_path, failed_path = result
     180        # The object exists.
     181        self.assertEqual(num_fail,1)
     182        # We remove the original contract.
     183        del self.customer['contracts'][self.contract.contract_id]
     184        result = processor.doImport(
     185            self.outfile,
     186            ['class_name','contract_category','contract_id','document_object',
     187            'history','last_product_id','product_object','state','title','customer_id'],
     188            mode='create')
     189        num_succ, num_fail, finished_path, failed_path = result
     190        self.assertEqual(num_fail,0)
     191        # We can import the same file in update mode.
     192        result = processor.doImport(
     193            self.outfile,
     194            ['class_name','contract_category','contract_id','document_object',
     195            'history','last_product_id','product_object','state','title','customer_id'],
     196            mode='update')
     197        num_succ, num_fail, finished_path, failed_path = result
     198        self.assertEqual(num_succ,1)
     199        self.assertEqual(num_fail,0)
     200        return
Note: See TracChangeset for help on using the changeset viewer.