author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 13 Aug 2009 10:58:29 +0200 | |
changeset 2809 | 04fa75d43af4 |
parent 2773 | b2530e3e0afb |
child 2968 | 0e3460341023 |
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) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
14 |
from cubicweb.devtools.testlib import CubicWebTC, get_versions |
0 | 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 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
29 |
class CoreHooksTC(CubicWebTC): |
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) |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
65 |
ueid = self.create_user('toto').eid |
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 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
159 |
class UserGroupHooksTC(CubicWebTC): |
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') |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
167 |
self.failIfEqual(cnxid, self.session.id) |
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): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
187 |
ueid = self.create_user('toto').eid |
0 | 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 |
|
2746 | 202 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
203 |
class CWPropertyHooksTC(CubicWebTC): |
1787 | 204 |
|
0 | 205 |
def test_unexistant_eproperty(self): |
206 |
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
|
207 |
self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop", X for_user U') |
0 | 208 |
self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
209 |
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
|
210 |
self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop"') |
0 | 211 |
self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
1787 | 212 |
|
0 | 213 |
def test_site_wide_eproperty(self): |
214 |
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
|
215 |
self.execute, 'INSERT CWProperty X: X pkey "ui.site-title", X value "hop", X for_user U') |
0 | 216 |
self.assertEquals(ex.errors, {'for_user': "site-wide property can't be set for user"}) |
1787 | 217 |
|
0 | 218 |
def test_bad_type_eproperty(self): |
219 |
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
|
220 |
self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop", X for_user U') |
0 | 221 |
self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
222 |
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
|
223 |
self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop"') |
0 | 224 |
self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
1787 | 225 |
|
226 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
227 |
class SchemaHooksTC(CubicWebTC): |
1787 | 228 |
|
0 | 229 |
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
|
230 |
# check we can't add a CWEType or CWRType entity if it already exists one |
0 | 231 |
# with the same name |
232 |
# |
|
233 |
# according to hook order, we'll get a repository or validation error |
|
234 |
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
|
235 |
self.execute, 'INSERT CWEType X: X name "Societe"') |
0 | 236 |
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
|
237 |
self.execute, 'INSERT CWRType X: X name "in_group"') |
1787 | 238 |
|
0 | 239 |
def test_validation_unique_constraint(self): |
240 |
self.assertRaises(ValidationError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
241 |
self.execute, 'INSERT CWUser X: X login "admin"') |
0 | 242 |
try: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
243 |
self.execute('INSERT CWUser X: X login "admin"') |
0 | 244 |
except ValidationError, ex: |
245 |
self.assertIsInstance(ex.entity, int) |
|
246 |
self.assertEquals(ex.errors, {'login': 'the value "admin" is already used, use another one'}) |
|
247 |
||
248 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
249 |
class SchemaModificationHooksTC(CubicWebTC): |
0 | 250 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
251 |
@classmethod |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
252 |
def init_config(cls, config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
253 |
super(SchemaModificationHooksTC, cls).init_config(config) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
254 |
config._cubes = None |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
255 |
cls.repo.fill_schema() |
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
|
256 |
|
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
|
257 |
def index_exists(self, etype, attr, unique=False): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
258 |
self.session.set_pool() |
1787 | 259 |
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
|
260 |
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
|
261 |
return dbhelper.index_exists(sqlcursor, SQL_PREFIX + etype, SQL_PREFIX + attr, unique=unique) |
1787 | 262 |
|
0 | 263 |
def test_base(self): |
264 |
schema = self.repo.schema |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
265 |
self.session.set_pool() |
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): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
383 |
self.session.set_pool() |
1787 | 384 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 385 |
sqlcursor = self.session.pool['system'] |
386 |
# Personne inline2 Affaire inline |
|
387 |
# insert a person without inline2 relation (not mandatory) |
|
388 |
self.execute('INSERT Personne X: X nom "toto"') |
|
389 |
peid = self.execute('INSERT Personne X: X nom "tutu"')[0][0] |
|
390 |
aeid = self.execute('INSERT Affaire X: X ref "tata"')[0][0] |
|
391 |
self.execute('SET X inline2 Y WHERE X eid %(x)s, Y eid %(y)s', {'x': peid, 'y': aeid}) |
|
392 |
self.failUnless(self.schema['inline2'].inlined) |
|
393 |
try: |
|
394 |
try: |
|
395 |
self.execute('SET X inlined FALSE WHERE X name "inline2"') |
|
396 |
self.failUnless(self.schema['inline2'].inlined) |
|
397 |
self.commit() |
|
398 |
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
|
399 |
self.failIf(self.index_exists('Personne', 'inline2')) |
0 | 400 |
rset = self.execute('Any X, Y WHERE X inline2 Y') |
401 |
self.assertEquals(len(rset), 1) |
|
402 |
self.assertEquals(rset.rows[0], [peid, aeid]) |
|
403 |
except: |
|
404 |
import traceback |
|
405 |
traceback.print_exc() |
|
406 |
raise |
|
407 |
finally: |
|
408 |
self.execute('SET X inlined TRUE WHERE X name "inline2"') |
|
409 |
self.failIf(self.schema['inline2'].inlined) |
|
410 |
self.commit() |
|
411 |
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
|
412 |
self.failUnless(self.index_exists('Personne', 'inline2')) |
0 | 413 |
rset = self.execute('Any X, Y WHERE X inline2 Y') |
414 |
self.assertEquals(len(rset), 1) |
|
415 |
self.assertEquals(rset.rows[0], [peid, aeid]) |
|
416 |
||
417 |
def test_indexed_change(self): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
418 |
self.session.set_pool() |
1787 | 419 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 420 |
sqlcursor = self.session.pool['system'] |
421 |
try: |
|
422 |
self.execute('SET X indexed TRUE WHERE X relation_type R, R name "sujet"') |
|
423 |
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
|
424 |
self.failIf(self.index_exists('Affaire', 'sujet')) |
0 | 425 |
self.commit() |
426 |
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
|
427 |
self.failUnless(self.index_exists('Affaire', 'sujet')) |
0 | 428 |
finally: |
429 |
self.execute('SET X indexed FALSE WHERE X relation_type R, R name "sujet"') |
|
430 |
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
|
431 |
self.failUnless(self.index_exists('Affaire', 'sujet')) |
0 | 432 |
self.commit() |
433 |
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
|
434 |
self.failIf(self.index_exists('Affaire', 'sujet')) |
0 | 435 |
|
436 |
def test_unique_change(self): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
437 |
self.session.set_pool() |
1787 | 438 |
dbhelper = self.session.pool.source('system').dbhelper |
0 | 439 |
sqlcursor = self.session.pool['system'] |
440 |
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
|
441 |
try: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
442 |
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
|
443 |
'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
|
444 |
'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
|
445 |
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
|
446 |
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
|
447 |
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
|
448 |
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
|
449 |
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
|
450 |
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
|
451 |
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
|
452 |
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
|
453 |
raise |
0 | 454 |
finally: |
455 |
self.execute('DELETE DEF constrained_by X WHERE X cstrtype CT, ' |
|
456 |
'CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
|
457 |
'RT name "sujet", E name "Affaire"') |
|
458 |
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
|
459 |
self.failUnless(self.index_exists('Affaire', 'sujet', unique=True)) |
0 | 460 |
self.commit() |
461 |
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
|
462 |
self.failIf(self.index_exists('Affaire', 'sujet', unique=True)) |
1787 | 463 |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
464 |
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
|
465 |
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
|
466 |
'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
|
467 |
'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
|
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 |
# 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
|
470 |
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
|
471 |
self.commit() |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
472 |
|
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
473 |
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
|
474 |
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
|
475 |
'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
|
476 |
'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
|
477 |
self.commit() |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1787
diff
changeset
|
478 |
# 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
|
479 |
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
|
480 |
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
|
481 |
'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
|
482 |
'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
|
483 |
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
|
484 |
|
0 | 485 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
486 |
class WorkflowHooksTC(CubicWebTC): |
0 | 487 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
488 |
def setup_database(self): |
0 | 489 |
self.s_activated = self.execute('State X WHERE X name "activated"')[0][0] |
490 |
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
|
491 |
self.s_dummy = self.execute('INSERT State X: X name "dummy", X state_of E WHERE E name "CWUser"')[0][0] |
0 | 492 |
self.create_user('stduser') |
493 |
# give access to users group on the user's wf transitions |
|
494 |
# so we can test wf enforcing on euser (managers don't have anymore this |
|
495 |
# enforcement |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
496 |
self.execute('SET X require_group G WHERE G name "users", X transition_of ET, ET name "CWUser"') |
0 | 497 |
|
498 |
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
|
499 |
ueid = self.execute('INSERT CWUser E: E login "x", E upassword "x", E in_group G ' |
0 | 500 |
'WHERE G name "users"')[0][0] |
501 |
self.failIf(self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s', |
|
502 |
{'x' : ueid})) |
|
503 |
self.commit() |
|
504 |
initialstate = self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s', |
|
505 |
{'x' : ueid})[0][0] |
|
506 |
self.assertEquals(initialstate, u'activated') |
|
1787 | 507 |
|
0 | 508 |
def test_initial_state(self): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
509 |
self.login('stduser') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
510 |
self.assertRaises(ValidationError, self.execute, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
511 |
'INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, ' |
0 | 512 |
'X in_state S WHERE S name "deactivated"', {'pwd': 'oops'}) |
513 |
# though managers can do whatever he want |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
514 |
self.restore_connection() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1251
diff
changeset
|
515 |
self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, ' |
0 | 516 |
'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'}) |
517 |
self.commit() |
|
1787 | 518 |
|
0 | 519 |
# test that the workflow is correctly enforced |
520 |
def test_transition_checking1(self): |
|
521 |
cnx = self.login('stduser') |
|
522 |
cu = cnx.cursor() |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
523 |
ueid = cnx.user(self.session).eid |
0 | 524 |
self.assertRaises(ValidationError, |
525 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
526 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
527 |
cnx.close() |
|
1787 | 528 |
|
0 | 529 |
def test_transition_checking2(self): |
530 |
cnx = self.login('stduser') |
|
531 |
cu = cnx.cursor() |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
532 |
ueid = cnx.user(self.session).eid |
0 | 533 |
self.assertRaises(ValidationError, |
534 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
535 |
{'x': ueid, 's': self.s_dummy}, 'x') |
|
536 |
cnx.close() |
|
1787 | 537 |
|
0 | 538 |
def test_transition_checking3(self): |
539 |
cnx = self.login('stduser') |
|
540 |
cu = cnx.cursor() |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
541 |
ueid = cnx.user(self.session).eid |
0 | 542 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
543 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
544 |
cnx.commit() |
|
545 |
self.assertRaises(ValidationError, |
|
546 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
547 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
548 |
# get back now |
|
549 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
550 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
551 |
cnx.commit() |
|
552 |
cnx.close() |
|
1787 | 553 |
|
0 | 554 |
def test_transition_checking4(self): |
555 |
cnx = self.login('stduser') |
|
556 |
cu = cnx.cursor() |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
557 |
ueid = cnx.user(self.session).eid |
0 | 558 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
559 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
560 |
cnx.commit() |
|
561 |
self.assertRaises(ValidationError, |
|
562 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
563 |
{'x': ueid, 's': self.s_dummy}, 'x') |
|
564 |
# get back now |
|
565 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
566 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
567 |
cnx.commit() |
|
568 |
cnx.close() |
|
569 |
||
570 |
def test_transition_information(self): |
|
571 |
ueid = self.session.user.eid |
|
572 |
self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
573 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
574 |
self.commit() |
|
575 |
rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
576 |
self.assertEquals(len(rset), 2) |
|
577 |
tr = rset.get_entity(1, 0) |
|
578 |
#tr.complete() |
|
579 |
self.assertEquals(tr.comment, None) |
|
580 |
self.assertEquals(tr.from_state[0].eid, self.s_activated) |
|
581 |
self.assertEquals(tr.to_state[0].eid, self.s_deactivated) |
|
1787 | 582 |
|
0 | 583 |
self.session.set_shared_data('trcomment', u'il est pas sage celui-la') |
584 |
self.session.set_shared_data('trcommentformat', u'text/plain') |
|
585 |
self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
586 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
587 |
self.commit() |
|
588 |
rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
589 |
self.assertEquals(len(rset), 3) |
|
590 |
tr = rset.get_entity(2, 0) |
|
591 |
#tr.complete() |
|
592 |
self.assertEquals(tr.comment, u'il est pas sage celui-la') |
|
593 |
self.assertEquals(tr.comment_format, u'text/plain') |
|
594 |
self.assertEquals(tr.from_state[0].eid, self.s_deactivated) |
|
595 |
self.assertEquals(tr.to_state[0].eid, self.s_activated) |
|
596 |
self.assertEquals(tr.owned_by[0].login, 'admin') |
|
597 |
||
598 |
def test_transition_information_on_creation(self): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2746
diff
changeset
|
599 |
ueid = self.create_user('toto').eid |
0 | 600 |
rset = self.execute('TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
601 |
self.assertEquals(len(rset), 1) |
|
602 |
tr = rset.get_entity(0, 0) |
|
603 |
#tr.complete() |
|
604 |
self.assertEquals(tr.comment, None) |
|
605 |
self.assertEquals(tr.from_state, []) |
|
606 |
self.assertEquals(tr.to_state[0].eid, self.s_activated) |
|
607 |
||
608 |
def test_std_users_can_create_trinfo(self): |
|
609 |
self.create_user('toto') |
|
610 |
cnx = self.login('toto') |
|
611 |
cu = cnx.cursor() |
|
612 |
self.failUnless(cu.execute("INSERT Note X: X type 'a', X in_state S WHERE S name 'todo'")) |
|
613 |
cnx.commit() |
|
1787 | 614 |
|
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2454
diff
changeset
|
615 |
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
|
616 |
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
|
617 |
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
|
618 |
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
|
619 |
|
2454
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
620 |
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
|
621 |
_now = datetime.now() |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
622 |
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
|
623 |
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
|
624 |
'X creation_date CD, ' |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
625 |
'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
|
626 |
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
|
627 |
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
|
628 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
629 |
def test_metadata__date(self): |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
630 |
_now = datetime.now() |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
631 |
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
|
632 |
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
|
633 |
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
|
634 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
635 |
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
|
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 |
self.commit() # fire operations |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
638 |
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
|
639 |
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
|
640 |
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
|
641 |
|
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
642 |
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
|
643 |
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
|
644 |
self.commit() # fire operations |
3648b718a0d3
add simple tests for standard metadata hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2453
diff
changeset
|
645 |
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
|
646 |
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
|
647 |
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
|
648 |
|
0 | 649 |
if __name__ == '__main__': |
650 |
unittest_main() |