[Erp5-report] r17504 - /erp5/trunk/products/ERP5Type/tests/

nobody at svn.erp5.org nobody at svn.erp5.org
Fri Nov 9 19:18:15 CET 2007


Author: jerome
Date: Fri Nov  9 19:18:14 2007
New Revision: 17504

URL: http://svn.erp5.org?rev=17504&view=rev
Log:
Check that we don't store persistent object in caching methods. The cache will
only be enabled in unit tests.
It uses pickle to check if the object is persistent or not, I'm too lazy to do
this by iterating in gc.get_referents graphs.

Modified:
    erp5/trunk/products/ERP5Type/tests/ERP5TypeTestCase.py
    erp5/trunk/products/ERP5Type/tests/testCacheTool.py

Modified: erp5/trunk/products/ERP5Type/tests/ERP5TypeTestCase.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/tests/ERP5TypeTestCase.py?rev=17504&r1=17503&r2=17504&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/tests/ERP5TypeTestCase.py (original)
+++ erp5/trunk/products/ERP5Type/tests/ERP5TypeTestCase.py Fri Nov  9 19:18:14 2007
@@ -779,5 +779,21 @@
   from Products.CMFCore.FSPythonScript import FSPythonScript
   FSPythonScript._compile = PythonScript_compile
 
-
 optimize()
+
+
+def fortify():
+  '''Add some extra checks that we don't have at runtime, not to slow down the
+  system.
+  '''
+  # check that we don't store persistent objects in cache
+  from pickle import dumps
+  from Products.ERP5Type.CachePlugins.BaseCache import CacheEntry
+  CacheEntry__init__ = CacheEntry.__init__
+  def __init__(self, value, *args, **kw):
+    # this will raise TypeError if you try to cache a persistent object
+    dumps(value)
+    CacheEntry__init__(self, value, *args, **kw)
+  CacheEntry.__init__ = __init__
+
+fortify()

Modified: erp5/trunk/products/ERP5Type/tests/testCacheTool.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/tests/testCacheTool.py?rev=17504&r1=17503&r2=17504&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/tests/testCacheTool.py (original)
+++ erp5/trunk/products/ERP5Type/tests/testCacheTool.py Fri Nov  9 19:18:14 2007
@@ -277,6 +277,23 @@
       ## Cache  cleared shouldn't be previously cached
       self.assert_(1.0 < calculation_time)
 
+  def test_CachePersistentObjects(self):
+    # storing persistent objects in cache is not allowed, but this check is
+    # only performed in unit tests.
+    from Products.ERP5Type.Cache import CachingMethod
+    def func():
+      # return a persistent object
+      return self.portal
+    cached_func = CachingMethod(func, 'cache_persistent_obj')
+    self.assertRaises(TypeError, cached_func)
+
+    def func():
+      # return a method bound on a persistent object
+      return self.portal.getTitle
+    cached_func = CachingMethod(func, 'cache_bound_method')
+    self.assertRaises(TypeError, cached_func)
+    
+
 def test_suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestCacheTool))




More information about the Erp5-report mailing list