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 testNamedAdapter(self): self.testAdapter() # 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, 'test') # ...otherwise, you get the default self.assertEquals(queryAdapter(ob, I2, 'test', Test), Test) class Comp2(Comp): pass getService(Adapters).register([I1], I2, 'test', Comp2) c = getAdapter(ob, I2, 'test') self.assertEquals(c.__class__, Comp2) self.assertEquals(c.context, ob)
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 zpt._zope.component import getView, queryView, getService from zpt._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 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 testView_w_provided(self): from zpt._zope.component import getView, queryView, getService from zpt._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(self): from zpt._zope.component import getResource, queryResource, getService from zpt._zope.component.exceptions import ComponentLookupError r1 = Request(I1) r2 = Request(I2) self.assertRaises(ComponentLookupError, getResource, 'foo', r1) self.assertRaises(ComponentLookupError, getResource, 'foo', r2) self.assertEquals(queryResource('foo', r2, Test), Test) getService(Adapters).register((I2, ), Interface, 'foo', Comp) c = getResource('foo', r2) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, r2) self.assertRaises(ComponentLookupError, getResource, 'foo2', r1, ob) self.assertRaises(ComponentLookupError, getResource, 'foo2', r2) self.assertEquals(queryResource('foo2', r2, Test, ob), Test) self.assertEquals(queryResource('foo2', r1, None), None)
def testNamedUtility(self): from zpt._zope.component import getUtility, queryUtility from zpt._zope.component import getService from zpt._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_getService(self): from zpt._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 testInterfaceCall(self): getService(Adapters).register([I1], I2, '', Comp) c = I2(ob) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob)
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 testNormal(self): ss = getGlobalServices() ss.defineService('one', IOne) c = ServiceOne() ss.provideService('one', c) self.assertEqual(id(getService('one',)), id(c))