[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/tests - test_adapter.py:1.1.2.2
Jim Fulton
jim@zope.com
Wed, 11 Dec 2002 06:41:10 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/tests
In directory cvs.zope.org:/tmp/cvs-serv23318/lib/python/Zope/App/OFS/Services/tests
Modified Files:
Tag: AdapterAndView-branch
test_adapter.py
Log Message:
Got AdapterService views working.
=== Zope3/lib/python/Zope/App/OFS/Services/tests/test_adapter.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/lib/python/Zope/App/OFS/Services/tests/test_adapter.py:1.1.2.1 Tue Dec 10 09:38:36 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/test_adapter.py Wed Dec 11 06:41:09 2002
@@ -25,8 +25,12 @@
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
import PlacefulSetup
from Zope.App.OFS.Services.ServiceManager.ServiceManager import ServiceManager
+from Zope.App.OFS.Services.adapter import AdapterConfiguration
+from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
from Zope.ComponentArchitecture import getServiceManager
from Zope.App.Traversing import traverse
+from Zope.ComponentArchitecture.IServiceService import IServiceService
+from Zope.ComponentArchitecture.GlobalAdapterService import provideAdapter
class I1(Interface):
pass
@@ -43,6 +47,8 @@
class I3(Interface):
pass
+class I4(Interface):
+ pass
class Configuration:
@@ -56,8 +62,20 @@
def deactivated(self): pass
class C: pass
-
-class Test(PlacefulSetup, TestingIConfigurable, TestCase):
+
+class A:
+ def __init__(self, object):
+ self.context = object
+
+
+class TestAdapterService(PlacefulSetup, TestingIConfigurable, TestCase):
+
+ def setUp(self):
+ PlacefulSetup.setUp(self)
+ self.buildFolders()
+ self.rootFolder.setServiceManager(ServiceManager())
+ self._service = ContextWrapper(AdapterService(), self.rootFolder)
+
def createTestingConfigurable(self):
return ContextWrapper(AdapterService(), C())
@@ -66,7 +84,7 @@
return Configuration()
def test_queryAdapter_no_adapter(self):
- service = AdapterService()
+ service = self._service
class O:
__implements__ = I1
@@ -78,7 +96,7 @@
self.assertEqual(service.queryAdapter(o, I1, 42), o)
def test_getAdapter_no_adapter(self):
- service = AdapterService()
+ service = self._service
class O:
__implements__ = I1
@@ -87,8 +105,7 @@
self.assertEqual(service.getAdapter(o, I1), o)
def test_queryAdapter_and_getAdapter(self):
- self.buildFolders()
- self.rootFolder.setServiceManager(ServiceManager())
+ service = self._service
sm = traverse(self.rootFolder, '++etc++Services')
@@ -97,14 +114,9 @@
configure.setObject('', configuration)
configuration = traverse(configure, '1')
- service = ContextWrapper(AdapterService(), sm)
class O:
__implements__ = I1
- class A:
- def __init__(self, object):
- self.context = object
-
configuration.factory = A
registry = service.createConfigurationsFor(configuration)
@@ -128,12 +140,78 @@
self.assertEqual(service.queryAdapter(o, I3), None)
self.assertEqual(service.queryAdapter(o, I3, 42), 42)
self.assertRaises(ComponentLookupError, service.getAdapter, O(), I3)
+
+ def test_queryAdapter_delegation(self):
+ service = self._service
+ self.buildFolders()
+ self.rootFolder.setServiceManager(ServiceManager())
+
+ sm = traverse(self.rootFolder, '++etc++Services')
+
+ configure = traverse(sm, 'Packages/default/configure')
+ configuration = Configuration()
+ configure.setObject('', configuration)
+ configuration = traverse(configure, '1')
+ class O:
+ __implements__ = I1
+
+ configuration.factory = A
+
+ registry = service.createConfigurationsFor(configuration)
+ registry.register(configuration)
+ registry.activate(configuration)
+
+ o = O()
+
+ class A2(A): pass
+
+ provideAdapter(I1, I4, A2)
+
+ adapter = service.queryAdapter(o, I4)
+ self.assertEqual(adapter.__class__, A2)
+ self.assertEqual(adapter.context, o)
+
+ def test_getRegisteredMatching(self):
+ self.test_queryAdapter_and_getAdapter()
+ registry = self._service.queryConfigurations(I1, I2)
+
+ for args in ((), (I1E, ), (None, I2), (I1E, I2), ):
+ r = self._service.getRegisteredMatching(*args)
+ self.assertEqual(list(r), [(I1, I2, registry)])
+
+class PhonyServiceManager:
+
+ __implements__ = IServiceService
+
+ def resolve(self, name):
+ if name == 'Foo.Bar.A':
+ return A
+
+class TestAdapterConfiguration(PlacefulSetup, TestCase):
+
+ def setUp(self):
+ PlacefulSetup.setUp(self)
+ rootFolder = RootFolder()
+ rootFolder.setServiceManager(PhonyServiceManager())
+ self.configuration = ContextWrapper(
+ AdapterConfiguration(I1, I2, "Foo.Bar.A"),
+ rootFolder,
+ )
+
+ def test_getAdapter(self):
+ c = C()
+ adapter = self.configuration.getAdapter(c)
+ self.assertEqual(adapter.__class__, A)
+ self.assertEqual(adapter.context, c)
+ self.assertEqual(self.configuration.forInterface, I1)
+ self.assertEqual(self.configuration.providedInterface, I2)
def test_suite():
return TestSuite((
- makeSuite(Test),
+ makeSuite(TestAdapterService),
+ makeSuite(TestAdapterConfiguration),
))
if __name__=='__main__':