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