test/unittest_rqlrewrite.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 25 Jul 2013 09:07:25 +0200
branchstable
changeset 9189 9448215c73c4
parent 9188 0677e03077fb
child 9262 7fc54e02291f
permissions -rw-r--r--
[rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144 The relations index used to determine if relation may be shared only considered a single relation node per relation type. So when the same relation type occurs several time, dict order give unpredictable result. We shall properly consider all relations instead. Tentative test case included (but bug reproduction is by definition unpredictable...)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
     1
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
    18
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from logilab.common.testlib import unittest_main, TestCase
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
from logilab.common.testlib import mock_object
4908
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
    21
from yams import BadSchemaDefinition
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
from rql import parse, nodes, RQLHelper
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
7850
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
    24
from cubicweb import Unauthorized, rqlrewrite
5582
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    25
from cubicweb.schema import RRQLExpression, ERQLExpression
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
    26
from cubicweb.devtools import repotest, TestServerConfiguration, BaseApptestConfiguration
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
6781
5062d86d6ffe [unittest2] use unittest2 module fixture api
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6640
diff changeset
    29
def setUpModule(*args):
6640
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    30
    global rqlhelper, schema
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    31
    config = TestServerConfiguration(RQLRewriteTC.datapath('rewrite'))
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    32
    config.bootstrap_cubes()
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    33
    schema = config.load_schema()
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    34
    from yams.buildobjs import RelationDefinition
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    35
    schema.add_relation_def(RelationDefinition(subject='Card', name='in_state',
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    36
                                               object='State', cardinality='1*'))
6640
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    37
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    38
    rqlhelper = RQLHelper(schema, special_relations={'eid': 'uid',
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    39
                                                     'has_text': 'fti'})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    repotest.do_monkey_patch()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
6781
5062d86d6ffe [unittest2] use unittest2 module fixture api
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6640
diff changeset
    42
def tearDownModule(*args):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    repotest.undo_monkey_patch()
6640
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    44
    global rqlhelper, schema
4c4616c02f69 [test] more cwd independant tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    45
    del rqlhelper, schema
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    46
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
def eid_func_map(eid):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 47
diff changeset
    48
    return {1: 'CWUser',
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
    49
            2: 'Card',
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
    50
            3: 'Affaire'}[eid]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
    52
def rewrite(rqlst, snippets_map, kwargs, existingvars=None):
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    53
    class FakeVReg:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        schema = schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        @staticmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        def solutions(sqlcursor, mainrqlst, kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
            rqlhelper.compute_solutions(rqlst, {'eid': eid_func_map}, kwargs=kwargs)
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    58
        class rqlhelper:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
            @staticmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
            def annotate(rqlst):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
                rqlhelper.annotate(rqlst)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
            @staticmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
            def simplify(mainrqlst, needcopy=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
                rqlhelper.simplify(rqlst, needcopy)
7850
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
    65
    rewriter = rqlrewrite.RQLRewriter(
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
    66
        mock_object(vreg=FakeVReg, user=(mock_object(eid=1))))
7139
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    67
    snippets = []
7850
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
    68
    for v, exprs in sorted(snippets_map.items()):
7139
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    69
        rqlexprs = [isinstance(snippet, basestring)
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    70
                    and mock_object(snippet_rqlst=parse('Any X WHERE '+snippet).children[0],
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    71
                                    expression='Any X WHERE '+snippet)
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    72
                    or snippet
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    73
                    for snippet in exprs]
20807d3d7cf6 [rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    74
        snippets.append((dict([v]), rqlexprs))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    rqlhelper.compute_solutions(rqlst.children[0], {'eid': eid_func_map}, kwargs=kwargs)
9188
0677e03077fb [rqlrewrite] rewrite doesn't need a solutions argument, always use the Select'ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9170
diff changeset
    76
    rewriter.rewrite(rqlst.children[0], snippets, kwargs, existingvars)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    test_vrefs(rqlst.children[0])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
    return rewriter.rewritten
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
def test_vrefs(node):
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    81
    vrefmaps = {}
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    82
    selects = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    for vref in node.iget_nodes(nodes.VariableRef):
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    84
        stmt = vref.stmt
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    85
        try:
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    86
            vrefmaps[stmt].setdefault(vref.name, set()).add(vref)
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    87
        except KeyError:
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    88
            vrefmaps[stmt] = {vref.name: set( (vref,) )}
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    89
            selects.append(stmt)
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    90
    assert node in selects
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    91
    for stmt in selects:
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    92
        for var in stmt.defined_vars.itervalues():
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    93
            assert var.stinfo['references']
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    94
            vrefmap = vrefmaps[stmt]
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
    95
            assert not (var.stinfo['references'] ^ vrefmap[var.name]), (node.as_string(), var, var.stinfo['references'], vrefmap[var.name])
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
    96
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
class RQLRewriteTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    """a faire:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    * optimisation: detecter les relations utilisees dans les rqlexpressions qui
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
      sont presentes dans la requete de depart pour les reutiliser si possible
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   103
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    * "has_<ACTION>_permission" ?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    """
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   106
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    def test_base_var(self):
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   108
        constraint = ('X in_state S, U in_group G, P require_state S,'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
                           'P name "read", P require_group G')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
        rqlst = parse('Card C')
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   111
        rewrite(rqlst, {('C', 'X'): (constraint,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   112
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   113
                         u"Any C WHERE C is Card, B eid %(D)s, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   114
                         "EXISTS(C in_state A, B in_group E, F require_state A, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   115
                         "F name 'read', F require_group E, A is State, E is CWGroup, F is CWPermission)")
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   116
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    def test_multiple_var(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        card_constraint = ('X in_state S, U in_group G, P require_state S,'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
                           'P name "read", P require_group G')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        affaire_constraints = ('X ref LIKE "PUBLIC%"', 'U in_group G, G name "public"')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        kwargs = {'u':2}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        rqlst = parse('Any S WHERE S documented_by C, C eid %(u)s')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   123
        rewrite(rqlst, {('C', 'X'): (card_constraint,), ('S', 'X'): affaire_constraints},
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
                kwargs)
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   125
        self.assertMultiLineEqual(
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   126
            rqlst.as_string(),
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   127
            "Any S WHERE S documented_by C, C eid %(u)s, B eid %(D)s, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   128
            "EXISTS(C in_state A, B in_group E, F require_state A, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   129
            "F name 'read', F require_group E, A is State, E is CWGroup, F is CWPermission), "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   130
            "(EXISTS(S ref LIKE 'PUBLIC%')) OR (EXISTS(B in_group G, G name 'public', G is CWGroup)), "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   131
            "S is Affaire")
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   132
        self.assertTrue('D' in kwargs)
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   133
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
    def test_or(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        constraint = '(X identity U) OR (X in_state ST, CL identity U, CL in_state ST, ST name "subscribed")'
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
   136
        rqlst = parse('Any S WHERE S owned_by C, C eid %(u)s, S is in (CWUser, CWGroup)')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   137
        rewrite(rqlst, {('C', 'X'): (constraint,)}, {'u':1})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   138
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   139
                         "Any S WHERE S owned_by C, C eid %(u)s, S is IN(CWUser, CWGroup), A eid %(B)s, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   140
                         "EXISTS((C identity A) OR (C in_state D, E identity A, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   141
                         "E in_state D, D name 'subscribed'), D is State, E is CWUser)")
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
    def test_simplified_rqlst(self):
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   144
        constraint = ('X in_state S, U in_group G, P require_state S,'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
                           'P name "read", P require_group G')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        rqlst = parse('Any 2') # this is the simplified rql st for Any X WHERE X eid 12
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   147
        rewrite(rqlst, {('2', 'X'): (constraint,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   148
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   149
                         u"Any 2 WHERE B eid %(C)s, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   150
                         "EXISTS(2 in_state A, B in_group D, E require_state A, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   151
                         "E name 'read', E require_group D, A is State, D is CWGroup, E is CWPermission)")
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   153
    def test_optional_var_1(self):
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   154
        constraint = ('X in_state S, U in_group G, P require_state S,'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
                           'P name "read", P require_group G')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
        rqlst = parse('Any A,C WHERE A documented_by C?')
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   157
        rewrite(rqlst, {('C', 'X'): (constraint,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   158
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   159
                         "Any A,C WHERE A documented_by C?, A is Affaire "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   160
                         "WITH C BEING "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   161
                         "(Any C WHERE EXISTS(C in_state B, D in_group F, G require_state B, G name 'read', "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   162
                         "G require_group F), D eid %(A)s, C is Card)")
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   163
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   164
    def test_optional_var_2(self):
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   165
        constraint = ('X in_state S, U in_group G, P require_state S,'
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   166
                           'P name "read", P require_group G')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
        rqlst = parse('Any A,C,T WHERE A documented_by C?, C title T')
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   168
        rewrite(rqlst, {('C', 'X'): (constraint,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   169
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   170
                         "Any A,C,T WHERE A documented_by C?, A is Affaire "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   171
                         "WITH C,T BEING "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   172
                         "(Any C,T WHERE C title T, EXISTS(C in_state B, D in_group F, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   173
                         "G require_state B, G name 'read', G require_group F), "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   174
                         "D eid %(A)s, C is Card)")
4907
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   175
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   176
    def test_optional_var_3(self):
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   177
        constraint1 = ('X in_state S, U in_group G, P require_state S,'
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   178
                       'P name "read", P require_group G')
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   179
        constraint2 = 'X in_state S, S name "public"'
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   180
        rqlst = parse('Any A,C,T WHERE A documented_by C?, C title T')
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   181
        rewrite(rqlst, {('C', 'X'): (constraint1, constraint2)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   182
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   183
                         "Any A,C,T WHERE A documented_by C?, A is Affaire "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   184
                         "WITH C,T BEING (Any C,T WHERE C title T, "
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   185
                         "(EXISTS(C in_state B, D in_group F, G require_state B, G name 'read', G require_group F)) "
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   186
                         "OR (EXISTS(C in_state E, E name 'public')), "
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   187
                         "D eid %(A)s, C is Card)")
7535
d5725a89dac9 [rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7251
diff changeset
   188
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   189
    def test_optional_var_4(self):
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   190
        constraint1 = 'A created_by U, X documented_by A'
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   191
        constraint2 = 'A created_by U, X concerne A'
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   192
        constraint3 = 'X created_by U'
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   193
        rqlst = parse('Any X,LA,Y WHERE LA? documented_by X, LA concerne Y')
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   194
        rewrite(rqlst, {('LA', 'X'): (constraint1, constraint2),
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   195
                        ('X', 'X'): (constraint3,),
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   196
                        ('Y', 'X'): (constraint3,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   197
        self.assertEqual(rqlst.as_string(),
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   198
                             u'Any X,LA,Y WHERE LA? documented_by X, LA concerne Y, B eid %(C)s, '
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   199
                             'EXISTS(X created_by B), EXISTS(Y created_by B), '
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   200
                             'X is Card, Y is IN(Division, Note, Societe) '
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   201
                             'WITH LA BEING (Any LA WHERE (EXISTS(A created_by B, LA documented_by A)) OR (EXISTS(E created_by B, LA concerne E)), '
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   202
                             'B eid %(D)s, LA is Affaire)')
7555
c3bf459268d7 [rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7535
diff changeset
   203
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   204
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   205
    def test_ambiguous_optional_same_exprs(self):
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   206
        """See #3013535"""
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   207
        # see test of the same name in RewriteFullTC: original problem is
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   208
        # unreproducible here because it actually lies in
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   209
        # RQLRewriter.insert_local_checks
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   210
        rqlst = parse('Any A,AR,X,CD WHERE A concerne X?, A ref AR, A eid %(a)s, X creation_date CD')
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   211
        rewrite(rqlst, {('X', 'X'): ('X created_by U',),}, {'a': 3})
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   212
        self.assertEqual(rqlst.as_string(),
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   213
                         u'Any A,AR,X,CD WHERE A concerne X?, A ref AR, A eid %(a)s WITH X,CD BEING (Any X,CD WHERE X creation_date CD, EXISTS(X created_by B), B eid %(A)s, X is IN(Division, Note, Societe))')
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   214
4907
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   215
    def test_optional_var_inlined(self):
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   216
        c1 = ('X require_permission P')
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   217
        c2 = ('X inlined_card O, O require_permission P')
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   218
        rqlst = parse('Any C,A,R WHERE A? inlined_card C, A ref R')
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   219
        rewrite(rqlst, {('C', 'X'): (c1,),
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   220
                        ('A', 'X'): (c2,),
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   221
                        }, {})
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   222
        # XXX suboptimal
7790
7e16e056eecb [test] fix test subsquently to localperms introduction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   223
        self.assertEqual(rqlst.as_string(),
7868
39a54b88906d backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845 7850
diff changeset
   224
                         "Any C,A,R WITH A,C,R BEING "
39a54b88906d backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845 7850
diff changeset
   225
                         "(Any A,C,R WHERE A? inlined_card C, A ref R, "
39a54b88906d backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845 7850
diff changeset
   226
                         "(A is NULL) OR (EXISTS(A inlined_card B, B require_permission D, "
39a54b88906d backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845 7850
diff changeset
   227
                         "B is Card, D is CWPermission)), "
39a54b88906d backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845 7850
diff changeset
   228
                         "A is Affaire, C is Card, EXISTS(C require_permission E, E is CWPermission))")
4907
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   229
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   230
    # def test_optional_var_inlined_has_perm(self):
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   231
    #     c1 = ('X require_permission P')
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   232
    #     c2 = ('X inlined_card O, U has_read_permission O')
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   233
    #     rqlst = parse('Any C,A,R WHERE A? inlined_card C, A ref R')
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   234
    #     rewrite(rqlst, {('C', 'X'): (c1,),
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   235
    #                     ('A', 'X'): (c2,),
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   236
    #                     }, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   237
    #     self.assertEqual(rqlst.as_string(),
4907
e623afd49356 [rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   238
    #                          "")
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   239
4908
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   240
    def test_optional_var_inlined_imbricated_error(self):
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   241
        c1 = ('X require_permission P')
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   242
        c2 = ('X inlined_card O, O require_permission P')
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   243
        rqlst = parse('Any C,A,R,A2,R2 WHERE A? inlined_card C, A ref R,A2? inlined_card C, A2 ref R2')
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   244
        self.assertRaises(BadSchemaDefinition,
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   245
                          rewrite, rqlst, {('C', 'X'): (c1,),
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   246
                                           ('A', 'X'): (c2,),
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   247
                                           ('A2', 'X'): (c2,),
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   248
                                           }, {})
b3ad329cbe17 [rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4907
diff changeset
   249
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   250
    def test_optional_var_inlined_linked(self):
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   251
        c1 = ('X require_permission P')
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   252
        c2 = ('X inlined_card O, O require_permission P')
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   253
        rqlst = parse('Any A,W WHERE A inlined_card C?, C inlined_note N, '
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   254
                      'N inlined_affaire W')
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   255
        rewrite(rqlst, {('C', 'X'): (c1,)}, {})
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   256
        self.assertEqual(rqlst.as_string(),
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   257
                         'Any A,W WHERE A inlined_card C?, A is Affaire '
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   258
                         'WITH C,N,W BEING (Any C,N,W WHERE C inlined_note N, '
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   259
                         'N inlined_affaire W, EXISTS(C require_permission B), '
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   260
                         'C is Card, N is Note, W is Affaire)')
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   261
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   262
    def test_relation_optimization_1_lhs(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        # since Card in_state State as monovalued cardinality, the in_state
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
        # relation used in the rql expression can be ignored and S replaced by
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
        # the variable from the incoming query
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   266
        snippet = ('X in_state S, S name "hop"')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
        rqlst = parse('Card C WHERE C in_state STATE')
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   268
        rewrite(rqlst, {('C', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   269
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   270
                         "Any C WHERE C in_state STATE, C is Card, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   271
                         "EXISTS(STATE name 'hop'), STATE is State")
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   272
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   273
    def test_relation_optimization_1_rhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   274
        snippet = ('TW subworkflow_exit X, TW name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   275
        rqlst = parse('WorkflowTransition C WHERE C subworkflow_exit EXIT')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   276
        rewrite(rqlst, {('EXIT', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   277
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   278
                         "Any C WHERE C subworkflow_exit EXIT, C is WorkflowTransition, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   279
                         "EXISTS(C name 'hop'), EXIT is SubWorkflowExitPoint")
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   280
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   281
    def test_relation_optimization_2_lhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   282
        # optional relation can be shared if also optional in the snippet
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   283
        snippet = ('X in_state S?, S name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   284
        rqlst = parse('Card C WHERE C in_state STATE?')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   285
        rewrite(rqlst, {('C', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   286
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   287
                         "Any C WHERE C in_state STATE?, C is Card, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   288
                         "EXISTS(STATE name 'hop'), STATE is State")
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   289
    def test_relation_optimization_2_rhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   290
        snippet = ('TW? subworkflow_exit X, TW name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   291
        rqlst = parse('SubWorkflowExitPoint EXIT WHERE C? subworkflow_exit EXIT')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   292
        rewrite(rqlst, {('EXIT', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   293
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   294
                         "Any EXIT WHERE C? subworkflow_exit EXIT, EXIT is SubWorkflowExitPoint, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   295
                         "EXISTS(C name 'hop'), C is WorkflowTransition")
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   296
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   297
    def test_relation_optimization_3_lhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   298
        # optional relation in the snippet but not in the orig tree can be shared
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   299
        snippet = ('X in_state S?, S name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   300
        rqlst = parse('Card C WHERE C in_state STATE')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   301
        rewrite(rqlst, {('C', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   302
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   303
                         "Any C WHERE C in_state STATE, C is Card, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   304
                         "EXISTS(STATE name 'hop'), STATE is State")
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   305
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   306
    def test_relation_optimization_3_rhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   307
        snippet = ('TW? subworkflow_exit X, TW name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   308
        rqlst = parse('WorkflowTransition C WHERE C subworkflow_exit EXIT')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   309
        rewrite(rqlst, {('EXIT', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   310
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   311
                         "Any C WHERE C subworkflow_exit EXIT, C is WorkflowTransition, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   312
                         "EXISTS(C name 'hop'), EXIT is SubWorkflowExitPoint")
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   313
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   314
    def test_relation_non_optimization_1_lhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   315
        # but optional relation in the orig tree but not in the snippet can't be shared
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   316
        snippet = ('X in_state S, S name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   317
        rqlst = parse('Card C WHERE C in_state STATE?')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   318
        rewrite(rqlst, {('C', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   319
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   320
                         "Any C WHERE C in_state STATE?, C is Card, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   321
                         "EXISTS(C in_state A, A name 'hop', A is State), STATE is State")
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   322
3443
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   323
    def test_relation_non_optimization_1_rhs(self):
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   324
        snippet = ('TW subworkflow_exit X, TW name "hop"')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   325
        rqlst = parse('SubWorkflowExitPoint EXIT WHERE C? subworkflow_exit EXIT')
34e451da9b5d [security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3240
diff changeset
   326
        rewrite(rqlst, {('EXIT', 'X'): (snippet,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   327
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   328
                         "Any EXIT WHERE C? subworkflow_exit EXIT, EXIT is SubWorkflowExitPoint, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   329
                         "EXISTS(A subworkflow_exit EXIT, A name 'hop', A is WorkflowTransition), "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   330
                         "C is WorkflowTransition")
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
9170
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   332
    def test_relation_non_optimization_2(self):
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   333
        """See #3024730"""
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   334
        # 'X inlined_note N' must not be shared with 'C inlined_note N'
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   335
        # previously inserted, else this may introduce duplicated results, as N
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   336
        # will then be shared by multiple EXISTS and so at SQL generation time,
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   337
        # the table will be in the FROM clause of the outermost query
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   338
        rqlst = parse('Any A,C WHERE A inlined_card C')
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   339
        rewrite(rqlst, {('A', 'X'): ('X inlined_card C, C inlined_note N, N owned_by U',),
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   340
                        ('C', 'X'): ('X inlined_note N, N owned_by U',)}, {})
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   341
        self.assertEqual(rqlst.as_string(),
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   342
                         'Any A,C WHERE A inlined_card C, D eid %(E)s, '
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   343
                         'EXISTS(C inlined_note B, B owned_by D, B is Note), '
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   344
                         'EXISTS(C inlined_note F, F owned_by D, F is Note), '
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   345
                         'A is Affaire, C is Card')
e6fe77dbcfdf [rql rewrite] may_be_shared_with should consider relation's scope (closes #3024730)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   346
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
    def test_unsupported_constraint_1(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 47
diff changeset
   348
        # CWUser doesn't have require_permission
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
        trinfo_constraint = ('X wf_info_for Y, Y require_permission P, P name "read"')
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 47
diff changeset
   350
        rqlst = parse('Any U,T WHERE U is CWUser, T wf_info_for U')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   351
        self.assertRaises(Unauthorized, rewrite, rqlst, {('T', 'X'): (trinfo_constraint,)}, {})
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   352
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
    def test_unsupported_constraint_2(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
        trinfo_constraint = ('X wf_info_for Y, Y require_permission P, P name "read"')
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 47
diff changeset
   355
        rqlst = parse('Any U,T WHERE U is CWUser, T wf_info_for U')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   356
        rewrite(rqlst, {('T', 'X'): (trinfo_constraint, 'X wf_info_for Y, Y in_group G, G name "managers"')}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   357
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   358
                         u"Any U,T WHERE U is CWUser, T wf_info_for U, "
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   359
                         "EXISTS(U in_group B, B name 'managers', B is CWGroup), T is TrInfo")
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
    def test_unsupported_constraint_3(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6337
diff changeset
   362
        self.skipTest('raise unauthorized for now')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
        trinfo_constraint = ('X wf_info_for Y, Y require_permission P, P name "read"')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
        rqlst = parse('Any T WHERE T wf_info_for X')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   365
        rewrite(rqlst, {('T', 'X'): (trinfo_constraint, 'X in_group G, G name "managers"')}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   366
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   367
                         u'XXX dunno what should be generated')
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   368
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
    def test_add_ambiguity_exists(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
        constraint = ('X concerne Y')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
        rqlst = parse('Affaire X')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   372
        rewrite(rqlst, {('X', 'X'): (constraint,)}, {})
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   373
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   374
                         u"Any X WHERE X is Affaire, ((EXISTS(X concerne A, A is Division)) OR (EXISTS(X concerne C, C is Societe))) OR (EXISTS(X concerne B, B is Note))")
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   375
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
    def test_add_ambiguity_outerjoin(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        constraint = ('X concerne Y')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        rqlst = parse('Any X,C WHERE X? documented_by C')
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   379
        rewrite(rqlst, {('X', 'X'): (constraint,)}, {})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
        # ambiguity are kept in the sub-query, no need to be resolved using OR
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   381
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   382
                         u"Any X,C WHERE X? documented_by C, C is Card WITH X BEING (Any X WHERE EXISTS(X concerne A), X is Affaire)")
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   383
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   384
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   385
    def test_rrqlexpr_nonexistant_subject_1(self):
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   386
        constraint = RRQLExpression('S owned_by U')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   387
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   388
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'SU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   389
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   390
                         u"Any C WHERE C is Card, A eid %(B)s, EXISTS(C owned_by A)")
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   391
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   392
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'OU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   393
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   394
                         u"Any C WHERE C is Card")
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   395
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   396
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'SOU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   397
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   398
                         u"Any C WHERE C is Card, A eid %(B)s, EXISTS(C owned_by A)")
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   399
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   400
    def test_rrqlexpr_nonexistant_subject_2(self):
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   401
        constraint = RRQLExpression('S owned_by U, O owned_by U, O is Card')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   402
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   403
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'SU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   404
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   405
                         'Any C WHERE C is Card, A eid %(B)s, EXISTS(C owned_by A)')
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   406
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   407
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'OU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   408
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   409
                         'Any C WHERE C is Card, B eid %(D)s, EXISTS(A owned_by B, A is Card)')
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   410
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   411
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'SOU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   412
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   413
                         'Any C WHERE C is Card, A eid %(B)s, EXISTS(C owned_by A, D owned_by A, D is Card)')
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   414
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   415
    def test_rrqlexpr_nonexistant_subject_3(self):
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   416
        constraint = RRQLExpression('U in_group G, G name "users"')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   417
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   418
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'SU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   419
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   420
                         u'Any C WHERE C is Card, A eid %(B)s, EXISTS(A in_group D, D name "users", D is CWGroup)')
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   421
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   422
    def test_rrqlexpr_nonexistant_subject_4(self):
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   423
        constraint = RRQLExpression('U in_group G, G name "users", S owned_by U')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   424
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   425
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'SU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   426
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   427
                         u'Any C WHERE C is Card, A eid %(B)s, EXISTS(A in_group D, D name "users", C owned_by A, D is CWGroup)')
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   428
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   429
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'OU')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   430
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   431
                         u'Any C WHERE C is Card, A eid %(B)s, EXISTS(A in_group D, D name "users", D is CWGroup)')
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   432
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   433
    def test_rrqlexpr_nonexistant_subject_5(self):
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   434
        constraint = RRQLExpression('S owned_by Z, O owned_by Z, O is Card')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   435
        rqlst = parse('Card C')
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   436
        rewrite(rqlst, {('C', 'S'): (constraint,)}, {}, 'S')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   437
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   438
                         u"Any C WHERE C is Card, EXISTS(C owned_by A, A is CWUser)")
3826
0c0c051863cb close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3443
diff changeset
   439
7251
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   440
    def test_rqlexpr_not_relation_1_1(self):
5582
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   441
        constraint = RRQLExpression('X owned_by Z, Z login "hop"', 'X')
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   442
        rqlst = parse('Affaire A WHERE NOT EXISTS(A documented_by C)')
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   443
        rewrite(rqlst, {('C', 'X'): (constraint,)}, {}, 'X')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   444
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   445
                         u'Any A WHERE NOT EXISTS(A documented_by C, EXISTS(C owned_by B, B login "hop", B is CWUser), C is Card), A is Affaire')
5582
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   446
7251
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   447
    def test_rqlexpr_not_relation_1_2(self):
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   448
        constraint = RRQLExpression('X owned_by Z, Z login "hop"', 'X')
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   449
        rqlst = parse('Affaire A WHERE NOT EXISTS(A documented_by C)')
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   450
        rewrite(rqlst, {('A', 'X'): (constraint,)}, {}, 'X')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   451
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   452
                         u'Any A WHERE NOT EXISTS(A documented_by C, C is Card), A is Affaire, EXISTS(A owned_by B, B login "hop", B is CWUser)')
7251
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   453
163a2eab0e55 [rql rewrite] backout 7177:f2a976cf7dac : since EXISTS is the variable scope, we want to insert snippets here anyway (other solution would be to NOT insert snippets at all in such case). Closes #1625464
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7139
diff changeset
   454
    def test_rqlexpr_not_relation_2(self):
5582
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   455
        constraint = RRQLExpression('X owned_by Z, Z login "hop"', 'X')
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   456
        rqlst = rqlhelper.parse('Affaire A WHERE NOT A documented_by C', annotate=False)
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   457
        rewrite(rqlst, {('C', 'X'): (constraint,)}, {}, 'X')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7790
diff changeset
   458
        self.assertEqual(rqlst.as_string(),
8113
1e8c92202f44 [test] reindent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7868
diff changeset
   459
                         u'Any A WHERE NOT EXISTS(A documented_by C, EXISTS(C owned_by B, B login "hop", B is CWUser), C is Card), A is Affaire')
5582
3e133b29a1a4 [rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   460
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   461
    def test_rqlexpr_multiexpr_outerjoin(self):
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   462
        c1 = RRQLExpression('X owned_by Z, Z login "hop"', 'X')
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   463
        c2 = RRQLExpression('X owned_by Z, Z login "hip"', 'X')
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   464
        c3 = RRQLExpression('X owned_by Z, Z login "momo"', 'X')
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   465
        rqlst = rqlhelper.parse('Any A WHERE A documented_by C?', annotate=False)
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   466
        rewrite(rqlst, {('C', 'X'): (c1, c2, c3)}, {}, 'X')
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   467
        self.assertEqual(rqlst.as_string(),
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   468
                         u'Any A WHERE A documented_by C?, A is Affaire '
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   469
                         'WITH C BEING (Any C WHERE ((EXISTS(C owned_by B, B login "hop")) '
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   470
                         'OR (EXISTS(C owned_by D, D login "momo"))) '
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   471
                         'OR (EXISTS(C owned_by A, A login "hip")), C is Card)')
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8113
diff changeset
   472
8296
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   473
    def test_multiple_erql_one_bad(self):
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   474
        #: reproduce bug #2236985
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   475
        #: (rqlrewrite fails to remove rewritten entry for unsupported constraint and then crash)
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   476
        #:
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   477
        #: This check a very rare code path triggered by the four condition below
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   478
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   479
        # 1. c_ok introduce an ambiguity
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   480
        c_ok = ERQLExpression('X concerne R')
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   481
        # 2. c_bad is just plain wrong and won't be kept
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   482
        # 3. but it declare a new variable
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   483
        # 4. this variable require a rewrite
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   484
        c_bad = ERQLExpression('X documented_by R, A in_state R')
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   485
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   486
        rqlst = parse('Any A, R WHERE A ref R, S is Affaire')
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   487
        rewrite(rqlst, {('A', 'X'): (c_ok, c_bad)}, {})
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   488
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   489
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   490
from cubicweb.devtools.testlib import CubicWebTC
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   491
from logilab.common.decorators import classproperty
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   492
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   493
class RewriteFullTC(CubicWebTC):
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   494
    @classproperty
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   495
    def config(cls):
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   496
        return BaseApptestConfiguration(apphome=cls.datapath('rewrite'))
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   497
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   498
    def process(self, rql, args=None):
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   499
        if args is None:
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   500
            args = {}
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   501
        querier = self.repo.querier
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   502
        union = querier.parse(rql)
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   503
        querier.solutions(self.session, union, args)
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   504
        querier._annotate(union)
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   505
        plan = querier.plan_factory(union, args, self.session)
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   506
        plan.preprocess(union)
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   507
        return union
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   508
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   509
    def test_ambiguous_optional_same_exprs(self):
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   510
        """See #3013535"""
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   511
        edef1 = self.schema['Societe']
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   512
        edef2 = self.schema['Division']
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   513
        edef3 = self.schema['Note']
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   514
        with self.temporary_permissions((edef1, {'read': (ERQLExpression('X owned_by U'),)}),
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   515
                                        (edef2, {'read': (ERQLExpression('X owned_by U'),)}),
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   516
                                        (edef3, {'read': (ERQLExpression('X owned_by U'),)})):
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   517
            union = self.process('Any A,AR,X,CD WHERE A concerne X?, A ref AR, X creation_date CD')
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   518
            self.assertEqual('Any A,AR,X,CD WHERE A concerne X?, A ref AR, A is Affaire '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   519
                             'WITH X,CD BEING (Any X,CD WHERE X creation_date CD, '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   520
                             'EXISTS(X owned_by %(A)s), X is IN(Division, Note, Societe))',
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   521
                             union.as_string())
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   522
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   523
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   524
    def test_xxxx(self):
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   525
        edef1 = self.schema['Societe']
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   526
        edef2 = self.schema['Division']
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   527
        read_expr = ERQLExpression('X responsable E, U has_read_permission E')
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   528
        with self.temporary_permissions((edef1, {'read': (read_expr,)}),
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   529
                                        (edef2, {'read': (read_expr,)})):
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   530
            union = self.process('Any X,AA,AC,AD ORDERBY AD DESC '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   531
                                 'WHERE X responsable E, X nom AA, '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   532
                                 'X responsable AC?, AC modification_date AD')
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   533
            self.assertEqual('Any X,AA,AC,AD ORDERBY AD DESC '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   534
                             'WHERE X responsable E, X nom AA, '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   535
                             'X responsable AC?, AC modification_date AD, '
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   536
                             'AC is CWUser, E is CWUser, X is IN(Division, Societe)',
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   537
                             union.as_string())
9168
0fb4b67bde58 [schema/security] add __hash__ to rql expression. Closes #3013535
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8342
diff changeset
   538
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
    unittest_main()