[Zope3-checkins] CVS: Zope3/src/zope/app/container/tests - test_zopecontainer.py:1.4

Albertas Agejevas alga@codeworks.lt
Thu, 17 Jul 2003 10:45:49 -0400


Update of /cvs-repository/Zope3/src/zope/app/container/tests
In directory cvs.zope.org:/tmp/cvs-serv1693/zope/app/container/tests

Modified Files:
	test_zopecontainer.py 
Log Message:
A temporary workaround for making context aware descriptors work with
ZopeContainerDecorator.  A more generic fix for this problem should be
made in wrapper.c.


=== Zope3/src/zope/app/container/tests/test_zopecontainer.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/container/tests/test_zopecontainer.py:1.3	Sun Jun 15 12:38:29 2003
+++ Zope3/src/zope/app/container/tests/test_zopecontainer.py	Thu Jul 17 10:45:14 2003
@@ -11,9 +11,7 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""XXX short summary goes here.
-
-XXX longer description goes here.
+"""ZopeContainerDecorator tests
 
 $Id$
 """
@@ -21,11 +19,17 @@
 from unittest import TestCase, TestSuite, main, makeSuite
 from zope.app.interfaces.container import IAddNotifiable
 from zope.app.interfaces.container import IDeleteNotifiable
+from zope.app.container.sample import SampleContainer
 from zope.app.container.tests.baseizopeitemcontainer import \
      BaseTestIZopeSimpleReadContainer, BaseTestIZopeReadContainer,\
      BaseTestIZopeWriteContainer
 from zope.app.tests.placelesssetup import PlacelessSetup
-from zope.interface import implements
+from zope.context import ContextAwareDescriptors
+from zope.app.context import ContextWrapper
+from zope.proxy import isProxy, getProxiedObject
+from zope.interface import implements, directlyProvides, directlyProvidedBy
+from zope.app.interfaces.container import IOptionalNamesContainer
+from zope.app.interfaces.container import IContainerNamesContainer
 
 class C:
     pass
@@ -50,6 +54,7 @@
     def __setitem__(self, key, value):
         self._d[key] = value
 
+
 class TestZopeItemContainerDecorator(PlacelessSetup,
                                      BaseTestIZopeSimpleReadContainer,
                                      TestCase):
@@ -112,7 +117,6 @@
 
     def setUp(self):
         PlacelessSetup.setUp(self)
-        from zope.app.container.sample import SampleContainer
         self._container = SampleContainer()
 
     def _sampleMapping(self):
@@ -126,11 +130,11 @@
             ZopeItemWriteContainerDecorator
         return ZopeItemWriteContainerDecorator(container)
 
-    __newItem = {'A': C(), 'B':C()}
+    __newItem = {'A': C(), 'B': C()}
     def _sample_newItem(self):
         return self.__newItem
 
-    __newItemHooked = {'B': H(), 'E':H()}
+    __newItemHooked = {'B': H(), 'E': H()}
     def _sample_newItemHooked(self):
         return self.__newItemHooked
 
@@ -143,6 +147,71 @@
         return ZopeContainerDecorator(container)
 
 
+class ContextAwareContainer(SampleContainer):
+    '''A container that checks that its methods are context wrapped.
+    '''
+
+    ContextAwareDescriptors()
+
+    def __getitem__(self, key):
+        assert isProxy(self), "self is not wrapped"
+        return super(ContextAwareContainer, getProxiedObject(self)).__getitem__(key)
+
+    def __contains__(self, key):
+        assert isProxy(self), "self is not wrapped"
+        return super(ContextAwareContainer, getProxiedObject(self)).__contains__(key)
+
+    def __delitem__(self, key):
+        assert isProxy(self), "self is not wrapped"
+        return super(ContextAwareContainer, getProxiedObject(self)).__delitem__(key)
+
+    def get(self, key, default):
+        assert isProxy(self), "self is not wrapped"
+        return super(ContextAwareContainer, getProxiedObject(self)).get(key, default)
+
+    def items(self):
+        assert isProxy(self), "self is not wrapped"
+        return super(ContextAwareContainer, getProxiedObject(self)).items()
+
+    def values(self):
+        assert isProxy(self), "self is not wrapped"
+        return super(ContextAwareContainer, getProxiedObject(self)).values()
+
+    def setObject(self, key, value):
+        # XXX: setObject is called on a non-decorated container in the test fixtures
+        # assert isProxy(self), "self is not wrapped"
+        if key == '':
+            key = str(id(value))
+        return super(ContextAwareContainer, getProxiedObject(self)).setObject(key, value)
+
+
+class TestZopeContainerDecoratorWrapper(TestZopeContainerDecorator):
+
+    def setUp(self):
+        TestZopeContainerDecorator.setUp(self)
+        self._container = ContextAwareContainer()
+
+    def test_setObject_optional_names(self):
+        # Test that empty keys are allowed for containers which implement
+        # IOptionalNamesContainer or IContainerNamesContainer
+        container = ContextAwareContainer()
+        directlyProvides(container, directlyProvidedBy(container),
+                         IOptionalNamesContainer)
+        decorated = self.decorate(container)
+        decorated.setObject('', 'foo')
+
+        container = ContextAwareContainer()
+        directlyProvides(container, directlyProvidedBy(container),
+                         IContainerNamesContainer)
+        decorated = self.decorate(container)
+        name = decorated.setObject('', 'foo')
+        self.assert_(name != '')
+
+        container = ContextAwareContainer()
+        decorated = self.decorate(container)
+        self.assertRaises(ValueError, decorated.setObject, '', 'foo')
+
+
 def test_suite():
     return TestSuite((
         makeSuite(TestZopeItemContainerDecorator),
@@ -150,6 +219,7 @@
         makeSuite(TestZopeReadContainerDecorator),
         makeSuite(TestZopeItemWriteContainerDecorator),
         makeSuite(TestZopeContainerDecorator),
+        makeSuite(TestZopeContainerDecoratorWrapper),
         ))
 
 if __name__=='__main__':