[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Container/tests - BaseTestIZopeItemContainer.py:1.2 testZopeContainerAdpter.py:1.2

Jim Fulton jim@zope.com
Mon, 18 Nov 2002 18:53:00 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Container/tests
In directory cvs.zope.org:/tmp/cvs-serv25757/tests

Added Files:
	BaseTestIZopeItemContainer.py testZopeContainerAdpter.py 
Log Message:
Created an adapter that provides Zope framework services for
containers, including:

  - Context wrapping

  - Event generation

  - Add and delete hooks

Modified the adding and contents views to use the new adapter.

Added interfaces defining add and delete hooks.





=== Zope3/lib/python/Zope/App/OFS/Container/tests/BaseTestIZopeItemContainer.py 1.1 => 1.2 ===
--- /dev/null	Mon Nov 18 18:53:00 2002
+++ Zope3/lib/python/Zope/App/OFS/Container/tests/BaseTestIZopeItemContainer.py	Mon Nov 18 18:52:59 2002
@@ -0,0 +1,221 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from Zope.Proxy.ContextWrapper import getWrapperContainer, getWrapperData
+from Interface.Common.tests.BaseTestMapping \
+     import BaseTestIReadMapping, BaseTestIEnumerableMapping
+from Zope.Event.tests.PlacelessSetup import getEvents
+from Zope.Event.IObjectEvent \
+     import IObjectRemovedEvent, IObjectModifiedEvent, IObjectAddedEvent
+
+
+
+class BaseTestIZopeItemContainer:
+    
+    def _sampleMapping(self):
+        """Return a new instance to be tested
+        """
+        raise TypeError('_new was not implemented')
+
+    def _sampleDict(self):
+        """Return a sequence of items that should be in the container
+        """
+        raise TypeError('_newItems was not implemented')
+
+    def _absentKeys(self):
+        """This should return the keys not in the container
+        """
+        absent_key = ''
+        for key, value in testItems:
+            absent_key += key
+        return [absent_key]    
+        
+    
+    def test__getitem__(self):
+        testOb = self._sampleMapping()
+        testItems = self._sampleDict().items()
+        testAd = self._sampleContainer()
+        for key, value in testItems:
+            stored = testOb[key]
+            self.assertEqual(stored, value)
+            self.assertEqual(getWrapperContainer(stored), testAd)
+
+        for key in self._absentKeys():
+            self.assertRaises(KeyError, testOb.__getitem__, key)        
+    
+
+class BaseTestIZopeSimpleReadContainer(BaseTestIZopeItemContainer,
+                                       BaseTestIReadMapping):
+
+    def _IReadMapping__sample(self):
+        return self._sampleMapping()
+    
+    def _IReadMapping__stateDict(self):
+        return self._sampleDict()
+ 
+    def _IReadMapping__absentKeys(self):
+        return self._absentKeys()
+
+    def test_zope_get(self):
+        testOb = self._sampleMapping()
+        testItems = self._sampleDict().items()
+        testAd = self._sampleContainer()
+        for key, value in testItems:
+            stored = testOb.get(key)
+            self.assertEqual(stored, value)
+            self.assertEqual(getWrapperContainer(stored), testAd)
+            self.assertEqual(getWrapperData(stored)['name'], key)
+       
+
+class BaseTestIZopeReadContainer(BaseTestIZopeItemContainer,
+                                 BaseTestIEnumerableMapping):
+    
+    def _IEnumerableMapping__sample(self):
+        return self._sampleMapping()
+    
+    def _IEnumerableMapping__stateDict(self):
+        return self._sampleDict()
+ 
+    def _IEnumerableMapping__absentKeys(self):
+        return self._absentKeys()
+
+
+    
+    def test_zope_values(self):
+        testOb = self._sampleMapping()
+        data = testOb.values()
+        testAd = self._sampleContainer()
+        for value in data:
+            self.assertEqual(getWrapperContainer(value), testAd)
+            getWrapperData(value)['name']
+        
+
+    def test_zope_items(self):
+        testOb = self._sampleMapping()
+        testItems = self._sampleDict().items()
+        testAd = self._sampleContainer()
+        for key, value in testItems:
+            stored = testOb[key]
+            self.assertEqual(getWrapperData(stored)['name'], key)
+            self.assertEqual(value, stored)
+        self.assertEqual(testOb.items(), getWrapperContainer(stored).items())
+
+
+class BaseTestIZopeWriteContainer(BaseTestIZopeItemContainer):
+    
+    def _sample_newItem(self):
+        """Return a new item key and value for testing addition
+
+        The new item must not have a IAddNotifiable adapter
+        or a IDeleteNotifyable adapter.    
+        """
+        raise TypeError("_sample_newItem was not implemented")
+    
+    def _sample_newItemHooked(self):
+        """Return a new item key and value for testing addition
+
+        The new item must implement IAddNotifiable 
+        and IDeleteNotifyable, and must have a notified attribute that
+        is incremented when manageAfterAdd is called and decremented
+        when manage_beforeDelete is called.
+        """
+        raise TypeError("_sample_newItem was not implemented")
+
+    def test_zope_setObject(self):
+        key, value = self._sample_newItem().items()[0]
+        testOb = self._sampleMapping()
+        testAd = self._sampleContainer()
+        newkey = testOb.setObject(key, value)
+        self.assertEqual(testOb[newkey], value)
+
+
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectAddedEvent)
+             if event.object == value]
+             )
+        
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectModifiedEvent)
+             if event.object == testAd]
+             )
+
+        key, value = self._sample_newItemHooked().items()[0]
+        oldnotified = value.notified +1
+        newkey = testOb.setObject(key, value)
+        self.assertEqual(value.notified , oldnotified)
+
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectAddedEvent)
+             if event.object == value]
+             )
+        
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectModifiedEvent)
+             if event.object == testAd]
+             )
+
+
+
+    def test__zope_delitem__(self):
+        testOb = self._sampleMapping()
+        testObHook = self._sample_newItemHooked()
+        testAd = self._sampleContainer()
+        self.assertRaises(KeyError, testOb.__delitem__, 'zc')
+        key, value = self._sample_newItem()
+        newkey = testOb.setObject(key, value)
+        del testOb[key] 
+        self.assertRaises(KeyError, testOb.__delitem__, key)
+        
+
+                    
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectRemovedEvent)
+             if event.object == value]
+             )
+
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectModifiedEvent)
+             if event.object == testAd]
+             )
+
+        key, value = self._sample_newItemHooked().items()[0]
+        newkey = testOb.setObject(key, value)
+        oldnotified = value.notified - 1
+        del testOb[key] 
+        self.assertRaises(KeyError, testOb.__delitem__, key)
+
+        self.assertEqual(value.notified , oldnotified)
+
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectRemovedEvent)
+             if event.object == value]
+             )
+
+        self.failUnless(
+            [event
+             for event in getEvents(IObjectModifiedEvent)
+             if event.object == testAd]
+             )


=== Zope3/lib/python/Zope/App/OFS/Container/tests/testZopeContainerAdpter.py 1.1 => 1.2 ===
--- /dev/null	Mon Nov 18 18:53:00 2002
+++ Zope3/lib/python/Zope/App/OFS/Container/tests/testZopeContainerAdpter.py	Mon Nov 18 18:52:59 2002
@@ -0,0 +1,91 @@
+
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""XXX short summary goes here.
+
+XXX longer description goes here.
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.App.OFS.Container.IAddNotifiable import IAddNotifiable
+from Zope.App.OFS.Container.IDeleteNotifiable import IDeleteNotifiable
+from BaseTestIZopeItemContainer \
+     import BaseTestIZopeItemContainer, BaseTestIZopeSimpleReadContainer,\
+            BaseTestIZopeReadContainer, BaseTestIZopeWriteContainer
+from Zope.App.Traversing import getParent
+from Zope.Proxy.ContextWrapper import ContextWrapper
+from Zope.App.tests.PlacelessSetup import PlacelessSetup
+class C: pass
+
+class H:
+    __implements__ = IAddNotifiable, IDeleteNotifiable
+    notified = 0
+    def manage_beforeDelete(self, object, container):
+        self.notified -= 1
+        
+
+    def manage_afterAdd(self, object, container):
+        self.notified += 1
+
+class Test(PlacelessSetup, BaseTestIZopeItemContainer, \
+           BaseTestIZopeSimpleReadContainer, \
+           BaseTestIZopeReadContainer, BaseTestIZopeWriteContainer,\
+           TestCase):
+
+
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+        from Zope.App.OFS.Container.SampleContainer \
+            import SampleContainer
+        self.__container = SampleContainer()
+        
+    def _sampleMapping(self):
+        from Zope.App.OFS.Container.ZopeContainerAdapter \
+            import ZopeContainerAdapter
+        container = self.__container
+        for k, v in self._sampleDict().items():
+            container.setObject(k, v)
+        return ZopeContainerAdapter(container)        
+        
+    def _sampleContainer(self):
+        return self.__container
+    
+    __sample = {'Z': C(), 'O': C(),'P': C()}
+    def _sampleDict(self):
+        return self.__sample
+             
+
+    def _absentKeys(self):
+        return 'zc', 'ny'
+    
+    __newItem = {'A': C(), 'B':C()}
+    def _sample_newItem(self):
+        return self.__newItem
+    
+    __newItemHooked = {'B': H(), 'E':H()}
+    def _sample_newItemHooked(self):
+        return self.__newItemHooked
+
+
+
+def test_suite():
+    return TestSuite((
+        makeSuite(Test),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')
+