[CMF-checkins] SVN: CMF/trunk/CMFCore/ - fixed IndexableObjectWrapper's __providedBy__ to allow the use of adapters

Yvo Schubbe y.2006_ at wcm-solutions.de
Sun Apr 2 17:14:03 EDT 2006


Log message for revision 66326:
  - fixed IndexableObjectWrapper's __providedBy__ to allow the use of adapters
  - added some basic IndexableObjectWrapper tests

Changed:
  U   CMF/trunk/CMFCore/CatalogTool.py
  U   CMF/trunk/CMFCore/tests/test_CatalogTool.py

-=-
Modified: CMF/trunk/CMFCore/CatalogTool.py
===================================================================
--- CMF/trunk/CMFCore/CatalogTool.py	2006-04-02 21:13:42 UTC (rev 66325)
+++ CMF/trunk/CMFCore/CatalogTool.py	2006-04-02 21:14:03 UTC (rev 66326)
@@ -22,6 +22,10 @@
 from Globals import InitializeClass
 from Products.ZCatalog.ZCatalog import ZCatalog
 from zope.interface import implements
+from zope.interface import providedBy
+from zope.interface.declarations import getObjectSpecification
+from zope.interface.declarations import ObjectSpecification
+from zope.interface.declarations import ObjectSpecificationDescriptor
 
 from ActionProviderBase import ActionProviderBase
 from interfaces import ICatalogTool
@@ -40,15 +44,34 @@
 from utils import UniqueObject
 
 
-class IndexableObjectWrapper:
+class IndexableObjectSpecification(ObjectSpecificationDescriptor):
 
+    def __get__(self, inst, cls=None):
+        if inst is None:
+            return getObjectSpecification(cls)
+        else:
+            provided = providedBy(inst._IndexableObjectWrapper__ob)
+            cls = type(inst)
+            return ObjectSpecification(provided, cls)
+
+
+class IndexableObjectWrapper(object):
+
     implements(IIndexableObjectWrapper)
     __implements__ = z2IIndexableObjectWrapper
+    __providedBy__ = IndexableObjectSpecification()
 
     def __init__(self, vars, ob):
         self.__vars = vars
         self.__ob = ob
 
+    def __str__(self):
+        try:
+            # __str__ is used to get the data of File objects
+            return self.__ob.__str__()
+        except AttributeError:
+            return object.__str__(self)
+
     def __getattr__(self, name):
         vars = self.__vars
         if vars.has_key(name):

Modified: CMF/trunk/CMFCore/tests/test_CatalogTool.py
===================================================================
--- CMF/trunk/CMFCore/tests/test_CatalogTool.py	2006-04-02 21:13:42 UTC (rev 66325)
+++ CMF/trunk/CMFCore/tests/test_CatalogTool.py	2006-04-02 21:14:03 UTC (rev 66326)
@@ -15,10 +15,8 @@
 $Id$
 """
 
-from unittest import TestCase, TestSuite, makeSuite, main
+import unittest
 import Testing
-import Zope2
-Zope2.startup()
 
 from AccessControl.SecurityManagement import newSecurityManager
 from DateTime import DateTime
@@ -30,52 +28,93 @@
 from Products.CMFCore.tests.base.testcase import SecurityTest
 
 
-class IndexableObjectWrapperTests(TestCase):
+class IndexableObjectWrapperTests(unittest.TestCase):
 
+    def _getTargetClass(self):
+        from Products.CMFCore.CatalogTool import IndexableObjectWrapper
+
+        return IndexableObjectWrapper
+
+    def _makeOne(self, *args, **kw):
+        return self._getTargetClass()(*args, **kw)
+
     def test_z2interfaces(self):
         from Interface.Verify import verifyClass
-        from Products.CMFCore.CatalogTool import IndexableObjectWrapper
         from Products.CMFCore.interfaces.portal_catalog \
                 import IndexableObjectWrapper as IIndexableObjectWrapper
 
-        verifyClass(IIndexableObjectWrapper, IndexableObjectWrapper)
+        verifyClass(IIndexableObjectWrapper, self._getTargetClass())
 
     def test_z3interfaces(self):
         from zope.interface.verify import verifyClass
-        from Products.CMFCore.CatalogTool import IndexableObjectWrapper
         from Products.CMFCore.interfaces import IIndexableObjectWrapper
 
-        verifyClass(IIndexableObjectWrapper, IndexableObjectWrapper)
+        verifyClass(IIndexableObjectWrapper, self._getTargetClass())
 
+    def test_allowedRolesAndUsers(self):
+        obj = DummyContent()
+        w = self._makeOne({}, obj)
+        self.assertEqual(w.allowedRolesAndUsers(), ['Manager'])
 
+    def test___str__(self):
+        obj = DummyContent('foo')
+        w = self._makeOne({}, obj)
+        self.assertEqual(str(w), str(obj))
+
+    def test_proxied_attributes(self):
+        obj = DummyContent('foo')
+        obj.title = 'Foo'
+        w = self._makeOne({}, obj)
+        self.assertEqual(w.getId(), 'foo')
+        self.assertEqual(w.Title(), 'Foo')
+
+    def test_vars(self):
+        obj = DummyContent()
+        w = self._makeOne({'bar': 1, 'baz': 2}, obj)
+        self.assertEqual(w.bar, 1)
+        self.assertEqual(w.baz, 2)
+
+    def test_provided(self):
+        from Products.CMFCore.interfaces import IContentish
+        from Products.CMFCore.interfaces import IIndexableObjectWrapper
+
+        obj = DummyContent()
+        w = self._makeOne({}, obj)
+        self.failUnless(IContentish.providedBy(w))
+        self.failUnless(IIndexableObjectWrapper.providedBy(w))
+
+
 class CatalogToolTests(SecurityTest):
 
-    def _makeOne(self, *args, **kw):
+    def _getTargetClass(self):
         from Products.CMFCore.CatalogTool import CatalogTool
 
-        return CatalogTool(*args, **kw)
+        return CatalogTool
 
+    def _makeOne(self, *args, **kw):
+        return self._getTargetClass()(*args, **kw)
+
     def test_z2interfaces(self):
         from Interface.Verify import verifyClass
-        from Products.CMFCore.CatalogTool import CatalogTool
         from Products.CMFCore.interfaces.portal_actions \
                 import ActionProvider as IActionProvider
         from Products.CMFCore.interfaces.portal_catalog \
                 import portal_catalog as ICatalogTool
         from Products.ZCatalog.IZCatalog import IZCatalog
 
-        verifyClass(IActionProvider, CatalogTool)
-        verifyClass(ICatalogTool, CatalogTool)
-        verifyClass(IZCatalog, CatalogTool)
+        verifyClass(IActionProvider, self._getTargetClass())
+        verifyClass(ICatalogTool, self._getTargetClass())
+        verifyClass(IZCatalog, self._getTargetClass())
 
     def test_z3interfaces(self):
         from zope.interface.verify import verifyClass
-        from Products.CMFCore.CatalogTool import CatalogTool
         from Products.CMFCore.interfaces import IActionProvider
         from Products.CMFCore.interfaces import ICatalogTool
+        from Products.ZCatalog.interfaces import IZCatalog
 
-        verifyClass(IActionProvider, CatalogTool)
-        verifyClass(ICatalogTool, CatalogTool)
+        verifyClass(IActionProvider, self._getTargetClass())
+        verifyClass(ICatalogTool, self._getTargetClass())
+        verifyClass(IZCatalog, self._getTargetClass())
 
     def loginWithRoles(self, *roles):
         user = UserWithRoles(*roles).__of__(self.root)
@@ -320,10 +359,10 @@
 
 
 def test_suite():
-    return TestSuite((
-        makeSuite(IndexableObjectWrapperTests),
-        makeSuite(CatalogToolTests),
+    return unittest.TestSuite((
+        unittest.makeSuite(IndexableObjectWrapperTests),
+        unittest.makeSuite(CatalogToolTests),
         ))
 
 if __name__ == '__main__':
-    main(defaultTest='test_suite')
+    unittest.main(defaultTest='test_suite')



More information about the CMF-checkins mailing list