[Zope-Checkins] CVS: Zope3/lib/python/Zope/Event/tests - subscriber.py:1.1.2.1 testDirectives.py:1.1.2.1 testEventService.py:1.1.2.3

Chris Withers chrisw@nipltd.com
Sat, 23 Feb 2002 12:47:35 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Event/tests
In directory cvs.zope.org:/tmp/cvs-serv29498/tests

Modified Files:
      Tag: Zope-3x-branch
	testEventService.py 
Added Files:
      Tag: Zope-3x-branch
	subscriber.py testDirectives.py 
Log Message:
Can now configure event subscriptions using ZCML.

=== Added File Zope3/lib/python/Zope/Event/tests/subscriber.py ===
##############################################################################
#
# 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
# 
##############################################################################
"""
This contains some dummy stuff to do with subscribing to event channels
that's useful in several test modules.

Revision information:
$Id: subscriber.py,v 1.1.2.1 2002/02/23 17:47:35 chrisw Exp $
"""


class DummySubscriber:

    def __init__(self):
        self.notified = 0
        
    def notify(self, event):
        self.notified += 1

subscriber = DummySubscriber()

class DummyFilter:
    
    def __init__(self,value=1):
        self.value = value
        
    def __call__(self, event):
        return self.value

filter = DummyFilter


=== Added File Zope3/lib/python/Zope/Event/tests/testDirectives.py ===
##############################################################################
#
# 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: testDirectives.py,v 1.1.2.1 2002/02/23 17:47:35 chrisw Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from StringIO import StringIO

from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from Zope.Configuration.xmlconfig import xmlconfig

from Zope.Exceptions import NotFoundError
from Zope.Event import subscribe, unsubscribe, publishEvent
from Zope.Event.ObjectEvent import ObjectAddedEvent
from Zope.Event.ObjectEvent import ObjectRemovedEvent
from Zope.Event.ObjectEvent import ObjectModifiedEvent
from testEventService import DummySubscriber, DummyFilter, DummyEvent

template = """<zopeConfigure
  xmlns='http://namespaces.zope.org/zope'
  xmlns:event='http://namespaces.zope.org/event'>
  %s
  </zopeConfigure>"""

class Test(CleanUp, TestCase):

    def testSubscribe(self):
        from Zope.Event.tests.subscriber import subscriber
        # This should fail, since we're not subscribed
        self.assertRaises(NotFoundError,unsubscribe,subscriber)
            
        xmlconfig(StringIO(template % (
            """
            <directive name="subscribe"
                       attributes="subscriber, event_types, filter"
                       handler="Zope.Event.metaConfigure.subscribe"
                       namespace="http://namespaces.zope.org/event"/>
            <event:subscribe subscriber="Zope.Event.tests.subscriber.subscriber"
                             event_types="Zope.Event.IObjectEvent.IObjectAddedEvent,
                                          Zope.Event.IObjectEvent.IObjectRemovedEvent"
                             filter="Zope.Event.tests.subscriber.filter"/>
            """)))

        publishEvent(ObjectAddedEvent('foo'))
        self.assertEqual(subscriber.notified,1)
        publishEvent(ObjectRemovedEvent('foo'))
        self.assertEqual(subscriber.notified,2)
        publishEvent(ObjectModifiedEvent('foo'))
        self.assertEqual(subscriber.notified,2) # NB: no increase ;-)
        publishEvent(DummyEvent())
        self.assertEqual(subscriber.notified,4) # NB: increased by 2 ;-)
        
        # This should not fail, since we should be subscribed by now ;-)
        unsubscribe(subscriber)

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/Event/tests/testEventService.py 1.1.2.2 => 1.1.2.3 ===
 from Zope import Event
 
-class DummySubscriber:
-
-    def __init__(self):
-        self.notified = 0
-        
-    def notify(self, event):
-        self.notified += 1
-    
-class DummyFilter:
-    
-    def __init__(self,value=1):
-        self.value = value
-        
-    def __call__(self, event):
-        return self.value
+from subscriber import DummySubscriber, DummyFilter
 
 class DummyEvent: