diff --git a/product/ERP5Type/tests/testMemcachedTool.py b/product/ERP5Type/tests/testMemcachedTool.py index eb69a3da34047f0f5f19cda4b522a14fd804a0b8..b72e3e69f2babb5f6431a94e6454a749a72edd24 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 8c259ffa3eaa25f9f92760ea5c7ad0ccd89a90ff..bcad408328794a7e8a8dfdfc00a3c89bcdc81f06 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()) +