[entity] Add a cw_related_rqlst method returning the RQL select node of cw_related method
authorDenis Laxalde <denis.laxalde@logilab.fr>
Fri, 15 Apr 2016 08:59:53 +0200
changeset 11271 fd177546bbea
parent 11270 a9dc97b87ced
child 11272 53fbd5644bff
[entity] Add a cw_related_rqlst method returning the RQL select node of cw_related method This is useful if one wants to modify the query before execution. A bit a PEP8 style fix in tests along the way. Related to #12306543.
cubicweb/entity.py
cubicweb/test/unittest_entity.py
--- a/cubicweb/entity.py	Fri May 20 17:05:39 2016 +0200
+++ b/cubicweb/entity.py	Fri Apr 15 08:59:53 2016 +0200
@@ -1009,6 +1009,10 @@
             return rset
 
     def cw_related_rql(self, rtype, role='subject', targettypes=None, limit=None):
+        return self.cw_related_rqlst(
+            rtype, role=role, targettypes=targettypes, limit=limit).as_string()
+
+    def cw_related_rqlst(self, rtype, role='subject', targettypes=None, limit=None):
         vreg = self._cw.vreg
         rschema = vreg.schema[rtype]
         select = Select()
@@ -1065,7 +1069,7 @@
                                     (mdvar,), VariableRef)
                 select.add_restriction(rel)
                 select.add_sort_var(mdvar, asc=False)
-        return select.as_string()
+        return select
 
     # generic vocabulary methods ##############################################
 
--- a/cubicweb/test/unittest_entity.py	Fri May 20 17:05:39 2016 +0200
+++ b/cubicweb/test/unittest_entity.py	Fri Apr 15 08:59:53 2016 +0200
@@ -290,17 +290,18 @@
             Personne.fetch_attrs, Personne.cw_fetch_order = fetch_config(('nom', ))
             # XXX
             self.assertEqual(p.cw_related_rql('evaluee'),
-                              'Any X,AA ORDERBY AA DESC '
-                              'WHERE E eid %(x)s, E evaluee X, X modification_date AA')
+                             'Any X,AA ORDERBY AA DESC '
+                             'WHERE E eid %(x)s, E evaluee X, X modification_date AA')
 
             tag = self.vreg['etypes'].etype_class('Tag')(req)
-            self.assertEqual(tag.cw_related_rql('tags', 'subject'),
-                              'Any X,AA ORDERBY AA DESC '
-                              'WHERE E eid %(x)s, E tags X, X modification_date AA')
+            select = tag.cw_related_rqlst('tags', 'subject')
+            self.assertEqual(select.as_string(),
+                             'Any X,AA ORDERBY AA DESC '
+                             'WHERE E eid %(x)s, E tags X, X modification_date AA')
             self.assertEqual(tag.cw_related_rql('tags', 'subject', ('Personne',)),
-                              'Any X,AA,AB ORDERBY AB '
-                              'WHERE E eid %(x)s, E tags X, X is Personne, X modification_date AA, '
-                              'X nom AB')
+                             'Any X,AA,AB ORDERBY AB '
+                             'WHERE E eid %(x)s, E tags X, X is Personne, X modification_date AA, '
+                             'X nom AB')
 
     def test_related_rql_ambiguous_cant_use_fetch_order(self):
         with self.admin_access.web_request() as req: