author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 04 Aug 2009 17:39:02 +0200 | |
changeset 2682 | 0188a4a02403 |
parent 2608 | 21856eda34f6 |
child 2746 | cfcc7f6121d6 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
"""functional tests for core hooks |
|
3 |
||
4 |
note: most schemahooks.py hooks are actually tested in unittest_migrations.py |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
5 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 6 |
""" |
7 |
||
8 |
from logilab.common.testlib import TestCase, unittest_main |
|
2454
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
9 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
10 |
from datetime import datetime |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
11 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
12 |
from cubicweb import (ConnectionError, RepositoryError, ValidationError, |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
13 |
AuthenticationError, BadConnectionId) |
0 | 14 |
from cubicweb.devtools.apptest import RepositoryBasedTC, get_versions |
15 |
||
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:
0
diff
changeset
|
16 |
from cubicweb.server.sqlutils import SQL_PREFIX |
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:
0
diff
changeset
|
17 |
from cubicweb.server.repository import Repository |
0 | 18 |
|
19 |
orig_get_versions = Repository.get_versions |
|
20 |
||
21 |
def setup_module(*args): |
|
22 |
Repository.get_versions = get_versions |
|
23 |
||
24 |
def teardown_module(*args): |
|
25 |
Repository.get_versions = orig_get_versions |
|
26 |
||
27 |
||
1787 | 28 |
|
0 | 29 |
class CoreHooksTC(RepositoryBasedTC): |
1787 | 30 |
|
0 | 31 |
def test_delete_internal_entities(self): |
32 |
self.assertRaises(RepositoryError, self.execute, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
33 |
'DELETE CWEType X WHERE X name "CWEType"') |
0 | 34 |
self.assertRaises(RepositoryError, self.execute, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
35 |
'DELETE CWRType X WHERE X name "relation_type"') |
0 | 36 |
self.assertRaises(RepositoryError, self.execute, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
37 |
'DELETE CWGroup X WHERE X name "owners"') |
0 | 38 |
|
39 |
def test_delete_required_relations_subject(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
40 |
self.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y, X in_state S ' |
0 | 41 |
'WHERE Y name "users", S name "activated"') |
42 |
self.commit() |
|
43 |
self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"') |
|
44 |
self.assertRaises(ValidationError, self.commit) |
|
45 |
self.execute('DELETE X in_group Y WHERE X login "toto"') |
|
46 |
self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"') |
|
47 |
self.commit() |
|
1787 | 48 |
|
0 | 49 |
def test_delete_required_relations_object(self): |
50 |
self.skip('no sample in the schema ! YAGNI ? Kermaat ?') |
|
1787 | 51 |
|
0 | 52 |
def test_static_vocabulary_check(self): |
53 |
self.assertRaises(ValidationError, |
|
54 |
self.execute, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
55 |
'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"') |
1787 | 56 |
|
0 | 57 |
def test_missing_required_relations_subject_inline(self): |
1787 | 58 |
# missing in_group relation |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
59 |
self.execute('INSERT CWUser X: X login "toto", X upassword "hop"') |
0 | 60 |
self.assertRaises(ValidationError, |
61 |
self.commit) |
|
62 |
||
63 |
def test_delete_if_singlecard1(self): |
|
64 |
self.assertEquals(self.repo.schema['in_state'].inlined, False) |
|
2608
21856eda34f6
[F repo tests] tests have to be updated:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2591
diff
changeset
|
65 |
ueid = self.create_user('toto') |
0 | 66 |
self.commit() |
67 |
self.execute('SET X in_state S WHERE S name "deactivated", X eid %(x)s', {'x': ueid}) |
|
68 |
rset = self.execute('Any S WHERE X in_state S, X eid %(x)s', {'x': ueid}) |
|
69 |
self.assertEquals(len(rset), 1) |
|
2608
21856eda34f6
[F repo tests] tests have to be updated:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2591
diff
changeset
|
70 |
self.commit() |
0 | 71 |
self.assertRaises(Exception, self.execute, 'SET X in_state S WHERE S name "deactivated", X eid %s' % ueid) |
72 |
rset2 = self.execute('Any S WHERE X in_state S, X eid %(x)s', {'x': ueid}) |
|
73 |
self.assertEquals(rset.rows, rset2.rows) |
|
74 |
||
75 |
def test_inlined(self): |
|
76 |
self.assertEquals(self.repo.schema['sender'].inlined, True) |
|
77 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
78 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
79 |
eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
80 |
'WHERE Y is EmailAddress, P is EmailPart')[0][0] |
|
81 |
self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress') |
|
82 |
rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid) |
|
83 |
self.assertEquals(len(rset), 1) |
|
1787 | 84 |
|
0 | 85 |
def test_composite_1(self): |
86 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
87 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
88 |
self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
89 |
'WHERE Y is EmailAddress, P is EmailPart') |
|
90 |
self.failUnless(self.execute('Email X WHERE X sender Y')) |
|
91 |
self.commit() |
|
92 |
self.execute('DELETE Email X') |
|
93 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
94 |
self.assertEquals(len(rset), 1) |
|
95 |
self.commit() |
|
96 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
97 |
self.assertEquals(len(rset), 0) |
|
1787 | 98 |
|
0 | 99 |
def test_composite_2(self): |
100 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
101 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
102 |
self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
103 |
'WHERE Y is EmailAddress, P is EmailPart') |
|
104 |
self.commit() |
|
105 |
self.execute('DELETE Email X') |
|
106 |
self.execute('DELETE EmailPart X') |
|
107 |
self.commit() |
|
108 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
109 |
self.assertEquals(len(rset), 0) |
|
1787 | 110 |
|
0 | 111 |
def test_composite_redirection(self): |
112 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
113 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
114 |
self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
115 |
'WHERE Y is EmailAddress, P is EmailPart') |
|
116 |
self.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, X recipients Y ' |
|
117 |
'WHERE Y is EmailAddress') |
|
118 |
self.commit() |
|
119 |
self.execute('DELETE X parts Y WHERE X messageid "<1234>"') |
|
120 |
self.execute('SET X parts Y WHERE X messageid "<2345>"') |
|
121 |
self.commit() |
|
122 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
123 |
self.assertEquals(len(rset), 1) |
|
124 |
self.assertEquals(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>') |
|
125 |
||
126 |
def test_unsatisfied_constraints(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
127 |
self.execute('INSERT CWRelation X: X from_entity FE, X relation_type RT, X to_entity TE ' |
0 | 128 |
'WHERE FE name "Affaire", RT name "concerne", TE name "String"') |
129 |
self.assertRaises(ValidationError, |
|
130 |
self.commit) |
|
131 |
||
132 |
||
133 |
def test_html_tidy_hook(self): |
|
134 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "yo"').get_entity(0, 0) |
|
135 |
self.assertEquals(entity.descr, u'yo') |
|
136 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "<b>yo"').get_entity(0, 0) |
|
137 |
self.assertEquals(entity.descr, u'<b>yo</b>') |
|
138 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "<b>yo</b>"').get_entity(0, 0) |
|
139 |
self.assertEquals(entity.descr, u'<b>yo</b>') |
|
140 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "<b>R&D</b>"').get_entity(0, 0) |
|
141 |
self.assertEquals(entity.descr, u'<b>R&D</b>') |
|
142 |
xml = u"<div>c'est <b>l'été" |
|
143 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr %(d)s', |
|
144 |
{'d': xml}).get_entity(0, 0) |
|
145 |
self.assertEquals(entity.descr, u"<div>c'est <b>l'été</b></div>") |
|
146 |
||
147 |
def test_nonregr_html_tidy_hook_no_update(self): |
|
148 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "yo"').get_entity(0, 0) |
|
149 |
self.assertEquals(entity.descr, u'yo') |
|
150 |
self.execute('SET A ref "REF" WHERE A eid %s' % entity.eid) |
|
151 |
entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
|
152 |
self.assertEquals(entity.descr, u'yo') |
|
153 |
self.execute('SET A descr "R&D<p>yo" WHERE A eid %s' % entity.eid) |
|
154 |
entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
|
155 |
self.assertEquals(entity.descr, u'R&D<p>yo</p>') |
|
1787 | 156 |
|
157 |
||
0 | 158 |
|
159 |
class UserGroupHooksTC(RepositoryBasedTC): |
|
1787 | 160 |
|
0 | 161 |
def test_user_synchronization(self): |
162 |
self.create_user('toto', password='hop', commit=False) |
|
163 |
self.assertRaises(AuthenticationError, |
|
164 |
self.repo.connect, u'toto', 'hop') |
|
165 |
self.commit() |
|
166 |
cnxid = self.repo.connect(u'toto', 'hop') |
|
167 |
self.failIfEqual(cnxid, self.cnxid) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
168 |
self.execute('DELETE CWUser X WHERE X login "toto"') |
0 | 169 |
self.repo.execute(cnxid, 'State X') |
170 |
self.commit() |
|
171 |
self.assertRaises(BadConnectionId, |
|
172 |
self.repo.execute, cnxid, 'State X') |
|
173 |
||
174 |
def test_user_group_synchronization(self): |
|
175 |
user = self.session.user |
|
176 |
self.assertEquals(user.groups, set(('managers',))) |
|
177 |
self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
178 |
self.assertEquals(user.groups, set(('managers',))) |
|
179 |
self.commit() |
|
180 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
181 |
self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
182 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
183 |
self.commit() |
|
184 |
self.assertEquals(user.groups, set(('managers',))) |
|
185 |
||
186 |
def test_user_composite_owner(self): |
|
187 |
ueid = self.create_user('toto') |
|
188 |
# composite of euser should be owned by the euser regardless of who created it |
|
189 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", U use_email X ' |
|
190 |
'WHERE U login "toto"') |
|
191 |
self.commit() |
|
192 |
self.assertEquals(self.execute('Any A WHERE X owned_by U, U use_email X,' |
|
193 |
'U login "toto", X address A')[0][0], |
|
194 |
'toto@logilab.fr') |
|
195 |
||
196 |
def test_no_created_by_on_deleted_entity(self): |
|
197 |
eid = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0] |
|
198 |
self.execute('DELETE EmailAddress X WHERE X eid %s' % eid) |
|
199 |
self.commit() |
|
200 |
self.failIf(self.execute('Any X WHERE X created_by Y, X eid >= %(x)s', {'x': eid})) |
|
1787 | 201 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
202 |
class CWPropertyHooksTC(RepositoryBasedTC): |
1787 | 203 |
|
0 | 204 |
def test_unexistant_eproperty(self): |
205 |
ex = self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
206 |
self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop", X for_user U') |
0 | 207 |
self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
208 |
ex = self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
209 |
self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop"') |
0 | 210 |
self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
1787 | 211 |
|
0 | 212 |
def test_site_wide_eproperty(self): |
213 |
ex = self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
214 |
self.execute, 'INSERT CWProperty X: X pkey "ui.site-title", X value "hop", X for_user U') |
0 | 215 |
self.assertEquals(ex.errors, {'for_user': "site-wide property can't be set for user"}) |
1787 | 216 |
|
0 | 217 |
def test_bad_type_eproperty(self): |
218 |
ex = self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
219 |
self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop", X for_user U') |
0 | 220 |
self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
221 |
ex = self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
222 |
self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop"') |
0 | 223 |
self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
1787 | 224 |
|
225 |
||
0 | 226 |
class SchemaHooksTC(RepositoryBasedTC): |
1787 | 227 |
|
0 | 228 |
def test_duplicate_etype_error(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
229 |
# check we can't add a CWEType or CWRType entity if it already exists one |
0 | 230 |
# with the same name |
231 |
# |
|
232 |
# according to hook order, we'll get a repository or validation error |
|
233 |
self.assertRaises((ValidationError, RepositoryError), |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
234 |
self.execute, 'INSERT CWEType X: X name "Societe"') |
0 | 235 |
self.assertRaises((ValidationError, RepositoryError), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
236 |
self.execute, 'INSERT CWRType X: X name "in_group"') |
1787 | 237 |
|
0 | 238 |
def test_validation_unique_constraint(self): |
239 |
self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
240 |
self.execute, 'INSERT CWUser X: X login "admin"') |
0 | 241 |
try: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
242 |
self.execute('INSERT CWUser X: X login "admin"') |
0 | 243 |
except ValidationError, ex: |
244 |
self.assertIsInstance(ex.entity, int) |
|
245 |
self.assertEquals(ex.errors, {'login': 'the value "admin" is already used, use another one'}) |
|
246 |
||
247 |
||
248 |
class SchemaModificationHooksTC(RepositoryBasedTC): |
|
249 |
||
250 |
def setUp(self): |
|
251 |
if not hasattr(self, '_repo'): |
|
252 |
# first initialization |
|
253 |
repo = self.repo # set by the RepositoryBasedTC metaclass |
|
2608
21856eda34f6
[F repo tests] tests have to be updated:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2591
diff
changeset
|
254 |
# force to read schema from the database to get proper eid set on schema instances |
0 | 255 |
repo.config._cubes = None |
256 |
repo.fill_schema() |
|
257 |
RepositoryBasedTC.setUp(self) |
|
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:
0
diff
changeset
|
258 |
|
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:
0
diff
changeset
|
259 |
def index_exists(self, etype, attr, unique=False): |
1787 | 260 |
dbhelper = self.session.pool.source('system').dbhelper |
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:
0
diff
changeset
|
261 |
sqlcursor = self.session.pool['system'] |
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:
0
diff
changeset
|
262 |
return dbhelper.index_exists(sqlcursor, SQL_PREFIX + etype, SQL_PREFIX + attr, unique=unique) |
1787 | 263 |
|
0 | 264 |
def test_base(self): |
265 |
schema = self.repo.schema |
|
1787 | 266 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 267 |
sqlcursor = self.session.pool['system'] |
268 |
self.failIf(schema.has_entity('Societe2')) |
|
269 |
self.failIf(schema.has_entity('concerne2')) |
|
270 |
# schema should be update on insertion (after commit) |
|
2453
0faf7b5cdc71
[tests] remove some other 'meta' relation usage in tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2251
diff
changeset
|
271 |
self.execute('INSERT CWEType X: X name "Societe2", X description "", X final FALSE') |
0faf7b5cdc71
[tests] remove some other 'meta' relation usage in tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2251
diff
changeset
|
272 |
self.execute('INSERT CWRType X: X name "concerne2", X description "", X final FALSE, X symetric FALSE') |
0 | 273 |
self.failIf(schema.has_entity('Societe2')) |
274 |
self.failIf(schema.has_entity('concerne2')) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
275 |
self.execute('SET X read_permission G WHERE X is CWEType, X name "Societe2", G is CWGroup') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
276 |
self.execute('SET X read_permission G WHERE X is CWRType, X name "concerne2", G is CWGroup') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
277 |
self.execute('SET X add_permission G WHERE X is CWEType, X name "Societe2", G is CWGroup, G name "managers"') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
278 |
self.execute('SET X add_permission G WHERE X is CWRType, X name "concerne2", G is CWGroup, G name "managers"') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
279 |
self.execute('SET X delete_permission G WHERE X is CWEType, X name "Societe2", G is CWGroup, G name "owners"') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
280 |
self.execute('SET X delete_permission G WHERE X is CWRType, X name "concerne2", G is CWGroup, G name "owners"') |
0 | 281 |
# have to commit before adding definition relations |
282 |
self.commit() |
|
283 |
self.failUnless(schema.has_entity('Societe2')) |
|
284 |
self.failUnless(schema.has_relation('concerne2')) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
285 |
self.execute('INSERT CWAttribute X: X cardinality "11", X defaultval "noname", X indexed TRUE, X relation_type RT, X from_entity E, X to_entity F ' |
0 | 286 |
'WHERE RT name "nom", E name "Societe2", F name "String"') |
287 |
concerne2_rdef_eid = self.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
288 |
'INSERT CWRelation X: X cardinality "**", X relation_type RT, X from_entity E, X to_entity E ' |
0 | 289 |
'WHERE RT name "concerne2", E name "Societe2"')[0][0] |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
290 |
self.execute('INSERT CWRelation X: X cardinality "?*", X relation_type RT, X from_entity E, X to_entity C ' |
0 | 291 |
'WHERE RT name "comments", E name "Societe2", C name "Comment"') |
292 |
self.failIf('nom' in schema['Societe2'].subject_relations()) |
|
293 |
self.failIf('concerne2' in schema['Societe2'].subject_relations()) |
|
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:
0
diff
changeset
|
294 |
self.failIf(self.index_exists('Societe2', 'nom')) |
0 | 295 |
self.commit() |
296 |
self.failUnless('nom' in schema['Societe2'].subject_relations()) |
|
297 |
self.failUnless('concerne2' in schema['Societe2'].subject_relations()) |
|
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:
0
diff
changeset
|
298 |
self.failUnless(self.index_exists('Societe2', 'nom')) |
0 | 299 |
# now we should be able to insert and query Societe2 |
300 |
s2eid = self.execute('INSERT Societe2 X: X nom "logilab"')[0][0] |
|
301 |
self.execute('Societe2 X WHERE X nom "logilab"') |
|
302 |
self.execute('SET X concerne2 X WHERE X nom "logilab"') |
|
303 |
rset = self.execute('Any X WHERE X concerne2 Y') |
|
304 |
self.assertEquals(rset.rows, [[s2eid]]) |
|
305 |
# check that when a relation definition is deleted, existing relations are deleted |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
306 |
self.execute('INSERT CWRelation X: X cardinality "**", X relation_type RT, X from_entity E, X to_entity E ' |
0 | 307 |
'WHERE RT name "concerne2", E name "Societe"') |
308 |
self.commit() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
309 |
self.execute('DELETE CWRelation X WHERE X eid %(x)s', {'x': concerne2_rdef_eid}, 'x') |
0 | 310 |
self.commit() |
311 |
self.failUnless('concerne2' in schema['Societe'].subject_relations()) |
|
312 |
self.failIf('concerne2' in schema['Societe2'].subject_relations()) |
|
313 |
self.failIf(self.execute('Any X WHERE X concerne2 Y')) |
|
314 |
# schema should be cleaned on delete (after commit) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
315 |
self.execute('DELETE CWEType X WHERE X name "Societe2"') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
316 |
self.execute('DELETE CWRType X WHERE X name "concerne2"') |
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:
0
diff
changeset
|
317 |
self.failUnless(self.index_exists('Societe2', 'nom')) |
0 | 318 |
self.failUnless(schema.has_entity('Societe2')) |
319 |
self.failUnless(schema.has_relation('concerne2')) |
|
320 |
self.commit() |
|
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:
0
diff
changeset
|
321 |
self.failIf(self.index_exists('Societe2', 'nom')) |
0 | 322 |
self.failIf(schema.has_entity('Societe2')) |
323 |
self.failIf(schema.has_entity('concerne2')) |
|
324 |
||
325 |
def test_is_instance_of_insertions(self): |
|
326 |
seid = self.execute('INSERT SubDivision S: S nom "subdiv"')[0][0] |
|
327 |
is_etypes = [etype for etype, in self.execute('Any ETN WHERE X eid %s, X is ET, ET name ETN' % seid)] |
|
328 |
self.assertEquals(is_etypes, ['SubDivision']) |
|
329 |
instanceof_etypes = [etype for etype, in self.execute('Any ETN WHERE X eid %s, X is_instance_of ET, ET name ETN' % seid)] |
|
330 |
self.assertEquals(sorted(instanceof_etypes), ['Division', 'Societe', 'SubDivision']) |
|
331 |
snames = [name for name, in self.execute('Any N WHERE S is Societe, S nom N')] |
|
332 |
self.failIf('subdiv' in snames) |
|
333 |
snames = [name for name, in self.execute('Any N WHERE S is Division, S nom N')] |
|
334 |
self.failIf('subdiv' in snames) |
|
335 |
snames = [name for name, in self.execute('Any N WHERE S is_instance_of Societe, S nom N')] |
|
336 |
self.failUnless('subdiv' in snames) |
|
337 |
snames = [name for name, in self.execute('Any N WHERE S is_instance_of Division, S nom N')] |
|
338 |
self.failUnless('subdiv' in snames) |
|
1787 | 339 |
|
340 |
||
0 | 341 |
def test_perms_synchronization_1(self): |
342 |
schema = self.repo.schema |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
343 |
self.assertEquals(schema['CWUser'].get_groups('read'), set(('managers', 'users'))) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
344 |
self.failUnless(self.execute('Any X, Y WHERE X is CWEType, X name "CWUser", Y is CWGroup, Y name "users"')[0]) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
345 |
self.execute('DELETE X read_permission Y WHERE X is CWEType, X name "CWUser", Y name "users"') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
346 |
self.assertEquals(schema['CWUser'].get_groups('read'), set(('managers', 'users', ))) |
0 | 347 |
self.commit() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
348 |
self.assertEquals(schema['CWUser'].get_groups('read'), set(('managers', ))) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
349 |
self.execute('SET X read_permission Y WHERE X is CWEType, X name "CWUser", Y name "users"') |
0 | 350 |
self.commit() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
351 |
self.assertEquals(schema['CWUser'].get_groups('read'), set(('managers', 'users',))) |
0 | 352 |
|
353 |
def test_perms_synchronization_2(self): |
|
354 |
schema = self.repo.schema['in_group'] |
|
355 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
356 |
self.execute('DELETE X read_permission Y WHERE X is CWRType, X name "in_group", Y name "guests"') |
0 | 357 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users', 'guests'))) |
358 |
self.commit() |
|
359 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users'))) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
360 |
self.execute('SET X read_permission Y WHERE X is CWRType, X name "in_group", Y name "guests"') |
0 | 361 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users'))) |
362 |
self.commit() |
|
363 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
364 |
||
365 |
def test_nonregr_user_edit_itself(self): |
|
366 |
ueid = self.session.user.eid |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
367 |
groupeids = [eid for eid, in self.execute('CWGroup G WHERE G name in ("managers", "users")')] |
0 | 368 |
self.execute('DELETE X in_group Y WHERE X eid %s' % ueid) |
369 |
self.execute('SET X surname "toto" WHERE X eid %s' % ueid) |
|
370 |
self.execute('SET X in_group Y WHERE X eid %s, Y name "managers"' % ueid) |
|
371 |
self.commit() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
372 |
eeid = self.execute('Any X WHERE X is CWEType, X name "CWEType"')[0][0] |
0 | 373 |
self.execute('DELETE X read_permission Y WHERE X eid %s' % eeid) |
374 |
self.execute('SET X final FALSE WHERE X eid %s' % eeid) |
|
375 |
self.execute('SET X read_permission Y WHERE X eid %s, Y eid in (%s, %s)' |
|
376 |
% (eeid, groupeids[0], groupeids[1])) |
|
377 |
self.commit() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
378 |
self.execute('Any X WHERE X is CWEType, X name "CWEType"') |
0 | 379 |
|
380 |
# schema modification hooks tests ######################################### |
|
1787 | 381 |
|
0 | 382 |
def test_uninline_relation(self): |
1787 | 383 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 384 |
sqlcursor = self.session.pool['system'] |
385 |
# Personne inline2 Affaire inline |
|
386 |
# insert a person without inline2 relation (not mandatory) |
|
387 |
self.execute('INSERT Personne X: X nom "toto"') |
|
388 |
peid = self.execute('INSERT Personne X: X nom "tutu"')[0][0] |
|
389 |
aeid = self.execute('INSERT Affaire X: X ref "tata"')[0][0] |
|
390 |
self.execute('SET X inline2 Y WHERE X eid %(x)s, Y eid %(y)s', {'x': peid, 'y': aeid}) |
|
391 |
self.failUnless(self.schema['inline2'].inlined) |
|
392 |
try: |
|
393 |
try: |
|
394 |
self.execute('SET X inlined FALSE WHERE X name "inline2"') |
|
395 |
self.failUnless(self.schema['inline2'].inlined) |
|
396 |
self.commit() |
|
397 |
self.failIf(self.schema['inline2'].inlined) |
|
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:
0
diff
changeset
|
398 |
self.failIf(self.index_exists('Personne', 'inline2')) |
0 | 399 |
rset = self.execute('Any X, Y WHERE X inline2 Y') |
400 |
self.assertEquals(len(rset), 1) |
|
401 |
self.assertEquals(rset.rows[0], [peid, aeid]) |
|
402 |
except: |
|
403 |
import traceback |
|
404 |
traceback.print_exc() |
|
405 |
raise |
|
406 |
finally: |
|
407 |
self.execute('SET X inlined TRUE WHERE X name "inline2"') |
|
408 |
self.failIf(self.schema['inline2'].inlined) |
|
409 |
self.commit() |
|
410 |
self.failUnless(self.schema['inline2'].inlined) |
|
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:
0
diff
changeset
|
411 |
self.failUnless(self.index_exists('Personne', 'inline2')) |
0 | 412 |
rset = self.execute('Any X, Y WHERE X inline2 Y') |
413 |
self.assertEquals(len(rset), 1) |
|
414 |
self.assertEquals(rset.rows[0], [peid, aeid]) |
|
415 |
||
416 |
def test_indexed_change(self): |
|
1787 | 417 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 418 |
sqlcursor = self.session.pool['system'] |
419 |
try: |
|
420 |
self.execute('SET X indexed TRUE WHERE X relation_type R, R name "sujet"') |
|
421 |
self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
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:
0
diff
changeset
|
422 |
self.failIf(self.index_exists('Affaire', 'sujet')) |
0 | 423 |
self.commit() |
424 |
self.failUnless(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
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:
0
diff
changeset
|
425 |
self.failUnless(self.index_exists('Affaire', 'sujet')) |
0 | 426 |
finally: |
427 |
self.execute('SET X indexed FALSE WHERE X relation_type R, R name "sujet"') |
|
428 |
self.failUnless(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
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:
0
diff
changeset
|
429 |
self.failUnless(self.index_exists('Affaire', 'sujet')) |
0 | 430 |
self.commit() |
431 |
self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
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:
0
diff
changeset
|
432 |
self.failIf(self.index_exists('Affaire', 'sujet')) |
0 | 433 |
|
434 |
def test_unique_change(self): |
|
1787 | 435 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 436 |
sqlcursor = self.session.pool['system'] |
437 |
try: |
|
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:
0
diff
changeset
|
438 |
try: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
439 |
self.execute('INSERT CWConstraint X: X cstrtype CT, DEF constrained_by X ' |
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:
0
diff
changeset
|
440 |
'WHERE CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
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:
0
diff
changeset
|
441 |
'RT name "sujet", E name "Affaire"') |
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:
0
diff
changeset
|
442 |
self.failIf(self.schema['Affaire'].has_unique_values('sujet')) |
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:
0
diff
changeset
|
443 |
self.failIf(self.index_exists('Affaire', 'sujet', unique=True)) |
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:
0
diff
changeset
|
444 |
self.commit() |
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:
0
diff
changeset
|
445 |
self.failUnless(self.schema['Affaire'].has_unique_values('sujet')) |
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:
0
diff
changeset
|
446 |
self.failUnless(self.index_exists('Affaire', 'sujet', unique=True)) |
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:
0
diff
changeset
|
447 |
except: |
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:
0
diff
changeset
|
448 |
import traceback |
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:
0
diff
changeset
|
449 |
traceback.print_exc() |
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:
0
diff
changeset
|
450 |
raise |
0 | 451 |
finally: |
452 |
self.execute('DELETE DEF constrained_by X WHERE X cstrtype CT, ' |
|
453 |
'CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
|
454 |
'RT name "sujet", E name "Affaire"') |
|
455 |
self.failUnless(self.schema['Affaire'].has_unique_values('sujet')) |
|
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:
0
diff
changeset
|
456 |
self.failUnless(self.index_exists('Affaire', 'sujet', unique=True)) |
0 | 457 |
self.commit() |
458 |
self.failIf(self.schema['Affaire'].has_unique_values('sujet')) |
|
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:
0
diff
changeset
|
459 |
self.failIf(self.index_exists('Affaire', 'sujet', unique=True)) |
1787 | 460 |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
461 |
def test_required_change_1(self): |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
462 |
self.execute('SET DEF cardinality "?1" ' |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
463 |
'WHERE DEF relation_type RT, DEF from_entity E,' |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
464 |
'RT name "nom", E name "Personne"') |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
465 |
self.commit() |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
466 |
# should now be able to add personne without nom |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
467 |
self.execute('INSERT Personne X') |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
468 |
self.commit() |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
469 |
|
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
470 |
def test_required_change_2(self): |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
471 |
self.execute('SET DEF cardinality "11" ' |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
472 |
'WHERE DEF relation_type RT, DEF from_entity E,' |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
473 |
'RT name "prenom", E name "Personne"') |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
474 |
self.commit() |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
475 |
# should not be able anymore to add personne without prenom |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
476 |
self.assertRaises(ValidationError, self.execute, 'INSERT Personne X: X nom "toto"') |
2251
799ff50ddfe8
fix tests to avoid schema copy, pytest unittest_migration.py OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1990
diff
changeset
|
477 |
self.execute('SET DEF cardinality "?1" ' |
799ff50ddfe8
fix tests to avoid schema copy, pytest unittest_migration.py OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1990
diff
changeset
|
478 |
'WHERE DEF relation_type RT, DEF from_entity E,' |
799ff50ddfe8
fix tests to avoid schema copy, pytest unittest_migration.py OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1990
diff
changeset
|
479 |
'RT name "prenom", E name "Personne"') |
799ff50ddfe8
fix tests to avoid schema copy, pytest unittest_migration.py OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1990
diff
changeset
|
480 |
self.commit() |
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
481 |
|
0 | 482 |
|
483 |
class WorkflowHooksTC(RepositoryBasedTC): |
|
484 |
||
485 |
def setUp(self): |
|
486 |
RepositoryBasedTC.setUp(self) |
|
487 |
self.s_activated = self.execute('State X WHERE X name "activated"')[0][0] |
|
488 |
self.s_deactivated = self.execute('State X WHERE X name "deactivated"')[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
489 |
self.s_dummy = self.execute('INSERT State X: X name "dummy", X state_of E WHERE E name "CWUser"')[0][0] |
0 | 490 |
self.create_user('stduser') |
491 |
# give access to users group on the user's wf transitions |
|
492 |
# so we can test wf enforcing on euser (managers don't have anymore this |
|
493 |
# enforcement |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
494 |
self.execute('SET X require_group G WHERE G name "users", X transition_of ET, ET name "CWUser"') |
0 | 495 |
self.commit() |
1787 | 496 |
|
0 | 497 |
def tearDown(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
498 |
self.execute('DELETE X require_group G WHERE G name "users", X transition_of ET, ET name "CWUser"') |
0 | 499 |
self.commit() |
500 |
RepositoryBasedTC.tearDown(self) |
|
501 |
||
502 |
def test_set_initial_state(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
503 |
ueid = self.execute('INSERT CWUser E: E login "x", E upassword "x", E in_group G ' |
0 | 504 |
'WHERE G name "users"')[0][0] |
505 |
self.failIf(self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s', |
|
506 |
{'x' : ueid})) |
|
507 |
self.commit() |
|
508 |
initialstate = self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s', |
|
509 |
{'x' : ueid})[0][0] |
|
510 |
self.assertEquals(initialstate, u'activated') |
|
1787 | 511 |
|
0 | 512 |
def test_initial_state(self): |
513 |
cnx = self.login('stduser') |
|
514 |
cu = cnx.cursor() |
|
515 |
self.assertRaises(ValidationError, cu.execute, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
516 |
'INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, ' |
0 | 517 |
'X in_state S WHERE S name "deactivated"', {'pwd': 'oops'}) |
518 |
cnx.close() |
|
519 |
# though managers can do whatever he want |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
520 |
self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, ' |
0 | 521 |
'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'}) |
522 |
self.commit() |
|
1787 | 523 |
|
0 | 524 |
# test that the workflow is correctly enforced |
525 |
def test_transition_checking1(self): |
|
526 |
cnx = self.login('stduser') |
|
527 |
cu = cnx.cursor() |
|
528 |
ueid = cnx.user(self.current_session()).eid |
|
529 |
self.assertRaises(ValidationError, |
|
530 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
531 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
532 |
cnx.close() |
|
1787 | 533 |
|
0 | 534 |
def test_transition_checking2(self): |
535 |
cnx = self.login('stduser') |
|
536 |
cu = cnx.cursor() |
|
537 |
ueid = cnx.user(self.current_session()).eid |
|
538 |
self.assertRaises(ValidationError, |
|
539 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
540 |
{'x': ueid, 's': self.s_dummy}, 'x') |
|
541 |
cnx.close() |
|
1787 | 542 |
|
0 | 543 |
def test_transition_checking3(self): |
544 |
cnx = self.login('stduser') |
|
545 |
cu = cnx.cursor() |
|
546 |
ueid = cnx.user(self.current_session()).eid |
|
547 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
548 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
549 |
cnx.commit() |
|
550 |
self.assertRaises(ValidationError, |
|
551 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
552 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
553 |
# get back now |
|
554 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
555 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
556 |
cnx.commit() |
|
557 |
cnx.close() |
|
1787 | 558 |
|
0 | 559 |
def test_transition_checking4(self): |
560 |
cnx = self.login('stduser') |
|
561 |
cu = cnx.cursor() |
|
562 |
ueid = cnx.user(self.current_session()).eid |
|
563 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
564 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
565 |
cnx.commit() |
|
566 |
self.assertRaises(ValidationError, |
|
567 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
568 |
{'x': ueid, 's': self.s_dummy}, 'x') |
|
569 |
# get back now |
|
570 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
571 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
572 |
cnx.commit() |
|
573 |
cnx.close() |
|
574 |
||
575 |
def test_transition_information(self): |
|
576 |
ueid = self.session.user.eid |
|
577 |
self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
578 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
579 |
self.commit() |
|
580 |
rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
581 |
self.assertEquals(len(rset), 2) |
|
582 |
tr = rset.get_entity(1, 0) |
|
583 |
#tr.complete() |
|
584 |
self.assertEquals(tr.comment, None) |
|
585 |
self.assertEquals(tr.from_state[0].eid, self.s_activated) |
|
586 |
self.assertEquals(tr.to_state[0].eid, self.s_deactivated) |
|
1787 | 587 |
|
0 | 588 |
self.session.set_shared_data('trcomment', u'il est pas sage celui-la') |
589 |
self.session.set_shared_data('trcommentformat', u'text/plain') |
|
590 |
self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
591 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
592 |
self.commit() |
|
593 |
rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
594 |
self.assertEquals(len(rset), 3) |
|
595 |
tr = rset.get_entity(2, 0) |
|
596 |
#tr.complete() |
|
597 |
self.assertEquals(tr.comment, u'il est pas sage celui-la') |
|
598 |
self.assertEquals(tr.comment_format, u'text/plain') |
|
599 |
self.assertEquals(tr.from_state[0].eid, self.s_deactivated) |
|
600 |
self.assertEquals(tr.to_state[0].eid, self.s_activated) |
|
601 |
self.assertEquals(tr.owned_by[0].login, 'admin') |
|
602 |
||
603 |
def test_transition_information_on_creation(self): |
|
604 |
ueid = self.create_user('toto') |
|
605 |
rset = self.execute('TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
606 |
self.assertEquals(len(rset), 1) |
|
607 |
tr = rset.get_entity(0, 0) |
|
608 |
#tr.complete() |
|
609 |
self.assertEquals(tr.comment, None) |
|
610 |
self.assertEquals(tr.from_state, []) |
|
611 |
self.assertEquals(tr.to_state[0].eid, self.s_activated) |
|
612 |
||
613 |
def test_std_users_can_create_trinfo(self): |
|
614 |
self.create_user('toto') |
|
615 |
cnx = self.login('toto') |
|
616 |
cu = cnx.cursor() |
|
617 |
self.failUnless(cu.execute("INSERT Note X: X type 'a', X in_state S WHERE S name 'todo'")) |
|
618 |
cnx.commit() |
|
1787 | 619 |
|
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2454
diff
changeset
|
620 |
def test_metadata_cwuri(self): |
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2454
diff
changeset
|
621 |
eid = self.execute('INSERT Note X')[0][0] |
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2454
diff
changeset
|
622 |
cwuri = self.execute('Any U WHERE X eid %s, X cwuri U' % eid)[0][0] |
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2454
diff
changeset
|
623 |
self.assertEquals(cwuri, self.repo.config['base-url'] + 'eid/%s' % eid) |
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2454
diff
changeset
|
624 |
|
2454
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
625 |
def test_metadata_creation_modification_date(self): |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
626 |
_now = datetime.now() |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
627 |
eid = self.execute('INSERT Note X')[0][0] |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
628 |
creation_date, modification_date = self.execute('Any CD, MD WHERE X eid %s, ' |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
629 |
'X creation_date CD, ' |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
630 |
'X modification_date MD' % eid)[0] |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
631 |
self.assertEquals((creation_date - _now).seconds, 0) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
632 |
self.assertEquals((modification_date - _now).seconds, 0) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
633 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
634 |
def test_metadata__date(self): |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
635 |
_now = datetime.now() |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
636 |
eid = self.execute('INSERT Note X')[0][0] |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
637 |
creation_date = self.execute('Any D WHERE X eid %s, X creation_date D' % eid)[0][0] |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
638 |
self.assertEquals((creation_date - _now).seconds, 0) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
639 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
640 |
def test_metadata_created_by(self): |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
641 |
eid = self.execute('INSERT Note X')[0][0] |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
642 |
self.commit() # fire operations |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
643 |
rset = self.execute('Any U WHERE X eid %s, X created_by U' % eid) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
644 |
self.assertEquals(len(rset), 1) # make sure we have only one creator |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
645 |
self.assertEquals(rset[0][0], self.session.user.eid) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
646 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
647 |
def test_metadata_owned_by(self): |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
648 |
eid = self.execute('INSERT Note X')[0][0] |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
649 |
self.commit() # fire operations |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
650 |
rset = self.execute('Any U WHERE X eid %s, X owned_by U' % eid) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
651 |
self.assertEquals(len(rset), 1) # make sure we have only one owner |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
652 |
self.assertEquals(rset[0][0], self.session.user.eid) |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
653 |
|
0 | 654 |
if __name__ == '__main__': |
655 |
unittest_main() |