From 755db8f71eba05535b5b0e2188ac304f90cc7b4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Nowak?= <luke@nexedi.com>
Date: Tue, 7 Apr 2009 12:22:06 +0000
Subject: [PATCH]  - remove not needed patch to BTreeFolder2  - add fix method
 for BTree if its _folder_handler is set to string  - add tests:    - to check
 if it is possible to recover functionality of isBTree and isHBTree methods
 using _fixFolderHandler    - that BTree after adding content is still BTree
 while using isBTree (same for HBTree)

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@26327 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/Core/Folder.py               | 15 ++++
 product/ERP5Type/patches/BTreeFolder2.py      |  7 --
 product/ERP5Type/tests/testFolderMigration.py | 76 +++++++++++++++++++
 3 files changed, 91 insertions(+), 7 deletions(-)

diff --git a/product/ERP5Type/Core/Folder.py b/product/ERP5Type/Core/Folder.py
index f3644af51a..a6a2b02961 100644
--- a/product/ERP5Type/Core/Folder.py
+++ b/product/ERP5Type/Core/Folder.py
@@ -581,6 +581,21 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
     else:
       return CMFBTreeFolder._fixCount(self)
 
+  def _fixFolderHandler(self):
+    """Fixes _folder_handler if it is a string
+
+    Bug affecting BTree folders in ERP5Type/patches/Folder.py introduced
+    string value for _folder_handler, which mades methods isBTree and isHBTree
+    fail.
+
+    Returns True in case of founded and fixed error, in case
+    of no error returns False.
+    """
+    if isinstance(self._folder_handler,str):
+      delattr(self, '_folder_handler')
+      return True
+    return False
+
   def manage_cleanup(self):
     """Calls self._cleanup() and reports the result as text.
     """
diff --git a/product/ERP5Type/patches/BTreeFolder2.py b/product/ERP5Type/patches/BTreeFolder2.py
index 528f8f11bb..38ccba6946 100644
--- a/product/ERP5Type/patches/BTreeFolder2.py
+++ b/product/ERP5Type/patches/BTreeFolder2.py
@@ -97,10 +97,3 @@ def _cleanup(self):
 
 BTreeFolder2Base._cleanup = _cleanup
 
-def __init__(self, id=None):
-  self._folder_handler = 'CMFBTreeFolderHandler'
-  if id is not None:
-      self.id = id
-  self._initBTrees()
-
-BTreeFolder2Base.__init__ = __init__
diff --git a/product/ERP5Type/tests/testFolderMigration.py b/product/ERP5Type/tests/testFolderMigration.py
index bdb0f4e8f8..78fdef59ad 100644
--- a/product/ERP5Type/tests/testFolderMigration.py
+++ b/product/ERP5Type/tests/testFolderMigration.py
@@ -146,6 +146,19 @@ class TestFolderMigration(ERP5TypeTestCase, LogInterceptor):
       self.assertEqual(self.folder.isHBTree(), False)
       
 
+    def test_03a_filledFolderIsBtree(self, quiet=0, run=1):
+      """
+      Test the folder is a BTree
+      """
+      if not run : return
+      if not quiet:
+        message = 'Test FilledFolderIsBtree'
+        LOG('Testing... ', 0, message)
+      self.folder.newContent()
+      self.assertRaises(NotImplementedError, self.folder.getTreeIdList)
+      self.assertEqual(self.folder.isBTree(), True)
+      self.assertEqual(self.folder.isHBTree(), False)
+
     def test_04_migrateEmptyFolder(self, quiet=0, run=1):
       """
       migrate empty folder from btree to hbtree
@@ -173,6 +186,10 @@ class TestFolderMigration(ERP5TypeTestCase, LogInterceptor):
       date = DateTime().Date()
       date = date.replace("/", "")
       self.failUnless(date in obj1.getId())
+      # check we still have a hbtree
+      self.assertEqual(self.folder.isBTree(), False)
+      self.assertEqual(self.folder.isHBTree(), True)
+      self.assertEqual(len(self.folder.objectIds()), 1)      
       
     def test_05_migrateFolderWithoutIdChange(self, quiet=0, run=1):
       """
@@ -493,6 +510,65 @@ class TestFolderMigration(ERP5TypeTestCase, LogInterceptor):
       self.assertEqual(len(self.folder.objectValues(base_id=id_prefix)), 3)
       
 
+    def test_13_wrongFolderHandlerFix(self, quiet=0, run=1):
+      if not run : return
+      if not quiet:
+        message = 'Test migrateFolder'
+        LOG('Testing... ', 0, message)
+
+      self.assertEqual(self.folder.isBTree(), True)
+      self.assertEqual(self.folder.isHBTree(), False)
+
+      setattr(self.folder,'_folder_handler','VeryWrongHandler')
+      get_transaction().commit()
+      self.tic()
+
+      self.assertEqual(self.folder.isBTree(), False)
+      self.assertEqual(self.folder.isHBTree(), False)
+
+      self.assertEquals(self.folder._fixFolderHandler(), True)
+      get_transaction().commit()
+
+      self.assertEqual(self.folder.isBTree(), True)
+      self.assertEqual(self.folder.isHBTree(), False)
+
+      self.folder.migrateToHBTree()
+      get_transaction().commit()
+      self.tic()
+     
+      self.assertEqual(self.folder.isBTree(), False)
+      self.assertEqual(self.folder.isHBTree(), True)
+
+    def test_14_wrongFolderHandlerMigrate(self, quiet=0, run=1):
+      if not run : return
+      if not quiet:
+        message = 'Test migrateFolder'
+        LOG('Testing... ', 0, message)
+
+      self.assertEqual(self.folder.isBTree(), True)
+      self.assertEqual(self.folder.isHBTree(), False)
+
+      setattr(self.folder,'_folder_handler','VeryWrongHandler')
+      get_transaction().commit()
+      self.tic()
+
+      self.assertEqual(self.folder.isBTree(), False)
+      self.assertEqual(self.folder.isHBTree(), False)
+
+      self.folder.migrateToHBTree()
+      get_transaction().commit()
+      self.tic()
+
+      self.assertEqual(self.folder.isBTree(), False)
+      self.assertEqual(self.folder.isHBTree(), True)
+
+      self.folder.newContent()
+      get_transaction().commit()
+      self.tic()
+
+      self.assertEqual(self.folder.isBTree(), False)
+      self.assertEqual(self.folder.isHBTree(), True)
+
 def test_suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestFolderMigration))
-- 
2.30.9