From aaca1dcef495c004967c413e5cb23a187b77c19e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 9 Jan 2013 11:39:15 +0100
Subject: [PATCH] Move OOoParser tests to a simple unittest

---
 product/ERP5OOo/tests/testOOoImport.py |  68 +--------------
 product/ERP5OOo/tests/testOOoParser.py | 113 +++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 66 deletions(-)
 create mode 100644 product/ERP5OOo/tests/testOOoParser.py

diff --git a/product/ERP5OOo/tests/testOOoImport.py b/product/ERP5OOo/tests/testOOoImport.py
index 21a7a71fd6..148096f3b6 100644
--- a/product/ERP5OOo/tests/testOOoImport.py
+++ b/product/ERP5OOo/tests/testOOoImport.py
@@ -33,26 +33,18 @@ import os
 from AccessControl.SecurityManagement import newSecurityManager
 from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
 from Products.ERP5Type.tests.ERP5TypeTestCase import _getConversionServerDict
+from Products.ERP5Type.tests.utils import FileUpload
 from Products.ERP5Type.tests.Sequence import SequenceList
 from Products.ERP5OOo.OOoUtils import OOoParser
 from Products.ERP5Form.PreferenceTool import Priority
 from DateTime import DateTime
 
-class FileUploadTest(file):
-
-  __allow_access_to_unprotected_subobjects__=1
-
-  def __init__(self, path, name):
-    self.filename = name
-    file.__init__(self, path, 'rb')
-    self.headers = {}
-
 def makeFilePath(name):
   return os.path.join(os.path.dirname(__file__), 'test_document', name)
 
 def makeFileUpload(name):
   path = makeFilePath(name)
-  return FileUploadTest(path, name)
+  return FileUpload(path, name)
 
 class TestOOoImportMixin(ERP5TypeTestCase):
   gender_base_cat_id    = 'gender'
@@ -766,62 +758,6 @@ class TestOOoImport(TestOOoImportMixin):
     self.assertEquals(['france'], list(region.europe.france.europe.objectIds()))
     self.assertEquals([], list(region.europe.france.europe.france.objectIds()))
 
-
-  # OOoParser tests
-  def test_getSpreadSheetMapping(self):
-    parser = OOoParser()
-    parser.openFile(open(makeFilePath('import_data_list.ods'), 'rb'))
-    mapping = parser.getSpreadsheetsMapping()
-    self.assertEquals(['Person'], mapping.keys())
-    person_mapping = mapping['Person']
-    self.assertTrue(isinstance(person_mapping, list))
-    self.assertTrue(102, len(person_mapping))
-    self.assertEquals(person_mapping[0],
-       ['Title', 'First Name', 'Last Name', 'Default Email Text'])
-    self.assertEquals(person_mapping[1],
-       ['John Doe 0', 'John', 'Doe 0', 'john.doe0@foo.com'])
-
-  def test_openFromString(self):
-    parser = OOoParser()
-    parser.openFromString(
-        open(makeFilePath('import_data_list.ods'), 'rb').read())
-    mapping = parser.getSpreadsheetsMapping()
-    self.assertEquals(['Person'], mapping.keys())
-
-  def test_getSpreadSheetMappingStyle(self):
-    parser = OOoParser()
-    parser.openFile(open(makeFilePath('import_data_list_with_style.ods'), 'rb'))
-    mapping = parser.getSpreadsheetsMapping()
-    self.assertEquals(['Feuille1'], mapping.keys())
-    self.assertEquals(mapping['Feuille1'][1],
-                      ['a line with style'])
-    self.assertEquals(mapping['Feuille1'][2],
-                      ['a line with multiple styles'])
-    self.assertEquals(mapping['Feuille1'][3],
-                      ['http://www.erp5.org'])
-    self.assertEquals(mapping['Feuille1'][4],
-                      ['john.doe@example.com'])
-
-  def test_getSpreadSheetMappingDataTypes(self):
-    parser = OOoParser()
-    parser.openFile(open(makeFilePath('import_data_list_data_type.ods'), 'rb'))
-    mapping = parser.getSpreadsheetsMapping()
-    self.assertEquals(['Feuille1'], mapping.keys())
-    self.assertEquals(mapping['Feuille1'][0],
-                      ['1234.5678'])
-    self.assertEquals(mapping['Feuille1'][1],
-                      ['1234.5678'])
-    self.assertEquals(mapping['Feuille1'][2],
-                      ['0.1'])
-    self.assertEquals(mapping['Feuille1'][3],
-                      ['2008-11-14'])
-    self.assertEquals(mapping['Feuille1'][4],
-                      ['2008-11-14T10:20:30']) # supported by DateTime
-    self.assertEquals(mapping['Feuille1'][5],
-                      ['PT12H34M56S']) # maybe not good, this is raw format
-    self.assertEquals(mapping['Feuille1'][6],
-                      ['With note'])
-
   # Base_getCategoriesSpreadSheetMapping tests
   def test_Base_getCategoriesSpreadSheetMapping(self):
     # test structure returned by Base_getCategoriesSpreadSheetMapping
diff --git a/product/ERP5OOo/tests/testOOoParser.py b/product/ERP5OOo/tests/testOOoParser.py
new file mode 100644
index 0000000000..c243d41027
--- /dev/null
+++ b/product/ERP5OOo/tests/testOOoParser.py
@@ -0,0 +1,113 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+# Copyright (c) 2012 Nexedi SA and Contributors. All Rights Reserved.
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability 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
+# garantees 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+##############################################################################
+
+
+import unittest
+import os
+from DateTime import DateTime
+
+from Products.ERP5OOo.OOoUtils import OOoParser
+
+
+def makeFilePath(name):
+  return os.path.join(os.path.dirname(__file__), 'test_document', name)
+
+class TestOOoParser(unittest.TestCase):
+  """ OOoParser tests
+  """
+  def test_getSpreadSheetMapping(self):
+    parser = OOoParser()
+    parser.openFile(open(makeFilePath('import_data_list.ods'), 'rb'))
+    mapping = parser.getSpreadsheetsMapping()
+    self.assertEquals(['Person'], mapping.keys())
+    person_mapping = mapping['Person']
+    self.assertTrue(isinstance(person_mapping, list))
+    self.assertTrue(102, len(person_mapping))
+    self.assertEquals(person_mapping[0],
+       ['Title', 'First Name', 'Last Name', 'Default Email Text'])
+    self.assertEquals(person_mapping[1],
+       ['John Doe 0', 'John', 'Doe 0', 'john.doe0@foo.com'])
+
+  def test_openFromString(self):
+    parser = OOoParser()
+    parser.openFromString(
+        open(makeFilePath('import_data_list.ods'), 'rb').read())
+    mapping = parser.getSpreadsheetsMapping()
+    self.assertEquals(['Person'], mapping.keys())
+
+  def test_getSpreadSheetMappingStyle(self):
+    parser = OOoParser()
+    parser.openFile(open(makeFilePath('import_data_list_with_style.ods'), 'rb'))
+    mapping = parser.getSpreadsheetsMapping()
+    self.assertEquals(['Feuille1'], mapping.keys())
+    self.assertEquals(mapping['Feuille1'][1],
+                      ['a line with style'])
+    self.assertEquals(mapping['Feuille1'][2],
+                      ['a line with multiple styles'])
+    self.assertEquals(mapping['Feuille1'][3],
+                      ['http://www.erp5.org'])
+    self.assertEquals(mapping['Feuille1'][4],
+                      ['john.doe@example.com'])
+
+  def test_getSpreadSheetMappingDataTypes(self):
+    parser = OOoParser()
+    parser.openFile(open(makeFilePath('import_data_list_data_type.ods'), 'rb'))
+    mapping = parser.getSpreadsheetsMapping()
+    self.assertEquals(['Feuille1'], mapping.keys())
+    self.assertEquals(mapping['Feuille1'][0],
+                      ['1234.5678'])
+    self.assertEquals(mapping['Feuille1'][1],
+                      ['1234.5678'])
+    self.assertEquals(mapping['Feuille1'][2],
+                      ['0.1'])
+    self.assertEquals(mapping['Feuille1'][3],
+                      ['2008-11-14'])
+    self.assertEquals(mapping['Feuille1'][4],
+                      ['2008-11-14T10:20:30']) # supported by DateTime
+    self.assertEquals(mapping['Feuille1'][5],
+                      ['PT12H34M56S']) # maybe not good, this is raw format
+    self.assertEquals(mapping['Feuille1'][6],
+                      ['With note'])
+
+  def test_BigSpreadSheet_can_be_parsed(self,):
+    """Test than OOoimport can parse a file with more than 40000 lines
+    """
+    parser = OOoParser()
+    parser.openFile(open(makeFilePath('import_big_spreadsheet.ods'), 'rb'))
+    mapping = parser.getSpreadsheetsMapping()
+    not_ok = 1
+    for spread, values in mapping.iteritems():
+      self.assertEquals(len(values), 41001)
+      not_ok = 0
+    if not_ok:
+      self.fail('Spreadsheet not read!')
+
+
+def test_suite():
+  suite = unittest.TestSuite()
+  suite.addTest(unittest.makeSuite(TestOOoParser))
+  return suite
-- 
2.30.9