author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 07 Aug 2009 12:20:37 +0200 | |
branch | stable |
changeset 2727 | 5275d015834c |
parent 1977 | 606923dff11b |
child 2650 | 18aec79ec3a3 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
2 |
"""unit tests for cubicweb.entities.base module |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1642
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
from logilab.common.testlib import unittest_main |
|
11 |
from logilab.common.decorators import clear_cache |
|
12 |
from logilab.common.interface import implements |
|
13 |
||
14 |
from cubicweb.devtools.apptest import EnvBasedTC |
|
15 |
||
16 |
from cubicweb import ValidationError |
|
17 |
from cubicweb.interfaces import IMileStone, IWorkflowable |
|
18 |
from cubicweb.entities import AnyEntity |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
19 |
from cubicweb.entities.authobjs import CWUser |
0 | 20 |
from cubicweb.web.widgets import AutoCompletionWidget |
21 |
||
22 |
||
23 |
class BaseEntityTC(EnvBasedTC): |
|
24 |
||
25 |
def setup_database(self): |
|
26 |
self.member = self.create_user('member') |
|
1642 | 27 |
|
28 |
||
29 |
||
0 | 30 |
class MetadataTC(BaseEntityTC): |
31 |
||
32 |
def test_creator(self): |
|
33 |
self.login(u'member') |
|
1642 | 34 |
entity = self.add_entity('Bookmark', title=u"hello", path=u'project/cubicweb') |
0 | 35 |
self.commit() |
1642 | 36 |
self.assertEquals(entity.creator.eid, self.member.eid) |
37 |
self.assertEquals(entity.dc_creator(), u'member') |
|
0 | 38 |
|
39 |
def test_type(self): |
|
1642 | 40 |
self.assertEquals(self.member.dc_type(), 'cwuser') |
0 | 41 |
|
1102
054dfee29bfc
some test for meta attributes (XXX should be in yams)
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
42 |
def test_entity_meta_attributes(self): |
054dfee29bfc
some test for meta attributes (XXX should be in yams)
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
43 |
# XXX move to yams |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
44 |
self.assertEquals(self.schema['CWUser'].meta_attributes(), {}) |
1642 | 45 |
self.assertEquals(dict((str(k), v) for k, v in self.schema['State'].meta_attributes().iteritems()), |
46 |
{'description_format': ('format', 'description')}) |
|
47 |
||
0 | 48 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
49 |
class CWUserTC(BaseEntityTC): |
0 | 50 |
def test_dc_title_and_name(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
51 |
e = self.entity('CWUser U WHERE U login "member"') |
0 | 52 |
self.assertEquals(e.dc_title(), 'member') |
53 |
self.assertEquals(e.name(), 'member') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
54 |
self.execute(u'SET X firstname "bouah" WHERE X is CWUser, X login "member"') |
0 | 55 |
self.assertEquals(e.dc_title(), 'member') |
56 |
self.assertEquals(e.name(), u'bouah') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
57 |
self.execute(u'SET X surname "lôt" WHERE X is CWUser, X login "member"') |
0 | 58 |
self.assertEquals(e.dc_title(), 'member') |
59 |
self.assertEquals(e.name(), u'bouah lôt') |
|
60 |
||
1642 | 61 |
|
0 | 62 |
class StateAndTransitionsTC(BaseEntityTC): |
1642 | 63 |
|
0 | 64 |
def test_transitions(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
65 |
user = self.entity('CWUser X') |
0 | 66 |
e = self.entity('State S WHERE S name "activated"') |
67 |
trs = list(e.transitions(user)) |
|
68 |
self.assertEquals(len(trs), 1) |
|
69 |
self.assertEquals(trs[0].name, u'deactivate') |
|
70 |
self.assertEquals(trs[0].destination().name, u'deactivated') |
|
71 |
self.assert_(user.can_pass_transition('deactivate')) |
|
72 |
self.assert_(not user.can_pass_transition('activate')) |
|
73 |
# test a std user get no possible transition |
|
74 |
self.login('member') |
|
75 |
# fetch the entity using the new session |
|
76 |
e = self.entity('State S WHERE S name "activated"') |
|
77 |
trs = list(e.transitions(user)) |
|
78 |
self.assertEquals(len(trs), 0) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
79 |
user = self.entity('CWUser X') |
0 | 80 |
self.assert_(not user.can_pass_transition('deactivate')) |
81 |
self.assert_(not user.can_pass_transition('activate')) |
|
1642 | 82 |
|
0 | 83 |
def test_transitions_with_dest_specfied(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
84 |
user = self.entity('CWUser X') |
0 | 85 |
e = self.entity('State S WHERE S name "activated"') |
86 |
e2 = self.entity('State S WHERE S name "deactivated"') |
|
87 |
trs = list(e.transitions(user, e2.eid)) |
|
88 |
self.assertEquals(len(trs), 1) |
|
89 |
self.assertEquals(trs[0].name, u'deactivate') |
|
90 |
self.assertEquals(trs[0].destination().name, u'deactivated') |
|
91 |
trs = list(e.transitions(user, e.eid)) |
|
92 |
self.assertEquals(len(trs), 0) |
|
1642 | 93 |
|
0 | 94 |
def test_transitions_maybe_passed(self): |
95 |
self.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", ' |
|
96 |
'X expression "X owned_by U", T condition X ' |
|
97 |
'WHERE T name "deactivate"') |
|
98 |
self._test_deactivated() |
|
1642 | 99 |
|
0 | 100 |
def test_transitions_maybe_passed_using_has_update_perm(self): |
101 |
self.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", ' |
|
102 |
'X expression "U has_update_permission X", T condition X ' |
|
103 |
'WHERE T name "deactivate"') |
|
104 |
self._test_deactivated() |
|
1642 | 105 |
|
106 |
||
0 | 107 |
def _test_deactivated(self): |
108 |
ueid = self.create_user('toto').eid |
|
109 |
self.create_user('tutu') |
|
110 |
cnx = self.login('tutu') |
|
111 |
cu = cnx.cursor() |
|
112 |
self.assertRaises(ValidationError, |
|
113 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S name "deactivated"', |
|
114 |
{'x': ueid}, 'x') |
|
115 |
cnx.close() |
|
116 |
cnx = self.login('toto') |
|
117 |
cu = cnx.cursor() |
|
118 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S name "deactivated"', |
|
119 |
{'x': ueid}, 'x') |
|
120 |
cnx.commit() |
|
121 |
self.assertRaises(ValidationError, |
|
122 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S name "activated"', |
|
123 |
{'x': ueid}, 'x') |
|
1642 | 124 |
|
0 | 125 |
|
126 |
def test_transitions_selection(self): |
|
127 |
""" |
|
128 |
------------------------ tr1 ----------------- |
|
1642 | 129 |
| state1 (CWGroup, Bookmark) | ------> | state2 (CWGroup) | |
0 | 130 |
------------------------ ----------------- |
131 |
| tr2 ------------------ |
|
132 |
`------> | state3 (Bookmark) | |
|
133 |
------------------ |
|
134 |
""" |
|
135 |
state1 = self.add_entity('State', name=u'state1') |
|
136 |
state2 = self.add_entity('State', name=u'state2') |
|
137 |
state3 = self.add_entity('State', name=u'state3') |
|
138 |
tr1 = self.add_entity('Transition', name=u'tr1') |
|
139 |
tr2 = self.add_entity('Transition', name=u'tr2') |
|
1642 | 140 |
self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "CWGroup"' % |
0 | 141 |
(state1.eid, state2.eid)) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
142 |
self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "Bookmark"' % |
0 | 143 |
(state1.eid, state3.eid)) |
1642 | 144 |
self.execute('SET X transition_of Y WHERE X eid %s, Y name "CWGroup"' % tr1.eid) |
0 | 145 |
self.execute('SET X transition_of Y WHERE X eid %s, Y name "Bookmark"' % tr2.eid) |
146 |
self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' % |
|
147 |
(state1.eid, tr1.eid)) |
|
148 |
self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' % |
|
149 |
(state1.eid, tr2.eid)) |
|
150 |
self.execute('SET X destination_state Y WHERE X eid %s, Y eid %s' % |
|
151 |
(tr1.eid, state2.eid)) |
|
152 |
self.execute('SET X destination_state Y WHERE X eid %s, Y eid %s' % |
|
153 |
(tr2.eid, state3.eid)) |
|
1642 | 154 |
self.execute('SET X initial_state Y WHERE Y eid %s, X name "CWGroup"' % state1.eid) |
0 | 155 |
self.execute('SET X initial_state Y WHERE Y eid %s, X name "Bookmark"' % state1.eid) |
1642 | 156 |
group = self.add_entity('CWGroup', name=u't1') |
157 |
transitions = list(state1.transitions(group)) |
|
0 | 158 |
self.assertEquals(len(transitions), 1) |
159 |
self.assertEquals(transitions[0].name, 'tr1') |
|
1642 | 160 |
bookmark = self.add_entity('Bookmark', title=u'111', path=u'/view') |
0 | 161 |
transitions = list(state1.transitions(bookmark)) |
162 |
self.assertEquals(len(transitions), 1) |
|
163 |
self.assertEquals(transitions[0].name, 'tr2') |
|
1642 | 164 |
|
0 | 165 |
|
166 |
def test_transitions_selection2(self): |
|
167 |
""" |
|
168 |
------------------------ tr1 (Bookmark) ----------------------- |
|
1642 | 169 |
| state1 (CWGroup, Bookmark) | -------------> | state2 (CWGroup,Bookmark) | |
0 | 170 |
------------------------ ----------------------- |
1642 | 171 |
| tr2 (CWGroup) | |
0 | 172 |
`---------------------------------/ |
173 |
""" |
|
174 |
state1 = self.add_entity('State', name=u'state1') |
|
175 |
state2 = self.add_entity('State', name=u'state2') |
|
176 |
tr1 = self.add_entity('Transition', name=u'tr1') |
|
177 |
tr2 = self.add_entity('Transition', name=u'tr2') |
|
1642 | 178 |
self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "CWGroup"' % |
0 | 179 |
(state1.eid, state2.eid)) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
180 |
self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "Bookmark"' % |
0 | 181 |
(state1.eid, state2.eid)) |
1642 | 182 |
self.execute('SET X transition_of Y WHERE X eid %s, Y name "CWGroup"' % tr1.eid) |
0 | 183 |
self.execute('SET X transition_of Y WHERE X eid %s, Y name "Bookmark"' % tr2.eid) |
184 |
self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' % |
|
185 |
(state1.eid, tr1.eid)) |
|
186 |
self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' % |
|
187 |
(state1.eid, tr2.eid)) |
|
188 |
self.execute('SET X destination_state Y WHERE X eid %s, Y eid %s' % |
|
189 |
(tr1.eid, state2.eid)) |
|
190 |
self.execute('SET X destination_state Y WHERE X eid %s, Y eid %s' % |
|
191 |
(tr2.eid, state2.eid)) |
|
1642 | 192 |
self.execute('SET X initial_state Y WHERE Y eid %s, X name "CWGroup"' % state1.eid) |
0 | 193 |
self.execute('SET X initial_state Y WHERE Y eid %s, X name "Bookmark"' % state1.eid) |
1642 | 194 |
group = self.add_entity('CWGroup', name=u't1') |
195 |
transitions = list(state1.transitions(group)) |
|
0 | 196 |
self.assertEquals(len(transitions), 1) |
197 |
self.assertEquals(transitions[0].name, 'tr1') |
|
1642 | 198 |
bookmark = self.add_entity('Bookmark', title=u'111', path=u'/view') |
0 | 199 |
transitions = list(state1.transitions(bookmark)) |
200 |
self.assertEquals(len(transitions), 1) |
|
201 |
self.assertEquals(transitions[0].name, 'tr2') |
|
1642 | 202 |
|
0 | 203 |
|
204 |
class EmailAddressTC(BaseEntityTC): |
|
205 |
def test_canonical_form(self): |
|
206 |
eid1 = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0] |
|
207 |
eid2 = self.execute('INSERT EmailAddress X: X address "maarten@philips.com", X canonical TRUE')[0][0] |
|
208 |
self.execute('SET X identical_to Y WHERE X eid %s, Y eid %s' % (eid1, eid2)) |
|
209 |
email1 = self.entity('Any X WHERE X eid %(x)s', {'x':eid1}, 'x') |
|
210 |
email2 = self.entity('Any X WHERE X eid %(x)s', {'x':eid2}, 'x') |
|
211 |
self.assertEquals(email1.canonical_form().eid, eid2) |
|
212 |
self.assertEquals(email2.canonical_form(), email2) |
|
213 |
eid3 = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0] |
|
214 |
email3 = self.entity('Any X WHERE X eid %s'%eid3) |
|
215 |
self.assertEquals(email3.canonical_form(), None) |
|
216 |
||
217 |
def test_mangling(self): |
|
218 |
eid = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0] |
|
219 |
email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x') |
|
220 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne@philips.com') |
|
221 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne@philips.com') |
|
222 |
self.vreg.config.global_set_option('mangle-emails', True) |
|
223 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne at philips dot com') |
|
224 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne at philips dot com') |
|
225 |
eid = self.execute('INSERT EmailAddress X: X address "syt"')[0][0] |
|
226 |
email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x') |
|
227 |
self.assertEquals(email.display_address(), 'syt') |
|
228 |
self.assertEquals(email.printable_value('address'), 'syt') |
|
229 |
||
230 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
231 |
class CWUserTC(BaseEntityTC): |
1642 | 232 |
|
0 | 233 |
def test_complete(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
234 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 235 |
e.complete() |
236 |
||
1642 | 237 |
|
0 | 238 |
def test_matching_groups(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
239 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 240 |
self.failUnless(e.matching_groups('managers')) |
241 |
self.failIf(e.matching_groups('xyz')) |
|
242 |
self.failUnless(e.matching_groups(('xyz', 'managers'))) |
|
243 |
self.failIf(e.matching_groups(('xyz', 'abcd'))) |
|
244 |
||
245 |
def test_workflow_base(self): |
|
246 |
e = self.create_user('toto') |
|
247 |
self.assertEquals(e.state, 'activated') |
|
248 |
activatedeid = self.execute('State X WHERE X name "activated"')[0][0] |
|
249 |
deactivatedeid = self.execute('State X WHERE X name "deactivated"')[0][0] |
|
250 |
e.change_state(deactivatedeid, u'deactivate 1') |
|
251 |
self.commit() |
|
252 |
e.change_state(activatedeid, u'activate 1') |
|
253 |
self.commit() |
|
254 |
e.change_state(deactivatedeid, u'deactivate 2') |
|
255 |
self.commit() |
|
256 |
# get a fresh user to avoid potential cache issues |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
257 |
e = self.entity('CWUser X WHERE X eid %s' % e.eid) |
0 | 258 |
self.assertEquals([tr.comment for tr in e.reverse_wf_info_for], |
259 |
[None, 'deactivate 1', 'activate 1', 'deactivate 2']) |
|
260 |
self.assertEquals(e.latest_trinfo().comment, 'deactivate 2') |
|
261 |
||
262 |
||
263 |
class InterfaceTC(EnvBasedTC): |
|
264 |
||
265 |
def test_nonregr_subclasses_and_mixins_interfaces(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
266 |
class MyUser(CWUser): |
0 | 267 |
__implements__ = (IMileStone,) |
1358 | 268 |
self.vreg._loadedmods[__name__] = {} |
0 | 269 |
self.vreg.register_vobject_class(MyUser) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
270 |
self.failUnless(implements(CWUser, IWorkflowable)) |
0 | 271 |
self.failUnless(implements(MyUser, IMileStone)) |
272 |
self.failUnless(implements(MyUser, IWorkflowable)) |
|
273 |
||
274 |
||
275 |
class SpecializedEntityClassesTC(EnvBasedTC): |
|
276 |
||
277 |
def select_eclass(self, etype): |
|
278 |
# clear selector cache |
|
279 |
clear_cache(self.vreg, 'etype_class') |
|
280 |
return self.vreg.etype_class(etype) |
|
1642 | 281 |
|
0 | 282 |
def test_etype_class_selection_and_specialization(self): |
283 |
# no specific class for Subdivisions, the default one should be selected |
|
284 |
eclass = self.select_eclass('SubDivision') |
|
285 |
self.failUnless(eclass.__autogenerated__) |
|
286 |
#self.assertEquals(eclass.__bases__, (AnyEntity,)) |
|
287 |
# build class from most generic to most specific and make |
|
288 |
# sure the most specific is always selected |
|
1358 | 289 |
self.vreg._loadedmods[__name__] = {} |
0 | 290 |
for etype in ('Company', 'Division', 'SubDivision'): |
291 |
class Foo(AnyEntity): |
|
292 |
id = etype |
|
293 |
self.vreg.register_vobject_class(Foo) |
|
294 |
eclass = self.select_eclass('SubDivision') |
|
295 |
if etype == 'SubDivision': |
|
296 |
self.failUnless(eclass is Foo) |
|
297 |
else: |
|
298 |
self.failUnless(eclass.__autogenerated__) |
|
299 |
self.assertEquals(eclass.__bases__, (Foo,)) |
|
300 |
# check Division eclass is still selected for plain Division entities |
|
301 |
eclass = self.select_eclass('Division') |
|
302 |
self.assertEquals(eclass.id, 'Division') |
|
1642 | 303 |
|
0 | 304 |
if __name__ == '__main__': |
305 |
unittest_main() |