def testView_w_provided(self): from zope.component import getView, queryView, getService from zope.component.exceptions import ComponentLookupError self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I1), providing=I3) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I2), providing=I3) self.assertEquals( queryView(ob, 'foo', Request(I2), Test, providing=I3), Test) getService(Adapters).register([I1, I2], Interface, 'foo', Comp) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I1), providing=I3) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I2), providing=I3) self.assertEquals( queryView(ob, 'foo', Request(I2), Test, providing=I3), Test) getService(Adapters).register([I1, I2], I3, 'foo', Comp) c = getView(ob, 'foo', Request(I2), providing=I3) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob)
def testResource_w_provided(self): from zope.component import getResource, queryResource, getService from zope.component.exceptions import ComponentLookupError r1 = Request(I1) r2 = Request(I2) self.assertRaises(ComponentLookupError, getResource, 'foo', r1, providing=I3) self.assertRaises(ComponentLookupError, getResource, 'foo', r2, providing=I3) self.assertEquals(queryResource('foo', r2, Test, providing=I3), Test) getService(Adapters).register((I2,), Interface, 'foo', Comp) self.assertRaises(ComponentLookupError, getResource, 'foo', r1, providing=I3) self.assertRaises(ComponentLookupError, getResource, 'foo', r2, providing=I3) self.assertEquals(queryResource('foo', r2, Test, providing=I3), Test) getService(Adapters).register((I2,), I3, 'foo', Comp) c = getResource('foo', r2, providing=I3) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, r2)
def testMultiView(self): from zope.component import queryMultiView, getService from zope.component.exceptions import ComponentLookupError class Ob2(object): implements(I2) ob2 = Ob2() class IRequest(Interface): pass request = Request(IRequest) class MV(object): implements(I3) def __init__(self, context, other, request): self.context, self.other, self.request = context, other, request self.assertEquals( queryMultiView((ob, ob2), request, I3, 'foo', 42), 42) getService(Adapters).register((I1, I2, IRequest), I3, 'foo', MV) view = queryMultiView((ob, ob2), request, I3, 'foo') self.assertEquals(view.__class__, MV) self.assertEquals(view.context, ob) self.assertEquals(view.other, ob2) self.assertEquals(view.request, request)
def testViewWithContextArgument(self): # Basically the same as testView, but exercising the context # argument. As this only tests global views, the context # argument is pretty much a no-operation. from zope.component import getView, queryView, getService from zope.component.exceptions import ComponentLookupError self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I1), context=ob) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I2), context=ob) self.assertEquals(queryView(ob, 'foo', Request(I2), Test, context=ob), Test) getService(Adapters, ob).register((I1, I2), Interface, 'foo', Comp) c = getView(ob, 'foo', Request(I2), context=ob) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob) self.assertRaises(ComponentLookupError, getView, ob, 'foo2', Request(I1), context=ob) self.assertRaises(ComponentLookupError, getView, ob, 'foo2', Request(I2), context=ob) self.assertEquals(queryView(ob, 'foo2', Request(I2), Test, context=ob), Test) self.assertEquals(queryView(ob, 'foo2', Request(I1), None, context=ob), None)
def testUtility(self): self.assertRaises(ComponentLookupError, getUtility, I1, context=ob) self.assertRaises(ComponentLookupError, getUtility, I2, context=ob) self.assertEquals(queryUtility(I2, default=Test, context=ob), Test) getService('Utilities').provideUtility(I2, comp) self.assertEquals(id(getUtility(I2, context=ob)), id(comp))
def testAdapterForInterfaceNone(self): # providing an adapter for None says that your adapter can # adapt anything to I2. getService(Adapters).register([None], I2, '', Comp) c = I2(ob) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob)
def testgetAdapters(self): getService(Adapters).register([I1], I2, '', Comp) getService(Adapters).register([None], I2, 'foo', Comp) c = getAdapters((ob,), I2) c.sort() self.assertEquals([(name, adapter.__class__, adapter.context) for name, adapter in c], [('', Comp, ob), ('foo', Comp, ob)])
def test_getUnnamedUtilities(self): service = getService(Utilities, self.app.portal) dummy1 = DummyUtility() service.registerUtility(IDummyUtility, dummy1, 'dummy1') dummy2 = DummyUtility() service.registerUtility(IDummyUtility, dummy2) service = getService(Utilities, self.app.portal) utility = service.getUtility(IDummyUtility) self.failUnless(utility.aq_base is dummy2)
def testDefaultViewName(self): from zope.component import getService getService(Adapters).register((I1, I2), IDefaultViewName, '', 'sample_name') self.assertRaises(ComponentLookupError, getDefaultViewName, ob, Request(I1)) self.assertEquals(getDefaultViewName(ob, Request(I2)), 'sample_name') self.assertRaises(ComponentLookupError, getDefaultViewName, ob, Request(I1))
def testAdapter(self): # If an adapter isn't registered for the given object and interface, # and you provide no default, raise ComponentLookupError... self.assertRaises(ComponentLookupError, getAdapter, ob, I2, '') # ...otherwise, you get the default self.assertEquals(queryAdapter(ob, I2, '', Test), Test) getService(Adapters).register([I1], I2, '', Comp) c = getAdapter(ob, I2, '') self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob)
def test_getAllUtilitiesRegisteredFor(self): class I21(I2): pass class Comp21(Comp): implements(I21) compbob = Comp('bob') comp21 = Comp21('21') comp21bob = Comp21('21bob') getService('Utilities').provideUtility(I2, comp) getService('Utilities').provideUtility(I21, comp21) getService('Utilities').provideUtility(I2, compbob, 'bob') getService('Utilities').provideUtility(I21, comp21bob, 'bob') comps = [comp, compbob, comp21, comp21bob] comps.sort() uts = list(component.getUtilitiesFor(I2)) uts.sort() self.assertEqual(uts, [('', comp), ('bob', compbob)]) uts = list(component.getAllUtilitiesRegisteredFor(I2)) uts.sort() self.assertEqual(uts, comps)
def testGetService(self): # Testing looking up a service from a service manager container that # doesn't have a service manager. getGlobalServices().defineService('one', IOne) c = ServiceOne() getGlobalServices().provideService('one', c) self.assertEqual(id(getService('one')), id(c))
def testgetUtilitiesFor(self): us = getService(Utilities) us.provideUtility(IDummyUtility, dummyUtility) self.assertEqual(list(getUtilitiesFor(IDummyUtility)), [('',dummyUtility)]) self.assertEqual(list(us.getUtilitiesFor(IDummyUtility)), [('',dummyUtility)])
def testQueryMultiAdapter(self): # Adapting a combination of 2 objects to an interface class DoubleAdapter(object): implements(I3) def __init__(self, first, second): self.first = first self.second = second class Ob2(object): implements(I2) ob2 = Ob2() context = None getService(Adapters, context).register([I1, I2], I3, '', DoubleAdapter) c = queryMultiAdapter((ob, ob2), I3, context=context) self.assertEquals(c.__class__, DoubleAdapter) self.assertEquals(c.first, ob) self.assertEquals(c.second, ob2)
def testNamedUtility(self): from zope.component import getUtility, queryUtility from zope.component import getService from zope.component.exceptions import ComponentLookupError self.testUtility() self.assertRaises(ComponentLookupError, getUtility, I1, 'test', context=ob) self.assertRaises(ComponentLookupError, getUtility, I2, 'test', context=ob) self.assertEquals(queryUtility(I2, 'test', Test, context=ob), Test) getService('Utilities').provideUtility(I2, comp, 'test') self.assertEquals(id(getUtility(I2, 'test', ob)), id(comp))
def test_getUtility(self): service = getService(Utilities, self.app.portal) dummy1 = DummyUtility() service.registerUtility(IDummyUtility, dummy1, 'dummy1') dummy2 = DummyUtility() service.registerUtility(IDummyUtility, dummy2, 'dummy2') service = getService(Utilities, self.app.portal) # Interface only: # By name utility = service.getUtility(IDummyUtility, 'dummy1') self.failUnless(utility.aq_base is dummy1) utility = service.getUtility(IDummyUtility, 'dummy2') self.failUnless(utility.aq_base is dummy2) # Get both utilities = service.getUtilitiesFor(IDummyUtility) self.assertEquals(len(list(utilities)), 2)
def test_getService(self): from zope.component import getService, getServices # Getting the adapter service with no context given is the same # as getting the adapter service from the no-context service manager. self.assert_(getService(Adapters).sm is getServices().getService(Adapters).sm) # And, a context of 'None' is the same as not providing a context. self.assert_(getService(Adapters, None).sm is getService(Adapters).sm) # If the context is adaptable to IServiceService then we use that # adapter. servicemanager = StubServiceService() adapterservice = object() servicemanager.setService(Adapters, adapterservice) context = ConformsToIServiceService(servicemanager) self.assert_(getService(Adapters, context) is adapterservice) # Using a context that is not adaptable to IServiceService should # fail. self.assertRaises(ComponentLookupError, getService, Adapters, object())
def testDup(self): getGlobalServices().defineService('one', IOne) self.assertRaises(DuplicationError, getGlobalServices().defineService, 'one', ITwo) c = ServiceOne() getGlobalServices().provideService('one', c) c2 = ServiceOne() self.assertRaises(DuplicationError, getGlobalServices().provideService, 'one', c2) self.assertEqual(id(getService('one')), id(c))
def test_getUtilityService(self): from Products.Five.localsite import SimpleLocalUtilityService utils = getService(Utilities) self.failUnless(isinstance(utils, SimpleLocalUtilityService)) self.assertRaises(ComponentLookupError, utils.getUtility, IDummyUtility) self.utils._setObject('dummy', DummyUtility('dummy')) dummy = self.utils._getOb('dummy') self.assertEquals(utils.getUtility(IDummyUtility, name='dummy'), dummy) self.assertEquals(list(utils.getUtilitiesFor(IDummyUtility)), [('', dummy)]) self.assertEquals( list(utils.getAllUtilitiesRegisteredFor(IDummyUtility)), [dummy])
def testOverrides(self): us = getService(Utilities) # fail if nothing registered: self.assertRaises( ComponentLookupError, getUtility, IDummyUtility) # set and retiev dummy us.provideUtility(IDummyUtility, dummyUtility) self.assertEqual(getUtility(IDummyUtility), dummyUtility) # dummer overrides us.provideUtility(IDummerUtility, dummerUtility) self.assertEqual(getUtility(IDummerUtility), dummerUtility) # But not if we ask for dummy self.assertEqual(getUtility(IDummyUtility), dummyUtility) # same for named: self.assertRaises( ComponentLookupError, getUtility, IDummyUtility, 'bob') us.provideUtility(IDummyUtility, dummyUtility, 'bob') self.assertEqual(getUtility(IDummyUtility), dummyUtility, 'bob') us.provideUtility(IDummerUtility, dummerUtility, 'bob') self.assertEqual(getUtility(IDummerUtility), dummerUtility, 'bob') self.assertEqual(getUtility(IDummyUtility), dummyUtility, 'bob') # getUtilitiesFor doesn the right thing: uts = list(us.getUtilitiesFor(IDummyUtility)) uts.sort() self.assertEqual(uts, [('', dummyUtility), ('bob', dummyUtility)]) uts = list(us.getUtilitiesFor(IDummerUtility)) uts.sort() self.assertEqual(uts, [('', dummerUtility), ('bob', dummerUtility)]) return us
def testQueryUtility(self): us = getService(Utilities) self.assertEqual(queryUtility(IDummyUtility), None) self.assertEqual(queryUtility(IDummyUtility, default=self), self) us.provideUtility(IDummyUtility, dummyUtility) self.assertEqual(queryUtility(IDummyUtility), dummyUtility)
def testGetUtility(self): us = getService(Utilities) self.assertRaises( ComponentLookupError, getUtility, IDummyUtility) us.provideUtility(IDummyUtility, dummyUtility) self.assertEqual(getUtility(IDummyUtility), dummyUtility)
def testregistrations(self): us = getService(Utilities) us.provideUtility(IDummyUtility, dummyUtility) self.assertEqual( map(str, us.registrations()), ["UtilityRegistration('IDummyUtility', '', 'DummyUtility', '')"])
def test_getServices(self): serviceservice = getServices(self.app.portal) self.failUnless(isinstance(serviceservice, LocalService)) service = getService(Utilities, self.app.portal) self.failUnless(isinstance(service, SimpleLocalUtilityService))
def testNormal(self): ss = getGlobalServices() ss.defineService('one', IOne) c = ServiceOne() ss.provideService('one', c) self.assertEqual(id(getService('one',)), id(c))
def test_getAllUtilitiesRegisteredFor_empty(self): us = getService(Utilities) class IFoo(Interface): pass self.assertEqual(list(us.getAllUtilitiesRegisteredFor(IFoo)), [])
def testInterfaceCall(self): getService(Adapters).register([I1], I2, '', Comp) c = I2(ob) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob)