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