[Erp5-report] r31240 fabien - in /erp5/trunk/products/ERP5OOo/tests: ./ test_document/

nobody at svn.erp5.org nobody at svn.erp5.org
Fri Dec 11 14:00:11 CET 2009


Author: fabien
Date: Fri Dec 11 14:00:05 2009
New Revision: 31240

URL: http://svn.erp5.org?rev=31240&view=rev
Log:
- add TODO
- add a method to get the odf document from the printout
- improve the way to validates original documents
- in test_09_FieldReplacement, validate generated documents
- initial import of testFormPrintoutAsODG.py. For now, it just contain a test
for text fields
- add test file Foo_001.odg

Added:
    erp5/trunk/products/ERP5OOo/tests/testFormPrintoutAsODG.py
    erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odg   (with props)
Modified:
    erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py

Modified: erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py?rev=31240&r1=31239&r2=31240&view=diff
==============================================================================
--- erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py [utf8] (original)
+++ erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py [utf8] Fri Dec 11 14:00:05 2009
@@ -37,9 +37,14 @@
 from Products.ERP5OOo.OOoUtils import OOoBuilder
 from Products.ERP5OOo.tests.utils import Validator
 from Products.ERP5Type.tests.utils import FileUpload
-from zLOG import LOG , INFO
+from StringIO import StringIO
 from lxml import etree
 import os
+
+# TODO : move code specific to ODT document to another file like
+# testFormPrintoutAsODT which inherite from this class.
+# This class should contain only common parts to all ODF type tests (odg, odt,
+# ods, ...).
 
 class TestFormPrintout(ERP5TypeTestCase):
   run_all_test = 1
@@ -146,21 +151,20 @@
     #default_pref.setPreferredConversionCacheFactory('document_cache_factory')
     if default_pref.getPreferenceState() != 'global':
       default_pref.enable()
-
  
   def _validate(self, odf_file_data):
     error_list = self.validator.validate(odf_file_data)
     if error_list:
       self.fail(''.join(error_list))
 
-  def _validateFromPrintout(self, document, printout_form):
-    '''validate test document before to modify it
+  def getODFDocumentFromPrintout(self, printout_form):
+    '''return odf document from the printout
     '''
-    document_file = getattr(document, printout_form.template, None)
+    document_file = getattr(self.portal, printout_form.template, None)
+    document_file = StringIO(document_file).read()
     if document_file is not None:
-      return self._validate(document_file.data)
-    raise ValueError ('%s template not found for %s' % (printout_form.template,
-        document))
+      return document_file
+    raise ValueError ('%s template not found' % printout_form.template)
 
   def test_01_Paragraph(self, run=run_all_test):
     """
@@ -179,7 +183,7 @@
 
     # test target
     foo_printout = portal.foo_module.test1.Foo_viewAsPrintout
-    self._validateFromPrintout(portal.foo_module.test1, foo_printout)
+    self._validate(self.getODFDocumentFromPrintout(foo_printout))
 
     request = self.app.REQUEST
     # 1. Normal case: "my_title" field to the "my_title" reference in the ODF document
@@ -870,7 +874,7 @@
                                    title='',
                                    form_name='FooReport_view',
                                    template='Foo2_getODTStyleSheet')
-    self._validateFromPrintout(test1, foo_report_printout)
+    self._validate(self.getODFDocumentFromPrintout(foo_report_printout))
     odf_document = foo_report_printout()
 
     #test_output = open("/tmp/test_04_Iteratoin.odf", "w")
@@ -975,7 +979,7 @@
                                    title='',
                                    form_name='FooReport_view',
                                    template='Foo3_getODTStyleSheet')
-    self._validateFromPrintout(test1, foo_report_printout)
+    self._validate(self.getODFDocumentFromPrintout(foo_report_printout))
     odf_document = foo_report_printout()
 
     #test_output = open("/tmp/test_04_Iteratoin_02_Section_01.odf", "w")
@@ -1088,7 +1092,7 @@
                                    title='',
                                    form_name='Foo2_view',
                                    template='Foo4_getODTStyleSheet')
-    self._validateFromPrintout(test1, foo_report_printout)
+    self._validate(self.getODFDocumentFromPrintout(foo_report_printout))
     odf_document = foo_report_printout()
 
     # test_output = open("/tmp/test_04_Iteratoin_03_Section_01.odf", "w")
@@ -1220,7 +1224,7 @@
   def test_09_FieldReplacement(self, run=run_all_test):
     """test field in ODF Documents"""
     foo_printout = self.portal.foo_module.test1.Foo5_viewAsPrintout
-    self._validateFromPrintout(self.portal.foo_module.test1, foo_printout)
+    self._validate(self.getODFDocumentFromPrintout(foo_printout))
     foo_form = self.portal.foo_module.test1.Foo_view
     field_name = 'your_checkbox'
     if foo_form._getOb(field_name, None) is None:
@@ -1229,6 +1233,7 @@
 
     checkbox.values['default'] = 1
     odf_document = foo_printout()
+    self._validate(odf_document)
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     document_tree = etree.XML(content_xml)
@@ -1237,6 +1242,7 @@
 
     checkbox.values['default'] = 0
     odf_document = foo_printout()
+    self._validate(odf_document)
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     document_tree = etree.XML(content_xml)

Added: erp5/trunk/products/ERP5OOo/tests/testFormPrintoutAsODG.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/tests/testFormPrintoutAsODG.py?rev=31240&view=auto
==============================================================================
--- erp5/trunk/products/ERP5OOo/tests/testFormPrintoutAsODG.py (added)
+++ erp5/trunk/products/ERP5OOo/tests/testFormPrintoutAsODG.py [utf8] Fri Dec 11 14:00:05 2009
@@ -1,0 +1,236 @@
+##############################################################################
+# -*- coding: utf-8 -*-
+# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
+#                    Fabien Morin <fabien at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
+# USA.
+#
+##############################################################################
+
+import unittest
+import transaction
+from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
+from Products.ERP5OOo.tests.testFormPrintout import TestFormPrintout
+from Products.ERP5OOo.OOoUtils import OOoBuilder
+from Products.ERP5OOo.tests.utils import Validator
+from lxml import etree
+import os
+
+class TestFormPrintoutAsODG(TestFormPrintout):
+  run_all_test = 1
+
+  def getTitle(self):
+    """
+      Return the title of the current test set.
+    """
+    return "FormPrintoutAsODG"
+
+  def afterSetUp(self):
+    self.login()
+    # XML validator
+    v12schema_url = os.path.join(os.path.dirname(__file__),
+                                 'OpenDocument-schema-v1.2-draft9.rng')
+    self.validator = Validator(schema_url=v12schema_url)
+
+    foo_file_path = os.path.join(os.path.dirname(__file__),
+                                'test_document',
+                                'Foo_001.odg')
+    foo_file = open(foo_file_path, 'rb')
+    self._validate(foo_file.read())
+    custom = self.portal.portal_skins.custom
+    addStyleSheet = custom.manage_addProduct['OFSP'].manage_addFile
+    if custom._getOb('Foo_getODGStyleSheet', None) is None:
+      addStyleSheet(id='Foo_getODGStyleSheet', file=foo_file, title='',
+                    precondition='',
+                    content_type='application/vnd.oasis.opendocument.graphics')
+    erp5OOo = custom.manage_addProduct['ERP5OOo']
+    addOOoTemplate = erp5OOo.addOOoTemplate
+    if custom._getOb('Foo_viewAsOdg', None) is None:
+      addOOoTemplate(id='Foo_viewAsOdg', title='')
+    request = self.app.REQUEST
+    Foo_viewAsOdg = custom.Foo_viewAsOdg
+    Foo_viewAsOdg.doSettings(request, title='', xml_file_id='content.xml',
+                             ooo_stylesheet='Foo_getODGStyleSheet')
+    builder = OOoBuilder(foo_file)
+    content = builder.extract('content.xml')
+    Foo_viewAsOdg.pt_edit(content,
+        content_type='application/vnd.oasis.opendocument.graphics')
+    if custom._getOb('Foo_viewAsODGPrintout', None) is None:
+      erp5OOo.addFormPrintout(id='Foo_viewAsODGPrintout', title='',
+                              form_name='Foo_view', template='Foo_getODGStyleSheet')
+    if custom._getOb('FooReport_viewAsODGPrintout', None) is None:
+      erp5OOo.addFormPrintout(id='FooReport_viewAsODGPrintout',
+                              title='')
+
+    ## append 'test1' data to a listbox
+    foo_module = self.portal.foo_module
+    if foo_module._getOb('test1', None) is None:
+      foo_module.newContent(id='test1', portal_type='Foo')
+    test1 =  foo_module.test1
+    if test1._getOb("foo_1", None) is None:
+      test1.newContent("foo_1", portal_type='Foo Line')
+    if test1._getOb("foo_2", None) is None:
+      test1.newContent("foo_2", portal_type='Foo Line')
+    transaction.commit()
+    self.tic()
+
+  def getStyleDictFromFieldName(self, content_xml, field_id):
+    '''parse content_xml string and return a dict with node node.tag
+    as key and style dict as value
+    '''
+    element_tree = etree.XML(content_xml)
+    text_xpath = '//draw:frame[@draw:name="%s"]/*' % field_id
+    node_list = element_tree.xpath(text_xpath, namespaces=element_tree.nsmap)
+    style_dict = {}
+    for target_node in node_list:
+      style_dict = {}
+      for descendant in target_node.iterdescendants():
+        style_dict.setdefault(descendant.tag, {}).update(descendant.attrib)
+    return style_dict
+
+  def test_01_TextField(self):
+    """
+    mapping a field to textbox
+    """
+    portal = self.getPortal()
+    foo_module = self.portal.foo_module
+    if foo_module._getOb('test1', None) is None:
+      foo_module.newContent(id='test1', portal_type='Foo')
+    test1 =  foo_module.test1
+    test1.setTitle('Foo title!')
+    transaction.commit()
+    self.tic()
+
+    style_dict = {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}span':
+                    {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name': 'T2'},
+                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p':
+                    {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name': 'P2'}
+                 }
+
+    # test target
+    foo_printout = portal.foo_module.test1.Foo_viewAsODGPrintout
+    original_file_content = self.getODFDocumentFromPrintout(foo_printout)
+    self._validate(original_file_content)
+
+    # extract content.xml from original odg document
+    original_doc_builder = OOoBuilder(original_file_content)
+    original_content_xml = original_doc_builder.extract("content.xml")
+    # get style of the title in the orignal test document
+    original_document_style_dict = self.getStyleDictFromFieldName(original_content_xml,
+        'my_title')
+
+    # check the style is good before the odg generation
+    self.assertEqual(original_document_style_dict, style_dict)
+
+    request = self.app.REQUEST
+    # 1. Normal case: "my_title" field to the "my_title" reference in the ODF document
+    self.portal.changeSkin('ODT')
+    odf_document = foo_printout.index_html(REQUEST=request)
+    self.assertTrue(odf_document is not None)
+    builder = OOoBuilder(odf_document)
+    content_xml = builder.extract("content.xml")
+    final_document_style_dict = self.getStyleDictFromFieldName(content_xml,
+        'my_title')
+
+    # check the style is keept after the odg generation
+    self.assertEqual(final_document_style_dict, style_dict)
+
+    self.assertTrue(content_xml.find("Foo title!") > 0)
+    self.assertEqual(request.RESPONSE.getHeader('content-type'),
+                     'application/vnd.oasis.opendocument.graphics; charset=utf-8')
+    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
+                     'inline;filename="Foo_viewAsODGPrintout.odg"')
+    self._validate(odf_document)
+
+    # 2. Normal case: change the field value and check again the ODF document
+    test1.setTitle("Changed Title!")
+    #foo_form.my_title.set_value('default', "Changed Title!")
+    odf_document = foo_printout.index_html(REQUEST=request)
+    self.assertTrue(odf_document is not None)
+    builder = OOoBuilder(odf_document)
+    content_xml = builder.extract("content.xml")
+    self.assertTrue(content_xml.find("Changed Title!") > 0)
+    self._validate(odf_document)
+
+    # 3. False case: change the field name
+    test1.setTitle("you cannot find")
+    # rename id 'my_title' to 'xxx_title', then does not match in the ODF document
+    foo_form = portal.foo_module.test1.Foo_view
+    foo_form.manage_renameObject('my_title', 'xxx_title', REQUEST=request)
+    odf_document = foo_printout.index_html(REQUEST=request)
+    self.assertTrue(odf_document is not None)
+    builder = OOoBuilder(odf_document)
+    content_xml = builder.extract("content.xml")
+    self.assertFalse(content_xml.find("you cannot find") > 0)
+    self._validate(odf_document)
+    # put back
+    foo_form.manage_renameObject('xxx_title', 'my_title', REQUEST=request)
+
+    ## 4. False case: does not set a ODF template
+    self.assertTrue(foo_printout.template == 'Foo_getODGStyleSheet')
+    tmp_template = foo_printout.template
+    foo_printout.template = None
+    # template == None, causes a ValueError
+    try:
+      foo_printout.index_html(REQUEST=request)
+    except ValueError, e:
+      # e -> 'Can not create a ODF Document without a odf_template'
+      self.assertTrue(True)
+
+    # put back
+    foo_printout.template = tmp_template
+
+    # 5. Normal case: just call a FormPrintout object
+    request.RESPONSE.setHeader('Content-Type', 'text/html')
+    test1.setTitle("call!")
+    odf_document = foo_printout() # call
+    self.assertTrue(odf_document is not None)
+    builder = OOoBuilder(odf_document)
+    content_xml = builder.extract("content.xml")
+    self.assertTrue(content_xml.find("call!") > 0)
+    # when just call FormPrintout, it does not change content-type
+    self.assertEqual(request.RESPONSE.getHeader('content-type'), 'text/html')
+    self._validate(odf_document)
+
+    # 5. Normal case: utf-8 string
+    test1.setTitle("Français")
+    odf_document = foo_printout()
+    self.assertTrue(odf_document is not None)
+    builder = OOoBuilder(odf_document)
+    content_xml = builder.extract("content.xml")
+    self.assertTrue(content_xml.find("Français") > 0)
+    self._validate(odf_document)
+
+    # 6. Normal case: unicode string
+    test1.setTitle(u'Français test2')
+    odf_document = foo_printout()
+    self.assertTrue(odf_document is not None)
+    builder = OOoBuilder(odf_document)
+    content_xml = builder.extract("content.xml")
+    self.assertTrue(content_xml.find("Français test2") > 0)
+    self._validate(odf_document)
+
+def test_suite():
+  suite = unittest.TestSuite()
+  suite.addTest(unittest.makeSuite(TestFormPrintoutAsODG))
+  return suite

Added: erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odg
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odg?rev=31240&view=auto
==============================================================================
Binary file - no diff available.

Propchange: erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odg
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream




More information about the Erp5-report mailing list