[Zope-Checkins] CVS: Packages/ZPublisher/tests - testBaseRequest.py:1.1.6.2.10.1

Sidnei da Silva sidnei at awkly.org
Mon Oct 4 16:29:44 EDT 2004


Update of /cvs-repository/Packages/ZPublisher/tests
In directory cvs.zope.org:/tmp/cvs-serv24107/lib/python/ZPublisher/tests

Modified Files:
      Tag: dc-bobo_traverse-branch
	testBaseRequest.py 
Log Message:
Change ZPublisher behavior to catch AttributeError and KeyError on __bobo_traverse__ and then fallback to attribute and item access if such an exception is caught. Added some tests to verify the behavior


=== Packages/ZPublisher/tests/testBaseRequest.py 1.1.6.2 => 1.1.6.2.10.1 ===
--- Packages/ZPublisher/tests/testBaseRequest.py:1.1.6.2	Mon Nov 17 17:34:20 2003
+++ Packages/ZPublisher/tests/testBaseRequest.py	Mon Oct  4 16:29:44 2004
@@ -1,7 +1,7 @@
 from unittest import TestCase, TestSuite, makeSuite, main
 
 import Zope
-Zope.startup()
+#Zope.startup()
 
 from Acquisition import Implicit
 from ZPublisher.BaseRequest import BaseRequest
@@ -17,7 +17,7 @@
         return getattr(self, id)
 
     def view(self):
-        """ Atrribute with docstring.
+        """ Atribute with docstring.
         """
         return 'view content'
 
@@ -27,7 +27,7 @@
     """
 
     def index_html(self):
-        """ Atrribute with docstring.
+        """ Atribute with docstring.
         """
         return 'index_html content'
 
@@ -52,6 +52,24 @@
         REQUEST._hacked_path=1
 
 
+class DummyObjectWithBBT(DummyObjectBasic):
+    """ Dummy class with __bobo_traverse__
+    """
+
+    def __bobo_traverse__(self, REQUEST, name):
+        raise AttributeError, name
+
+    def dummyMethod(self):
+        """Dummy method with docstring"""
+        return 'Dummy Value'
+
+    def __getitem__(self, name):
+        if name.startswith('no_key_'):
+            raise KeyError, name
+        name = name.replace('key_', '')
+        return getattr(self, name)
+
+
 class DummyObjectWithBD(DummyObjectBasic):
     """ Dummy class with docstring.
     """
@@ -63,6 +81,12 @@
             raise RuntimeError('Infinite loop detected.')
         return self, self._default_path
 
+class DummyObjectWithBDBBT(DummyObjectWithBD):
+
+    def __bobo_traverse__(self, REQUEST, name):
+        if name == self.default_path[0]:
+            return getattr(self, name)
+        raise AttributeError, name
 
 class TestBaseRequest(TestCase):
 
@@ -74,6 +98,8 @@
         self.f1._setObject('objWithDefaultNone', DummyObjectWithDefaultNone() )
         self.f1._setObject('objWithBPTH', DummyObjectWithBPTH() )
         self.f1._setObject('objWithBD', DummyObjectWithBD() )
+        self.f1._setObject('objWithBBT', DummyObjectWithBBT() )
+        self.f1._setObject('objWithBDBBT', DummyObjectWithBDBBT() )
 
     def makeBaseRequest(self):
         response = HTTPResponse()
@@ -140,6 +166,58 @@
         r = self.makeBaseRequest()
         self.f1.objWithBD._default_path = ['']
         self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBD')
+
+    def test_traverse_withBBT_handles_AttributeError(self):
+        # Test that if __bobo_traverse__ raises AttributeError
+        # that we get a NotFound
+        from ZPublisher import NotFound
+        r = self.makeBaseRequest()
+        self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBBT/bbt_foo')
+
+    def test_traverse_withBBT_fallback_getattr(self):
+        # Test that if __bobo_traverse__ raises AttributeError
+        # that we fallback to getattr()
+        r = self.makeBaseRequest()
+        r.traverse('folder/objWithBBT/dummyMethod')
+        self.assertEqual(r.URL, '/folder/objWithBBT/dummyMethod')
+
+    def test_traverse_withBBT_fallback_getitem(self):
+        # Test that if __bobo_traverse__ raises AttributeError
+        # and getattr raises AttributeError
+        # that we fallback to __getitem__
+        r = self.makeBaseRequest()
+        r.traverse('folder/objWithBBT/key_dummyMethod')
+        self.assertEqual(r.URL, '/folder/objWithBBT/key_dummyMethod')
+
+    def test_traverse_withBBT_fallback_getitem_NotFound(self):
+        # Test that if all else fails, we get a NotFound
+        from ZPublisher import NotFound
+        r = self.makeBaseRequest()
+        self.failUnlessRaises(NotFound, r.traverse,
+                              'folder/objWithBBT/no_key_dummyMethod')
+
+    def test_traverse_withBDBBT(self):
+        # Test for an object which has a __browser_default__
+        # and __bobo_traverse__
+        # __bobo_traverse__ should return the object
+        # pointed by __browser_default__
+        r = self.makeBaseRequest()
+        self.f1.objWithBDBBT._default_path = ['view']
+        r.traverse('folder/objWithBDBBT')
+        self.assertEqual(r.URL, '/folder/objWithBDBBT/view')
+        self.assertEqual(r.response.base, '/folder/objWithBDBBT/')
+
+    def test_traverse_withBDBBT_NotFound(self):
+        # Test for an object which has a __browser_default__
+        # and __bobo_traverse__
+        # __bobo_traverse__ should raise an AttributeError, which will
+        # end up falling back to getattr, then __getitem__ to finally
+        # raise a NotFound
+        from ZPublisher import NotFound
+        r = self.makeBaseRequest()
+        self.f1.objWithBDBBT._default_path = ['xxx']
+        r = self.makeBaseRequest()
+        self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBDBBT')
 
     def test_traverse_slash(self):
         r = self.makeBaseRequest()



More information about the Zope-Checkins mailing list