[Zope3-checkins] CVS: Zope3/src/zope/app/presentation - tests.py:1.2 presentation.py:1.2

Bjorn Tillenius bjoti777 at student.liu.se
Tue Mar 9 11:34:32 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/presentation
In directory cvs.zope.org:/tmp/cvs-serv24301/src/zope/app/presentation

Modified Files:
	tests.py presentation.py 
Log Message:
PageRegistration's addNotify and removeNotify didn't handle the case
when an attribute was specified instead of a template.

Also, in removeNotify, addDependent was called instead of removeDependent.


=== Zope3/src/zope/app/presentation/tests.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/presentation/tests.py:1.1	Mon Mar  8 14:40:26 2004
+++ Zope3/src/zope/app/presentation/tests.py	Tue Mar  9 11:34:31 2004
@@ -47,10 +47,13 @@
 
 from zope.publisher.browser import TestRequest
 from zope.publisher.interfaces.browser import IBrowserRequest
-from zope.app.container.contained import contained
+from zope.app.container.contained import contained, uncontained, setitem
+from zope.app.container.interfaces import IContained, ILocation
 
 from zope.app.interfaces.dependable import IDependable
 from zope.app.interfaces.annotation import IAttributeAnnotatable
+from zope.app.interfaces.services.registration import IRegistered
+from zope.app.interfaces.traversing import IPhysicallyLocatable
 from zope.app.dependable import Dependable
 
 class I1(Interface):
@@ -86,7 +89,41 @@
 
 class PhonyTemplate:
     __name__ = __parent__ = None
-    implements(IZPTTemplate)
+    implements(IZPTTemplate, IDependable, IRegistered)
+
+    _dependents = ()
+    _usages = ()
+
+    def addDependent(self, location):
+        self._dependents = tuple(
+            [d for d in self._dependents if d != location]
+            +
+            [location]
+            )
+
+    def removeDependent(self, location):
+        self._dependents = tuple(
+            [d for d in self._dependents if d != location]
+            )
+
+    def dependents(self):
+        return self._dependents
+
+    def addUsage(self, location):
+        self._usages = tuple(
+            [d for d in self._usages if d != location]
+            +
+            [location]
+            )
+
+    def removeUsage(self, location):
+        self._usages = tuple(
+            [d for d in self._usages if d != location]
+            )
+
+    def usages(self):
+        return self._usages
+ 
 
 class A:
     def __init__(self, object, request):
@@ -283,11 +320,43 @@
 
 class ModuleFinder:
 
+    implements(IContained)
+
+    __parent__ = __name__ = None
+
+    def __init__(self):
+        self._dict = {}
+
     def resolve(self, name):
         if name == "Foo.Bar.A":
             return A
         raise ImportError(name)
 
+    def __setitem__(self, key, ob):
+        setitem(self, self.__setitem, key, ob)
+    
+    def __setitem(self, key, ob):
+        self._dict[key] = ob
+
+    def get(self, key, default=None):
+        return self._dict.get(key, default)
+
+
+class PhonyPathAdapter:
+    implements(IPhysicallyLocatable)
+
+    def __init__(self, context):
+        self.context = context
+
+    def getPath(self):
+        return self.context.__name__
+
+    def getRoot(self):
+        root = self.context
+        while root.__parent__ is not None:
+            root = root.__parent__
+        return root
+
 
 class TestViewRegistration(PlacefulSetup, TestCase):
 
@@ -365,16 +434,33 @@
         registration.attribute = 'run'
         self.assertRaises(ConfigurationError, lambda: registration.factories)
 
-
-def test_PageRegistration_addremoveNotify():
-    """for addNotify and removeNotify
-
-    XXX
-      - Jim suggested we can write unit test later.
-
-      - It will be easiar to write unit test, for Direct reference.
-
-    """
+    def test_addremoveNotify_template(self):
+        ztapi.provideAdapter(ILocation, IPhysicallyLocatable,
+                             PhonyPathAdapter)
+        registration = PageRegistration(I1, 'test', 'zope.View', "Foo.Bar.A",
+                                        template='/++etc++site/default/t')
+        # Test addNotify
+        self.folder['test'] = registration
+        dependents = zapi.getAdapter(self.__template, IDependable)
+        self.assert_('test' in dependents.dependents())
+        usages = zapi.getAdapter(self.__template, IRegistered)
+        self.assert_('test' in usages.usages())
+
+        # Test removeNotify
+        uncontained(registration, self.folder, 'test')
+        dependents = zapi.getAdapter(self.__template, IDependable)
+        self.assert_('test' not in dependents.dependents())
+        usages = zapi.getAdapter(self.__template, IRegistered)
+        self.assert_('test' not in usages.usages())
+        
+    def test_addremoveNotify_attribute(self):
+        ztapi.provideAdapter(ILocation, IPhysicallyLocatable,
+                             PhonyPathAdapter)
+        registration = PageRegistration(I1, 'test', 'zope.View',
+                                        "Foo.Bar.A", attribute='run')
+        # Just add and remove registration to see that no errors occur
+        self.folder['test'] = registration
+        uncontained(registration, self.folder, 'test')
 
 
 def test_suite():


=== Zope3/src/zope/app/presentation/presentation.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/presentation/presentation.py:1.1	Mon Mar  8 14:40:26 2004
+++ Zope3/src/zope/app/presentation/presentation.py	Tue Mar  9 11:34:31 2004
@@ -488,27 +488,29 @@
 
     def addNotify(self, event):
         "See IAddNotifiable"
-        template = zapi.traverse(self.__parent__.__parent__,self.template)
-        dependents = IDependable(template)
-        objectpath = zapi.getPath(self)
-        dependents.addDependent(objectpath)
-        # Also update usage, if supported
-        adapter = IRegistered(template, None)
-        if adapter is not None:
-            adapter.addUsage(objectpath)
+        if self.template:
+            template = zapi.traverse(self.__parent__.__parent__,self.template)
+            dependents = IDependable(template)
+            objectpath = zapi.getPath(self)
+            dependents.addDependent(objectpath)
+            # Also update usage, if supported
+            adapter = IRegistered(template, None)
+            if adapter is not None:
+                adapter.addUsage(objectpath)
 
 
     def removeNotify(self, event):
         "See IRemoveNotifiable"
         super(PageRegistration, self).removeNotify(event)
-        template = zapi.traverse(self.__parent__.__parent__,self.template)
-        dependents = IDependable(template)
-        objectpath = zapi.getPath(self)
-        dependents.addDependent(objectpath)
-        # Also update usage, if supported
-        adapter = IRegistered(template, None)
-        if adapter is not None:
-            adapter.removeUsage(zapi.getPath(self))
+        if self.template:
+            template = zapi.traverse(self.__parent__.__parent__,self.template)
+            dependents = IDependable(template)
+            objectpath = zapi.getPath(self)
+            dependents.removeDependent(objectpath)
+            # Also update usage, if supported
+            adapter = IRegistered(template, None)
+            if adapter is not None:
+                adapter.removeUsage(zapi.getPath(self))
 
 
 class TemplateViewFactory:




More information about the Zope3-Checkins mailing list