From 7c22e2e334c3867f290d8d42998a4921ece07ed2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Tue, 19 Dec 2006 14:00:36 +0000
Subject: [PATCH] add a @reindex decorator to commit transaction and flush
 activites after calling the method automatically. Use it in some tests.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11762 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../ERP5/tests/testAccounting_l10n_fr_m9.py   | 14 +++++-------
 product/ERP5/tests/testInventoryAPI.py        | 20 +++++++----------
 product/ERP5Type/tests/utils.py               | 22 +++++++++++++++++++
 3 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/product/ERP5/tests/testAccounting_l10n_fr_m9.py b/product/ERP5/tests/testAccounting_l10n_fr_m9.py
index 5d4da673b3..1d3a936535 100644
--- a/product/ERP5/tests/testAccounting_l10n_fr_m9.py
+++ b/product/ERP5/tests/testAccounting_l10n_fr_m9.py
@@ -36,6 +36,7 @@ os.environ.setdefault('EVENT_LOG_FILE', 'zLOG.log')
 os.environ.setdefault('EVENT_LOG_SEVERITY', '-300')
 
 from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
+from Products.ERP5Type.tests.utils import reindex
 from AccessControl.SecurityManagement import newSecurityManager
 from Products.DCWorkflow.DCWorkflow import ValidationFailed
 
@@ -89,16 +90,16 @@ class TestAccounting_l10n_M9(ERP5TypeTestCase):
     self.category_tool = portal.portal_categories
     self.section = self._createOrganisation()
     self.mirror_section = self._createOrganisation()
-
+  
+  @reindex
   def _createOrganisation(self, **kw):
     """Create an organisation and index it.
     """
     org = self.getOrganisationModule().newContent(portal_type='Organisation')
     org.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return org
 
+  @reindex
   def _getAccount(self, account_id, **kw):
     """Get an account or create it.
     """
@@ -107,10 +108,9 @@ class TestAccounting_l10n_M9(ERP5TypeTestCase):
     if account is None:
       account = account_module.newContent(id=account_id)
     account.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return account
-
+  
+  @reindex
   def _createPurchaseInvoice(self, amount=100, **kw):
     """Create a purchase invoice and index it.
     """
@@ -130,8 +130,6 @@ class TestAccounting_l10n_M9(ERP5TypeTestCase):
                        source_value=payable_account,
                        quantity=-amount)
     invoice.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return invoice
   
   def test_TransmissionSheetModule(self):
diff --git a/product/ERP5/tests/testInventoryAPI.py b/product/ERP5/tests/testInventoryAPI.py
index 8c398fb625..7a362ae2da 100644
--- a/product/ERP5/tests/testInventoryAPI.py
+++ b/product/ERP5/tests/testInventoryAPI.py
@@ -42,6 +42,7 @@ from Testing import ZopeTestCase
 
 from Products.ERP5.Document.OrderRule import OrderRule
 from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
+from Products.ERP5Type.tests.utils import reindex
 
 # Needed in order to have a log file inside the current folder
 os.environ.setdefault('EVENT_LOG_FILE', 'zLOG.log')
@@ -162,34 +163,32 @@ class InventoryAPITestCase(ERP5TypeTestCase):
     return ('erp5_base', 'erp5_dummy_movement')
 
   # TODO: move this to a base class {{{
+  @reindex
   def _makeOrganisation(self, **kw):
     """Creates an organisation."""
     org = self.getPortal().organisation_module.newContent(
           portal_type='Organisation',
           **kw)
-    get_transaction().commit()
-    self.tic()
     return org
 
+  @reindex
   def _makeSalePackingList(self, **kw):
     """Creates a sale packing list."""
     spl = self.getPortal().sale_packing_list_module.newContent(
           portal_type='Sale Packing List',)
     spl.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return spl
   
+  @reindex
   def _makeSaleInvoice(self, created_by_builder=0, **kw):
     """Creates a sale invoice."""
     sit = self.getPortal().accounting_module.newContent(
           portal_type='Sale Invoice Transaction',
           created_by_builder=created_by_builder)
     sit.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return sit
 
+  @reindex
   def _makeCurrency(self, **kw):
     """Creates a currency."""
     currency = self.getCurrencyModule().newContent(
@@ -200,6 +199,7 @@ class InventoryAPITestCase(ERP5TypeTestCase):
   _makeResource = _makeCurrency
   # }}}
 
+  @reindex
   def _makeMovement(self, **kw):
     """Creates a movement.
     """
@@ -210,10 +210,9 @@ class InventoryAPITestCase(ERP5TypeTestCase):
     kw.setdefault('source_value', self.mirror_node)
     kw.setdefault('resource_value', self.resource)
     mvt.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return mvt
-
+  
+  @reindex
   def _makeSimulationMovement(self, **kw):
     """Creates a simulation movement.
     """
@@ -227,8 +226,6 @@ class InventoryAPITestCase(ERP5TypeTestCase):
     kw.setdefault('source_value', self.mirror_node)
     kw.setdefault('resource_value', self.resource)
     mvt.edit(**kw)
-    get_transaction().commit()
-    self.tic()
     return mvt
 
 # }}}
@@ -240,7 +237,6 @@ class TestInventory(InventoryAPITestCase):
   
   def testReturnedTypeIsFloat(self):
     """getInventory returns a float"""
-    # XXX it may return a Decimal some day
     getInventory = self.getSimulationTool().getInventory
     self.assertEquals(type(getInventory()), type(0.1))
     # default is 0
diff --git a/product/ERP5Type/tests/utils.py b/product/ERP5Type/tests/utils.py
index bcad408328..20cdb33c7b 100644
--- a/product/ERP5Type/tests/utils.py
+++ b/product/ERP5Type/tests/utils.py
@@ -32,6 +32,7 @@
 import Products.ERP5Type
 from Products.MailHost.MailHost import MailHost
 
+# dummy objects
 class DummyMailHost(MailHost):
   """Dummy Mail Host that doesn't really send messages and keep a copy in
   _last_message attribute.
@@ -46,6 +47,7 @@ class DummyMailHost(MailHost):
     """Record message in _last_message."""
     self._last_message = (mfrom, mto, messageText)
 
+# python scripts
 def createZODBPythonScript(container, script_id, script_params,
                            script_content):
   """Creates a Python script `script_id` in the given `container`, with
@@ -70,6 +72,7 @@ def removeZODBPythonScript(container, script_id):
   """
   container.manage_delObjects([script_id])
 
+# class tool
 def installRealClassTool(portal):
   """Replaces portal_classes by a real class tool object.
   """
@@ -90,6 +93,7 @@ def _recreateClassTool(portal):
   portal.manage_delObjects(['portal_classes'])
   portal._setObject('portal_classes', ClassTool.ClassTool())
 
+# memcache tool
 def installRealMemcachedTool(portal):
   """Replaces portal_memcached by a real memcached tool object.
   """
@@ -104,3 +108,21 @@ def _recreateMemcachedTool(portal):
   portal.manage_delObjects(['portal_memcached'])
   portal._setObject('portal_memcached', MemcachedTool.MemcachedTool())
 
+# decorators
+class reindex(object):
+  """Decorator to commit transaction and flush activities after the method is
+  called.
+  """
+  def __init__(self, func):
+    self._func = func
+
+  def __get__(self, instance, cls=None):
+    self._instance = instance
+    return self
+  
+  def __call__(self, *args, **kw):
+    ret = self._func(self._instance, *args, **kw)
+    get_transaction().commit()
+    self._instance.tic()
+    return ret
+
-- 
2.30.9