test/unittest_rqlrewrite.py
changeset 9955 60a9cd1b3a4b
parent 9953 643b19d79e4a
child 10612 84468b90e9c1
equal deleted inserted replaced
9954:79d34ba48612 9955:60a9cd1b3a4b
   695         rules = {'participated_in': 'S contributor O'}
   695         rules = {'participated_in': 'S contributor O'}
   696         rqlst = rqlhelper.parse('Any A,B WHERE A participated_in B '
   696         rqlst = rqlhelper.parse('Any A,B WHERE A participated_in B '
   697                                 'WITH A, B BEING(Any X,Y WHERE X contributor Y)')
   697                                 'WITH A, B BEING(Any X,Y WHERE X contributor Y)')
   698         rule_rewrite(rqlst, rules)
   698         rule_rewrite(rqlst, rules)
   699         self.assertEqual('Any A,B WHERE A contributor B WITH A,B BEING '
   699         self.assertEqual('Any A,B WHERE A contributor B WITH A,B BEING '
   700                          '(Any X,Y WHERE X contributor Y)', 
   700                          '(Any X,Y WHERE X contributor Y)',
   701                          rqlst.as_string())
   701                          rqlst.as_string())
   702 
   702 
   703     def test_rewrite_with4(self):
   703     def test_rewrite_with4(self):
   704         rules = {'illustrator_of': 'C is Contribution, C contributor S, '
   704         rules = {'illustrator_of': 'C is Contribution, C contributor S, '
   705                 'C manifestation O, C role R, R name "illustrator"'}
   705                 'C manifestation O, C role R, R name "illustrator"'}
   709         self.assertEqual('Any A,B WHERE C is Contribution, '
   709         self.assertEqual('Any A,B WHERE C is Contribution, '
   710                          'C contributor A, C manifestation B, C role D, '
   710                          'C contributor A, C manifestation B, C role D, '
   711                          'D name "illustrator" WITH A,B BEING '
   711                          'D name "illustrator" WITH A,B BEING '
   712                          '(Any X,Y WHERE A is Contribution, A contributor X, '
   712                          '(Any X,Y WHERE A is Contribution, A contributor X, '
   713                          'A manifestation Y, A role B, B name "illustrator")',
   713                          'A manifestation Y, A role B, B name "illustrator")',
   714                           rqlst.as_string()) 
   714                           rqlst.as_string())
   715 
   715 
   716     # Tests for the union
   716     # Tests for the union
   717     def test_rewrite_union(self):
   717     def test_rewrite_union(self):
   718         rules = {'illustrator_of': 'C is Contribution, C contributor S, '
   718         rules = {'illustrator_of': 'C is Contribution, C contributor S, '
   719                 'C manifestation O, C role R, R name "illustrator"'}
   719                 'C manifestation O, C role R, R name "illustrator"'}
   777         rqlst = rqlhelper.parse('Any SUM(SA) GROUPBY S WHERE P participated_in S, P manifestation SA')
   777         rqlst = rqlhelper.parse('Any SUM(SA) GROUPBY S WHERE P participated_in S, P manifestation SA')
   778         rule_rewrite(rqlst, rules)
   778         rule_rewrite(rqlst, rules)
   779         self.assertEqual('Any SUM(SA) GROUPBY S WHERE P manifestation SA, P contributor S',
   779         self.assertEqual('Any SUM(SA) GROUPBY S WHERE P manifestation SA, P contributor S',
   780                          rqlst.as_string())
   780                          rqlst.as_string())
   781 
   781 
       
   782 
       
   783 class RQLRelationRewriterTC(CubicWebTC):
       
   784 
       
   785     appid = 'data/rewrite'
       
   786 
       
   787     def test_base_rule(self):
       
   788         with self.admin_access.client_cnx() as cnx:
       
   789             art = cnx.create_entity('ArtWork', name=u'Les travailleurs de la Mer')
       
   790             role = cnx.create_entity('Role', name=u'illustrator')
       
   791             vic = cnx.create_entity('Person', name=u'Victor Hugo')
       
   792             contrib = cnx.create_entity('Contribution', code=96, contributor=vic,
       
   793                                         manifestation=art, role=role)
       
   794             rset = cnx.execute('Any X WHERE X illustrator_of S')
       
   795             self.assertEqual([u'Victor Hugo'],
       
   796                              [result.name for result in rset.entities()])
       
   797             rset = cnx.execute('Any S WHERE X illustrator_of S, X eid %(x)s',
       
   798                                {'x': vic.eid})
       
   799             self.assertEqual([u'Les travailleurs de la Mer'],
       
   800                              [result.name for result in rset.entities()])
   782 
   801 
   783 
   802 
   784 def rule_rewrite(rqlst, kwargs=None):
   803 def rule_rewrite(rqlst, kwargs=None):
   785     rewriter = _prepare_rewriter(rqlrewrite.RQLRelationRewriter, kwargs)
   804     rewriter = _prepare_rewriter(rqlrewrite.RQLRelationRewriter, kwargs)
   786     rqlhelper.compute_solutions(rqlst.children[0], {'eid': eid_func_map},
   805     rqlhelper.compute_solutions(rqlst.children[0], {'eid': eid_func_map},