[CWEP002] Plug the computed relation rewriter in the querier
authorDenis Laxalde <denis.laxalde@logilab.fr>
Fri, 27 Jun 2014 16:11:53 +0200
changeset 9955 60a9cd1b3a4b
parent 9954 79d34ba48612
child 9956 19a683a0047c
[CWEP002] Plug the computed relation rewriter in the querier Related to #3546717.
server/querier.py
test/unittest_rqlrewrite.py
--- a/server/querier.py	Thu Feb 13 13:58:28 2014 +0100
+++ b/server/querier.py	Fri Jun 27 16:11:53 2014 +0200
@@ -594,6 +594,9 @@
             # bother modifying it. This is not necessary on write queries since
             # a new syntax tree is built from them.
             rqlst = rqlst.copy()
+            # Rewrite computed relations
+            rewriter = RQLRelationRewriter(cnx)
+            rewriter.rewrite(rqlst, args)
             self._annotate(rqlst)
             if args:
                 # different SQL generated when some argument is None or not (IS
--- a/test/unittest_rqlrewrite.py	Thu Feb 13 13:58:28 2014 +0100
+++ b/test/unittest_rqlrewrite.py	Fri Jun 27 16:11:53 2014 +0200
@@ -697,7 +697,7 @@
                                 'WITH A, B BEING(Any X,Y WHERE X contributor Y)')
         rule_rewrite(rqlst, rules)
         self.assertEqual('Any A,B WHERE A contributor B WITH A,B BEING '
-                         '(Any X,Y WHERE X contributor Y)', 
+                         '(Any X,Y WHERE X contributor Y)',
                          rqlst.as_string())
 
     def test_rewrite_with4(self):
@@ -711,7 +711,7 @@
                          'D name "illustrator" WITH A,B BEING '
                          '(Any X,Y WHERE A is Contribution, A contributor X, '
                          'A manifestation Y, A role B, B name "illustrator")',
-                          rqlst.as_string()) 
+                          rqlst.as_string())
 
     # Tests for the union
     def test_rewrite_union(self):
@@ -780,6 +780,25 @@
                          rqlst.as_string())
 
 
+class RQLRelationRewriterTC(CubicWebTC):
+
+    appid = 'data/rewrite'
+
+    def test_base_rule(self):
+        with self.admin_access.client_cnx() as cnx:
+            art = cnx.create_entity('ArtWork', name=u'Les travailleurs de la Mer')
+            role = cnx.create_entity('Role', name=u'illustrator')
+            vic = cnx.create_entity('Person', name=u'Victor Hugo')
+            contrib = cnx.create_entity('Contribution', code=96, contributor=vic,
+                                        manifestation=art, role=role)
+            rset = cnx.execute('Any X WHERE X illustrator_of S')
+            self.assertEqual([u'Victor Hugo'],
+                             [result.name for result in rset.entities()])
+            rset = cnx.execute('Any S WHERE X illustrator_of S, X eid %(x)s',
+                               {'x': vic.eid})
+            self.assertEqual([u'Les travailleurs de la Mer'],
+                             [result.name for result in rset.entities()])
+
 
 def rule_rewrite(rqlst, kwargs=None):
     rewriter = _prepare_rewriter(rqlrewrite.RQLRelationRewriter, kwargs)