--- a/selectors.py Wed Jan 19 21:49:29 2011 +0100
+++ b/selectors.py Thu Jan 20 13:16:30 2011 +0100
@@ -817,6 +817,9 @@
This is a very useful selector that will usually interest you since it
allows a lot of things without having to write a specific selector.
+ The function can return arbitrary value which will be casted to an integer
+ value at the end.
+
See :class:`~cubicweb.selectors.EntitySelector` documentation for entity
lookup / score rules according to the input context.
"""
--- a/test/unittest_selectors.py Wed Jan 19 21:49:29 2011 +0100
+++ b/test/unittest_selectors.py Thu Jan 20 13:16:30 2011 +0100
@@ -24,7 +24,7 @@
from cubicweb.devtools.testlib import CubicWebTC
from cubicweb.appobject import Selector, AndSelector, OrSelector
from cubicweb.selectors import (is_instance, adaptable, match_user_groups,
- multi_lines_rset)
+ multi_lines_rset, score_entity)
from cubicweb.interfaces import IDownloadable
from cubicweb.web import action
@@ -245,6 +245,24 @@
yield self.assertEqual, selector(None, self.req, self.rset), assertion
+class ScoreEntitySelectorTC(CubicWebTC):
+
+ def test_intscore_entity_selector(self):
+ req = request()
+ selector = score_entity(lambda x: None)
+ rset = req.execute('Any E WHERE E eid 0')
+ self.assertEqual(selector(None, req, rset), 0)
+ selector = score_entity(lambda x: "something")
+ self.assertEqual(selector(None, req, rset), 1)
+ selector = score_entity(lambda x: object)
+ self.assertEqual(selector(None, req, rset), 1)
+ rset = req.execute('Any G LIMIT 2 WHERE G is CWGroup')
+ selector = score_entity(lambda x: 10)
+ self.assertEqual(selector(None, req, rset), 20)
+ selector = score_entity(lambda x: 10, once_is_enough=True)
+ self.assertEqual(selector(None, req, rset), 10)
+
+
if __name__ == '__main__':
unittest_main()