[selectors] test/fix rql_condition to make it works when entity is specified via 'entity' keyword argument instead of rset, row and col stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 09 May 2011 16:12:17 +0200
branchstable
changeset 7345 b9eaad6e34c3
parent 7343 977a49472806
child 7346 c6d8c3ce4a3e
[selectors] test/fix rql_condition to make it works when entity is specified via 'entity' keyword argument instead of rset, row and col
selectors.py
test/unittest_selectors.py
--- a/selectors.py	Mon May 09 14:50:14 2011 +0200
+++ b/selectors.py	Mon May 09 16:12:17 2011 +0200
@@ -1171,13 +1171,19 @@
     def __str__(self):
         return '%s(%r)' % (self.__class__.__name__, self.rql)
 
-    def score(self, req, rset, row, col):
+    def _score(self, req, eid):
         try:
-            return req.execute(self.rql, {'x': rset[row][col],
-                                          'u': req.user.eid})[0][0]
+            return req.execute(self.rql, {'x': eid, 'u': req.user.eid})[0][0]
         except Unauthorized:
             return 0
 
+    def score(self, req, rset, row, col):
+        return self._score(req, rset[row][col])
+
+    def score_entity(self, entity):
+        return self._score(entity._cw, entity.eid)
+
+
 # workflow selectors ###########################################################
 
 class is_in_state(score_entity):
--- a/test/unittest_selectors.py	Mon May 09 14:50:14 2011 +0200
+++ b/test/unittest_selectors.py	Mon May 09 16:12:17 2011 +0200
@@ -26,7 +26,7 @@
 from cubicweb.appobject import Selector, AndSelector, OrSelector
 from cubicweb.selectors import (is_instance, adaptable, match_user_groups,
                                 multi_lines_rset, score_entity, is_in_state,
-                                on_transition)
+                                on_transition, rql_condition)
 from cubicweb.web import action
 
 
@@ -221,7 +221,7 @@
     def test_is_in_state(self):
         for state in ('created', 'validated', 'abandoned'):
             selector = is_in_state(state)
-            self.assertEqual(selector(None, self.req, self.rset),
+            self.assertEqual(selector(None, self.req, rset=self.rset),
                              state=="created")
 
         self.adapter.fire_transition('validate')
@@ -229,75 +229,75 @@
         self.assertEqual(self.adapter.state, 'validated')
 
         selector = is_in_state('created')
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         selector = is_in_state('validated')
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         selector = is_in_state('validated', 'abandoned')
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         selector = is_in_state('abandoned')
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
 
         self.adapter.fire_transition('forsake')
         self._commit()
         self.assertEqual(self.adapter.state, 'abandoned')
 
         selector = is_in_state('created')
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         selector = is_in_state('validated')
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         selector = is_in_state('validated', 'abandoned')
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         self.assertEqual(self.adapter.state, 'abandoned')
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
 
     def test_is_in_state_unvalid_names(self):
         selector = is_in_state("unknown")
         with self.assertRaises(ValueError) as cm:
-            selector(None, self.req, self.rset)
+            selector(None, self.req, rset=self.rset)
         self.assertEqual(str(cm.exception),
                          "wf_test: unknown state(s): unknown")
         selector = is_in_state("weird", "unknown", "created", "weird")
         with self.assertRaises(ValueError) as cm:
-            selector(None, self.req, self.rset)
+            selector(None, self.req, rset=self.rset)
         self.assertEqual(str(cm.exception),
                          "wf_test: unknown state(s): unknown,weird")
 
     def test_on_transition(self):
         for transition in ('validate', 'forsake'):
             selector = on_transition(transition)
-            self.assertEqual(selector(None, self.req, self.rset), 0)
+            self.assertEqual(selector(None, self.req, rset=self.rset), 0)
 
         self.adapter.fire_transition('validate')
         self._commit()
         self.assertEqual(self.adapter.state, 'validated')
 
         selector = on_transition("validate")
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         selector = on_transition("validate", "forsake")
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         selector = on_transition("forsake")
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
 
         self.adapter.fire_transition('forsake')
         self._commit()
         self.assertEqual(self.adapter.state, 'abandoned')
 
         selector = on_transition("validate")
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         selector = on_transition("validate", "forsake")
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         selector = on_transition("forsake")
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
 
     def test_on_transition_unvalid_names(self):
         selector = on_transition("unknown")
         with self.assertRaises(ValueError) as cm:
-            selector(None, self.req, self.rset)
+            selector(None, self.req, rset=self.rset)
         self.assertEqual(str(cm.exception),
                          "wf_test: unknown transition(s): unknown")
         selector = on_transition("weird", "unknown", "validate", "weird")
         with self.assertRaises(ValueError) as cm:
-            selector(None, self.req, self.rset)
+            selector(None, self.req, rset=self.rset)
         self.assertEqual(str(cm.exception),
                          "wf_test: unknown transition(s): unknown,weird")
 
@@ -308,11 +308,11 @@
         self.assertEqual(self.adapter.state, 'validated')
 
         selector = on_transition("validate")
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         selector = on_transition("validate", "forsake")
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         selector = on_transition("forsake")
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
 
 
 class MatchUserGroupsTC(CubicWebTC):
@@ -362,13 +362,13 @@
     def test_default_op_in_selector(self):
         expected = len(self.rset)
         selector = multi_lines_rset(expected)
-        self.assertEqual(selector(None, self.req, self.rset), 1)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 1)
         self.assertEqual(selector(None, self.req, None), 0)
         selector = multi_lines_rset(expected + 1)
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         self.assertEqual(selector(None, self.req, None), 0)
         selector = multi_lines_rset(expected - 1)
-        self.assertEqual(selector(None, self.req, self.rset), 0)
+        self.assertEqual(selector(None, self.req, rset=self.rset), 0)
         self.assertEqual(selector(None, self.req, None), 0)
 
     def test_without_rset(self):
@@ -399,7 +399,7 @@
 
         for (expected, operator, assertion) in testdata:
             selector = multi_lines_rset(expected, operator)
-            yield self.assertEqual, selector(None, self.req, self.rset), assertion
+            yield self.assertEqual, selector(None, self.req, rset=self.rset), assertion
 
 
 class ScoreEntitySelectorTC(CubicWebTC):
@@ -408,17 +408,24 @@
         req = self.request()
         rset = req.execute('Any E WHERE E eid 1')
         selector = score_entity(lambda x: None)
-        self.assertEqual(selector(None, req, rset), 0)
+        self.assertEqual(selector(None, req, rset=rset), 0)
         selector = score_entity(lambda x: "something")
-        self.assertEqual(selector(None, req, rset), 1)
+        self.assertEqual(selector(None, req, rset=rset), 1)
         selector = score_entity(lambda x: object)
-        self.assertEqual(selector(None, req, rset), 1)
+        self.assertEqual(selector(None, req, rset=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)
+        self.assertEqual(selector(None, req, rset=rset), 20)
         selector = score_entity(lambda x: 10, once_is_enough=True)
-        self.assertEqual(selector(None, req, rset), 10)
+        self.assertEqual(selector(None, req, rset=rset), 10)
 
+    def test_rql_condition_entity(self):
+        req = self.request()
+        selector = rql_condition('X identity U')
+        rset = req.user.as_rset()
+        self.assertEqual(selector(None, req, rset=rset), 1)
+        self.assertEqual(selector(None, req, entity=req.user), 1)
+        self.assertEqual(selector(None, req), 0)
 
 if __name__ == '__main__':
     unittest_main()