From 35c87db3b37cd9574b59147d76a12950a47da410 Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Mon, 11 Dec 2006 15:27:18 +0000
Subject: [PATCH] Use a monkey patch to automaticaly enable Memcached Tool. Add
 a test for non-string dictionary keys.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11668 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/tests/testMemcachedTool.py | 44 ++++++++++++++-------
 product/ERP5Type/tests/utils.py             | 16 +++++++-
 2 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/product/ERP5Type/tests/testMemcachedTool.py b/product/ERP5Type/tests/testMemcachedTool.py
index eb69a3da34..b72e3e69f2 100644
--- a/product/ERP5Type/tests/testMemcachedTool.py
+++ b/product/ERP5Type/tests/testMemcachedTool.py
@@ -28,17 +28,15 @@
 
 from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
 from Products.ERP5Type.Tool.MemcachedTool import MemcachedTool
-from Products.ERP5Type import allowMemcachedTool
 from AccessControl.SecurityManagement import newSecurityManager
+from Products.ERP5Type.tests.utils import installRealMemcachedTool
 
 class TestMemcachedTool(ERP5TypeTestCase):
   """
     Test MemcachedTool.
-    Note : MemcachedTool needs to be enabled to be tested.
-           This test will fail if it's not the case.
-    Note 2 : When writing tests, keep in mind that the test must not
-             give false positive or negative if the value already exists in
-             an existing memcached server.
+    Note : When writing tests, keep in mind that the test must not give false
+           positive or negative if the value already exists in an existing
+           memcached server.
   """
 
   def getBusinessTemplateList(self):
@@ -47,6 +45,10 @@ class TestMemcachedTool(ERP5TypeTestCase):
   def getTitle(self):
     return "MemcachedTool"
 
+  def setUp(self):
+    ERP5TypeTestCase.setUp(self)
+    installRealMemcachedTool(self.getPortal())
+
   def afterSetUp(self):
     self.login()
 
@@ -59,13 +61,6 @@ class TestMemcachedTool(ERP5TypeTestCase):
   def getMemcachedDict(self):
     return self.getPortal().portal_memcached.getMemcachedDict(key_prefix='unit_test')
 
-  def test_00_memcachedToolIsEnabled(self):
-    """
-      Tests that memcached tool is enabled. Otherwise, testing it is
-      pointless.
-    """
-    self.assertTrue(allowMemcachedTool)
-
   def test_01_dictionnaryIsUsable(self):
     """
       Check that the received class has the minimum requirements which makes
@@ -135,7 +130,28 @@ class TestMemcachedTool(ERP5TypeTestCase):
     self.assertTrue(tested_dict[tested_key] == tested_value)
     del tested_dict[tested_key]
     get_transaction().commit()
-    self.assertTrue(tested_dict[tested_key] is None)
+    try:
+      dummy = tested_dict[tested_key]
+    except KeyError:
+      pass
+    except:
+      self.fail('Wrong error type is raised when key is not found.')
+    else:
+      self.fail('No error is raised when key is not found.')
+
+  def test_06_checkNonStringKeyFails(self):
+    """
+      Tests that a non-string key is not accepted by SharedDict.
+    """
+    tested_dict = self.getMemcachedDict()
+    tested_key = tuple()
+    tested_value = 'test_value'
+    try:
+      tested_dict[tested_key] = tested_value
+    except TypeError:
+      pass
+    else:
+      self.fail('No error was raised when assigning a value to a non-string key.')
 
 if __name__ == '__main__':
   unittest.main()
diff --git a/product/ERP5Type/tests/utils.py b/product/ERP5Type/tests/utils.py
index 8c259ffa3e..bcad408328 100644
--- a/product/ERP5Type/tests/utils.py
+++ b/product/ERP5Type/tests/utils.py
@@ -89,4 +89,18 @@ def _recreateClassTool(portal):
   reload(ClassTool)
   portal.manage_delObjects(['portal_classes'])
   portal._setObject('portal_classes', ClassTool.ClassTool())
-  
+
+def installRealMemcachedTool(portal):
+  """Replaces portal_memcached by a real memcached tool object.
+  """
+  Products.ERP5Type.allowMemcachedTool = lambda: 1
+  _recreateMemcachedTool(portal)
+
+def _recreateMemcachedTool(portal):
+  """Recreate the memcached tool for this portal.
+  """
+  from Products.ERP5Type.Tool import MemcachedTool
+  reload(MemcachedTool)
+  portal.manage_delObjects(['portal_memcached'])
+  portal._setObject('portal_memcached', MemcachedTool.MemcachedTool())
+
-- 
2.30.9