[Zope3-checkins] CVS: Zope3/src/zope/app/registration/tests - test_registrationmanager.py:1.2 test_registrations.py:1.3

Mark McEahern mark at mceahern.com
Tue Mar 23 10:15:37 EST 2004


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

Modified Files:
	test_registrationmanager.py test_registrations.py 
Log Message:


Converted the use of IAddNotifiable and IRemoveNotifiable to the use
of subscriber adapters.




=== Zope3/src/zope/app/registration/tests/test_registrationmanager.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/registration/tests/test_registrationmanager.py:1.1	Sat Mar 13 13:01:18 2004
+++ Zope3/src/zope/app/registration/tests/test_registrationmanager.py	Tue Mar 23 10:15:06 2004
@@ -14,10 +14,12 @@
 """
 $Id$
 """
-from unittest import TestCase, main, makeSuite
+from unittest import TestSuite, TestCase, main, makeSuite
+from doctest import DocTestSuite
 from zope.app.container.interfaces import IRemoveNotifiable
 from zope.app.registration.interfaces import IRegistrationManager
 from zope.app.registration.registration import RegistrationManager
+from zope.app.registration.registration import RegistrationManagerRemoveSubscriber
 from zope.app.site.tests import placefulsetup
 from zope.app.tests.placelesssetup import PlacelessSetup
 from zope.app.traversing import traverse
@@ -352,17 +354,34 @@
 
     #########################################################
 
-    def test_removeNotify(self):
-        manager = RegistrationManager()
-        thingy = Undeletable()
-        manager['xyzzy'] = thingy
-        event = ObjectRemovedEvent(manager, 'xxx', 'yyy')
-        manager.removeNotify(event)
-        self.failUnless(thingy.was_called)
+class DummyRM(dict):
+    def __iter__(self):
+        for n in self.keys():
+            yield n
+        
+class TestRegistrationManagerRemoveSubscriber:
+    def test_subscriber():
+        """
+        First create a dummy registration manager with some
+        initial data.
+        
+        >>> regmgr = DummyRM()
+        >>> regmgr['foo'] = 'bar'
+
+        Create an adapter for it.
+        >>> adapter = RegistrationManagerRemoveSubscriber(regmgr, None)
+
+        Trigger the event.
+        >>> adapter.notify(None)
+
+        Check the results.
+        >>> regmgr
+        {}
+        
+        """
 
 class RegistrationManagerContainerTests(placefulsetup.PlacefulSetup):
 
-
     def test_getRegistrationManager(self):
         sm = self.buildFolders(site=True)
         default = traverse(sm, 'default')
@@ -389,7 +408,11 @@
 
 
 def test_suite():
-    return makeSuite(Test)
+    import sys
+    return TestSuite((
+        makeSuite(Test),
+        DocTestSuite(sys.modules[__name__]),
+        ))
 
 if __name__=='__main__':
     main(defaultTest='test_suite')


=== Zope3/src/zope/app/registration/tests/test_registrations.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/registration/tests/test_registrations.py:1.2	Sat Mar 13 17:02:07 2004
+++ Zope3/src/zope/app/registration/tests/test_registrations.py	Tue Mar 23 10:15:06 2004
@@ -17,6 +17,7 @@
 """
 
 from unittest import TestCase, TestSuite, main, makeSuite
+from doctest import DocTestSuite
 
 from zope.interface import Interface, implements
 from zope.app.registration.interfaces import UnregisteredStatus
@@ -30,6 +31,12 @@
 from zope.app.traversing import traverse
 from zope.security.proxy import Proxy
 from zope.app.container.contained import ObjectRemovedEvent
+from zope.app.tests import ztapi
+from zope.app.registration.interfaces import IRegistration
+from zope.app.container.interfaces import IObjectRemovedEvent
+from zope.app.event.interfaces import ISubscriber
+from zope.app.registration.registration import SimpleRegistrationRemoveSubscriber, ComponentRegistrationRemoveSubscriber, ComponentRegistrationAddSubscriber
+from zope.app.traversing.interfaces import IPhysicallyLocatable
 
 class ITestComponent(Interface):
     pass
@@ -56,9 +63,38 @@
         return self._dependents
 
 
+class DummyRegistration(ComponentStub):
+    implements (IRegistration, IPhysicallyLocatable)
+
+    def __init__(self):
+        self.status = UnregisteredStatus
+        
+    def getPath(self):
+        return 'dummy!'
+
+    def getComponent(self):
+        return self
+    
+class TestSimpleRegistrationEvents(TestCase):
+
+    def test_RemoveSubscriber(self):
+        reg = DummyRegistration()
+        adapter = SimpleRegistrationRemoveSubscriber(reg, None)
+
+        # test that removal fails with Active status
+        reg.status = ActiveStatus
+        self.assertRaises(DependencyError, adapter.notify, None)
+
+        # test that removal succeeds with Registered status
+        reg.status = RegisteredStatus
+        adapter.notify(None)
+
+        self.assertEquals(reg.status, UnregisteredStatus)
+        
 class TestSimpleRegistration(TestCase):
 
     def setUp(self):
+        # XXX: May need more setup related to Adapter service?
         # We can't use the status property on a SimpleRegistration instance.
         # we disable it for these tests
         self.__oldprop = SimpleRegistration.status
@@ -68,21 +104,6 @@
         # Restore the status prop
         SimpleRegistration.status = self.__oldprop
 
-    def test_removeNotify(self):
-        cfg = SimpleRegistration()
-
-        # cannot delete an active registration
-        cfg.status = ActiveStatus
-        event = ObjectRemovedEvent(cfg, None, 'somename')
-        self.assertRaises(DependencyError, cfg.removeNotify, event)
-
-        # deletion of a registered registration causes it to become
-        # unregistered
-        cfg.status = RegisteredStatus
-        cfg.removeNotify(event)
-        self.assertEquals(cfg.status, UnregisteredStatus)
-
-
 class TestComponentRegistration(TestSimpleRegistration, PlacefulSetup):
 
     def setUp(self):
@@ -113,37 +134,53 @@
         self.assertEquals(result, component)
         self.failUnless(type(result) is Proxy)
 
+class TestComponentRegistrationEvents:
     def test_addNotify(self):
-        # set up a component
-        name, component = 'foo', ComponentStub()
-        self.rootFolder[name] = component
-        # set up a registration
-        cfg = ComponentRegistration("/"+name)
-        self.rootFolder['cfg'] = cfg
-        cfg = traverse(self.rootFolder, 'cfg')
-        # check that the dependency tracking works
-        self.assertEquals(component.dependents(), ('/cfg',))
-
+        """
+        First we create a dummy registration and an adapter for it.
+        
+        >>> reg = DummyRegistration()
+        >>> adapter = ComponentRegistrationAddSubscriber(reg, None)
+
+        Now call notification
+        >>> adapter.notify(None)
+
+        Check to make sure the adapter added the path
+        >>> reg.dependents()
+        ('dummy!',)
+        """
+        
     def test_removeNotify_dependents(self):
-        # set up a component
-        name, component = 'foo', ComponentStub()
-        self.rootFolder[name] = component
-        component.addDependent('/cfg')
-        # set up a registration
-        cfg = ComponentRegistration("/"+name)
-        cfg.status = UnregisteredStatus
-        self.rootFolder['cfg'] = cfg
-        cfg = traverse(self.rootFolder, 'cfg')
-        # simulate IRemoveNotifiable
-        event = ObjectRemovedEvent(cfg, self.rootFolder, 'cfg')
-        cfg.removeNotify(event)
-        # check that the dependency tracking works
-        self.assertEquals(component.dependents(), ())
+        """
+        First we create a dummy registration and an adapter for it.
+        
+        >>> reg = DummyRegistration()
+        >>> adapter = ComponentRegistrationAddSubscriber(reg, None)
+
+        Now call notification
+        >>> adapter.notify(None)
+
+        Check to make sure the adapter added the path
+        >>> reg.dependents()
+        ('dummy!',)
+
+        Now create a removal adapter and call it.
+        >>> removal_adapter = ComponentRegistrationRemoveSubscriber(reg, None)
+        >>> removal_adapter.notify(None)
+
+        Check to make sure the adapter removed the dependencie(s).
+        >>> reg.dependents()
+        ()
+        
+        """
 
 def test_suite():
+    import sys
     return TestSuite((
         makeSuite(TestSimpleRegistration),
         makeSuite(TestComponentRegistration),
+        makeSuite(TestSimpleRegistrationEvents),
+        DocTestSuite(sys.modules[__name__]),
         ))
 
 if __name__=='__main__':




More information about the Zope3-Checkins mailing list