[selectors] ensure adaptable('IDownloadable') takes precedence over implements('Any')
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 21 May 2010 13:34:03 +0200
changeset 5561 58b05c314443
parent 5559 6b183d860295
child 5562 4ccd599b5cf0
[selectors] ensure adaptable('IDownloadable') takes precedence over implements('Any')
selectors.py
test/unittest_selectors.py
--- a/selectors.py	Fri May 21 07:56:39 2010 +0200
+++ b/selectors.py	Fri May 21 13:34:03 2010 +0200
@@ -531,6 +531,7 @@
     * `regids`, object identifiers in this registry, one of them should be
       selectable.
     """
+    selectable_score = 1
     def __init__(self, registry, *regids):
         self.registry = registry
         self.regids = regids
@@ -539,7 +540,7 @@
         for regid in self.regids:
             try:
                 req.vreg[self.registry].select(regid, req, **kwargs)
-                return 1
+                return self.selectable_score
             except NoSelectableObject:
                 return 0
 
@@ -553,6 +554,9 @@
       (usually entities) should be adaptable. One of them should be selectable
       when multiple identifiers are given.
     """
+    # implementing an interface takes precedence other special Any interface,
+    # hence return 2 (implements('Any') score is 1)
+    selectable_score = 2
     def __init__(self, *regids):
         super(adaptable, self).__init__('adapters', *regids)
 
--- a/test/unittest_selectors.py	Fri May 21 07:56:39 2010 +0200
+++ b/test/unittest_selectors.py	Fri May 21 13:34:03 2010 +0200
@@ -15,15 +15,14 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""unit tests for selectors mechanism
-
-"""
+"""unit tests for selectors mechanism"""
 
 from logilab.common.testlib import TestCase, unittest_main
 
+from cubicweb import Binary
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.appobject import Selector, AndSelector, OrSelector
-from cubicweb.selectors import implements, match_user_groups
+from cubicweb.selectors import implements, adaptable, match_user_groups
 from cubicweb.interfaces import IDownloadable
 from cubicweb.web import action
 
@@ -140,11 +139,12 @@
 class ImplementsSelectorTC(CubicWebTC):
     def test_etype_priority(self):
         req = self.request()
-        cls = self.vreg['etypes'].etype_class('File')
-        anyscore = implements('Any').score_class(cls, req)
-        idownscore = implements(IDownloadable).score_class(cls, req)
+        f = req.create_entity('File', data_name=u'hop.txt', data=Binary('hop'))
+        rset = f.as_rset()
+        anyscore = implements('Any')(f.__class__, req, rset=rset)
+        idownscore = adaptable('IDownloadable')(f.__class__, req, rset=rset)
         self.failUnless(idownscore > anyscore, (idownscore, anyscore))
-        filescore = implements('File').score_class(cls, req)
+        filescore = implements('File')(f.__class__, req, rset=rset)
         self.failUnless(filescore > idownscore, (filescore, idownscore))
 
     def test_etype_inheritance_no_yams_inheritance(self):