## $Id: test_browser.py 12371 2015-01-03 07:38:07Z 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
##
"""
Document browser and functional tests.
"""
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.documents.tests.test_batching import DocumentImportExportSetup
from ikobacustom.pcn.documents.document import PCNDocument
from ikobacustom.pcn.documents.export import PCNDocumentExporter
from ikobacustom.pcn.documents.batching import PCNDocumentProcessor
from ikobacustom.pcn.testing import FunctionalLayer


class DocumentImportExportTest(DocumentImportExportSetup):

    layer = FunctionalLayer

    def setup_for_export(self):
        document = PCNDocument()
        document.document_id = u'DOC1'
        document.title = u'My first document'
        self.app['documents'][document.document_id] = self.document = document
        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
        return

    def test_export_reimport_all(self):
        # we can export all documents in a portal
        # set values we can expect in export file
        self.setup_for_export()
        exporter = PCNDocumentExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(result,
            'class_name,description,document_id,history,state,title,'
            'users_with_local_roles\r\n'
            'PCNDocument,,DOC1,'
            '[u\'2014-12-21 23:27:15 WAT - Document created by system\'],'
            'created,My first document,[]\r\n')
        # We can import the same file.
        processor = PCNDocumentProcessor()
        result = processor.doImport(
            self.outfile,
            ['class_name','description','document_id','history','state',
             'title','users_with_local_roles'],
            mode='create')
        num, num_fail, finished_path, failed_path = result
        #content = open(failed_path).read()
        # The object exists.
        self.assertEqual(num_fail,1)
        # We remove the original document.
        del self.app['documents']['DOC1']
        result = processor.doImport(
            self.outfile,
            ['class_name','description','document_id','history','state',
             'title','users_with_local_roles'],
            mode='create')
        num_succ, num_fail, finished_path, failed_path = result
        self.assertEqual(num_fail,0)
        # We can import the same file in update mode.
        processor = PCNDocumentProcessor()
        result = processor.doImport(
            self.outfile,
            ['class_name','description','document_id','history','state',
             'title','users_with_local_roles'],
            mode='update')
        num_succ, num_fail, finished_path, failed_path = result
        self.assertEqual(num_succ,1)
        self.assertEqual(num_fail,0)
        return