author | sylvain.thenault@logilab.fr |
Wed, 13 May 2009 16:59:50 +0200 | |
branch | tls-sprint |
changeset 1787 | 71c143c0ada3 |
parent 1398 | 5fe84a5f7035 |
child 1880 | 293fe4b49e28 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
2 |
"""unit tests for module cubicweb.server.repository""" |
|
3 |
||
4 |
import os |
|
5 |
import sys |
|
6 |
import threading |
|
7 |
import time |
|
8 |
from copy import deepcopy |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
9 |
from datetime import datetime |
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:
17
diff
changeset
|
10 |
|
0 | 11 |
from logilab.common.testlib import TestCase, unittest_main |
12 |
||
13 |
from yams.constraints import UniqueConstraint |
|
14 |
||
15 |
from cubicweb import BadConnectionId, RepositoryError, ValidationError, UnknownEid, AuthenticationError |
|
16 |
from cubicweb.schema import CubicWebSchema, RQLConstraint |
|
17 |
from cubicweb.dbapi import connect, repo_connect |
|
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:
17
diff
changeset
|
18 |
from cubicweb.devtools.apptest import RepositoryBasedTC |
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:
17
diff
changeset
|
19 |
from cubicweb.devtools.repotest import tuplify |
1787 | 20 |
from cubicweb.server import repository |
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:
17
diff
changeset
|
21 |
from cubicweb.server.sqlutils import SQL_PREFIX |
0 | 22 |
|
23 |
||
24 |
# start name server anyway, process will fail if already running |
|
25 |
os.system('pyro-ns >/dev/null 2>/dev/null &') |
|
26 |
||
27 |
||
28 |
class RepositoryTC(RepositoryBasedTC): |
|
29 |
""" singleton providing access to a persistent storage for entities |
|
30 |
and relation |
|
31 |
""" |
|
1787 | 32 |
|
0 | 33 |
# def setUp(self): |
34 |
# pass |
|
1787 | 35 |
|
0 | 36 |
# def tearDown(self): |
37 |
# self.repo.config.db_perms = True |
|
38 |
# cnxid = self.repo.connect(*self.default_user_password()) |
|
39 |
# for etype in ('Affaire', 'Note', 'Societe', 'Personne'): |
|
40 |
# self.repo.execute(cnxid, 'DELETE %s X' % etype) |
|
41 |
# self.repo.commit(cnxid) |
|
42 |
# self.repo.close(cnxid) |
|
43 |
||
44 |
def test_fill_schema(self): |
|
45 |
self.repo.schema = CubicWebSchema(self.repo.config.appid) |
|
46 |
self.repo.config._cubes = None # avoid assertion error |
|
47 |
self.repo.fill_schema() |
|
48 |
pool = self.repo._get_pool() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
49 |
table = SQL_PREFIX + 'CWEType' |
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:
17
diff
changeset
|
50 |
namecol = SQL_PREFIX + 'name' |
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:
17
diff
changeset
|
51 |
finalcol = SQL_PREFIX + 'final' |
0 | 52 |
try: |
53 |
sqlcursor = pool['system'] |
|
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:
17
diff
changeset
|
54 |
sqlcursor.execute('SELECT %s FROM %s WHERE %s is NULL' % ( |
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:
17
diff
changeset
|
55 |
namecol, table, finalcol)) |
0 | 56 |
self.assertEquals(sqlcursor.fetchall(), []) |
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:
17
diff
changeset
|
57 |
sqlcursor.execute('SELECT %s FROM %s WHERE %s=%%(final)s ORDER BY %s' |
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:
17
diff
changeset
|
58 |
% (namecol, table, finalcol, namecol), {'final': 'TRUE'}) |
0 | 59 |
self.assertEquals(sqlcursor.fetchall(), [(u'Boolean',), (u'Bytes',), |
60 |
(u'Date',), (u'Datetime',), |
|
61 |
(u'Decimal',),(u'Float',), |
|
62 |
(u'Int',), |
|
63 |
(u'Interval',), (u'Password',), |
|
64 |
(u'String',), (u'Time',)]) |
|
65 |
finally: |
|
66 |
self.repo._free_pool(pool) |
|
1787 | 67 |
|
0 | 68 |
def test_schema_has_owner(self): |
69 |
repo = self.repo |
|
70 |
cnxid = repo.connect(*self.default_user_password()) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
71 |
self.failIf(repo.execute(cnxid, 'CWEType X WHERE NOT X owned_by U')) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
72 |
self.failIf(repo.execute(cnxid, 'CWRType X WHERE NOT X owned_by U')) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
73 |
self.failIf(repo.execute(cnxid, 'CWAttribute X WHERE NOT X owned_by U')) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
74 |
self.failIf(repo.execute(cnxid, 'CWRelation X WHERE NOT X owned_by U')) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
75 |
self.failIf(repo.execute(cnxid, 'CWConstraint X WHERE NOT X owned_by U')) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
76 |
self.failIf(repo.execute(cnxid, 'CWConstraintType X WHERE NOT X owned_by U')) |
1787 | 77 |
|
0 | 78 |
def test_connect(self): |
79 |
login, passwd = self.default_user_password() |
|
80 |
self.assert_(self.repo.connect(login, passwd)) |
|
81 |
self.assertRaises(AuthenticationError, |
|
82 |
self.repo.connect, login, 'nimportnawak') |
|
83 |
self.assertRaises(AuthenticationError, |
|
84 |
self.repo.connect, login, None) |
|
85 |
self.assertRaises(AuthenticationError, |
|
86 |
self.repo.connect, None, None) |
|
1787 | 87 |
|
0 | 88 |
def test_execute(self): |
89 |
repo = self.repo |
|
90 |
cnxid = repo.connect(*self.default_user_password()) |
|
91 |
repo.execute(cnxid, 'Any X') |
|
92 |
repo.execute(cnxid, 'Any X where X is Personne') |
|
93 |
repo.execute(cnxid, 'Any X where X is Personne, X nom ~= "to"') |
|
94 |
repo.execute(cnxid, 'Any X WHERE X has_text %(text)s', {'text': u'\xe7a'}) |
|
95 |
repo.close(cnxid) |
|
1787 | 96 |
|
0 | 97 |
def test_login_upassword_accent(self): |
98 |
repo = self.repo |
|
99 |
cnxid = repo.connect(*self.default_user_password()) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
100 |
repo.execute(cnxid, 'INSERT CWUser X: X login %(login)s, X upassword %(passwd)s, X in_state S, X in_group G WHERE S name "activated", G name "users"', |
0 | 101 |
{'login': u"barnab�", 'passwd': u"h�h�h�".encode('UTF8')}) |
102 |
repo.commit(cnxid) |
|
103 |
repo.close(cnxid) |
|
104 |
self.assert_(repo.connect(u"barnab�", u"h�h�h�".encode('UTF8'))) |
|
1787 | 105 |
|
0 | 106 |
def test_invalid_entity_rollback(self): |
107 |
repo = self.repo |
|
108 |
cnxid = repo.connect(*self.default_user_password()) |
|
1787 | 109 |
# no group |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
110 |
repo.execute(cnxid, 'INSERT CWUser X: X login %(login)s, X upassword %(passwd)s, X in_state S WHERE S name "activated"', |
0 | 111 |
{'login': u"tutetute", 'passwd': 'tutetute'}) |
112 |
self.assertRaises(ValidationError, repo.commit, cnxid) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
113 |
rset = repo.execute(cnxid, 'CWUser X WHERE X login "tutetute"') |
0 | 114 |
self.assertEquals(rset.rowcount, 0) |
1787 | 115 |
|
0 | 116 |
def test_close(self): |
117 |
repo = self.repo |
|
118 |
cnxid = repo.connect(*self.default_user_password()) |
|
119 |
self.assert_(cnxid) |
|
120 |
repo.close(cnxid) |
|
121 |
self.assertRaises(BadConnectionId, repo.execute, cnxid, 'Any X') |
|
1787 | 122 |
|
0 | 123 |
def test_invalid_cnxid(self): |
124 |
self.assertRaises(BadConnectionId, self.repo.execute, 0, 'Any X') |
|
125 |
self.assertRaises(BadConnectionId, self.repo.close, None) |
|
1787 | 126 |
|
0 | 127 |
def test_shared_data(self): |
128 |
repo = self.repo |
|
129 |
cnxid = repo.connect(*self.default_user_password()) |
|
130 |
repo.set_shared_data(cnxid, 'data', 4) |
|
131 |
cnxid2 = repo.connect(*self.default_user_password()) |
|
132 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), 4) |
|
133 |
self.assertEquals(repo.get_shared_data(cnxid2, 'data'), None) |
|
134 |
repo.set_shared_data(cnxid2, 'data', 5) |
|
135 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), 4) |
|
136 |
self.assertEquals(repo.get_shared_data(cnxid2, 'data'), 5) |
|
137 |
repo.get_shared_data(cnxid2, 'data', pop=True) |
|
138 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), 4) |
|
139 |
self.assertEquals(repo.get_shared_data(cnxid2, 'data'), None) |
|
140 |
repo.close(cnxid) |
|
141 |
repo.close(cnxid2) |
|
142 |
self.assertRaises(BadConnectionId, repo.get_shared_data, cnxid, 'data') |
|
143 |
self.assertRaises(BadConnectionId, repo.get_shared_data, cnxid2, 'data') |
|
144 |
self.assertRaises(BadConnectionId, repo.set_shared_data, cnxid, 'data', 1) |
|
145 |
self.assertRaises(BadConnectionId, repo.set_shared_data, cnxid2, 'data', 1) |
|
146 |
||
147 |
def test_check_session(self): |
|
148 |
repo = self.repo |
|
149 |
cnxid = repo.connect(*self.default_user_password()) |
|
150 |
self.assertEquals(repo.check_session(cnxid), None) |
|
151 |
repo.close(cnxid) |
|
152 |
self.assertRaises(BadConnectionId, repo.check_session, cnxid) |
|
153 |
||
154 |
def test_transaction_base(self): |
|
155 |
repo = self.repo |
|
156 |
cnxid = repo.connect(*self.default_user_password()) |
|
157 |
# check db state |
|
158 |
result = repo.execute(cnxid, 'Personne X') |
|
159 |
self.assertEquals(result.rowcount, 0) |
|
160 |
# rollback entity insertion |
|
161 |
repo.execute(cnxid, "INSERT Personne X: X nom 'bidule'") |
|
162 |
result = repo.execute(cnxid, 'Personne X') |
|
163 |
self.assertEquals(result.rowcount, 1) |
|
164 |
repo.rollback(cnxid) |
|
165 |
result = repo.execute(cnxid, 'Personne X') |
|
166 |
self.assertEquals(result.rowcount, 0, result.rows) |
|
167 |
# commit |
|
168 |
repo.execute(cnxid, "INSERT Personne X: X nom 'bidule'") |
|
169 |
repo.commit(cnxid) |
|
170 |
result = repo.execute(cnxid, 'Personne X') |
|
171 |
self.assertEquals(result.rowcount, 1) |
|
172 |
||
173 |
def test_transaction_base2(self): |
|
174 |
repo = self.repo |
|
175 |
cnxid = repo.connect(*self.default_user_password()) |
|
176 |
# rollback relation insertion |
|
177 |
repo.execute(cnxid, "SET U in_group G WHERE U login 'admin', G name 'guests'") |
|
178 |
result = repo.execute(cnxid, "Any U WHERE U in_group G, U login 'admin', G name 'guests'") |
|
179 |
self.assertEquals(result.rowcount, 1) |
|
180 |
repo.rollback(cnxid) |
|
181 |
result = repo.execute(cnxid, "Any U WHERE U in_group G, U login 'admin', G name 'guests'") |
|
182 |
self.assertEquals(result.rowcount, 0, result.rows) |
|
1787 | 183 |
|
0 | 184 |
def test_transaction_base3(self): |
185 |
repo = self.repo |
|
186 |
cnxid = repo.connect(*self.default_user_password()) |
|
187 |
# rollback state change which trigger TrInfo insertion |
|
188 |
ueid = repo._get_session(cnxid).user.eid |
|
189 |
rset = repo.execute(cnxid, 'TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
190 |
self.assertEquals(len(rset), 1) |
|
191 |
repo.execute(cnxid, 'SET X in_state S WHERE X eid %(x)s, S name "deactivated"', |
|
192 |
{'x': ueid}, 'x') |
|
193 |
rset = repo.execute(cnxid, 'TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
194 |
self.assertEquals(len(rset), 2) |
|
195 |
repo.rollback(cnxid) |
|
196 |
rset = repo.execute(cnxid, 'TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
197 |
self.assertEquals(len(rset), 1) |
|
1787 | 198 |
|
0 | 199 |
def test_transaction_interleaved(self): |
200 |
self.skip('implement me') |
|
201 |
||
202 |
def test_initial_schema(self): |
|
203 |
schema = self.repo.schema |
|
204 |
# check order of attributes is respected |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
205 |
self.assertListEquals([r.type for r in schema.eschema('CWAttribute').ordered_relations() |
1787 | 206 |
if not r.type in ('eid', 'is', 'is_instance_of', 'identity', |
0 | 207 |
'creation_date', 'modification_date', |
208 |
'owned_by', 'created_by')], |
|
209 |
['relation_type', 'from_entity', 'to_entity', 'constrained_by', |
|
1787 | 210 |
'cardinality', 'ordernum', |
0 | 211 |
'indexed', 'fulltextindexed', 'internationalizable', |
212 |
'defaultval', 'description_format', 'description']) |
|
213 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
214 |
self.assertEquals(schema.eschema('CWEType').main_attribute(), 'name') |
0 | 215 |
self.assertEquals(schema.eschema('State').main_attribute(), 'name') |
216 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
217 |
constraints = schema.rschema('name').rproperty('CWEType', 'String', 'constraints') |
0 | 218 |
self.assertEquals(len(constraints), 2) |
219 |
for cstr in constraints[:]: |
|
220 |
if isinstance(cstr, UniqueConstraint): |
|
221 |
constraints.remove(cstr) |
|
222 |
break |
|
223 |
else: |
|
224 |
self.fail('unique constraint not found') |
|
225 |
sizeconstraint = constraints[0] |
|
226 |
self.assertEquals(sizeconstraint.min, None) |
|
227 |
self.assertEquals(sizeconstraint.max, 64) |
|
228 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
229 |
constraints = schema.rschema('relation_type').rproperty('CWAttribute', 'CWRType', 'constraints') |
0 | 230 |
self.assertEquals(len(constraints), 1) |
231 |
cstr = constraints[0] |
|
232 |
self.assert_(isinstance(cstr, RQLConstraint)) |
|
233 |
self.assertEquals(cstr.restriction, 'O final TRUE') |
|
234 |
||
235 |
ownedby = schema.rschema('owned_by') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
236 |
self.assertEquals(ownedby.objects('CWEType'), ('CWUser',)) |
0 | 237 |
|
238 |
def test_pyro(self): |
|
239 |
import Pyro |
|
240 |
Pyro.config.PYRO_MULTITHREADED = 0 |
|
241 |
lock = threading.Lock() |
|
242 |
# the client part has to be in the thread due to sqlite limitations |
|
243 |
t = threading.Thread(target=self._pyro_client, args=(lock,)) |
|
244 |
try: |
|
245 |
daemon = self.repo.pyro_register() |
|
246 |
t.start() |
|
247 |
# connection |
|
248 |
daemon.handleRequests(1.0) |
|
249 |
daemon.handleRequests(1.0) |
|
250 |
daemon.handleRequests(1.0) |
|
251 |
# get schema |
|
252 |
daemon.handleRequests(1.0) |
|
253 |
# execute |
|
254 |
daemon.handleRequests(1.0) |
|
255 |
t.join() |
|
256 |
finally: |
|
257 |
repository.pyro_unregister(self.repo.config) |
|
1787 | 258 |
|
0 | 259 |
def _pyro_client(self, lock): |
260 |
cnx = connect(self.repo.config.appid, u'admin', 'gingkow') |
|
261 |
# check we can get the schema |
|
262 |
schema = cnx.get_schema() |
|
263 |
self.assertEquals(schema.__hashmode__, None) |
|
264 |
rset = cnx.cursor().execute('Any U,G WHERE U in_group G') |
|
1787 | 265 |
|
0 | 266 |
|
267 |
def test_internal_api(self): |
|
268 |
repo = self.repo |
|
269 |
cnxid = repo.connect(*self.default_user_password()) |
|
270 |
session = repo._get_session(cnxid, setpool=True) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
271 |
self.assertEquals(repo.type_and_source_from_eid(1, session), ('CWGroup', 'system', None)) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
272 |
self.assertEquals(repo.type_from_eid(1, session), 'CWGroup') |
0 | 273 |
self.assertEquals(repo.source_from_eid(1, session).uri, 'system') |
274 |
self.assertEquals(repo.eid2extid(repo.system_source, 1, session), None) |
|
275 |
class dummysource: uri = 'toto' |
|
276 |
self.assertRaises(UnknownEid, repo.eid2extid, dummysource, 1, session) |
|
277 |
||
278 |
def test_public_api(self): |
|
279 |
self.assertEquals(self.repo.get_schema(), self.repo.schema) |
|
280 |
self.assertEquals(self.repo.source_defs(), {'system': {'adapter': 'native', 'uri': 'system'}}) |
|
281 |
# .properties() return a result set |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
282 |
self.assertEquals(self.repo.properties().rql, 'Any K,V WHERE P is CWProperty,P pkey K, P value V, NOT P for_user U') |
0 | 283 |
|
284 |
def test_session_api(self): |
|
285 |
repo = self.repo |
|
286 |
cnxid = repo.connect(*self.default_user_password()) |
|
287 |
self.assertEquals(repo.user_info(cnxid), (5, 'admin', set([u'managers']), {})) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
288 |
self.assertEquals(repo.describe(cnxid, 1), (u'CWGroup', u'system', None)) |
0 | 289 |
repo.close(cnxid) |
290 |
self.assertRaises(BadConnectionId, repo.user_info, cnxid) |
|
291 |
self.assertRaises(BadConnectionId, repo.describe, cnxid, 1) |
|
292 |
||
293 |
def test_shared_data_api(self): |
|
294 |
repo = self.repo |
|
295 |
cnxid = repo.connect(*self.default_user_password()) |
|
296 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), None) |
|
297 |
repo.set_shared_data(cnxid, 'data', 4) |
|
298 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), 4) |
|
299 |
repo.get_shared_data(cnxid, 'data', pop=True) |
|
300 |
repo.get_shared_data(cnxid, 'whatever', pop=True) |
|
301 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), None) |
|
302 |
repo.close(cnxid) |
|
303 |
self.assertRaises(BadConnectionId, repo.set_shared_data, cnxid, 'data', 0) |
|
304 |
self.assertRaises(BadConnectionId, repo.get_shared_data, cnxid, 'data') |
|
1787 | 305 |
|
0 | 306 |
|
307 |
class DataHelpersTC(RepositoryBasedTC): |
|
1787 | 308 |
|
0 | 309 |
def setUp(self): |
310 |
""" called before each test from this class """ |
|
311 |
cnxid = self.repo.connect(*self.default_user_password()) |
|
312 |
self.session = self.repo._sessions[cnxid] |
|
313 |
self.session.set_pool() |
|
314 |
||
315 |
def tearDown(self): |
|
316 |
self.session.rollback() |
|
1787 | 317 |
|
0 | 318 |
def test_create_eid(self): |
319 |
self.assert_(self.repo.system_source.create_eid(self.session)) |
|
320 |
||
321 |
def test_source_from_eid(self): |
|
322 |
self.assertEquals(self.repo.source_from_eid(1, self.session), |
|
323 |
self.repo.sources_by_uri['system']) |
|
324 |
||
325 |
def test_source_from_eid_raise(self): |
|
326 |
self.assertRaises(UnknownEid, self.repo.source_from_eid, -2, self.session) |
|
327 |
||
328 |
def test_type_from_eid(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
329 |
self.assertEquals(self.repo.type_from_eid(1, self.session), 'CWGroup') |
1787 | 330 |
|
0 | 331 |
def test_type_from_eid_raise(self): |
332 |
self.assertRaises(UnknownEid, self.repo.type_from_eid, -2, self.session) |
|
1787 | 333 |
|
0 | 334 |
def test_add_delete_info(self): |
335 |
entity = self.repo.vreg.etype_class('Personne')(self.session, None, None) |
|
336 |
entity.eid = -1 |
|
337 |
entity.complete = lambda x: None |
|
338 |
self.repo.add_info(self.session, entity, self.repo.sources_by_uri['system']) |
|
339 |
cursor = self.session.pool['system'] |
|
340 |
cursor.execute('SELECT * FROM entities WHERE eid = -1') |
|
341 |
data = cursor.fetchall() |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
342 |
self.assertIsInstance(data[0][3], datetime) |
0 | 343 |
data[0] = list(data[0]) |
344 |
data[0][3] = None |
|
345 |
self.assertEquals(tuplify(data), [(-1, 'Personne', 'system', None, None)]) |
|
346 |
self.repo.delete_info(self.session, -1) |
|
347 |
#self.repo.commit() |
|
348 |
cursor.execute('SELECT * FROM entities WHERE eid = -1') |
|
349 |
data = cursor.fetchall() |
|
350 |
self.assertEquals(data, []) |
|
351 |
||
352 |
||
353 |
class FTITC(RepositoryBasedTC): |
|
1787 | 354 |
|
0 | 355 |
def test_reindex_and_modified_since(self): |
356 |
cursor = self.session.pool['system'] |
|
357 |
eidp = self.execute('INSERT Personne X: X nom "toto", X prenom "tutu"')[0][0] |
|
358 |
self.commit() |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
359 |
ts = datetime.now() |
0 | 360 |
self.assertEquals(len(self.execute('Personne X WHERE X has_text "tutu"')), 1) |
361 |
cursor.execute('SELECT mtime, eid FROM entities WHERE eid = %s' % eidp) |
|
362 |
omtime = cursor.fetchone()[0] |
|
363 |
# our sqlite datetime adapter is ignore seconds fraction, so we have to |
|
364 |
# ensure update is done the next seconds |
|
365 |
time.sleep(1 - (ts.second - int(ts.second))) |
|
366 |
self.execute('SET X nom "tata" WHERE X eid %(x)s', {'x': eidp}, 'x') |
|
367 |
self.commit() |
|
368 |
self.assertEquals(len(self.execute('Personne X WHERE X has_text "tutu"')), 1) |
|
369 |
cursor.execute('SELECT mtime FROM entities WHERE eid = %s' % eidp) |
|
370 |
mtime = cursor.fetchone()[0] |
|
371 |
self.failUnless(omtime < mtime) |
|
372 |
self.commit() |
|
373 |
date, modified, deleted = self.repo.entities_modified_since(('Personne',), omtime) |
|
374 |
self.assertEquals(modified, [('Personne', eidp)]) |
|
375 |
self.assertEquals(deleted, []) |
|
376 |
date, modified, deleted = self.repo.entities_modified_since(('Personne',), mtime) |
|
377 |
self.assertEquals(modified, []) |
|
378 |
self.assertEquals(deleted, []) |
|
379 |
self.execute('DELETE Personne X WHERE X eid %(x)s', {'x': eidp}) |
|
380 |
self.commit() |
|
381 |
date, modified, deleted = self.repo.entities_modified_since(('Personne',), omtime) |
|
382 |
self.assertEquals(modified, []) |
|
383 |
self.assertEquals(deleted, [('Personne', eidp)]) |
|
384 |
||
385 |
def test_composite_entity(self): |
|
386 |
assert self.schema.rschema('use_email').fulltext_container == 'subject' |
|
387 |
eid = self.add_entity('EmailAddress', address=u'toto@logilab.fr').eid |
|
388 |
self.commit() |
|
389 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'}) |
|
390 |
self.assertEquals(rset.rows, [[eid]]) |
|
391 |
self.execute('SET X use_email Y WHERE X login "admin", Y eid %(y)s', {'y': eid}) |
|
392 |
self.commit() |
|
393 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'}) |
|
394 |
self.assertEquals(rset.rows, [[self.session.user.eid]]) |
|
395 |
self.execute('DELETE X use_email Y WHERE X login "admin", Y eid %(y)s', {'y': eid}) |
|
396 |
self.commit() |
|
397 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'}) |
|
398 |
self.assertEquals(rset.rows, []) |
|
399 |
eid = self.add_entity('EmailAddress', address=u'tutu@logilab.fr').eid |
|
400 |
self.execute('SET X use_email Y WHERE X login "admin", Y eid %(y)s', {'y': eid}) |
|
401 |
self.commit() |
|
402 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'tutu'}) |
|
403 |
self.assertEquals(rset.rows, [[self.session.user.eid]]) |
|
1787 | 404 |
|
405 |
||
0 | 406 |
class DBInitTC(RepositoryBasedTC): |
1787 | 407 |
|
0 | 408 |
def test_versions_inserted(self): |
409 |
inserted = [r[0] for r in self.execute('Any K ORDERBY K WHERE P pkey K, P pkey ~= "system.version.%"')] |
|
410 |
self.assertEquals(inserted, |
|
1787 | 411 |
[u'system.version.basket', u'system.version.card', u'system.version.comment', |
412 |
u'system.version.cubicweb', u'system.version.email', |
|
413 |
u'system.version.file', u'system.version.folder', |
|
17
62ce3e6126e0
work in progress: fix cubicweb/server tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
414 |
u'system.version.tag']) |
1787 | 415 |
|
0 | 416 |
class InlineRelHooksTC(RepositoryBasedTC): |
417 |
"""test relation hooks are called for inlined relations |
|
418 |
""" |
|
419 |
def setUp(self): |
|
420 |
RepositoryBasedTC.setUp(self) |
|
421 |
self.hm = self.repo.hm |
|
422 |
self.called = [] |
|
1787 | 423 |
|
0 | 424 |
def _before_relation_hook(self, pool, fromeid, rtype, toeid): |
425 |
self.called.append((fromeid, rtype, toeid)) |
|
426 |
||
427 |
def _after_relation_hook(self, pool, fromeid, rtype, toeid): |
|
428 |
self.called.append((fromeid, rtype, toeid)) |
|
1787 | 429 |
|
0 | 430 |
def test_before_add_inline_relation(self): |
431 |
"""make sure before_<event>_relation hooks are called directly""" |
|
432 |
self.hm.register_hook(self._before_relation_hook, |
|
433 |
'before_add_relation', 'ecrit_par') |
|
434 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
435 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
436 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
437 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp)]) |
|
1787 | 438 |
|
0 | 439 |
def test_after_add_inline_relation(self): |
440 |
"""make sure after_<event>_relation hooks are deferred""" |
|
441 |
self.hm.register_hook(self._after_relation_hook, |
|
442 |
'after_add_relation', 'ecrit_par') |
|
443 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
444 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
445 |
self.assertEquals(self.called, []) |
|
446 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
447 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp,)]) |
|
1787 | 448 |
|
0 | 449 |
def test_after_add_inline(self): |
450 |
"""make sure after_<event>_relation hooks are deferred""" |
|
451 |
self.hm.register_hook(self._after_relation_hook, |
|
452 |
'after_add_relation', 'in_state') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
453 |
eidp = self.execute('INSERT CWUser X: X login "toto", X upassword "tutu", X in_state S WHERE S name "activated"')[0][0] |
0 | 454 |
eids = self.execute('State X WHERE X name "activated"')[0][0] |
455 |
self.assertEquals(self.called, [(eidp, 'in_state', eids,)]) |
|
1787 | 456 |
|
0 | 457 |
def test_before_delete_inline_relation(self): |
458 |
"""make sure before_<event>_relation hooks are called directly""" |
|
459 |
self.hm.register_hook(self._before_relation_hook, |
|
460 |
'before_delete_relation', 'ecrit_par') |
|
461 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
462 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
463 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
464 |
self.execute('DELETE N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
465 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp)]) |
|
466 |
rset = self.execute('Any Y where N ecrit_par Y, N type "T", Y nom "toto"') |
|
467 |
# make sure the relation is really deleted |
|
468 |
self.failUnless(len(rset) == 0, "failed to delete inline relation") |
|
469 |
||
470 |
def test_after_delete_inline_relation(self): |
|
471 |
"""make sure after_<event>_relation hooks are deferred""" |
|
472 |
self.hm.register_hook(self._after_relation_hook, |
|
473 |
'after_delete_relation', 'ecrit_par') |
|
474 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
475 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
476 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
477 |
self.assertEquals(self.called, []) |
|
478 |
self.execute('DELETE N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
479 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp,)]) |
|
480 |
||
1787 | 481 |
|
0 | 482 |
if __name__ == '__main__': |
483 |
unittest_main() |