author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 14 Oct 2009 12:49:20 +0200 | |
branch | stable |
changeset 3665 | 42f3a66cab51 |
parent 3443 | 34e451da9b5d |
child 3826 | 0c0c051863cb |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
1 |
""" |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1787
diff
changeset
|
7 |
""" |
0 | 8 |
from logilab.common.testlib import unittest_main, TestCase |
9 |
from logilab.common.testlib import mock_object |
|
10 |
||
11 |
from rql import parse, nodes, RQLHelper |
|
12 |
||
13 |
from cubicweb import Unauthorized |
|
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
|
14 |
from cubicweb.rqlrewrite import RQLRewriter |
0 | 15 |
from cubicweb.devtools import repotest, TestServerConfiguration |
16 |
||
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
|
17 |
config = TestServerConfiguration('data/rewrite') |
0 | 18 |
config.bootstrap_cubes() |
19 |
schema = config.load_schema() |
|
20 |
schema.add_relation_def(mock_object(subject='Card', name='in_state', object='State', cardinality='1*')) |
|
1787 | 21 |
|
0 | 22 |
rqlhelper = RQLHelper(schema, special_relations={'eid': 'uid', |
23 |
'has_text': 'fti'}) |
|
24 |
||
25 |
def setup_module(*args): |
|
26 |
repotest.do_monkey_patch() |
|
27 |
||
28 |
def teardown_module(*args): |
|
29 |
repotest.undo_monkey_patch() |
|
1787 | 30 |
|
0 | 31 |
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
|
32 |
return {1: 'CWUser', |
0 | 33 |
2: 'Card'}[eid] |
34 |
||
35 |
def rewrite(rqlst, snippets_map, 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
|
36 |
class FakeVReg: |
0 | 37 |
schema = schema |
38 |
@staticmethod |
|
39 |
def solutions(sqlcursor, mainrqlst, kwargs): |
|
40 |
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
|
41 |
class rqlhelper: |
0 | 42 |
@staticmethod |
43 |
def annotate(rqlst): |
|
44 |
rqlhelper.annotate(rqlst) |
|
45 |
@staticmethod |
|
46 |
def simplify(mainrqlst, needcopy=False): |
|
47 |
rqlhelper.simplify(rqlst, needcopy) |
|
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
|
48 |
rewriter = RQLRewriter(mock_object(vreg=FakeVReg, user=(mock_object(eid=1)))) |
0 | 49 |
for v, snippets in snippets_map.items(): |
50 |
snippets_map[v] = [mock_object(snippet_rqlst=parse('Any X WHERE '+snippet).children[0], |
|
1787 | 51 |
expression='Any X WHERE '+snippet) |
0 | 52 |
for snippet in snippets] |
53 |
rqlhelper.compute_solutions(rqlst.children[0], {'eid': eid_func_map}, kwargs=kwargs) |
|
54 |
solutions = rqlst.children[0].solutions |
|
55 |
rewriter.rewrite(rqlst.children[0], snippets_map.items(), solutions, kwargs) |
|
56 |
test_vrefs(rqlst.children[0]) |
|
57 |
return rewriter.rewritten |
|
58 |
||
59 |
def test_vrefs(node): |
|
60 |
vrefmap = {} |
|
61 |
for vref in node.iget_nodes(nodes.VariableRef): |
|
62 |
vrefmap.setdefault(vref.name, set()).add(vref) |
|
63 |
for var in node.defined_vars.itervalues(): |
|
64 |
assert not (var.stinfo['references'] ^ vrefmap[var.name]) |
|
65 |
assert (var.stinfo['references']) |
|
66 |
||
67 |
class RQLRewriteTC(TestCase): |
|
68 |
"""a faire: |
|
69 |
||
70 |
* optimisation: detecter les relations utilisees dans les rqlexpressions qui |
|
71 |
sont presentes dans la requete de depart pour les reutiliser si possible |
|
1787 | 72 |
|
0 | 73 |
* "has_<ACTION>_permission" ? |
74 |
""" |
|
1787 | 75 |
|
0 | 76 |
def test_base_var(self): |
77 |
card_constraint = ('X in_state S, U in_group G, P require_state S,' |
|
78 |
'P name "read", P require_group G') |
|
79 |
rqlst = parse('Card 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
|
80 |
rewrite(rqlst, {('C', 'X'): (card_constraint,)}, {}) |
0 | 81 |
self.failUnlessEqual(rqlst.as_string(), |
82 |
u"Any C WHERE C is Card, B eid %(D)s, " |
|
83 |
"EXISTS(C in_state A, B in_group E, F require_state A, " |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
47
diff
changeset
|
84 |
"F name 'read', F require_group E, A is State, E is CWGroup, F is CWPermission)") |
1787 | 85 |
|
0 | 86 |
def test_multiple_var(self): |
87 |
card_constraint = ('X in_state S, U in_group G, P require_state S,' |
|
88 |
'P name "read", P require_group G') |
|
89 |
affaire_constraints = ('X ref LIKE "PUBLIC%"', 'U in_group G, G name "public"') |
|
90 |
kwargs = {'u':2} |
|
91 |
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
|
92 |
rewrite(rqlst, {('C', 'X'): (card_constraint,), ('S', 'X'): affaire_constraints}, |
0 | 93 |
kwargs) |
94 |
self.assertTextEquals(rqlst.as_string(), |
|
95 |
"Any S WHERE S documented_by C, C eid %(u)s, B eid %(D)s, " |
|
96 |
"EXISTS(C in_state A, B in_group E, F require_state A, " |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
47
diff
changeset
|
97 |
"F name 'read', F require_group E, A is State, E is CWGroup, F is CWPermission), " |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
47
diff
changeset
|
98 |
"(EXISTS(S ref LIKE 'PUBLIC%')) OR (EXISTS(B in_group G, G name 'public', G is CWGroup)), " |
0 | 99 |
"S is Affaire") |
100 |
self.failUnless('D' in kwargs) |
|
1787 | 101 |
|
0 | 102 |
def test_or(self): |
103 |
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
|
104 |
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
|
105 |
rewrite(rqlst, {('C', 'X'): (constraint,)}, {'u':1}) |
0 | 106 |
self.failUnlessEqual(rqlst.as_string(), |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
107 |
"Any S WHERE S owned_by C, C eid %(u)s, S is IN(CWUser, CWGroup), A eid %(B)s, " |
0 | 108 |
"EXISTS((C identity A) OR (C in_state D, E identity A, " |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
109 |
"E in_state D, D name 'subscribed'), D is State, E is CWUser)") |
0 | 110 |
|
111 |
def test_simplified_rqlst(self): |
|
112 |
card_constraint = ('X in_state S, U in_group G, P require_state S,' |
|
113 |
'P name "read", P require_group G') |
|
114 |
rqlst = parse('Any 2') # this is the simplified rql st for Any X WHERE X eid 12 |
|
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
|
115 |
rewrite(rqlst, {('2', 'X'): (card_constraint,)}, {}) |
0 | 116 |
self.failUnlessEqual(rqlst.as_string(), |
117 |
u"Any 2 WHERE B eid %(C)s, " |
|
118 |
"EXISTS(2 in_state A, B in_group D, E require_state A, " |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
47
diff
changeset
|
119 |
"E name 'read', E require_group D, A is State, D is CWGroup, E is CWPermission)") |
0 | 120 |
|
121 |
def test_optional_var(self): |
|
122 |
card_constraint = ('X in_state S, U in_group G, P require_state S,' |
|
123 |
'P name "read", P require_group G') |
|
124 |
rqlst = parse('Any A,C WHERE A 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
|
125 |
rewrite(rqlst, {('C', 'X'): (card_constraint,)}, {}) |
0 | 126 |
self.failUnlessEqual(rqlst.as_string(), |
127 |
"Any A,C WHERE A documented_by C?, A is Affaire " |
|
128 |
"WITH C BEING " |
|
129 |
"(Any C WHERE C in_state B, D in_group F, G require_state B, G name 'read', " |
|
130 |
"G require_group F, D eid %(A)s, C is Card)") |
|
131 |
rqlst = parse('Any A,C,T WHERE A documented_by C?, C title T') |
|
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
|
132 |
rewrite(rqlst, {('C', 'X'): (card_constraint,)}, {}) |
0 | 133 |
self.failUnlessEqual(rqlst.as_string(), |
134 |
"Any A,C,T WHERE A documented_by C?, A is Affaire " |
|
135 |
"WITH C,T BEING " |
|
136 |
"(Any C,T WHERE C in_state B, D in_group F, G require_state B, G name 'read', " |
|
137 |
"G require_group F, C title T, D eid %(A)s, C is Card)") |
|
1787 | 138 |
|
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
|
139 |
def test_relation_optimization_1_lhs(self): |
0 | 140 |
# since Card in_state State as monovalued cardinality, the in_state |
141 |
# relation used in the rql expression can be ignored and S replaced by |
|
142 |
# 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
|
143 |
snippet = ('X in_state S, S name "hop"') |
0 | 144 |
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
|
145 |
rewrite(rqlst, {('C', 'X'): (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
|
146 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
147 |
"Any C WHERE C in_state STATE, C is Card, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
148 |
"EXISTS(STATE name 'hop'), STATE is 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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
rewrite(rqlst, {('EXIT', 'X'): (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
|
153 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
154 |
"Any C WHERE C subworkflow_exit EXIT, C is WorkflowTransition, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
155 |
"EXISTS(C name 'hop'), EXIT is SubWorkflowExitPoint") |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
156 |
|
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
157 |
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
|
158 |
# 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
|
159 |
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
|
160 |
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
|
161 |
rewrite(rqlst, {('C', 'X'): (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
|
162 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
163 |
"Any C WHERE C in_state STATE?, C is Card, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
164 |
"EXISTS(STATE name 'hop'), STATE is 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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
rewrite(rqlst, {('EXIT', 'X'): (snippet,)}, {}) |
0 | 169 |
self.failUnlessEqual(rqlst.as_string(), |
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
|
170 |
"Any EXIT WHERE C? subworkflow_exit EXIT, EXIT is SubWorkflowExitPoint, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
171 |
"EXISTS(C name 'hop'), C is WorkflowTransition") |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
172 |
|
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
173 |
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
|
174 |
# 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
|
175 |
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
|
176 |
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
|
177 |
rewrite(rqlst, {('C', 'X'): (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
|
178 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
179 |
"Any C WHERE C in_state STATE, C is Card, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
180 |
"EXISTS(STATE name 'hop'), STATE is 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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
rewrite(rqlst, {('EXIT', 'X'): (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
|
185 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
186 |
"Any C WHERE C subworkflow_exit EXIT, C is WorkflowTransition, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
187 |
"EXISTS(C name 'hop'), EXIT is SubWorkflowExitPoint") |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
188 |
|
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
189 |
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
|
190 |
# 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
|
191 |
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
|
192 |
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
|
193 |
rewrite(rqlst, {('C', 'X'): (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
|
194 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
195 |
"Any C WHERE C in_state STATE?, C is Card, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
196 |
"EXISTS(C in_state A, A name 'hop', A is State), STATE is 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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
rewrite(rqlst, {('EXIT', 'X'): (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
|
201 |
self.failUnlessEqual(rqlst.as_string(), |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
202 |
"Any EXIT WHERE C? subworkflow_exit EXIT, EXIT is SubWorkflowExitPoint, " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
203 |
"EXISTS(A subworkflow_exit EXIT, A name 'hop', A is WorkflowTransition), " |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
204 |
"C is WorkflowTransition") |
0 | 205 |
|
206 |
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
|
207 |
# CWUser doesn't have require_permission |
0 | 208 |
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
|
209 |
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
|
210 |
self.assertRaises(Unauthorized, rewrite, rqlst, {('T', 'X'): (trinfo_constraint,)}, {}) |
1787 | 211 |
|
0 | 212 |
def test_unsupported_constraint_2(self): |
213 |
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
|
214 |
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
|
215 |
rewrite(rqlst, {('T', 'X'): (trinfo_constraint, 'X wf_info_for Y, Y in_group G, G name "managers"')}, {}) |
0 | 216 |
self.failUnlessEqual(rqlst.as_string(), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
47
diff
changeset
|
217 |
u"Any U,T WHERE U is CWUser, T wf_info_for U, " |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
47
diff
changeset
|
218 |
"EXISTS(U in_group B, B name 'managers', B is CWGroup), T is TrInfo") |
0 | 219 |
|
220 |
def test_unsupported_constraint_3(self): |
|
221 |
self.skip('raise unauthorized for now') |
|
222 |
trinfo_constraint = ('X wf_info_for Y, Y require_permission P, P name "read"') |
|
223 |
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
|
224 |
rewrite(rqlst, {('T', 'X'): (trinfo_constraint, 'X in_group G, G name "managers"')}, {}) |
0 | 225 |
self.failUnlessEqual(rqlst.as_string(), |
226 |
u'XXX dunno what should be generated') |
|
1787 | 227 |
|
0 | 228 |
def test_add_ambiguity_exists(self): |
229 |
constraint = ('X concerne Y') |
|
230 |
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
|
231 |
rewrite(rqlst, {('X', 'X'): (constraint,)}, {}) |
0 | 232 |
self.failUnlessEqual(rqlst.as_string(), |
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
|
233 |
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 | 234 |
|
0 | 235 |
def test_add_ambiguity_outerjoin(self): |
236 |
constraint = ('X concerne Y') |
|
237 |
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
|
238 |
rewrite(rqlst, {('X', 'X'): (constraint,)}, {}) |
0 | 239 |
# ambiguity are kept in the sub-query, no need to be resolved using OR |
240 |
self.failUnlessEqual(rqlst.as_string(), |
|
1787 | 241 |
u"Any X,C WHERE X? documented_by C, C is Card WITH X BEING (Any X WHERE X concerne A, X is Affaire)") |
242 |
||
243 |
||
244 |
||
0 | 245 |
if __name__ == '__main__': |
246 |
unittest_main() |