author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 May 2009 09:31:44 +0200 | |
changeset 1938 | bf27b4190739 |
parent 1880 | 293fe4b49e28 |
child 1954 | 9b20f3504af8 |
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 |
||
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
202 |
def test_close_wait_processing_request(self): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
203 |
repo = self.repo |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
204 |
cnxid = repo.connect(*self.default_user_password()) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
205 |
repo.execute(cnxid, 'INSERT CWUser X: X login "toto", X upassword "tutu", X in_group G WHERE G name "users"') |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
206 |
repo.commit(cnxid) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
207 |
# close has to be in the thread due to sqlite limitations |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
208 |
def close_in_a_few_moment(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
209 |
time.sleep(0.1) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
210 |
repo.close(cnxid) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
211 |
t = threading.Thread(target=close_in_a_few_moment) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
212 |
t.start() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
213 |
try: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
214 |
print 'execute' |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
215 |
repo.execute(cnxid, 'DELETE CWUser X WHERE X login "toto"') |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
216 |
print 'commit' |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
217 |
repo.commit(cnxid) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
218 |
print 'commited' |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
219 |
finally: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
220 |
t.join() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
221 |
|
0 | 222 |
def test_initial_schema(self): |
223 |
schema = self.repo.schema |
|
224 |
# 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
|
225 |
self.assertListEquals([r.type for r in schema.eschema('CWAttribute').ordered_relations() |
1787 | 226 |
if not r.type in ('eid', 'is', 'is_instance_of', 'identity', |
0 | 227 |
'creation_date', 'modification_date', |
228 |
'owned_by', 'created_by')], |
|
229 |
['relation_type', 'from_entity', 'to_entity', 'constrained_by', |
|
1787 | 230 |
'cardinality', 'ordernum', |
0 | 231 |
'indexed', 'fulltextindexed', 'internationalizable', |
232 |
'defaultval', 'description_format', 'description']) |
|
233 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
234 |
self.assertEquals(schema.eschema('CWEType').main_attribute(), 'name') |
0 | 235 |
self.assertEquals(schema.eschema('State').main_attribute(), 'name') |
236 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
237 |
constraints = schema.rschema('name').rproperty('CWEType', 'String', 'constraints') |
0 | 238 |
self.assertEquals(len(constraints), 2) |
239 |
for cstr in constraints[:]: |
|
240 |
if isinstance(cstr, UniqueConstraint): |
|
241 |
constraints.remove(cstr) |
|
242 |
break |
|
243 |
else: |
|
244 |
self.fail('unique constraint not found') |
|
245 |
sizeconstraint = constraints[0] |
|
246 |
self.assertEquals(sizeconstraint.min, None) |
|
247 |
self.assertEquals(sizeconstraint.max, 64) |
|
248 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
249 |
constraints = schema.rschema('relation_type').rproperty('CWAttribute', 'CWRType', 'constraints') |
0 | 250 |
self.assertEquals(len(constraints), 1) |
251 |
cstr = constraints[0] |
|
252 |
self.assert_(isinstance(cstr, RQLConstraint)) |
|
253 |
self.assertEquals(cstr.restriction, 'O final TRUE') |
|
254 |
||
255 |
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
|
256 |
self.assertEquals(ownedby.objects('CWEType'), ('CWUser',)) |
0 | 257 |
|
258 |
def test_pyro(self): |
|
259 |
import Pyro |
|
260 |
Pyro.config.PYRO_MULTITHREADED = 0 |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
261 |
done = [] |
0 | 262 |
# the client part has to be in the thread due to sqlite limitations |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
263 |
t = threading.Thread(target=self._pyro_client, args=(done,)) |
0 | 264 |
try: |
265 |
daemon = self.repo.pyro_register() |
|
266 |
t.start() |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
267 |
while not done: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
268 |
daemon.handleRequests(1.0) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
269 |
t.join(1) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
270 |
if t.isAlive(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
271 |
self.fail('something went wrong, thread still alive') |
0 | 272 |
finally: |
273 |
repository.pyro_unregister(self.repo.config) |
|
1787 | 274 |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
275 |
def _pyro_client(self, done): |
0 | 276 |
cnx = connect(self.repo.config.appid, u'admin', 'gingkow') |
277 |
# check we can get the schema |
|
278 |
schema = cnx.get_schema() |
|
279 |
self.assertEquals(schema.__hashmode__, None) |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
280 |
cu = cnx.cursor() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
281 |
rset = cu.execute('Any U,G WHERE U in_group G') |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
282 |
cnx.close() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
283 |
done.append(True) |
0 | 284 |
|
285 |
def test_internal_api(self): |
|
286 |
repo = self.repo |
|
287 |
cnxid = repo.connect(*self.default_user_password()) |
|
288 |
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
|
289 |
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
|
290 |
self.assertEquals(repo.type_from_eid(1, session), 'CWGroup') |
0 | 291 |
self.assertEquals(repo.source_from_eid(1, session).uri, 'system') |
292 |
self.assertEquals(repo.eid2extid(repo.system_source, 1, session), None) |
|
293 |
class dummysource: uri = 'toto' |
|
294 |
self.assertRaises(UnknownEid, repo.eid2extid, dummysource, 1, session) |
|
295 |
||
296 |
def test_public_api(self): |
|
297 |
self.assertEquals(self.repo.get_schema(), self.repo.schema) |
|
298 |
self.assertEquals(self.repo.source_defs(), {'system': {'adapter': 'native', 'uri': 'system'}}) |
|
299 |
# .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
|
300 |
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 | 301 |
|
302 |
def test_session_api(self): |
|
303 |
repo = self.repo |
|
304 |
cnxid = repo.connect(*self.default_user_password()) |
|
305 |
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
|
306 |
self.assertEquals(repo.describe(cnxid, 1), (u'CWGroup', u'system', None)) |
0 | 307 |
repo.close(cnxid) |
308 |
self.assertRaises(BadConnectionId, repo.user_info, cnxid) |
|
309 |
self.assertRaises(BadConnectionId, repo.describe, cnxid, 1) |
|
310 |
||
311 |
def test_shared_data_api(self): |
|
312 |
repo = self.repo |
|
313 |
cnxid = repo.connect(*self.default_user_password()) |
|
314 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), None) |
|
315 |
repo.set_shared_data(cnxid, 'data', 4) |
|
316 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), 4) |
|
317 |
repo.get_shared_data(cnxid, 'data', pop=True) |
|
318 |
repo.get_shared_data(cnxid, 'whatever', pop=True) |
|
319 |
self.assertEquals(repo.get_shared_data(cnxid, 'data'), None) |
|
320 |
repo.close(cnxid) |
|
321 |
self.assertRaises(BadConnectionId, repo.set_shared_data, cnxid, 'data', 0) |
|
322 |
self.assertRaises(BadConnectionId, repo.get_shared_data, cnxid, 'data') |
|
1787 | 323 |
|
0 | 324 |
|
325 |
class DataHelpersTC(RepositoryBasedTC): |
|
1787 | 326 |
|
0 | 327 |
def setUp(self): |
328 |
""" called before each test from this class """ |
|
329 |
cnxid = self.repo.connect(*self.default_user_password()) |
|
330 |
self.session = self.repo._sessions[cnxid] |
|
331 |
self.session.set_pool() |
|
332 |
||
333 |
def tearDown(self): |
|
334 |
self.session.rollback() |
|
1787 | 335 |
|
0 | 336 |
def test_create_eid(self): |
337 |
self.assert_(self.repo.system_source.create_eid(self.session)) |
|
338 |
||
339 |
def test_source_from_eid(self): |
|
340 |
self.assertEquals(self.repo.source_from_eid(1, self.session), |
|
341 |
self.repo.sources_by_uri['system']) |
|
342 |
||
343 |
def test_source_from_eid_raise(self): |
|
344 |
self.assertRaises(UnknownEid, self.repo.source_from_eid, -2, self.session) |
|
345 |
||
346 |
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
|
347 |
self.assertEquals(self.repo.type_from_eid(1, self.session), 'CWGroup') |
1787 | 348 |
|
0 | 349 |
def test_type_from_eid_raise(self): |
350 |
self.assertRaises(UnknownEid, self.repo.type_from_eid, -2, self.session) |
|
1787 | 351 |
|
0 | 352 |
def test_add_delete_info(self): |
353 |
entity = self.repo.vreg.etype_class('Personne')(self.session, None, None) |
|
354 |
entity.eid = -1 |
|
355 |
entity.complete = lambda x: None |
|
356 |
self.repo.add_info(self.session, entity, self.repo.sources_by_uri['system']) |
|
357 |
cursor = self.session.pool['system'] |
|
358 |
cursor.execute('SELECT * FROM entities WHERE eid = -1') |
|
359 |
data = cursor.fetchall() |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
360 |
self.assertIsInstance(data[0][3], datetime) |
0 | 361 |
data[0] = list(data[0]) |
362 |
data[0][3] = None |
|
363 |
self.assertEquals(tuplify(data), [(-1, 'Personne', 'system', None, None)]) |
|
364 |
self.repo.delete_info(self.session, -1) |
|
365 |
#self.repo.commit() |
|
366 |
cursor.execute('SELECT * FROM entities WHERE eid = -1') |
|
367 |
data = cursor.fetchall() |
|
368 |
self.assertEquals(data, []) |
|
369 |
||
370 |
||
371 |
class FTITC(RepositoryBasedTC): |
|
1787 | 372 |
|
0 | 373 |
def test_reindex_and_modified_since(self): |
374 |
cursor = self.session.pool['system'] |
|
375 |
eidp = self.execute('INSERT Personne X: X nom "toto", X prenom "tutu"')[0][0] |
|
376 |
self.commit() |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
377 |
ts = datetime.now() |
0 | 378 |
self.assertEquals(len(self.execute('Personne X WHERE X has_text "tutu"')), 1) |
379 |
cursor.execute('SELECT mtime, eid FROM entities WHERE eid = %s' % eidp) |
|
380 |
omtime = cursor.fetchone()[0] |
|
381 |
# our sqlite datetime adapter is ignore seconds fraction, so we have to |
|
382 |
# ensure update is done the next seconds |
|
383 |
time.sleep(1 - (ts.second - int(ts.second))) |
|
384 |
self.execute('SET X nom "tata" WHERE X eid %(x)s', {'x': eidp}, 'x') |
|
385 |
self.commit() |
|
386 |
self.assertEquals(len(self.execute('Personne X WHERE X has_text "tutu"')), 1) |
|
387 |
cursor.execute('SELECT mtime FROM entities WHERE eid = %s' % eidp) |
|
388 |
mtime = cursor.fetchone()[0] |
|
389 |
self.failUnless(omtime < mtime) |
|
390 |
self.commit() |
|
391 |
date, modified, deleted = self.repo.entities_modified_since(('Personne',), omtime) |
|
392 |
self.assertEquals(modified, [('Personne', eidp)]) |
|
393 |
self.assertEquals(deleted, []) |
|
394 |
date, modified, deleted = self.repo.entities_modified_since(('Personne',), mtime) |
|
395 |
self.assertEquals(modified, []) |
|
396 |
self.assertEquals(deleted, []) |
|
397 |
self.execute('DELETE Personne X WHERE X eid %(x)s', {'x': eidp}) |
|
398 |
self.commit() |
|
399 |
date, modified, deleted = self.repo.entities_modified_since(('Personne',), omtime) |
|
400 |
self.assertEquals(modified, []) |
|
401 |
self.assertEquals(deleted, [('Personne', eidp)]) |
|
402 |
||
403 |
def test_composite_entity(self): |
|
404 |
assert self.schema.rschema('use_email').fulltext_container == 'subject' |
|
405 |
eid = self.add_entity('EmailAddress', address=u'toto@logilab.fr').eid |
|
406 |
self.commit() |
|
407 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'}) |
|
408 |
self.assertEquals(rset.rows, [[eid]]) |
|
409 |
self.execute('SET X use_email Y WHERE X login "admin", Y eid %(y)s', {'y': eid}) |
|
410 |
self.commit() |
|
411 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'}) |
|
412 |
self.assertEquals(rset.rows, [[self.session.user.eid]]) |
|
413 |
self.execute('DELETE X use_email Y WHERE X login "admin", Y eid %(y)s', {'y': eid}) |
|
414 |
self.commit() |
|
415 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'}) |
|
416 |
self.assertEquals(rset.rows, []) |
|
417 |
eid = self.add_entity('EmailAddress', address=u'tutu@logilab.fr').eid |
|
418 |
self.execute('SET X use_email Y WHERE X login "admin", Y eid %(y)s', {'y': eid}) |
|
419 |
self.commit() |
|
420 |
rset = self.execute('Any X WHERE X has_text %(t)s', {'t': 'tutu'}) |
|
421 |
self.assertEquals(rset.rows, [[self.session.user.eid]]) |
|
1787 | 422 |
|
423 |
||
0 | 424 |
class DBInitTC(RepositoryBasedTC): |
1787 | 425 |
|
0 | 426 |
def test_versions_inserted(self): |
427 |
inserted = [r[0] for r in self.execute('Any K ORDERBY K WHERE P pkey K, P pkey ~= "system.version.%"')] |
|
428 |
self.assertEquals(inserted, |
|
1787 | 429 |
[u'system.version.basket', u'system.version.card', u'system.version.comment', |
430 |
u'system.version.cubicweb', u'system.version.email', |
|
431 |
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
|
432 |
u'system.version.tag']) |
1787 | 433 |
|
0 | 434 |
class InlineRelHooksTC(RepositoryBasedTC): |
435 |
"""test relation hooks are called for inlined relations |
|
436 |
""" |
|
437 |
def setUp(self): |
|
438 |
RepositoryBasedTC.setUp(self) |
|
439 |
self.hm = self.repo.hm |
|
440 |
self.called = [] |
|
1787 | 441 |
|
0 | 442 |
def _before_relation_hook(self, pool, fromeid, rtype, toeid): |
443 |
self.called.append((fromeid, rtype, toeid)) |
|
444 |
||
445 |
def _after_relation_hook(self, pool, fromeid, rtype, toeid): |
|
446 |
self.called.append((fromeid, rtype, toeid)) |
|
1787 | 447 |
|
0 | 448 |
def test_before_add_inline_relation(self): |
449 |
"""make sure before_<event>_relation hooks are called directly""" |
|
450 |
self.hm.register_hook(self._before_relation_hook, |
|
451 |
'before_add_relation', 'ecrit_par') |
|
452 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
453 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
454 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
455 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp)]) |
|
1787 | 456 |
|
0 | 457 |
def test_after_add_inline_relation(self): |
458 |
"""make sure after_<event>_relation hooks are deferred""" |
|
459 |
self.hm.register_hook(self._after_relation_hook, |
|
460 |
'after_add_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.assertEquals(self.called, []) |
|
464 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
465 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp,)]) |
|
1787 | 466 |
|
0 | 467 |
def test_after_add_inline(self): |
468 |
"""make sure after_<event>_relation hooks are deferred""" |
|
469 |
self.hm.register_hook(self._after_relation_hook, |
|
470 |
'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
|
471 |
eidp = self.execute('INSERT CWUser X: X login "toto", X upassword "tutu", X in_state S WHERE S name "activated"')[0][0] |
0 | 472 |
eids = self.execute('State X WHERE X name "activated"')[0][0] |
473 |
self.assertEquals(self.called, [(eidp, 'in_state', eids,)]) |
|
1787 | 474 |
|
0 | 475 |
def test_before_delete_inline_relation(self): |
476 |
"""make sure before_<event>_relation hooks are called directly""" |
|
477 |
self.hm.register_hook(self._before_relation_hook, |
|
478 |
'before_delete_relation', 'ecrit_par') |
|
479 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
480 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
481 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
482 |
self.execute('DELETE N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
483 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp)]) |
|
484 |
rset = self.execute('Any Y where N ecrit_par Y, N type "T", Y nom "toto"') |
|
485 |
# make sure the relation is really deleted |
|
486 |
self.failUnless(len(rset) == 0, "failed to delete inline relation") |
|
487 |
||
488 |
def test_after_delete_inline_relation(self): |
|
489 |
"""make sure after_<event>_relation hooks are deferred""" |
|
490 |
self.hm.register_hook(self._after_relation_hook, |
|
491 |
'after_delete_relation', 'ecrit_par') |
|
492 |
eidp = self.execute('INSERT Personne X: X nom "toto"')[0][0] |
|
493 |
eidn = self.execute('INSERT Note X: X type "T"')[0][0] |
|
494 |
self.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
495 |
self.assertEquals(self.called, []) |
|
496 |
self.execute('DELETE N ecrit_par Y WHERE N type "T", Y nom "toto"') |
|
497 |
self.assertEquals(self.called, [(eidn, 'ecrit_par', eidp,)]) |
|
498 |
||
1787 | 499 |
|
0 | 500 |
if __name__ == '__main__': |
501 |
unittest_main() |