[tests] new unit tests about score_entity selector (inscore machinery) stable
authorJulien Jehannet <julien@smaf.org>
Thu, 20 Jan 2011 13:16:30 +0100
branchstable
changeset 6869 900cb9b1b687
parent 6868 2d40f3c48f31
child 6870 658039c5eeac
[tests] new unit tests about score_entity selector (inscore machinery)
selectors.py
test/unittest_selectors.py
--- 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()