[Zope3-checkins] CVS: Zope3/src/zope/app/services/tests -
test_surrogates.py:1.1.2.2
Jim Fulton
cvs-admin at zope.org
Thu Nov 13 12:32:59 EST 2003
Update of /cvs-repository/Zope3/src/zope/app/services/tests
In directory cvs.zope.org:/tmp/cvs-serv11971/src/zope/app/services/tests
Modified Files:
Tag: adaptergeddon-branch
test_surrogates.py
Log Message:
Added tests for multi-adapters.
=== Zope3/src/zope/app/services/tests/test_surrogates.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/services/tests/test_surrogates.py:1.1.2.1 Wed Nov 12 15:08:13 2003
+++ Zope3/src/zope/app/services/tests/test_surrogates.py Thu Nov 13 12:32:58 2003
@@ -238,7 +238,7 @@
but not outside L1:
- >>> G.queryAdapter(f0, IB1)
+ >>> G.queryNamedAdapter(f0, IB1, 'bob')
Note that it doesn't override the non-local adapter:
@@ -354,9 +354,170 @@
True
"""
+def test_multi_adapters():
+ """
+ Suppose we have a global SurrogateRegistry:
+
+ >>> G = SurrogateRegistry()
+
+ we also have a local surrogate registry, with G as it's base:
+
+ >>> L1 = LocalSurrogateRegistry(G)
+
+ and so on:
+
+ >>> L2 = LocalSurrogateRegistry(G, L1)
+
+ Now, if we declare an adapter globally:
+ >>> G.provideAdapter(IF1, IB1, [A11G], name='bob', with=(IR0,))
+ we can query it locally:
+
+ >>> f2 = F2()
+ >>> r = R1()
+ >>> a = L1.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A11G'
+ >>> a.args == (f2, r)
+ True
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A11G'
+ >>> a.args == (f2, r)
+ True
+
+ We can add local definitions:
+
+ >>> ra011 = Registration(required = IF0, provided=IB1, factory=A011,
+ ... name='bob', with=(IR0,))
+ >>> L1.createRegistrationsFor(ra011).activate(ra011)
+
+ and use it:
+
+ >>> f0 = F0()
+
+ >>> a = L1.queryMultiAdapter((f0, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A011'
+ >>> a.args == (f0, r)
+ True
+
+ >>> a = L2.queryMultiAdapter((f0, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A011'
+ >>> a.args == (f0, r)
+ True
+
+ but not outside L1:
+
+ >>> G.queryMultiAdapter((f0, r), IB1, 'bob')
+
+ Note that it doesn't override the non-local adapter:
+
+ >>> a = L1.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A11G'
+ >>> a.args == (f2, r)
+ True
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A11G'
+ >>> a.args == (f2, r)
+ True
+
+ because it was more specific.
+
+ Let's override the adapter in L2:
+
+ >>> ra112 = Registration(required = IF1, provided=IB1, factory=A112,
+ ... name='bob', with=(IR0,))
+ >>> L2.createRegistrationsFor(ra112).activate(ra112)
+
+ Now, in L2, we get the new adapter, because it's as specific and more
+ local than the one from G:
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A112'
+ >>> a.args == (f2, r)
+ True
+
+ But we still get the old one in L1
+
+ >>> a = L1.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A11G'
+ >>> a.args == (f2, r)
+ True
+
+ Note that we can ask for less specific interfaces and still get
+ the adapter:
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB0, 'bob')
+ >>> a.__class__.__name__
+ 'A112'
+ >>> a.args == (f2, r)
+ True
+
+ >>> a = L1.queryMultiAdapter((f2, r), IB0, 'bob')
+ >>> a.__class__.__name__
+ 'A11G'
+ >>> a.args == (f2, r)
+ True
+
+ We get the more specific adapter even if there is a less-specific
+ adapter to B0:
+
+ >>> G.provideAdapter(IF1, IB1, [A10G], name='bob', with=(IR0,))
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB0, 'bob')
+ >>> a.__class__.__name__
+ 'A112'
+ >>> a.args == (f2, r)
+ True
+
+ But if we have an equally specific and equally local adapter to B0, it
+ will win:
+
+ >>> ra102 = Registration(required = IF1, provided=IB0, factory=A102,
+ ... name='bob', with=(IR0,))
+ >>> L2.createRegistrationsFor(ra102).activate(ra102)
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB0, 'bob')
+ >>> a.__class__.__name__
+ 'A102'
+ >>> a.args == (f2, r)
+ True
+
+ We can deactivate registrations, which has the effect of deleting adapters:
+
+ >>> L2.queryRegistrationsFor(ra112).deactivate(ra112)
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB0, 'bob')
+ >>> a.__class__.__name__
+ 'A102'
+ >>> a.args == (f2, r)
+ True
+
+ >>> a = L2.queryMultiAdapter((f2, r), IB1, 'bob')
+ >>> a.__class__.__name__
+ 'A10G'
+ >>> a.args == (f2, r)
+ True
+
+ >>> L2.queryRegistrationsFor(ra102).deactivate(ra102)
+
+ >>> L2.queryAdapter(f2, IB0)
+ >>> a = L2.queryMultiAdapter((f2, r), IB0, 'bob')
+ >>> a.__class__.__name__
+ 'A10G'
+ >>> a.args == (f2, r)
+ True
+ """
def test_persistence():
"""
@@ -590,6 +751,15 @@
class IB1(IB0):
pass
+
+class IR0(zope.interface.Interface):
+ pass
+
+class IR1(IR0):
+ pass
+
+class R1:
+ zope.interface.implements(IR1)
class F0:
zope.interface.implements(IF0)
More information about the Zope3-Checkins
mailing list