def test_extendning(self): c1 = Components('1') self.assertEqual(c1.__bases__, ()) c2 = Components('2', (c1, )) self.assertTrue(c2.__bases__ == (c1, )) test_object1 = U1(1) test_object2 = U1(2) test_object3 = U12(1) test_object4 = U12(3) self.assertEqual(len(list(c1.registeredUtilities())), 0) self.assertEqual(len(list(c2.registeredUtilities())), 0) c1.registerUtility(test_object1) self.assertEqual(len(list(c1.registeredUtilities())), 1) self.assertEqual(len(list(c2.registeredUtilities())), 0) self.assertEqual(c1.queryUtility(I1), test_object1) self.assertEqual(c2.queryUtility(I1), test_object1) c1.registerUtility(test_object2) self.assertEqual(len(list(c1.registeredUtilities())), 1) self.assertEqual(len(list(c2.registeredUtilities())), 0) self.assertEqual(c1.queryUtility(I1), test_object2) self.assertEqual(c2.queryUtility(I1), test_object2) c3 = Components('3', (c1, )) c4 = Components('4', (c2, c3)) self.assertEqual(c4.queryUtility(I1), test_object2) c1.registerUtility(test_object3, I2) self.assertEqual(c4.queryUtility(I2), test_object3) c3.registerUtility(test_object4, I2) self.assertEqual(c4.queryUtility(I2), test_object4) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) def handle(*objects): self.assertEqual(objects, (test_object1,)) @adapter(I1) def handle3(x): self.assertEqual(x, test_object1) @adapter(I1) def handle4(x): self.assertEqual(x, test_object1) c1.registerHandler(handle1, info=u'First handler') c2.registerHandler(handle, required=[U]) c3.registerHandler(handle3) c4.registerHandler(handle4) c4.handle(test_object1)
def test_extendning(self): c1 = Components('1') self.assertEqual(c1.__bases__, ()) c2 = Components('2', (c1, )) self.assertTrue(c2.__bases__ == (c1, )) test_object1 = U1(1) test_object2 = U1(2) test_object3 = U12(1) test_object4 = U12(3) self.assertEqual(len(list(c1.registeredUtilities())), 0) self.assertEqual(len(list(c2.registeredUtilities())), 0) c1.registerUtility(test_object1) self.assertEqual(len(list(c1.registeredUtilities())), 1) self.assertEqual(len(list(c2.registeredUtilities())), 0) self.assertEqual(c1.queryUtility(I1), test_object1) self.assertEqual(c2.queryUtility(I1), test_object1) c1.registerUtility(test_object2) self.assertEqual(len(list(c1.registeredUtilities())), 1) self.assertEqual(len(list(c2.registeredUtilities())), 0) self.assertEqual(c1.queryUtility(I1), test_object2) self.assertEqual(c2.queryUtility(I1), test_object2) c3 = Components('3', (c1, )) c4 = Components('4', (c2, c3)) self.assertEqual(c4.queryUtility(I1), test_object2) c1.registerUtility(test_object3, I2) self.assertEqual(c4.queryUtility(I2), test_object3) c3.registerUtility(test_object4, I2) self.assertEqual(c4.queryUtility(I2), test_object4) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) def handle(*objects): self.assertEqual(objects, (test_object1, )) @adapter(I1) def handle3(x): self.assertEqual(x, test_object1) @adapter(I1) def handle4(x): self.assertEqual(x, test_object1) c1.registerHandler(handle1, info=u'First handler') c2.registerHandler(handle, required=[U]) c3.registerHandler(handle3) c4.registerHandler(handle4) c4.handle(test_object1)
registry.registerSubscriptionAdapter(PowderExtinguisher, (IFire, )) registry.registerSubscriptionAdapter(SprinklerSystem, (IFire, ), IFireExtinguisher) extinguishers = registry.subscribers((Fire(), ), IFireExtinguisher) extinguishers_results = [ extinguisher.extinguish() for extinguisher in extinguishers ] assert (extinguishers_results == ['PowderExtinguisher', 'SprinklerSystem']) class IDocumentCreated(zope.interface.Interface): doc = zope.interface.Attribute("The document that was created") @zope.interface.implementer(IDocumentCreated) class DocumentCreated(object): def __init__(self, doc): self.doc = doc def setCreationDate(event: IDocumentCreated): event.doc['created'] = 'now' doc = {} registry.registerHandler(setCreationDate, (IDocumentCreated, )) # handler registration registry.handle(DocumentCreated(doc)) # initiation of event handlers assert doc == {'created': 'now'}
class TestHandler(unittest.TestCase): def setUp(self): self.components = Components('comps') def test_register_handler(self): test_object1 = U1(1) test_object2 = U12(2) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) self.components.registerHandler(handle1, info=u'First handler') self.components.handle(test_object1) @adapter(I1, I2) def handle12(x, y): self.assertEqual(x, test_object1) self.assertEqual(y, test_object2) self.components.registerHandler(handle12) self.components.handle(test_object1, test_object2) def test_register_noncompliant_handler(self): handle_calls = [] def handle(*objects): handle_calls.append(objects) self.assertRaises(TypeError, self.components.registerHandler, handle) self.components.registerHandler( handle, required=[I1], info=u'a comment') self.components.registerHandler( handle, required=[U], info=u'handle a class') test_object = U1(1) self.components.handle(test_object) self.assertEqual(len(handle_calls), 2) map(self.assertEqual, handle_calls, [(test_object,), (test_object,)]) def test_list_handlers(self): test_object1 = U1(1) test_object2 = U12(2) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) @adapter(I1, I2) def handle12(x, y): self.assertEqual(x, test_object1) self.assertEqual(y, test_object2) handle_calls = [] def handle(*objects): handle_calls.append(objects) self.components.registerHandler(handle1, info=u'First handler') self.components.registerHandler(handle12) self.components.registerHandler( handle, required=[I1], info=u'a comment') self.components.registerHandler( handle, required=[U], info=u'handle a class') handlers = list(self.components.registeredHandlers()) handlers_required = map(lambda x: getattr(x, 'required'), handlers) handlers_handler = map(lambda x: getattr(x, 'handler'), handlers) handlers_info = map(lambda x: getattr(x, 'info'), handlers) self.assertEqual(len(handlers), 4) self.assertEqual(handlers_required, [(I1,), (I1, I2), (I1,), (implementedBy(U),)]) self.assertEqual(handlers_handler, [handle1, handle12, handle, handle]) self.assertEqual( handlers_info, [u'First handler', u'', u'a comment', u'handle a class']) def test_unregister_handler(self): test_object1 = U1(1) test_object2 = U12(2) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) @adapter(I1, I2) def handle12(x, y): self.assertEqual(x, test_object1) self.assertEqual(y, test_object2) handle_calls = [] def handle(*objects): handle_calls.append(objects) self.components.registerHandler(handle1, info=u'First handler') self.components.registerHandler(handle12) self.components.registerHandler( handle, required=[I1], info=u'a comment') self.components.registerHandler( handle, required=[U], info=u'handle a class') self.assertEqual(len(list(self.components.registeredHandlers())), 4) self.assertTrue(self.components.unregisterHandler(handle12)) self.assertEqual(len(list(self.components.registeredHandlers())), 3) self.assertFalse(self.components.unregisterHandler(handle12)) self.assertEqual(len(list(self.components.registeredHandlers())), 3) self.assertRaises(TypeError, self.components.unregisterHandler) self.assertEqual(len(list(self.components.registeredHandlers())), 3) self.assertTrue( self.components.unregisterHandler(handle, required=[I1])) self.assertEqual(len(list(self.components.registeredHandlers())), 2) self.assertTrue(self.components.unregisterHandler(handle, required=[U])) self.assertEqual(len(list(self.components.registeredHandlers())), 1) def test_multi_handler_unregistration(self): """ There was a bug where multiple handlers for the same required specification would all be removed when one of them was unregistered. """ from zope import interface calls = [] class I(interface.Interface): pass def factory1(event): calls.append(2) def factory2(event): calls.append(3) class Event(object): interface.implements(I) self.components.registerHandler(factory1, [I,]) self.components.registerHandler(factory2, [I,]) self.components.handle(Event()) self.assertEqual(sum(calls), 5) self.assertTrue(self.components.unregisterHandler(factory1, [I,])) calls = [] self.components.handle(Event()) self.assertEqual(sum(calls), 3)
class TestHandler(unittest.TestCase): def setUp(self): self.components = Components('comps') def test_register_handler(self): test_object1 = U1(1) test_object2 = U12(2) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) self.components.registerHandler(handle1, info=u'First handler') self.components.handle(test_object1) @adapter(I1, I2) def handle12(x, y): self.assertEqual(x, test_object1) self.assertEqual(y, test_object2) self.components.registerHandler(handle12) self.components.handle(test_object1, test_object2) def test_register_noncompliant_handler(self): handle_calls = [] def handle(*objects): handle_calls.append(objects) self.assertRaises(TypeError, self.components.registerHandler, handle) self.components.registerHandler(handle, required=[I1], info=u'a comment') self.components.registerHandler(handle, required=[U], info=u'handle a class') test_object = U1(1) self.components.handle(test_object) self.assertEqual(len(handle_calls), 2) map(self.assertEqual, handle_calls, [(test_object, ), (test_object, )]) def test_list_handlers(self): test_object1 = U1(1) test_object2 = U12(2) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) @adapter(I1, I2) def handle12(x, y): self.assertEqual(x, test_object1) self.assertEqual(y, test_object2) handle_calls = [] def handle(*objects): handle_calls.append(objects) self.components.registerHandler(handle1, info=u'First handler') self.components.registerHandler(handle12) self.components.registerHandler(handle, required=[I1], info=u'a comment') self.components.registerHandler(handle, required=[U], info=u'handle a class') handlers = list(self.components.registeredHandlers()) handlers_required = map(lambda x: getattr(x, 'required'), handlers) handlers_handler = map(lambda x: getattr(x, 'handler'), handlers) handlers_info = map(lambda x: getattr(x, 'info'), handlers) self.assertEqual(len(handlers), 4) self.assertEqual(handlers_required, [(I1, ), (I1, I2), (I1, ), (implementedBy(U), )]) self.assertEqual(handlers_handler, [handle1, handle12, handle, handle]) self.assertEqual( handlers_info, [u'First handler', u'', u'a comment', u'handle a class']) def test_unregister_handler(self): test_object1 = U1(1) test_object2 = U12(2) @adapter(I1) def handle1(x): self.assertEqual(x, test_object1) @adapter(I1, I2) def handle12(x, y): self.assertEqual(x, test_object1) self.assertEqual(y, test_object2) handle_calls = [] def handle(*objects): handle_calls.append(objects) self.components.registerHandler(handle1, info=u'First handler') self.components.registerHandler(handle12) self.components.registerHandler(handle, required=[I1], info=u'a comment') self.components.registerHandler(handle, required=[U], info=u'handle a class') self.assertEqual(len(list(self.components.registeredHandlers())), 4) self.assertTrue(self.components.unregisterHandler(handle12)) self.assertEqual(len(list(self.components.registeredHandlers())), 3) self.assertFalse(self.components.unregisterHandler(handle12)) self.assertEqual(len(list(self.components.registeredHandlers())), 3) self.assertRaises(TypeError, self.components.unregisterHandler) self.assertEqual(len(list(self.components.registeredHandlers())), 3) self.assertTrue( self.components.unregisterHandler(handle, required=[I1])) self.assertEqual(len(list(self.components.registeredHandlers())), 2) self.assertTrue(self.components.unregisterHandler(handle, required=[U])) self.assertEqual(len(list(self.components.registeredHandlers())), 1) def test_multi_handler_unregistration(self): """ There was a bug where multiple handlers for the same required specification would all be removed when one of them was unregistered. """ from zope import interface calls = [] class I(interface.Interface): pass def factory1(event): calls.append(2) def factory2(event): calls.append(3) class Event(object): interface.implements(I) self.components.registerHandler(factory1, [ I, ]) self.components.registerHandler(factory2, [ I, ]) self.components.handle(Event()) self.assertEqual(sum(calls), 5) self.assertTrue(self.components.unregisterHandler(factory1, [ I, ])) calls = [] self.components.handle(Event()) self.assertEqual(sum(calls), 3)