author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Thu, 23 Jul 2009 13:03:50 +0200 | |
changeset 2433 | 1d46c016a564 |
parent 1977 | 606923dff11b |
child 2608 | 21856eda34f6 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
2 |
"""unit tests for modules cubicweb.server.querier and cubicweb.server.querier_steps |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1951
diff
changeset
|
3 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 4 |
""" |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
5 |
from datetime import date, datetime |
0 | 6 |
|
7 |
from logilab.common.testlib import TestCase, unittest_main |
|
8 |
from rql import BadRQLQuery, RQLSyntaxError |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
9 |
|
1949
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
10 |
from cubicweb import QueryError, Unauthorized, Binary |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
11 |
from cubicweb.server.sqlutils import SQL_PREFIX |
0 | 12 |
from cubicweb.server.utils import crypt_password |
13 |
from cubicweb.server.sources.native import make_schema |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
14 |
from cubicweb.devtools import init_test_database |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
15 |
from cubicweb.devtools.repotest import tuplify, BaseQuerierTC |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
16 |
|
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
17 |
from unittest_session import Variable |
0 | 18 |
|
19 |
||
20 |
# register priority/severity sorting registered procedure |
|
21 |
from rql.utils import register_function, FunctionDescr |
|
22 |
||
23 |
class group_sort_value(FunctionDescr): |
|
24 |
supported_backends = ('sqlite',) |
|
25 |
rtype = 'Int' |
|
26 |
try: |
|
27 |
register_function(group_sort_value) |
|
28 |
except AssertionError: |
|
29 |
pass |
|
30 |
from cubicweb.server.sqlutils import SQL_CONNECT_HOOKS |
|
31 |
def init_sqlite_connexion(cnx): |
|
32 |
def group_sort_value(text): |
|
33 |
return {"managers": "3", "users": "2", "guests": "1", "owners": "0"}[text] |
|
34 |
cnx.create_function("GROUP_SORT_VALUE", 1, group_sort_value) |
|
35 |
SQL_CONNECT_HOOKS['sqlite'].append(init_sqlite_connexion) |
|
36 |
||
37 |
||
38 |
from logilab.common.adbh import _GenericAdvFuncHelper |
|
39 |
TYPEMAP = _GenericAdvFuncHelper.TYPE_MAPPING |
|
40 |
||
41 |
class MakeSchemaTC(TestCase): |
|
42 |
def test_known_values(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
43 |
solution = {'A': 'String', 'B': 'CWUser'} |
1787 | 44 |
self.assertEquals(make_schema((Variable('A'), Variable('B')), solution, |
0 | 45 |
'table0', TYPEMAP), |
46 |
('C0 text,C1 integer', {'A': 'table0.C0', 'B': 'table0.C1'})) |
|
1787 | 47 |
|
0 | 48 |
|
49 |
repo, cnx = init_test_database('sqlite') |
|
50 |
||
51 |
||
52 |
||
53 |
class UtilsTC(BaseQuerierTC): |
|
54 |
repo = repo |
|
1787 | 55 |
|
0 | 56 |
def get_max_eid(self): |
57 |
# no need for cleanup here |
|
58 |
return None |
|
59 |
def cleanup(self): |
|
60 |
# no need for cleanup here |
|
61 |
pass |
|
1787 | 62 |
|
0 | 63 |
def test_preprocess_1(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
64 |
reid = self.execute('Any X WHERE X is CWRType, X name "owned_by"')[0][0] |
0 | 65 |
rqlst = self._prepare('Any COUNT(RDEF) WHERE RDEF relation_type X, X eid %(x)s', {'x': reid}) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
66 |
self.assertEquals(rqlst.solutions, [{'RDEF': 'CWAttribute'}, {'RDEF': 'CWRelation'}]) |
1787 | 67 |
|
0 | 68 |
def test_preprocess_2(self): |
69 |
teid = self.execute("INSERT Tag X: X name 'tag'")[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
70 |
#geid = self.execute("CWGroup G WHERE G name 'users'")[0][0] |
0 | 71 |
#self.execute("SET X tags Y WHERE X eid %(t)s, Y eid %(g)s", |
72 |
# {'g': geid, 't': teid}, 'g') |
|
73 |
rqlst = self._prepare('Any X WHERE E eid %(x)s, E tags X', {'x': teid}) |
|
74 |
# the query may be optimized, should keep only one solution |
|
75 |
# (any one, etype will be discarded) |
|
76 |
self.assertEquals(len(rqlst.solutions), 1) |
|
1787 | 77 |
|
0 | 78 |
def test_preprocess_security(self): |
79 |
plan = self._prepare_plan('Any ETN,COUNT(X) GROUPBY ETN ' |
|
80 |
'WHERE X is ET, ET name ETN') |
|
81 |
plan.session = self._user_session(('users',))[1] |
|
82 |
union = plan.rqlst |
|
83 |
plan.preprocess(union) |
|
84 |
self.assertEquals(len(union.children), 1) |
|
85 |
self.assertEquals(len(union.children[0].with_), 1) |
|
86 |
subq = union.children[0].with_[0].query |
|
87 |
self.assertEquals(len(subq.children), 3) |
|
88 |
self.assertEquals([t.as_string() for t in union.children[0].selection], |
|
89 |
['ETN','COUNT(X)']) |
|
90 |
self.assertEquals([t.as_string() for t in union.children[0].groupby], |
|
91 |
['ETN']) |
|
92 |
partrqls = sorted(((rqlst.as_string(), rqlst.solutions) for rqlst in subq.children)) |
|
93 |
rql, solutions = partrqls[0] |
|
94 |
self.assertEquals(rql, |
|
95 |
'Any ETN,X WHERE X is ET, ET name ETN, (EXISTS(X owned_by %(B)s))' |
|
96 |
' OR ((((EXISTS(D concerne C?, C owned_by %(B)s, X identity D, C is Division, D is Affaire))' |
|
97 |
' OR (EXISTS(H concerne G?, G owned_by %(B)s, G is SubDivision, X identity H, H is Affaire)))' |
|
98 |
' OR (EXISTS(I concerne F?, F owned_by %(B)s, F is Societe, X identity I, I is Affaire)))' |
|
99 |
' OR (EXISTS(J concerne E?, E owned_by %(B)s, E is Note, X identity J, J is Affaire)))' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
100 |
', ET is CWEType, X is Affaire') |
0 | 101 |
self.assertEquals(solutions, [{'C': 'Division', |
102 |
'D': 'Affaire', |
|
103 |
'E': 'Note', |
|
104 |
'F': 'Societe', |
|
105 |
'G': 'SubDivision', |
|
106 |
'H': 'Affaire', |
|
107 |
'I': 'Affaire', |
|
108 |
'J': 'Affaire', |
|
109 |
'X': 'Affaire', |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
110 |
'ET': 'CWEType', 'ETN': 'String'}]) |
0 | 111 |
rql, solutions = partrqls[1] |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
112 |
self.assertEquals(rql, 'Any ETN,X WHERE X is ET, ET name ETN, ET is CWEType, ' |
1787 | 113 |
'X is IN(Bookmark, CWAttribute, CWCache, CWConstraint, CWConstraintType, CWEType, CWGroup, CWPermission, CWProperty, CWRType, CWRelation, CWUser, Card, Comment, Division, Email, EmailAddress, EmailPart, EmailThread, File, Folder, Image, Note, Personne, RQLExpression, Societe, State, SubDivision, Tag, TrInfo, Transition)') |
0 | 114 |
self.assertListEquals(sorted(solutions), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
115 |
sorted([{'X': 'Bookmark', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
116 |
{'X': 'Card', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
117 |
{'X': 'Comment', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
118 |
{'X': 'Division', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
119 |
{'X': 'CWCache', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
120 |
{'X': 'CWConstraint', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
121 |
{'X': 'CWConstraintType', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
122 |
{'X': 'CWEType', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
123 |
{'X': 'CWAttribute', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
124 |
{'X': 'CWGroup', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
125 |
{'X': 'Email', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
126 |
{'X': 'EmailAddress', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
127 |
{'X': 'EmailPart', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
128 |
{'X': 'EmailThread', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
129 |
{'X': 'CWRelation', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
130 |
{'X': 'CWPermission', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
131 |
{'X': 'CWProperty', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
132 |
{'X': 'CWRType', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
133 |
{'X': 'CWUser', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
134 |
{'X': 'File', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
135 |
{'X': 'Folder', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
136 |
{'X': 'Image', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
137 |
{'X': 'Note', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
138 |
{'X': 'Personne', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
139 |
{'X': 'RQLExpression', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
140 |
{'X': 'Societe', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
141 |
{'X': 'State', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
142 |
{'X': 'SubDivision', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
143 |
{'X': 'Tag', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
144 |
{'X': 'Transition', 'ETN': 'String', 'ET': 'CWEType'}, |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
145 |
{'X': 'TrInfo', 'ETN': 'String', 'ET': 'CWEType'}])) |
0 | 146 |
rql, solutions = partrqls[2] |
147 |
self.assertEquals(rql, |
|
148 |
'Any ETN,X WHERE X is ET, ET name ETN, EXISTS(X owned_by %(C)s), ' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
149 |
'ET is CWEType, X is Basket') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
150 |
self.assertEquals(solutions, [{'ET': 'CWEType', |
0 | 151 |
'X': 'Basket', |
152 |
'ETN': 'String', |
|
153 |
}]) |
|
154 |
||
155 |
def test_preprocess_security_aggregat(self): |
|
156 |
plan = self._prepare_plan('Any MAX(X)') |
|
157 |
plan.session = self._user_session(('users',))[1] |
|
158 |
union = plan.rqlst |
|
159 |
plan.preprocess(union) |
|
160 |
self.assertEquals(len(union.children), 1) |
|
161 |
self.assertEquals(len(union.children[0].with_), 1) |
|
162 |
subq = union.children[0].with_[0].query |
|
163 |
self.assertEquals(len(subq.children), 3) |
|
164 |
self.assertEquals([t.as_string() for t in union.children[0].selection], |
|
165 |
['MAX(X)']) |
|
1787 | 166 |
|
0 | 167 |
def test_preprocess_nonregr(self): |
168 |
rqlst = self._prepare('Any S ORDERBY SI WHERE NOT S ecrit_par O, S para SI') |
|
169 |
self.assertEquals(len(rqlst.solutions), 1) |
|
1787 | 170 |
|
0 | 171 |
def test_build_description(self): |
172 |
# should return an empty result set |
|
173 |
rset = self.execute('Any X WHERE X eid %(x)s', {'x': self.session.user.eid}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
174 |
self.assertEquals(rset.description[0][0], 'CWUser') |
0 | 175 |
rset = self.execute('Any 1') |
176 |
self.assertEquals(rset.description[0][0], 'Int') |
|
177 |
rset = self.execute('Any TRUE') |
|
178 |
self.assertEquals(rset.description[0][0], 'Boolean') |
|
179 |
rset = self.execute('Any "hop"') |
|
180 |
self.assertEquals(rset.description[0][0], 'String') |
|
181 |
rset = self.execute('Any TODAY') |
|
182 |
self.assertEquals(rset.description[0][0], 'Date') |
|
183 |
rset = self.execute('Any NOW') |
|
184 |
self.assertEquals(rset.description[0][0], 'Datetime') |
|
185 |
rset = self.execute('Any %(x)s', {'x': 1}) |
|
186 |
self.assertEquals(rset.description[0][0], 'Int') |
|
187 |
rset = self.execute('Any %(x)s', {'x': 1L}) |
|
188 |
self.assertEquals(rset.description[0][0], 'Int') |
|
189 |
rset = self.execute('Any %(x)s', {'x': True}) |
|
190 |
self.assertEquals(rset.description[0][0], 'Boolean') |
|
191 |
rset = self.execute('Any %(x)s', {'x': 1.0}) |
|
192 |
self.assertEquals(rset.description[0][0], 'Float') |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
193 |
rset = self.execute('Any %(x)s', {'x': datetime.now()}) |
0 | 194 |
self.assertEquals(rset.description[0][0], 'Datetime') |
195 |
rset = self.execute('Any %(x)s', {'x': 'str'}) |
|
196 |
self.assertEquals(rset.description[0][0], 'String') |
|
197 |
rset = self.execute('Any %(x)s', {'x': u'str'}) |
|
198 |
self.assertEquals(rset.description[0][0], 'String') |
|
199 |
||
200 |
||
201 |
class QuerierTC(BaseQuerierTC): |
|
202 |
repo = repo |
|
203 |
||
204 |
def test_encoding_pb(self): |
|
205 |
self.assertRaises(RQLSyntaxError, self.execute, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
206 |
'Any X WHERE X is CWRType, X name "�wned_by"') |
0 | 207 |
|
208 |
def test_unknown_eid(self): |
|
209 |
# should return an empty result set |
|
210 |
self.failIf(self.execute('Any X WHERE X eid 99999999')) |
|
1787 | 211 |
|
1949
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
212 |
def test_bytes_storage(self): |
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
213 |
feid = self.execute('INSERT File X: X name "foo.pdf", X data_format "text/plain", X data %(data)s', |
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
214 |
{'data': Binary("xxx")})[0][0] |
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
215 |
fdata = self.execute('Any D WHERE X data D, X eid %(x)s', {'x': feid}, 'x')[0][0] |
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
216 |
self.assertIsInstance(fdata, Binary) |
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
217 |
self.assertEquals(fdata.getvalue(), 'xxx') |
9064102b4deb
test Bytes storage/retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
218 |
|
0 | 219 |
# selection queries tests ################################################# |
1787 | 220 |
|
0 | 221 |
def test_select_1(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
222 |
rset = self.execute('Any X ORDERBY X WHERE X is CWGroup') |
0 | 223 |
result, descr = rset.rows, rset.description |
224 |
self.assertEquals(tuplify(result), [(1,), (2,), (3,), (4,)]) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
225 |
self.assertEquals(descr, [('CWGroup',), ('CWGroup',), ('CWGroup',), ('CWGroup',)]) |
1787 | 226 |
|
0 | 227 |
def test_select_2(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
228 |
rset = self.execute('Any X ORDERBY N WHERE X is CWGroup, X name N') |
0 | 229 |
self.assertEquals(tuplify(rset.rows), [(3,), (1,), (4,), (2,)]) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
230 |
self.assertEquals(rset.description, [('CWGroup',), ('CWGroup',), ('CWGroup',), ('CWGroup',)]) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
231 |
rset = self.execute('Any X ORDERBY N DESC WHERE X is CWGroup, X name N') |
0 | 232 |
self.assertEquals(tuplify(rset.rows), [(2,), (4,), (1,), (3,)]) |
1787 | 233 |
|
0 | 234 |
def test_select_3(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
235 |
rset = self.execute('Any N GROUPBY N WHERE X is CWGroup, X name N') |
0 | 236 |
result, descr = rset.rows, rset.description |
237 |
result.sort() |
|
238 |
self.assertEquals(tuplify(result), [('guests',), ('managers',), ('owners',), ('users',)]) |
|
239 |
self.assertEquals(descr, [('String',), ('String',), ('String',), ('String',)]) |
|
1787 | 240 |
|
0 | 241 |
def test_select_is(self): |
242 |
rset = self.execute('Any X, TN ORDERBY TN LIMIT 10 WHERE X is T, T name TN') |
|
243 |
result, descr = rset.rows, rset.description |
|
244 |
self.assertEquals(result[0][1], descr[0][0]) |
|
1787 | 245 |
|
0 | 246 |
def test_select_is_aggr(self): |
247 |
rset = self.execute('Any TN, COUNT(X) GROUPBY TN ORDERBY 2 DESC WHERE X is T, T name TN') |
|
248 |
result, descr = rset.rows, rset.description |
|
249 |
self.assertEquals(descr[0][0], 'String') |
|
250 |
self.assertEquals(descr[0][1], 'Int') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
251 |
self.assertEquals(result[0][0], 'CWRelation') |
1787 | 252 |
|
0 | 253 |
def test_select_groupby_orderby(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
254 |
rset = self.execute('Any N GROUPBY N ORDERBY N WHERE X is CWGroup, X name N') |
0 | 255 |
self.assertEquals(tuplify(rset.rows), [('guests',), ('managers',), ('owners',), ('users',)]) |
256 |
self.assertEquals(rset.description, [('String',), ('String',), ('String',), ('String',)]) |
|
1787 | 257 |
|
0 | 258 |
def test_select_complex_groupby(self): |
259 |
rset = self.execute('Any N GROUPBY N WHERE X name N') |
|
260 |
rset = self.execute('Any N,MAX(D) GROUPBY N LIMIT 5 WHERE X name N, X creation_date D') |
|
1787 | 261 |
|
0 | 262 |
def test_select_inlined_groupby(self): |
263 |
seid = self.execute('State X WHERE X name "deactivated"')[0][0] |
|
264 |
rset = self.execute('Any U,L,S GROUPBY U,L,S WHERE X in_state S, U login L, S eid %s' % seid) |
|
1787 | 265 |
|
0 | 266 |
def test_select_complex_orderby(self): |
267 |
rset1 = self.execute('Any N ORDERBY N WHERE X name N') |
|
268 |
self.assertEquals(sorted(rset1.rows), rset1.rows) |
|
269 |
rset = self.execute('Any N ORDERBY N LIMIT 5 OFFSET 1 WHERE X name N') |
|
1787 | 270 |
self.assertEquals(rset.rows[0][0], rset1.rows[1][0]) |
0 | 271 |
self.assertEquals(len(rset), 5) |
1787 | 272 |
|
0 | 273 |
def test_select_5(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
274 |
rset = self.execute('Any X, TMP ORDERBY TMP WHERE X name TMP, X is CWGroup') |
0 | 275 |
self.assertEquals(tuplify(rset.rows), [(3, 'guests',), (1, 'managers',), (4, 'owners',), (2, 'users',)]) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
276 |
self.assertEquals(rset.description, [('CWGroup', 'String',), ('CWGroup', 'String',), ('CWGroup', 'String',), ('CWGroup', 'String',)]) |
1787 | 277 |
|
0 | 278 |
def test_select_6(self): |
279 |
self.execute("INSERT Personne X: X nom 'bidule'")[0] |
|
280 |
rset = self.execute('Any Y where X name TMP, Y nom in (TMP, "bidule")') |
|
281 |
#self.assertEquals(rset.description, [('Personne',), ('Personne',)]) |
|
282 |
self.assert_(('Personne',) in rset.description) |
|
283 |
rset = self.execute('DISTINCT Any Y where X name TMP, Y nom in (TMP, "bidule")') |
|
284 |
self.assert_(('Personne',) in rset.description) |
|
1787 | 285 |
|
0 | 286 |
def test_select_not_attr(self): |
287 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
288 |
self.execute("INSERT Societe X: X nom 'chouette'") |
|
289 |
rset = self.execute('Personne X WHERE NOT X nom "bidule"') |
|
290 |
self.assertEquals(len(rset.rows), 0, rset.rows) |
|
291 |
rset = self.execute('Personne X WHERE NOT X nom "bid"') |
|
292 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
293 |
self.execute("SET P travaille S WHERE P nom 'bidule', S nom 'chouette'") |
|
294 |
rset = self.execute('Personne X WHERE NOT X travaille S') |
|
295 |
self.assertEquals(len(rset.rows), 0, rset.rows) |
|
1787 | 296 |
|
0 | 297 |
def test_select_is_in(self): |
298 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
299 |
self.execute("INSERT Societe X: X nom 'chouette'") |
|
300 |
self.assertEquals(len(self.execute("Any X WHERE X is IN (Personne, Societe)")), |
|
301 |
2) |
|
1787 | 302 |
|
0 | 303 |
def test_select_not_rel(self): |
304 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
305 |
self.execute("INSERT Societe X: X nom 'chouette'") |
|
306 |
self.execute("INSERT Personne X: X nom 'autre'") |
|
307 |
self.execute("SET P travaille S WHERE P nom 'bidule', S nom 'chouette'") |
|
308 |
rset = self.execute('Personne X WHERE NOT X travaille S') |
|
309 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
310 |
rset = self.execute('Personne X WHERE NOT X travaille S, S nom "chouette"') |
|
311 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1787 | 312 |
|
0 | 313 |
def test_select_nonregr_inlined(self): |
314 |
self.execute("INSERT Note X: X para 'bidule'") |
|
315 |
self.execute("INSERT Personne X: X nom 'chouette'") |
|
316 |
self.execute("INSERT Personne X: X nom 'autre'") |
|
317 |
self.execute("SET X ecrit_par P WHERE X para 'bidule', P nom 'chouette'") |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
318 |
rset = self.execute('Any U,T ORDERBY T DESC WHERE U is CWUser, ' |
0 | 319 |
'N ecrit_par U, N type T')#, {'x': self.ueid}) |
320 |
self.assertEquals(len(rset.rows), 0) |
|
1787 | 321 |
|
0 | 322 |
def test_select_nonregr_edition_not(self): |
323 |
groupeids = set((1, 2, 3)) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
324 |
groupreadperms = set(r[0] for r in self.execute('Any Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), X read_permission Y')) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
325 |
rset = self.execute('DISTINCT Any Y WHERE X is CWEType, X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y') |
0 | 326 |
self.assertEquals(sorted(r[0] for r in rset.rows), sorted(groupeids - groupreadperms)) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
327 |
rset = self.execute('DISTINCT Any Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y') |
0 | 328 |
self.assertEquals(sorted(r[0] for r in rset.rows), sorted(groupeids - groupreadperms)) |
1787 | 329 |
|
0 | 330 |
def test_select_outer_join(self): |
331 |
peid1 = self.execute("INSERT Personne X: X nom 'bidule'")[0][0] |
|
332 |
peid2 = self.execute("INSERT Personne X: X nom 'autre'")[0][0] |
|
333 |
seid1 = self.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
|
334 |
seid2 = self.execute("INSERT Societe X: X nom 'chouetos'")[0][0] |
|
335 |
rset = self.execute('Any X,S ORDERBY X WHERE X travaille S?') |
|
336 |
self.assertEquals(rset.rows, [[peid1, None], [peid2, None]]) |
|
337 |
self.execute("SET P travaille S WHERE P nom 'bidule', S nom 'chouette'") |
|
338 |
rset = self.execute('Any X,S ORDERBY X WHERE X travaille S?') |
|
339 |
self.assertEquals(rset.rows, [[peid1, seid1], [peid2, None]]) |
|
340 |
rset = self.execute('Any S,X ORDERBY S WHERE X? travaille S') |
|
341 |
self.assertEquals(rset.rows, [[seid1, peid1], [seid2, None]]) |
|
1787 | 342 |
|
0 | 343 |
def test_select_outer_join_optimized(self): |
344 |
peid1 = self.execute("INSERT Personne X: X nom 'bidule'")[0][0] |
|
345 |
rset = self.execute('Any X WHERE X eid %(x)s, P? connait X', {'x':peid1}, 'x') |
|
346 |
self.assertEquals(rset.rows, [[peid1]]) |
|
347 |
rset = self.execute('Any X WHERE X eid %(x)s, X require_permission P?', {'x':peid1}, 'x') |
|
348 |
self.assertEquals(rset.rows, [[peid1]]) |
|
349 |
||
350 |
def test_select_left_outer_join(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
351 |
ueid = self.execute("INSERT CWUser X: X login 'bob', X upassword 'toto', X in_group G " |
0 | 352 |
"WHERE G name 'users'")[0][0] |
353 |
self.commit() |
|
354 |
try: |
|
355 |
rset = self.execute('Any FS,TS,C,D,U ORDERBY D DESC ' |
|
356 |
'WHERE WF wf_info_for X,' |
|
357 |
'WF from_state FS?, WF to_state TS, WF comment C,' |
|
358 |
'WF creation_date D, WF owned_by U, X eid %(x)s', |
|
359 |
{'x': ueid}, 'x') |
|
360 |
self.assertEquals(len(rset), 1) |
|
361 |
self.execute('SET X in_state S WHERE X eid %(x)s, S name "deactivated"', |
|
362 |
{'x': ueid}, 'x') |
|
363 |
rset = self.execute('Any FS,TS,C,D,U ORDERBY D DESC ' |
|
364 |
'WHERE WF wf_info_for X,' |
|
365 |
'WF from_state FS?, WF to_state TS, WF comment C,' |
|
366 |
'WF creation_date D, WF owned_by U, X eid %(x)s', |
|
367 |
{'x': ueid}, 'x') |
|
368 |
self.assertEquals(len(rset), 2) |
|
369 |
finally: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
370 |
self.execute('DELETE CWUser X WHERE X eid %s' % ueid) |
0 | 371 |
self.commit() |
372 |
||
373 |
def test_select_ambigous_outer_join(self): |
|
374 |
teid = self.execute("INSERT Tag X: X name 'tag'")[0][0] |
|
375 |
self.execute("INSERT Tag X: X name 'tagbis'")[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
376 |
geid = self.execute("CWGroup G WHERE G name 'users'")[0][0] |
0 | 377 |
self.execute("SET X tags Y WHERE X eid %(t)s, Y eid %(g)s", |
378 |
{'g': geid, 't': teid}, 'g') |
|
379 |
rset = self.execute("Any GN,TN ORDERBY GN WHERE T? tags G, T name TN, G name GN") |
|
380 |
self.failUnless(['users', 'tag'] in rset.rows) |
|
381 |
self.failUnless(['activated', None] in rset.rows) |
|
382 |
rset = self.execute("Any GN,TN ORDERBY GN WHERE T tags G?, T name TN, G name GN") |
|
1787 | 383 |
self.assertEquals(rset.rows, [[None, 'tagbis'], ['users', 'tag']]) |
384 |
||
0 | 385 |
def test_select_not_inline_rel(self): |
386 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
387 |
self.execute("INSERT Note X: X type 'a'") |
|
388 |
self.execute("INSERT Note X: X type 'b'") |
|
389 |
self.execute("SET X ecrit_par Y WHERE X type 'a', Y nom 'bidule'") |
|
390 |
rset = self.execute('Note X WHERE NOT X ecrit_par P') |
|
391 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1787 | 392 |
|
0 | 393 |
def test_select_not_unlinked_multiple_solutions(self): |
394 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
395 |
self.execute("INSERT Note X: X type 'a'") |
|
396 |
self.execute("INSERT Note X: X type 'b'") |
|
397 |
self.execute("SET Y evaluee X WHERE X type 'a', Y nom 'bidule'") |
|
398 |
rset = self.execute('Note X WHERE NOT Y evaluee X') |
|
399 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
400 |
||
401 |
def test_select_aggregat_count(self): |
|
402 |
rset = self.execute('Any COUNT(X)') |
|
403 |
self.assertEquals(len(rset.rows), 1) |
|
404 |
self.assertEquals(len(rset.rows[0]), 1) |
|
405 |
self.assertEquals(rset.description, [('Int',)]) |
|
1787 | 406 |
|
0 | 407 |
def test_select_aggregat_sum(self): |
408 |
rset = self.execute('Any SUM(O) WHERE X ordernum O') |
|
409 |
self.assertEquals(len(rset.rows), 1) |
|
410 |
self.assertEquals(len(rset.rows[0]), 1) |
|
411 |
self.assertEquals(rset.description, [('Int',)]) |
|
1787 | 412 |
|
0 | 413 |
def test_select_aggregat_min(self): |
414 |
rset = self.execute('Any MIN(X) WHERE X is Personne') |
|
415 |
self.assertEquals(len(rset.rows), 1) |
|
416 |
self.assertEquals(len(rset.rows[0]), 1) |
|
417 |
self.assertEquals(rset.description, [('Personne',)]) |
|
418 |
rset = self.execute('Any MIN(O) WHERE X ordernum O') |
|
419 |
self.assertEquals(len(rset.rows), 1) |
|
420 |
self.assertEquals(len(rset.rows[0]), 1) |
|
421 |
self.assertEquals(rset.description, [('Int',)]) |
|
1787 | 422 |
|
0 | 423 |
def test_select_aggregat_max(self): |
424 |
rset = self.execute('Any MAX(X) WHERE X is Personne') |
|
425 |
self.assertEquals(len(rset.rows), 1) |
|
426 |
self.assertEquals(len(rset.rows[0]), 1) |
|
427 |
self.assertEquals(rset.description, [('Personne',)]) |
|
428 |
rset = self.execute('Any MAX(O) WHERE X ordernum O') |
|
429 |
self.assertEquals(len(rset.rows), 1) |
|
430 |
self.assertEquals(len(rset.rows[0]), 1) |
|
431 |
self.assertEquals(rset.description, [('Int',)]) |
|
432 |
||
433 |
def test_select_custom_aggregat_concat_string(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
434 |
rset = self.execute('Any CONCAT_STRINGS(N) WHERE X is CWGroup, X name N') |
0 | 435 |
self.failUnless(rset) |
436 |
self.failUnlessEqual(sorted(rset[0][0].split(', ')), ['guests', 'managers', |
|
437 |
'owners', 'users']) |
|
438 |
||
439 |
def test_select_custom_regproc_limit_size(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
440 |
rset = self.execute('Any TEXT_LIMIT_SIZE(N, 3) WHERE X is CWGroup, X name N, X name "managers"') |
0 | 441 |
self.failUnless(rset) |
442 |
self.failUnlessEqual(rset[0][0], 'man...') |
|
443 |
self.execute("INSERT Basket X: X name 'bidule', X description '<b>hop hop</b>', X description_format 'text/html'") |
|
444 |
rset = self.execute('Any LIMIT_SIZE(D, DF, 3) WHERE X is Basket, X description D, X description_format DF') |
|
445 |
self.failUnless(rset) |
|
446 |
self.failUnlessEqual(rset[0][0], 'hop...') |
|
447 |
||
448 |
def test_select_regproc_orderby(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
449 |
rset = self.execute('DISTINCT Any X,N ORDERBY GROUP_SORT_VALUE(N) WHERE X is CWGroup, X name N, X name "managers"') |
0 | 450 |
self.failUnlessEqual(len(rset), 1) |
451 |
self.failUnlessEqual(rset[0][1], 'managers') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
452 |
rset = self.execute('Any X,N ORDERBY GROUP_SORT_VALUE(N) WHERE X is CWGroup, X name N, NOT U in_group X, U login "admin"') |
0 | 453 |
self.failUnlessEqual(len(rset), 3) |
454 |
self.failUnlessEqual(rset[0][1], 'owners') |
|
1787 | 455 |
|
0 | 456 |
def test_select_aggregat_sort(self): |
457 |
rset = self.execute('Any G, COUNT(U) GROUPBY G ORDERBY 2 WHERE U in_group G') |
|
458 |
self.assertEquals(len(rset.rows), 2) |
|
459 |
self.assertEquals(len(rset.rows[0]), 2) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
460 |
self.assertEquals(rset.description[0], ('CWGroup', 'Int',)) |
0 | 461 |
|
462 |
def test_select_aggregat_having(self): |
|
463 |
rset = self.execute('Any N,COUNT(RDEF) GROUPBY N ORDERBY 2,N ' |
|
464 |
'WHERE RT name N, RDEF relation_type RT ' |
|
465 |
'HAVING COUNT(RDEF) > 10') |
|
466 |
self.assertListEquals(rset.rows, |
|
439 | 467 |
[[u'description', 11], ['in_basket', 11], |
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
0
diff
changeset
|
468 |
[u'name', 13], [u'created_by', 33], |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
0
diff
changeset
|
469 |
[u'creation_date', 33], [u'is', 33], [u'is_instance_of', 33], |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
0
diff
changeset
|
470 |
[u'modification_date', 33], [u'owned_by', 33]]) |
0 | 471 |
|
472 |
def test_select_aggregat_having_dumb(self): |
|
473 |
# dumb but should not raise an error |
|
474 |
rset = self.execute('Any U,COUNT(X) GROUPBY U ' |
|
475 |
'WHERE U eid %(x)s, X owned_by U ' |
|
476 |
'HAVING COUNT(X) > 10', {'x': self.ueid}) |
|
477 |
self.assertEquals(len(rset.rows), 1) |
|
478 |
self.assertEquals(rset.rows[0][0], self.ueid) |
|
479 |
||
480 |
def test_select_complex_sort(self): |
|
1950
ab7a41b2cd26
reactivate skipped tests due to sqlite bug which is now fixed (sqlite 3.6.13)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1949
diff
changeset
|
481 |
"""need sqlite including http://www.sqlite.org/cvstrac/tktview?tn=3773 fix""" |
0 | 482 |
rset = self.execute('Any X ORDERBY X,D LIMIT 5 WHERE X creation_date D') |
483 |
result = rset.rows |
|
484 |
result.sort() |
|
485 |
self.assertEquals(tuplify(result), [(1,), (2,), (3,), (4,), (5,)]) |
|
1787 | 486 |
|
0 | 487 |
def test_select_upper(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
488 |
rset = self.execute('Any X, UPPER(L) ORDERBY L WHERE X is CWUser, X login L') |
0 | 489 |
self.assertEquals(len(rset.rows), 2) |
490 |
self.assertEquals(rset.rows[0][1], 'ADMIN') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
491 |
self.assertEquals(rset.description[0], ('CWUser', 'String',)) |
0 | 492 |
self.assertEquals(rset.rows[1][1], 'ANON') |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
493 |
self.assertEquals(rset.description[1], ('CWUser', 'String',)) |
0 | 494 |
eid = rset.rows[0][0] |
495 |
rset = self.execute('Any UPPER(L) WHERE X eid %s, X login L'%eid) |
|
496 |
self.assertEquals(rset.rows[0][0], 'ADMIN') |
|
497 |
self.assertEquals(rset.description, [('String',)]) |
|
498 |
||
499 |
## def test_select_simplified(self): |
|
500 |
## ueid = self.session.user.eid |
|
501 |
## rset = self.execute('Any L WHERE %s login L'%ueid) |
|
502 |
## self.assertEquals(rset.rows[0][0], 'admin') |
|
503 |
## rset = self.execute('Any L WHERE %(x)s login L', {'x':ueid}) |
|
504 |
## self.assertEquals(rset.rows[0][0], 'admin') |
|
1787 | 505 |
|
0 | 506 |
def test_select_searchable_text_1(self): |
507 |
rset = self.execute(u"INSERT Personne X: X nom 'bid�le'") |
|
508 |
rset = self.execute(u"INSERT Societe X: X nom 'bid�le'") |
|
509 |
rset = self.execute("INSERT Societe X: X nom 'chouette'") |
|
510 |
self.commit() |
|
511 |
rset = self.execute('Any X where X has_text %(text)s', {'text': u'bid�le'}) |
|
512 |
self.assertEquals(len(rset.rows), 2, rset.rows) |
|
513 |
rset = self.execute(u'Any N where N has_text "bid�le"') |
|
514 |
self.assertEquals(len(rset.rows), 2, rset.rows) |
|
515 |
biduleeids = [r[0] for r in rset.rows] |
|
516 |
rset = self.execute(u'Any N where NOT N has_text "bid�le"') |
|
517 |
self.failIf([r[0] for r in rset.rows if r[0] in biduleeids]) |
|
518 |
# duh? |
|
519 |
rset = self.execute('Any X WHERE X has_text %(text)s', {'text': u'�a'}) |
|
1787 | 520 |
|
0 | 521 |
def test_select_searchable_text_2(self): |
522 |
rset = self.execute("INSERT Personne X: X nom 'bidule'") |
|
523 |
rset = self.execute("INSERT Personne X: X nom 'chouette'") |
|
524 |
rset = self.execute("INSERT Societe X: X nom 'bidule'") |
|
525 |
self.commit() |
|
526 |
rset = self.execute('Personne N where N has_text "bidule"') |
|
527 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1787 | 528 |
|
0 | 529 |
def test_select_searchable_text_3(self): |
530 |
rset = self.execute("INSERT Personne X: X nom 'bidule', X sexe 'M'") |
|
531 |
rset = self.execute("INSERT Personne X: X nom 'bidule', X sexe 'F'") |
|
532 |
rset = self.execute("INSERT Societe X: X nom 'bidule'") |
|
533 |
self.commit() |
|
534 |
rset = self.execute('Any X where X has_text "bidule" and X sexe "M"') |
|
535 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1787 | 536 |
|
0 | 537 |
def test_select_multiple_searchable_text(self): |
538 |
self.execute(u"INSERT Personne X: X nom 'bid�le'") |
|
539 |
self.execute("INSERT Societe X: X nom 'chouette', S travaille X") |
|
540 |
self.execute(u"INSERT Personne X: X nom 'bid�le'") |
|
541 |
self.commit() |
|
542 |
rset = self.execute('Personne X WHERE X has_text %(text)s, X travaille S, S has_text %(text2)s', |
|
543 |
{'text': u'bid�le', |
|
544 |
'text2': u'chouette',} |
|
545 |
) |
|
546 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1787 | 547 |
|
0 | 548 |
def test_select_no_descr(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
549 |
rset = self.execute('Any X WHERE X is CWGroup', build_descr=0) |
0 | 550 |
rset.rows.sort() |
551 |
self.assertEquals(tuplify(rset.rows), [(1,), (2,), (3,), (4,)]) |
|
552 |
self.assertEquals(rset.description, ()) |
|
553 |
||
554 |
def test_select_limit_offset(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
555 |
rset = self.execute('CWGroup X ORDERBY N LIMIT 2 WHERE X name N') |
0 | 556 |
self.assertEquals(tuplify(rset.rows), [(3,), (1,)]) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
557 |
self.assertEquals(rset.description, [('CWGroup',), ('CWGroup',)]) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
558 |
rset = self.execute('CWGroup X ORDERBY N LIMIT 2 OFFSET 2 WHERE X name N') |
0 | 559 |
self.assertEquals(tuplify(rset.rows), [(4,), (2,)]) |
1787 | 560 |
|
0 | 561 |
def test_select_symetric(self): |
562 |
self.execute("INSERT Personne X: X nom 'machin'") |
|
563 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
564 |
self.execute("INSERT Personne X: X nom 'chouette'") |
|
565 |
self.execute("INSERT Personne X: X nom 'trucmuche'") |
|
566 |
self.execute("SET X connait Y WHERE X nom 'chouette', Y nom 'bidule'") |
|
567 |
self.execute("SET X connait Y WHERE X nom 'machin', Y nom 'chouette'") |
|
568 |
rset = self.execute('Any P where P connait P2') |
|
569 |
self.assertEquals(len(rset.rows), 3, rset.rows) |
|
570 |
rset = self.execute('Any P where NOT P connait P2') |
|
571 |
self.assertEquals(len(rset.rows), 1, rset.rows) # trucmuche |
|
572 |
rset = self.execute('Any P where P connait P2, P2 nom "bidule"') |
|
573 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
574 |
rset = self.execute('Any P where P2 connait P, P2 nom "bidule"') |
|
575 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
576 |
rset = self.execute('Any P where P connait P2, P2 nom "chouette"') |
|
577 |
self.assertEquals(len(rset.rows), 2, rset.rows) |
|
578 |
rset = self.execute('Any P where P2 connait P, P2 nom "chouette"') |
|
579 |
self.assertEquals(len(rset.rows), 2, rset.rows) |
|
1787 | 580 |
|
0 | 581 |
def test_select_inline(self): |
582 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
583 |
self.execute("INSERT Note X: X type 'a'") |
|
584 |
self.execute("SET X ecrit_par Y WHERE X type 'a', Y nom 'bidule'") |
|
585 |
rset = self.execute('Any N where N ecrit_par X, X nom "bidule"') |
|
586 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1787 | 587 |
|
0 | 588 |
def test_select_creation_date(self): |
589 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
590 |
rset = self.execute('Any D WHERE X nom "bidule", X creation_date D') |
|
591 |
self.assertEqual(len(rset.rows), 1) |
|
592 |
||
593 |
def test_select_or_relation(self): |
|
594 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
595 |
self.execute("INSERT Personne X: X nom 'chouette'") |
|
596 |
self.execute("INSERT Societe X: X nom 'logilab'") |
|
597 |
self.execute("INSERT Societe X: X nom 'caesium'") |
|
598 |
self.execute("SET P travaille S WHERE P nom 'bidule', S nom 'logilab'") |
|
599 |
rset = self.execute('DISTINCT Any P WHERE P travaille S1 OR P travaille S2, S1 nom "logilab", S2 nom "caesium"') |
|
600 |
self.assertEqual(len(rset.rows), 1) |
|
601 |
self.execute("SET P travaille S WHERE P nom 'chouette', S nom 'caesium'") |
|
602 |
rset = self.execute('DISTINCT Any P WHERE P travaille S1 OR P travaille S2, S1 nom "logilab", S2 nom "caesium"') |
|
603 |
self.assertEqual(len(rset.rows), 2) |
|
1787 | 604 |
|
0 | 605 |
def test_select_or_sym_relation(self): |
606 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
607 |
self.execute("INSERT Personne X: X nom 'chouette'") |
|
608 |
self.execute("INSERT Personne X: X nom 'truc'") |
|
609 |
self.execute("SET P connait S WHERE P nom 'bidule', S nom 'chouette'") |
|
610 |
rset = self.execute('DISTINCT Any P WHERE S connait P, S nom "chouette"') |
|
611 |
self.assertEqual(len(rset.rows), 1, rset.rows) |
|
612 |
rset = self.execute('DISTINCT Any P WHERE P connait S or S connait P, S nom "chouette"') |
|
613 |
self.assertEqual(len(rset.rows), 1, rset.rows) |
|
614 |
self.execute("SET P connait S WHERE P nom 'chouette', S nom 'truc'") |
|
615 |
rset = self.execute('DISTINCT Any P WHERE S connait P, S nom "chouette"') |
|
616 |
self.assertEqual(len(rset.rows), 2, rset.rows) |
|
617 |
rset = self.execute('DISTINCT Any P WHERE P connait S OR S connait P, S nom "chouette"') |
|
618 |
self.assertEqual(len(rset.rows), 2, rset.rows) |
|
1787 | 619 |
|
0 | 620 |
def test_select_follow_relation(self): |
621 |
self.execute("INSERT Affaire X: X sujet 'cool'") |
|
622 |
self.execute("INSERT Societe X: X nom 'chouette'") |
|
623 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
624 |
self.execute("INSERT Note X: X para 'truc'") |
|
625 |
self.execute("SET S evaluee N WHERE S is Societe, N is Note") |
|
626 |
self.execute("INSERT Societe X: X nom 'bidule'") |
|
627 |
self.execute("INSERT Note X: X para 'troc'") |
|
628 |
self.execute("SET S evaluee N WHERE S nom 'bidule', N para 'troc'") |
|
629 |
rset = self.execute('DISTINCT Any A,N WHERE A concerne S, S evaluee N') |
|
630 |
self.assertEqual(len(rset.rows), 1, rset.rows) |
|
631 |
||
632 |
def test_select_ordered_distinct_1(self): |
|
633 |
self.execute("INSERT Affaire X: X sujet 'cool', X ref '1'") |
|
634 |
self.execute("INSERT Affaire X: X sujet 'cool', X ref '2'") |
|
635 |
rset = self.execute('DISTINCT Any S ORDERBY R WHERE A is Affaire, A sujet S, A ref R') |
|
636 |
self.assertEqual(rset.rows, [['cool']]) |
|
637 |
||
638 |
def test_select_ordered_distinct_2(self): |
|
639 |
self.execute("INSERT Affaire X: X sujet 'minor'") |
|
640 |
self.execute("INSERT Affaire X: X sujet 'important'") |
|
641 |
self.execute("INSERT Affaire X: X sujet 'normal'") |
|
642 |
self.execute("INSERT Affaire X: X sujet 'zou'") |
|
643 |
self.execute("INSERT Affaire X: X sujet 'abcd'") |
|
644 |
rset = self.execute('DISTINCT Any S ORDERBY S WHERE A is Affaire, A sujet S') |
|
645 |
self.assertEqual(rset.rows, [['abcd'], ['important'], ['minor'], ['normal'], ['zou']]) |
|
1787 | 646 |
|
0 | 647 |
def test_select_ordered_distinct_3(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
648 |
rset = self.execute('DISTINCT Any N ORDERBY GROUP_SORT_VALUE(N) WHERE X is CWGroup, X name N') |
0 | 649 |
self.assertEqual(rset.rows, [['owners'], ['guests'], ['users'], ['managers']]) |
650 |
||
651 |
def test_select_or_value(self): |
|
652 |
rset = self.execute('Any U WHERE U in_group G, G name "owners" OR G name "users"') |
|
653 |
self.assertEqual(len(rset.rows), 0) |
|
654 |
rset = self.execute('Any U WHERE U in_group G, G name "guests" OR G name "managers"') |
|
655 |
self.assertEqual(len(rset.rows), 2) |
|
656 |
||
657 |
def test_select_explicit_eid(self): |
|
658 |
rset = self.execute('Any X,E WHERE X owned_by U, X eid E, U eid %(u)s', {'u': self.session.user.eid}) |
|
659 |
self.failUnless(rset) |
|
660 |
self.assertEquals(rset.description[0][1], 'Int') |
|
1787 | 661 |
|
0 | 662 |
# def test_select_rewritten_optional(self): |
663 |
# eid = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
664 |
# rset = self.execute('Any X WHERE X eid %(x)s, EXISTS(X owned_by U) OR EXISTS(X concerne S?, S owned_by U)', |
|
665 |
# {'x': eid}, 'x') |
|
666 |
# self.assertEquals(rset.rows, [[eid]]) |
|
1787 | 667 |
|
0 | 668 |
def test_today_bug(self): |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
669 |
self.execute("INSERT Tag X: X name 'bidule', X creation_date NOW") |
0 | 670 |
self.execute("INSERT Tag Y: Y name 'toto'") |
671 |
rset = self.execute("Any D WHERE X name in ('bidule', 'toto') , X creation_date D") |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
672 |
self.assert_(isinstance(rset.rows[0][0], datetime), rset.rows) |
0 | 673 |
rset = self.execute('Tag X WHERE X creation_date TODAY') |
674 |
self.assertEqual(len(rset.rows), 2) |
|
675 |
rset = self.execute('Any MAX(D) WHERE X is Tag, X creation_date D') |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
676 |
self.failUnless(isinstance(rset[0][0], datetime), type(rset[0][0])) |
0 | 677 |
|
678 |
def test_today(self): |
|
679 |
self.execute("INSERT Tag X: X name 'bidule', X creation_date TODAY") |
|
680 |
self.execute("INSERT Tag Y: Y name 'toto'") |
|
681 |
rset = self.execute('Tag X WHERE X creation_date TODAY') |
|
682 |
self.assertEqual(len(rset.rows), 2) |
|
683 |
||
684 |
def test_select_boolean(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
685 |
rset = self.execute('Any N WHERE X is CWEType, X name N, X final %(val)s', |
0 | 686 |
{'val': True}) |
687 |
self.assertEquals(sorted(r[0] for r in rset.rows), ['Boolean', 'Bytes', |
|
688 |
'Date', 'Datetime', |
|
689 |
'Decimal', 'Float', |
|
690 |
'Int', 'Interval', |
|
691 |
'Password', 'String', |
|
692 |
'Time']) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
693 |
rset = self.execute('Any N WHERE X is CWEType, X name N, X final TRUE') |
0 | 694 |
self.assertEquals(sorted(r[0] for r in rset.rows), ['Boolean', 'Bytes', |
695 |
'Date', 'Datetime', |
|
696 |
'Decimal', 'Float', |
|
697 |
'Int', 'Interval', |
|
698 |
'Password', 'String', |
|
699 |
'Time']) |
|
1787 | 700 |
|
0 | 701 |
def test_select_constant(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
702 |
rset = self.execute('Any X, "toto" ORDERBY X WHERE X is CWGroup') |
0 | 703 |
self.assertEquals(rset.rows, |
704 |
map(list, zip((1,2,3,4), ('toto','toto','toto','toto',)))) |
|
705 |
self.assertIsInstance(rset[0][1], unicode) |
|
706 |
self.assertEquals(rset.description, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
707 |
zip(('CWGroup', 'CWGroup', 'CWGroup', 'CWGroup'), |
0 | 708 |
('String', 'String', 'String', 'String',))) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
709 |
rset = self.execute('Any X, %(value)s ORDERBY X WHERE X is CWGroup', {'value': 'toto'}) |
0 | 710 |
self.assertEquals(rset.rows, |
711 |
map(list, zip((1,2,3,4), ('toto','toto','toto','toto',)))) |
|
712 |
self.assertIsInstance(rset[0][1], unicode) |
|
713 |
self.assertEquals(rset.description, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
714 |
zip(('CWGroup', 'CWGroup', 'CWGroup', 'CWGroup'), |
0 | 715 |
('String', 'String', 'String', 'String',))) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
716 |
rset = self.execute('Any X,GN WHERE X is CWUser, G is CWGroup, X login "syt", X in_group G, G name GN') |
0 | 717 |
|
718 |
def test_select_union(self): |
|
719 |
rset = self.execute('Any X,N ORDERBY N WITH X,N BEING ' |
|
720 |
'((Any X,N WHERE X name N, X transition_of E, E name %(name)s)' |
|
721 |
' UNION ' |
|
722 |
'(Any X,N WHERE X name N, X state_of E, E name %(name)s))', |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
723 |
{'name': 'CWUser'}) |
0 | 724 |
self.assertEquals([x[1] for x in rset.rows], |
725 |
['activate', 'activated', 'deactivate', 'deactivated']) |
|
726 |
self.assertEquals(rset.description, |
|
727 |
[('Transition', 'String'), ('State', 'String'), |
|
728 |
('Transition', 'String'), ('State', 'String')]) |
|
1787 | 729 |
|
0 | 730 |
def test_select_union_aggregat(self): |
731 |
# meaningless, the goal in to have group by done on different attribute |
|
732 |
# for each sub-query |
|
733 |
self.execute('(Any N,COUNT(X) GROUPBY N WHERE X name N, X is State)' |
|
734 |
' UNION ' |
|
735 |
'(Any N,COUNT(X) GROUPBY N ORDERBY 2 WHERE X login N)') |
|
1787 | 736 |
|
0 | 737 |
def test_select_union_aggregat_independant_group(self): |
738 |
self.execute('INSERT State X: X name "hop"') |
|
739 |
self.execute('INSERT State X: X name "hop"') |
|
740 |
self.execute('INSERT Transition X: X name "hop"') |
|
741 |
self.execute('INSERT Transition X: X name "hop"') |
|
742 |
rset = self.execute('Any N,NX ORDERBY 2 WITH N,NX BEING ' |
|
743 |
'((Any N,COUNT(X) GROUPBY N WHERE X name N, X is State HAVING COUNT(X)>1)' |
|
744 |
' UNION ' |
|
745 |
'(Any N,COUNT(X) GROUPBY N WHERE X name N, X is Transition HAVING COUNT(X)>1))') |
|
746 |
self.assertEquals(rset.rows, [[u'hop', 2], [u'hop', 2]]) |
|
1787 | 747 |
|
0 | 748 |
def test_select_union_selection_with_diff_variables(self): |
749 |
rset = self.execute('(Any N WHERE X name N, X is State)' |
|
750 |
' UNION ' |
|
751 |
'(Any NN WHERE XX name NN, XX is Transition)') |
|
752 |
self.assertEquals(sorted(r[0] for r in rset.rows), |
|
753 |
['abort', 'activate', 'activated', 'ben non', |
|
754 |
'deactivate', 'deactivated', 'done', 'en cours', |
|
755 |
'end', 'finie', 'markasdone', 'pitetre', 'redoit', |
|
756 |
'start', 'todo']) |
|
1787 | 757 |
|
0 | 758 |
def test_exists(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
759 |
geid = self.execute("INSERT CWGroup X: X name 'lulufanclub'")[0][0] |
0 | 760 |
self.execute("SET U in_group G WHERE G name 'lulufanclub'") |
761 |
peid = self.execute("INSERT Personne X: X prenom 'lulu', X nom 'petit'")[0][0] |
|
762 |
rset = self.execute("Any X WHERE X prenom 'lulu'," |
|
763 |
"EXISTS (U in_group G, G name 'lulufanclub' OR G name 'managers');") |
|
764 |
self.assertEquals(rset.rows, [[peid]]) |
|
765 |
||
766 |
def test_identity(self): |
|
767 |
eid = self.execute('Any X WHERE X identity Y, Y eid 1')[0][0] |
|
768 |
self.assertEquals(eid, 1) |
|
769 |
eid = self.execute('Any X WHERE Y identity X, Y eid 1')[0][0] |
|
770 |
self.assertEquals(eid, 1) |
|
771 |
login = self.execute('Any L WHERE X login "admin", X identity Y, Y login L')[0][0] |
|
772 |
self.assertEquals(login, 'admin') |
|
773 |
||
774 |
def test_select_date_mathexp(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
775 |
rset = self.execute('Any X, TODAY - CD WHERE X is CWUser, X creation_date CD') |
0 | 776 |
self.failUnless(rset) |
777 |
self.failUnlessEqual(rset.description[0][1], 'Interval') |
|
778 |
eid, = self.execute("INSERT Personne X: X nom 'bidule'")[0] |
|
779 |
rset = self.execute('Any X, NOW - CD WHERE X is Personne, X creation_date CD') |
|
780 |
self.failUnlessEqual(rset.description[0][1], 'Interval') |
|
781 |
||
782 |
def test_select_subquery_aggregat(self): |
|
783 |
# percent users by groups |
|
784 |
self.execute('SET X in_group G WHERE G name "users"') |
|
785 |
rset = self.execute('Any GN, COUNT(X)*100/T GROUPBY GN ORDERBY 2,1' |
|
786 |
' WHERE G name GN, X in_group G' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
787 |
' WITH T BEING (Any COUNT(U) WHERE U is CWUser)') |
0 | 788 |
self.assertEquals(rset.rows, [[u'guests', 50], [u'managers', 50], [u'users', 100]]) |
789 |
self.assertEquals(rset.description, [('String', 'Int'), ('String', 'Int'), ('String', 'Int')]) |
|
790 |
||
791 |
def test_select_subquery_const(self): |
|
792 |
rset = self.execute('Any X WITH X BEING ((Any NULL) UNION (Any "toto"))') |
|
793 |
self.assertEquals(rset.rows, [[None], ['toto']]) |
|
794 |
self.assertEquals(rset.description, [(None,), ('String',)]) |
|
1787 | 795 |
|
0 | 796 |
# insertion queries tests ################################################# |
1787 | 797 |
|
0 | 798 |
def test_insert_is(self): |
799 |
eid, = self.execute("INSERT Personne X: X nom 'bidule'")[0] |
|
800 |
etype, = self.execute("Any TN WHERE X is T, X eid %s, T name TN" % eid)[0] |
|
801 |
self.assertEquals(etype, 'Personne') |
|
802 |
self.execute("INSERT Personne X: X nom 'managers'") |
|
1787 | 803 |
|
0 | 804 |
def test_insert_1(self): |
805 |
rset = self.execute("INSERT Personne X: X nom 'bidule'") |
|
806 |
self.assertEquals(len(rset.rows), 1) |
|
807 |
self.assertEquals(rset.description, [('Personne',)]) |
|
808 |
rset = self.execute('Personne X WHERE X nom "bidule"') |
|
809 |
self.assert_(rset.rows) |
|
810 |
self.assertEquals(rset.description, [('Personne',)]) |
|
811 |
||
812 |
def test_insert_1_multiple(self): |
|
813 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
814 |
self.execute("INSERT Personne X: X nom 'chouette'") |
|
815 |
rset = self.execute("INSERT Societe Y: Y nom N, P travaille Y WHERE P nom N") |
|
816 |
self.assertEquals(len(rset.rows), 2) |
|
817 |
self.assertEquals(rset.description, [('Societe',), ('Societe',)]) |
|
818 |
||
819 |
def test_insert_2(self): |
|
820 |
rset = self.execute("INSERT Personne X, Personne Y: X nom 'bidule', Y nom 'tutu'") |
|
821 |
self.assertEquals(rset.description, [('Personne', 'Personne')]) |
|
822 |
rset = self.execute('Personne X WHERE X nom "bidule" or X nom "tutu"') |
|
823 |
self.assert_(rset.rows) |
|
824 |
self.assertEquals(rset.description, [('Personne',), ('Personne',)]) |
|
825 |
||
826 |
def test_insert_3(self): |
|
827 |
self.execute("INSERT Personne X: X nom Y WHERE U login 'admin', U login Y") |
|
828 |
rset = self.execute('Personne X WHERE X nom "admin"') |
|
829 |
self.assert_(rset.rows) |
|
1787 | 830 |
self.assertEquals(rset.description, [('Personne',)]) |
0 | 831 |
|
832 |
def test_insert_4(self): |
|
833 |
self.execute("INSERT Societe Y: Y nom 'toto'") |
|
834 |
self.execute("INSERT Personne X: X nom 'bidule', X travaille Y WHERE Y nom 'toto'") |
|
835 |
rset = self.execute('Any X, Y WHERE X nom "bidule", Y nom "toto", X travaille Y') |
|
836 |
self.assert_(rset.rows) |
|
837 |
self.assertEquals(rset.description, [('Personne', 'Societe',)]) |
|
1787 | 838 |
|
0 | 839 |
def test_insert_4bis(self): |
840 |
peid = self.execute("INSERT Personne X: X nom 'bidule'")[0][0] |
|
841 |
seid = self.execute("INSERT Societe Y: Y nom 'toto', X travaille Y WHERE X eid %(x)s", |
|
842 |
{'x': str(peid)})[0][0] |
|
843 |
self.assertEqual(len(self.execute('Any X, Y WHERE X travaille Y')), 1) |
|
844 |
self.execute("INSERT Personne X: X nom 'chouette', X travaille Y WHERE Y eid %(x)s", |
|
845 |
{'x': str(seid)}) |
|
846 |
self.assertEqual(len(self.execute('Any X, Y WHERE X travaille Y')), 2) |
|
1787 | 847 |
|
0 | 848 |
def test_insert_4ter(self): |
849 |
peid = self.execute("INSERT Personne X: X nom 'bidule'")[0][0] |
|
850 |
seid = self.execute("INSERT Societe Y: Y nom 'toto', X travaille Y WHERE X eid %(x)s", |
|
851 |
{'x': unicode(peid)})[0][0] |
|
852 |
self.assertEqual(len(self.execute('Any X, Y WHERE X travaille Y')), 1) |
|
853 |
self.execute("INSERT Personne X: X nom 'chouette', X travaille Y WHERE Y eid %(x)s", |
|
854 |
{'x': unicode(seid)}) |
|
855 |
self.assertEqual(len(self.execute('Any X, Y WHERE X travaille Y')), 2) |
|
856 |
||
857 |
def test_insert_5(self): |
|
858 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
859 |
self.execute("INSERT Societe Y: Y nom 'toto', X travaille Y WHERE X nom 'bidule'") |
|
860 |
rset = self.execute('Any X, Y WHERE X nom "bidule", Y nom "toto", X travaille Y') |
|
861 |
self.assert_(rset.rows) |
|
862 |
self.assertEquals(rset.description, [('Personne', 'Societe',)]) |
|
863 |
||
864 |
def test_insert_6(self): |
|
865 |
self.execute("INSERT Personne X, Societe Y: X nom 'bidule', Y nom 'toto', X travaille Y") |
|
866 |
rset = self.execute('Any X, Y WHERE X nom "bidule", Y nom "toto", X travaille Y') |
|
867 |
self.assert_(rset.rows) |
|
868 |
self.assertEquals(rset.description, [('Personne', 'Societe',)]) |
|
869 |
||
870 |
def test_insert_7(self): |
|
871 |
self.execute("INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y WHERE U login 'admin', U login N") |
|
872 |
rset = self.execute('Any X, Y WHERE X nom "admin", Y nom "toto", X travaille Y') |
|
873 |
self.assert_(rset.rows) |
|
874 |
self.assertEquals(rset.description, [('Personne', 'Societe',)]) |
|
875 |
||
876 |
def test_insert_8(self): |
|
877 |
self.execute("INSERT Societe Y, Personne X: Y nom N, X nom 'toto', X travaille Y WHERE U login 'admin', U login N") |
|
878 |
rset = self.execute('Any X, Y WHERE X nom "toto", Y nom "admin", X travaille Y') |
|
879 |
self.assert_(rset.rows) |
|
880 |
self.assertEquals(rset.description, [('Personne', 'Societe',)]) |
|
881 |
||
882 |
def test_insert_query_error(self): |
|
883 |
self.assertRaises(Exception, |
|
884 |
self.execute, |
|
885 |
"INSERT Personne X: X nom 'toto', X is Personne") |
|
886 |
self.assertRaises(Exception, |
|
887 |
self.execute, |
|
888 |
"INSERT Personne X: X nom 'toto', X is_instance_of Personne") |
|
889 |
self.assertRaises(QueryError, |
|
890 |
self.execute, |
|
891 |
"INSERT Personne X: X nom 'toto', X has_text 'tutu'") |
|
892 |
||
893 |
self.assertRaises(QueryError, |
|
894 |
self.execute, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
895 |
"INSERT CWUser X: X login 'toto', X eid %s" % cnx.user(self.session).eid) |
0 | 896 |
|
897 |
def test_insertion_description_with_where(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
898 |
rset = self.execute('INSERT CWUser E, EmailAddress EM: E login "X", E upassword "X", ' |
0 | 899 |
'E primary_email EM, EM address "X", E in_group G ' |
900 |
'WHERE G name "managers"') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
901 |
self.assertEquals(list(rset.description[0]), ['CWUser', 'EmailAddress']) |
1787 | 902 |
|
0 | 903 |
# deletion queries tests ################################################## |
904 |
||
905 |
def test_delete_1(self): |
|
906 |
self.execute("INSERT Personne Y: Y nom 'toto'") |
|
907 |
rset = self.execute('Personne X WHERE X nom "toto"') |
|
908 |
self.assertEqual(len(rset.rows), 1) |
|
909 |
self.execute("DELETE Personne Y WHERE Y nom 'toto'") |
|
910 |
rset = self.execute('Personne X WHERE X nom "toto"') |
|
911 |
self.assertEqual(len(rset.rows), 0) |
|
1787 | 912 |
|
0 | 913 |
def test_delete_2(self): |
914 |
rset = self.execute("INSERT Personne X, Personne Y, Societe Z : X nom 'syt', Y nom 'adim', Z nom 'Logilab', X travaille Z, Y travaille Z") |
|
915 |
self.assertEquals(len(rset), 1) |
|
916 |
self.assertEquals(len(rset[0]), 3) |
|
917 |
self.assertEquals(rset.description[0], ('Personne', 'Personne', 'Societe')) |
|
918 |
self.assertEquals(self.execute('Any N WHERE X nom N, X eid %s'% rset[0][0])[0][0], 'syt') |
|
919 |
rset = self.execute('Personne X WHERE X travaille Y, Y nom "Logilab"') |
|
920 |
self.assertEqual(len(rset.rows), 2, rset.rows) |
|
921 |
self.execute("DELETE X travaille Y WHERE X is Personne, Y nom 'Logilabo'") |
|
922 |
rset = self.execute('Personne X WHERE X travaille Y, Y nom "Logilab"') |
|
923 |
self.assertEqual(len(rset.rows), 2, rset.rows) |
|
924 |
self.execute("DELETE X travaille Y WHERE X is Personne, Y nom 'Logilab'") |
|
925 |
rset = self.execute('Personne X WHERE X travaille Y, Y nom "Logilab"') |
|
926 |
self.assertEqual(len(rset.rows), 0, rset.rows) |
|
927 |
||
928 |
def test_delete_3(self): |
|
929 |
u, s = self._user_session(('users',)) |
|
930 |
peid, = self.o.execute(s, "INSERT Personne P: P nom 'toto'")[0] |
|
931 |
seid, = self.o.execute(s, "INSERT Societe S: S nom 'logilab'")[0] |
|
932 |
self.o.execute(s, "SET P travaille S") |
|
933 |
rset = self.execute('Personne P WHERE P travaille S') |
|
934 |
self.assertEqual(len(rset.rows), 1) |
|
935 |
self.execute("DELETE X travaille Y WHERE X eid %s, Y eid %s" % (peid, seid)) |
|
936 |
rset = self.execute('Personne P WHERE P travaille S') |
|
937 |
self.assertEqual(len(rset.rows), 0) |
|
938 |
||
939 |
def test_delete_symetric(self): |
|
940 |
teid1 = self.execute("INSERT Folder T: T name 'toto'")[0][0] |
|
941 |
teid2 = self.execute("INSERT Folder T: T name 'tutu'")[0][0] |
|
942 |
self.execute('SET X see_also Y WHERE X eid %s, Y eid %s' % (teid1, teid2)) |
|
943 |
rset = self.execute('Any X,Y WHERE X see_also Y') |
|
944 |
self.assertEquals(len(rset) , 2, rset.rows) |
|
945 |
self.execute('DELETE X see_also Y WHERE X eid %s, Y eid %s' % (teid1, teid2)) |
|
946 |
rset = self.execute('Any X,Y WHERE X see_also Y') |
|
947 |
self.assertEquals(len(rset) , 0) |
|
948 |
self.execute('SET X see_also Y WHERE X eid %s, Y eid %s' % (teid1, teid2)) |
|
949 |
rset = self.execute('Any X,Y WHERE X see_also Y') |
|
950 |
self.assertEquals(len(rset) , 2) |
|
951 |
self.execute('DELETE X see_also Y WHERE X eid %s, Y eid %s' % (teid2, teid1)) |
|
952 |
rset = self.execute('Any X,Y WHERE X see_also Y') |
|
953 |
self.assertEquals(len(rset) , 0) |
|
954 |
||
955 |
def test_nonregr_delete_cache(self): |
|
956 |
"""test that relations are properly cleaned when an entity is deleted |
|
957 |
(using cachekey on sql generation returned always the same query for an eid, |
|
958 |
whatever the relation) |
|
959 |
""" |
|
960 |
u, s = self._user_session(('users',)) |
|
961 |
aeid, = self.o.execute(s, 'INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')[0] |
|
962 |
# XXX would be nice if the rql below was enough... |
|
963 |
#'INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y' |
|
964 |
eeid, = self.o.execute(s, 'INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y WHERE Y is EmailAddress')[0] |
|
965 |
self.o.execute(s, "DELETE Email X") |
|
966 |
sqlc = s.pool['system'] |
|
967 |
sqlc.execute('SELECT * FROM recipients_relation') |
|
968 |
self.assertEquals(len(sqlc.fetchall()), 0) |
|
969 |
sqlc.execute('SELECT * FROM owned_by_relation WHERE eid_from=%s'%eeid) |
|
970 |
self.assertEquals(len(sqlc.fetchall()), 0) |
|
1787 | 971 |
|
0 | 972 |
def test_nonregr_delete_cache2(self): |
973 |
eid = self.execute("INSERT Folder T: T name 'toto'")[0][0] |
|
974 |
self.commit() |
|
975 |
# fill the cache |
|
976 |
self.execute("Any X WHERE X eid %(x)s", {'x': eid}, 'x') |
|
977 |
self.execute("Any X WHERE X eid %s" %eid) |
|
978 |
self.execute("Folder X WHERE X eid %(x)s", {'x': eid}, 'x') |
|
979 |
self.execute("Folder X WHERE X eid %s" %eid) |
|
980 |
self.execute("DELETE Folder T WHERE T eid %s"%eid) |
|
981 |
self.commit() |
|
982 |
rset = self.execute("Any X WHERE X eid %(x)s", {'x': eid}, 'x') |
|
983 |
self.assertEquals(rset.rows, []) |
|
984 |
rset = self.execute("Any X WHERE X eid %s" %eid) |
|
985 |
self.assertEquals(rset.rows, []) |
|
986 |
rset = self.execute("Folder X WHERE X eid %(x)s", {'x': eid}, 'x') |
|
987 |
self.assertEquals(rset.rows, []) |
|
988 |
rset = self.execute("Folder X WHERE X eid %s" %eid) |
|
989 |
self.assertEquals(rset.rows, []) |
|
1787 | 990 |
|
0 | 991 |
# update queries tests #################################################### |
992 |
||
993 |
def test_update_1(self): |
|
994 |
self.execute("INSERT Personne Y: Y nom 'toto'") |
|
995 |
rset = self.execute('Personne X WHERE X nom "toto"') |
|
996 |
self.assertEqual(len(rset.rows), 1) |
|
997 |
self.execute("SET X nom 'tutu', X prenom 'original' WHERE X is Personne, X nom 'toto'") |
|
998 |
rset = self.execute('Any Y, Z WHERE X is Personne, X nom Y, X prenom Z') |
|
999 |
self.assertEqual(tuplify(rset.rows), [('tutu', 'original')]) |
|
1787 | 1000 |
|
0 | 1001 |
def test_update_2(self): |
1002 |
self.execute("INSERT Personne X, Societe Y: X nom 'bidule', Y nom 'toto'") |
|
1003 |
#rset = self.execute('Any X, Y WHERE X nom "bidule", Y nom "toto"') |
|
1004 |
#self.assertEqual(len(rset.rows), 1) |
|
1005 |
#rset = self.execute('Any X, Y WHERE X travaille Y') |
|
1006 |
#self.assertEqual(len(rset.rows), 0) |
|
1007 |
self.execute("SET X travaille Y WHERE X nom 'bidule', Y nom 'toto'") |
|
1008 |
rset = self.execute('Any X, Y WHERE X travaille Y') |
|
1009 |
self.assertEqual(len(rset.rows), 1) |
|
1787 | 1010 |
|
0 | 1011 |
def test_update_2bis(self): |
1012 |
rset = self.execute("INSERT Personne X, Societe Y: X nom 'bidule', Y nom 'toto'") |
|
1013 |
eid1, eid2 = rset[0][0], rset[0][1] |
|
1014 |
self.execute("SET X travaille Y WHERE X eid %(x)s, Y eid %(y)s", |
|
1015 |
{'x': str(eid1), 'y': str(eid2)}) |
|
1016 |
rset = self.execute('Any X, Y WHERE X travaille Y') |
|
1017 |
self.assertEqual(len(rset.rows), 1) |
|
1787 | 1018 |
|
0 | 1019 |
def test_update_2ter(self): |
1020 |
rset = self.execute("INSERT Personne X, Societe Y: X nom 'bidule', Y nom 'toto'") |
|
1021 |
eid1, eid2 = rset[0][0], rset[0][1] |
|
1022 |
self.execute("SET X travaille Y WHERE X eid %(x)s, Y eid %(y)s", |
|
1023 |
{'x': unicode(eid1), 'y': unicode(eid2)}) |
|
1024 |
rset = self.execute('Any X, Y WHERE X travaille Y') |
|
1025 |
self.assertEqual(len(rset.rows), 1) |
|
1787 | 1026 |
|
0 | 1027 |
## def test_update_4(self): |
1028 |
## self.execute("SET X know Y WHERE X ami Y") |
|
1787 | 1029 |
|
0 | 1030 |
def test_update_multiple1(self): |
1031 |
peid1 = self.execute("INSERT Personne Y: Y nom 'tutu'")[0][0] |
|
1032 |
peid2 = self.execute("INSERT Personne Y: Y nom 'toto'")[0][0] |
|
1033 |
self.execute("SET X nom 'tutu', Y nom 'toto' WHERE X nom 'toto', Y nom 'tutu'") |
|
1034 |
self.assertEquals(self.execute('Any X WHERE X nom "toto"').rows, [[peid1]]) |
|
1035 |
self.assertEquals(self.execute('Any X WHERE X nom "tutu"').rows, [[peid2]]) |
|
1036 |
||
1037 |
def test_update_multiple2(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1038 |
ueid = self.execute("INSERT CWUser X: X login 'bob', X upassword 'toto'")[0][0] |
0 | 1039 |
peid1 = self.execute("INSERT Personne Y: Y nom 'turlu'")[0][0] |
1040 |
peid2 = self.execute("INSERT Personne Y: Y nom 'tutu'")[0][0] |
|
1041 |
self.execute('SET P1 owned_by U, P2 owned_by U ' |
|
1042 |
'WHERE P1 eid %s, P2 eid %s, U eid %s' % (peid1, peid2, ueid)) |
|
1043 |
self.failUnless(self.execute('Any X WHERE X eid %s, X owned_by U, U eid %s' |
|
1044 |
% (peid1, ueid))) |
|
1045 |
self.failUnless(self.execute('Any X WHERE X eid %s, X owned_by U, U eid %s' |
|
1046 |
% (peid2, ueid))) |
|
1047 |
||
1048 |
def test_update_math_expr(self): |
|
1049 |
orders = [r[0] for r in self.execute('Any O ORDERBY O WHERE ST name "Personne", X from_entity ST, X ordernum O')] |
|
1050 |
for i,v in enumerate(orders): |
|
1051 |
if v != orders[0]: |
|
1052 |
splitidx = i |
|
1053 |
break |
|
1054 |
self.execute('SET X ordernum Y+1 WHERE X from_entity SE, SE name "Personne", X ordernum Y, X ordernum >= %(order)s', |
|
1055 |
{'order': orders[splitidx]}) |
|
1056 |
orders2 = [r[0] for r in self.execute('Any O ORDERBY O WHERE ST name "Personne", X from_entity ST, X ordernum O')] |
|
1057 |
orders = orders[:splitidx] + [o+1 for o in orders[splitidx:]] |
|
1058 |
self.assertEquals(orders2, orders) |
|
1059 |
||
1060 |
def test_update_string_concat(self): |
|
1061 |
beid = self.execute("INSERT Bookmark Y: Y title 'toto', Y path '/view'")[0][0] |
|
1062 |
self.execute('SET X title XN + %(suffix)s WHERE X is Bookmark, X title XN', {'suffix': u'-moved'}) |
|
1063 |
newname = self.execute('Any XN WHERE X eid %(x)s, X title XN', {'x': beid}, 'x')[0][0] |
|
1064 |
self.assertEquals(newname, 'toto-moved') |
|
1787 | 1065 |
|
0 | 1066 |
def test_update_query_error(self): |
1067 |
self.execute("INSERT Personne Y: Y nom 'toto'") |
|
1068 |
self.assertRaises(Exception, self.execute, "SET X nom 'toto', X is Personne") |
|
1069 |
self.assertRaises(QueryError, self.execute, "SET X nom 'toto', X has_text 'tutu' WHERE X is Personne") |
|
1070 |
self.assertRaises(QueryError, self.execute, "SET X login 'tutu', X eid %s" % cnx.user(self.session).eid) |
|
1071 |
||
1787 | 1072 |
|
0 | 1073 |
# upassword encryption tests ################################################# |
1787 | 1074 |
|
0 | 1075 |
def test_insert_upassword(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1076 |
rset = self.execute("INSERT CWUser X: X login 'bob', X upassword 'toto'") |
0 | 1077 |
self.assertEquals(len(rset.rows), 1) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1078 |
self.assertEquals(rset.description, [('CWUser',)]) |
0 | 1079 |
self.assertRaises(Unauthorized, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1080 |
self.execute, "Any P WHERE X is CWUser, X login 'bob', X upassword P") |
0 | 1081 |
cursor = self.pool['system'] |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1082 |
cursor.execute("SELECT %supassword from %sCWUser WHERE %slogin='bob'" |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
1083 |
% (SQL_PREFIX, SQL_PREFIX, SQL_PREFIX)) |
1951
f28e7f300d3f
getting a StringIO here was due to lgc.db sqlite adapter bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1950
diff
changeset
|
1084 |
passwd = str(cursor.fetchone()[0]) |
1787 | 1085 |
self.assertEquals(passwd, crypt_password('toto', passwd[:2])) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1086 |
rset = self.execute("Any X WHERE X is CWUser, X login 'bob', X upassword '%s'" % passwd) |
0 | 1087 |
self.assertEquals(len(rset.rows), 1) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1088 |
self.assertEquals(rset.description, [('CWUser',)]) |
1787 | 1089 |
|
0 | 1090 |
def test_update_upassword(self): |
1091 |
cursor = self.pool['system'] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1092 |
rset = self.execute("INSERT CWUser X: X login 'bob', X upassword %(pwd)s", {'pwd': 'toto'}) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1093 |
self.assertEquals(rset.description[0][0], 'CWUser') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1094 |
rset = self.execute("SET X upassword %(pwd)s WHERE X is CWUser, X login 'bob'", |
0 | 1095 |
{'pwd': 'tutu'}) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1096 |
cursor.execute("SELECT %supassword from %sCWUser WHERE %slogin='bob'" |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
1097 |
% (SQL_PREFIX, SQL_PREFIX, SQL_PREFIX)) |
1951
f28e7f300d3f
getting a StringIO here was due to lgc.db sqlite adapter bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1950
diff
changeset
|
1098 |
passwd = str(cursor.fetchone()[0]) |
1787 | 1099 |
self.assertEquals(passwd, crypt_password('tutu', passwd[:2])) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1100 |
rset = self.execute("Any X WHERE X is CWUser, X login 'bob', X upassword '%s'" % passwd) |
0 | 1101 |
self.assertEquals(len(rset.rows), 1) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1102 |
self.assertEquals(rset.description, [('CWUser',)]) |
0 | 1103 |
|
1104 |
# non regression tests #################################################### |
|
1787 | 1105 |
|
0 | 1106 |
def test_nonregr_1(self): |
1107 |
teid = self.execute("INSERT Tag X: X name 'tag'")[0][0] |
|
1108 |
self.execute("SET X tags Y WHERE X name 'tag', Y is State, Y name 'activated'") |
|
1109 |
rset = self.execute('Any X WHERE T tags X') |
|
1110 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
1111 |
rset = self.execute('Any T WHERE T tags X, X is State') |
|
1112 |
self.assertEquals(rset.rows, [[teid]]) |
|
1113 |
rset = self.execute('Any T WHERE T tags X') |
|
1114 |
self.assertEquals(rset.rows, [[teid]]) |
|
1115 |
||
1116 |
def test_nonregr_2(self): |
|
1117 |
teid = self.execute("INSERT Tag X: X name 'tag'")[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1118 |
geid = self.execute("CWGroup G WHERE G name 'users'")[0][0] |
0 | 1119 |
self.execute("SET X tags Y WHERE X eid %(t)s, Y eid %(g)s", |
1120 |
{'g': geid, 't': teid}) |
|
1121 |
rset = self.execute('Any X WHERE E eid %(x)s, E tags X', |
|
1122 |
{'x': teid}) |
|
1123 |
self.assertEquals(rset.rows, [[geid]]) |
|
1787 | 1124 |
|
0 | 1125 |
def test_nonregr_3(self): |
1126 |
"""bad sql generated on the second query (destination_state is not |
|
1127 |
detected as an inlined relation) |
|
1128 |
""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1129 |
rset = self.execute('Any S,ES,T WHERE S state_of ET, ET name "CWUser",' |
0 | 1130 |
'ES allowed_transition T, T destination_state S') |
1131 |
self.assertEquals(len(rset.rows), 2) |
|
1132 |
||
1133 |
def test_nonregr_4(self): |
|
1134 |
# fix variables'type, else we get (nb of entity types with a 'name' attribute)**3 |
|
1135 |
# union queries and that make for instance a 266Ko sql query which is refused |
|
1136 |
# by the server (or client lib) |
|
1137 |
rset = self.execute('Any ER,SE,OE WHERE SE name "Comment", ER name "comments", OE name "Comment",' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1138 |
'ER is CWRType, SE is CWEType, OE is CWEType') |
0 | 1139 |
self.assertEquals(len(rset), 1) |
1140 |
||
1141 |
def test_nonregr_5(self): |
|
1142 |
# jpl #15505: equivalent queries returning different result sets |
|
1143 |
teid1 = self.execute("INSERT Folder X: X name 'hop'")[0][0] |
|
1144 |
teid2 = self.execute("INSERT Folder X: X name 'hip'")[0][0] |
|
1145 |
neid = self.execute("INSERT Note X: X todo_by U, X filed_under T WHERE U login 'admin', T name 'hop'")[0][0] |
|
1146 |
weid = self.execute("INSERT Affaire X: X concerne N, X filed_under T WHERE N is Note, T name 'hip'")[0][0] |
|
1147 |
rset1 = self.execute('Any N,U WHERE N filed_under T, T eid %s,' |
|
1148 |
'N todo_by U, W concerne N,' |
|
1149 |
'W is Affaire, W filed_under A, A eid %s' % (teid1, teid2)) |
|
1150 |
rset2 = self.execute('Any N,U WHERE N filed_under T, T eid %s,' |
|
1151 |
'N todo_by U, W concerne N,' |
|
1152 |
'W filed_under A, A eid %s' % (teid1, teid2)) |
|
1153 |
rset3 = self.execute('Any N,U WHERE N todo_by U, T eid %s,' |
|
1154 |
'N filed_under T, W concerne N,' |
|
1155 |
'W is Affaire, W filed_under A, A eid %s' % (teid1, teid2)) |
|
1156 |
rset4 = self.execute('Any N,U WHERE N todo_by U, T eid %s,' |
|
1157 |
'N filed_under T, W concerne N,' |
|
1158 |
'W filed_under A, A eid %s' % (teid1, teid2)) |
|
1159 |
self.assertEquals(rset1.rows, rset2.rows) |
|
1160 |
self.assertEquals(rset1.rows, rset3.rows) |
|
1161 |
self.assertEquals(rset1.rows, rset4.rows) |
|
1787 | 1162 |
|
0 | 1163 |
def test_nonregr_6(self): |
1164 |
self.execute('Any N,COUNT(S) GROUPBY N ORDERBY COUNT(N) WHERE S name N, S is State') |
|
1787 | 1165 |
|
0 | 1166 |
def test_sqlite_encoding(self): |
1167 |
"""XXX this test was trying to show a bug on use of lower which only |
|
1168 |
occurs with non ascii string and misconfigured locale |
|
1169 |
""" |
|
1170 |
self.execute("INSERT Tag X: X name %(name)s," |
|
1171 |
"X modification_date %(modification_date)s," |
|
1172 |
"X creation_date %(creation_date)s", |
|
1173 |
{'name': u'�name0', |
|
1174 |
'modification_date': '2003/03/12 11:00', |
|
1175 |
'creation_date': '2000/07/03 11:00'}) |
|
1176 |
rset = self.execute('Any lower(N) ORDERBY LOWER(N) WHERE X is Tag, X name N,' |
|
1177 |
'X owned_by U, U eid %(x)s', |
|
1178 |
{'x':self.session.user.eid}, 'x') |
|
1179 |
self.assertEquals(rset.rows, [[u'\xe9name0']]) |
|
1180 |
||
1181 |
||
1182 |
def test_nonregr_description(self): |
|
1183 |
"""check that a correct description is built in case where infered |
|
1184 |
solutions may be "fusionned" into one by the querier while all solutions |
|
1185 |
are needed to build the result's description |
|
1186 |
""" |
|
1187 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
1188 |
self.execute("INSERT Societe Y: Y nom 'toto'") |
|
1189 |
beid = self.execute("INSERT Basket B: B name 'mybasket'")[0][0] |
|
1190 |
self.execute("SET X in_basket B WHERE X is Personne") |
|
1191 |
self.execute("SET X in_basket B WHERE X is Societe") |
|
1192 |
rset = self.execute('Any X WHERE X in_basket B, B eid %s' % beid) |
|
1193 |
self.assertEquals(len(rset), 2) |
|
1194 |
self.assertEquals(rset.description, [('Personne',), ('Societe',)]) |
|
1195 |
||
1196 |
||
1197 |
def test_nonregr_cache_1(self): |
|
1198 |
peid = self.execute("INSERT Personne X: X nom 'bidule'")[0][0] |
|
1199 |
beid = self.execute("INSERT Basket X: X name 'tag'")[0][0] |
|
1200 |
self.execute("SET X in_basket Y WHERE X is Personne, Y eid %(y)s", |
|
1201 |
{'y': beid}) |
|
1202 |
rset = self.execute("Any X WHERE X in_basket B, B eid %(x)s", |
|
1203 |
{'x': beid}) |
|
1204 |
self.assertEquals(rset.rows, [[peid]]) |
|
1205 |
rset = self.execute("Any X WHERE X in_basket B, B eid %(x)s", |
|
1206 |
{'x': beid}) |
|
1207 |
self.assertEquals(rset.rows, [[peid]]) |
|
1208 |
||
1209 |
def test_nonregr_has_text_cache(self): |
|
1210 |
eid1 = self.execute("INSERT Personne X: X nom 'bidule'")[0][0] |
|
1211 |
eid2 = self.execute("INSERT Personne X: X nom 'tag'")[0][0] |
|
1212 |
self.commit() |
|
1213 |
rset = self.execute("Any X WHERE X has_text %(text)s", {'text': 'bidule'}) |
|
1214 |
self.assertEquals(rset.rows, [[eid1]]) |
|
1215 |
rset = self.execute("Any X WHERE X has_text %(text)s", {'text': 'tag'}) |
|
1216 |
self.assertEquals(rset.rows, [[eid2]]) |
|
1217 |
||
1218 |
def test_nonregr_sortterm_management(self): |
|
1219 |
"""Error: Variable has no attribute 'sql' in rql2sql.py (visit_variable) |
|
1220 |
||
1221 |
cause: old variable ref inserted into a fresh rqlst copy |
|
1222 |
(in RQLSpliter._complex_select_plan) |
|
1950
ab7a41b2cd26
reactivate skipped tests due to sqlite bug which is now fixed (sqlite 3.6.13)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1949
diff
changeset
|
1223 |
|
ab7a41b2cd26
reactivate skipped tests due to sqlite bug which is now fixed (sqlite 3.6.13)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1949
diff
changeset
|
1224 |
need sqlite including http://www.sqlite.org/cvstrac/tktview?tn=3773 fix |
0 | 1225 |
""" |
1226 |
self.execute('Any X ORDERBY D DESC WHERE X creation_date D') |
|
1787 | 1227 |
|
0 | 1228 |
def test_nonregr_extra_joins(self): |
1229 |
ueid = self.session.user.eid |
|
1230 |
teid1 = self.execute("INSERT Folder X: X name 'folder1'")[0][0] |
|
1231 |
teid2 = self.execute("INSERT Folder X: X name 'folder2'")[0][0] |
|
1232 |
neid1 = self.execute("INSERT Note X: X para 'note1'")[0][0] |
|
1233 |
neid2 = self.execute("INSERT Note X: X para 'note2'")[0][0] |
|
1234 |
self.execute("SET X filed_under Y WHERE X eid %s, Y eid %s" |
|
1235 |
% (neid1, teid1)) |
|
1236 |
self.execute("SET X filed_under Y WHERE X eid %s, Y eid %s" |
|
1237 |
% (neid2, teid2)) |
|
1238 |
self.execute("SET X todo_by Y WHERE X is Note, Y eid %s" % ueid) |
|
1239 |
rset = self.execute('Any N WHERE N todo_by U, N is Note, U eid %s, N filed_under T, T eid %s' |
|
1240 |
% (ueid, teid1)) |
|
1241 |
self.assertEquals(len(rset), 1) |
|
1242 |
||
1243 |
def test_nonregr_XXX(self): |
|
1244 |
teid = self.execute('Transition S WHERE S name "deactivate"')[0][0] |
|
1245 |
rset = self.execute('Any O WHERE O is State, ' |
|
1246 |
'S eid %(x)s, S transition_of ET, O state_of ET', {'x': teid}) |
|
1247 |
self.assertEquals(len(rset), 2) |
|
1248 |
rset = self.execute('Any O WHERE O is State, NOT S destination_state O, ' |
|
1249 |
'S eid %(x)s, S transition_of ET, O state_of ET', {'x': teid}) |
|
1250 |
self.assertEquals(len(rset), 1) |
|
1251 |
||
1252 |
||
1253 |
def test_nonregr_set_datetime(self): |
|
1254 |
# huum, psycopg specific |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
439
diff
changeset
|
1255 |
self.execute('SET X creation_date %(date)s WHERE X eid 1', {'date': date.today()}) |
0 | 1256 |
|
1257 |
def test_nonregr_set_query(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1258 |
ueid = self.execute("INSERT CWUser X: X login 'bob', X upassword 'toto'")[0][0] |
0 | 1259 |
self.execute("SET E in_group G, E in_state S, " |
1260 |
"E firstname %(firstname)s, E surname %(surname)s " |
|
1261 |
"WHERE E eid %(x)s, G name 'users', S name 'activated'", |
|
1262 |
{'x':ueid, 'firstname': u'jean', 'surname': u'paul'}, 'x') |
|
1787 | 1263 |
|
0 | 1264 |
def test_nonregr_u_owned_by_u(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1265 |
ueid = self.execute("INSERT CWUser X: X login 'bob', X upassword 'toto', X in_group G " |
0 | 1266 |
"WHERE G name 'users'")[0][0] |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1267 |
rset = self.execute("CWUser U") |
0 | 1268 |
self.assertEquals(len(rset), 3) # bob + admin + anon |
1269 |
rset = self.execute("Any U WHERE NOT U owned_by U") |
|
1270 |
self.assertEquals(len(rset), 0) # even admin created at repo initialization time should belong to itself |
|
1271 |
||
1272 |
def test_nonreg_update_index(self): |
|
1273 |
# this is the kind of queries generated by "cubicweb-ctl db-check -ry" |
|
1274 |
self.execute("SET X description D WHERE X is State, X description D") |
|
1275 |
||
1276 |
def test_nonregr_is(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1277 |
uteid = self.execute('Any ET WHERE ET name "CWUser"')[0][0] |
0 | 1278 |
self.execute('Any X, ET WHERE X is ET, ET eid %s' % uteid) |
1279 |
||
1280 |
def test_nonregr_orderby(self): |
|
1281 |
seid = self.execute('Any X WHERE X name "activated"')[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1282 |
self.execute('Any X,S, MAX(T) GROUPBY X,S ORDERBY S WHERE X is CWUser, T tags X, S eid IN(%s), X in_state S' % seid) |
0 | 1283 |
|
1284 |
def test_nonregr_solution_cache(self): |
|
1285 |
self.skip('XXX should be fixed or documented') # (doesn't occur if cache key is provided.) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1286 |
rset = self.execute('Any X WHERE X is CWUser, X eid %(x)s', {'x':self.ueid}) |
0 | 1287 |
self.assertEquals(len(rset), 1) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1288 |
rset = self.execute('Any X WHERE X is CWUser, X eid %(x)s', {'x':12345}) |
0 | 1289 |
self.assertEquals(len(rset), 0) |
1290 |
||
1291 |
def test_nonregr_final_norestr(self): |
|
1292 |
self.assertRaises(BadRQLQuery, self.execute, 'Date X') |
|
1293 |
||
1294 |
||
1295 |
if __name__ == '__main__': |
|
1296 |
unittest_main() |