author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 02 Sep 2009 10:23:38 +0200 | |
branch | 3.5 |
changeset 3084 | 096d680c9da2 |
parent 2920 | 64322aa83a1d |
child 2968 | 0e3460341023 |
child 3140 | 1f64e9a5e1f8 |
child 3204 | 0b766b8a13e1 |
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 |
||
61 |
class EmailAddressTC(BaseEntityTC): |
|
62 |
def test_canonical_form(self): |
|
63 |
eid1 = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0] |
|
64 |
eid2 = self.execute('INSERT EmailAddress X: X address "maarten@philips.com", X canonical TRUE')[0][0] |
|
65 |
self.execute('SET X identical_to Y WHERE X eid %s, Y eid %s' % (eid1, eid2)) |
|
66 |
email1 = self.entity('Any X WHERE X eid %(x)s', {'x':eid1}, 'x') |
|
67 |
email2 = self.entity('Any X WHERE X eid %(x)s', {'x':eid2}, 'x') |
|
68 |
self.assertEquals(email1.canonical_form().eid, eid2) |
|
69 |
self.assertEquals(email2.canonical_form(), email2) |
|
70 |
eid3 = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0] |
|
71 |
email3 = self.entity('Any X WHERE X eid %s'%eid3) |
|
72 |
self.assertEquals(email3.canonical_form(), None) |
|
73 |
||
74 |
def test_mangling(self): |
|
75 |
eid = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0] |
|
76 |
email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x') |
|
77 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne@philips.com') |
|
78 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne@philips.com') |
|
79 |
self.vreg.config.global_set_option('mangle-emails', True) |
|
80 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne at philips dot com') |
|
81 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne at philips dot com') |
|
82 |
eid = self.execute('INSERT EmailAddress X: X address "syt"')[0][0] |
|
83 |
email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x') |
|
84 |
self.assertEquals(email.display_address(), 'syt') |
|
85 |
self.assertEquals(email.printable_value('address'), 'syt') |
|
86 |
||
87 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
88 |
class CWUserTC(BaseEntityTC): |
1642 | 89 |
|
0 | 90 |
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
|
91 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 92 |
e.complete() |
93 |
||
94 |
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
|
95 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 96 |
self.failUnless(e.matching_groups('managers')) |
97 |
self.failIf(e.matching_groups('xyz')) |
|
98 |
self.failUnless(e.matching_groups(('xyz', 'managers'))) |
|
99 |
self.failIf(e.matching_groups(('xyz', 'abcd'))) |
|
100 |
||
101 |
||
102 |
class InterfaceTC(EnvBasedTC): |
|
103 |
||
104 |
def test_nonregr_subclasses_and_mixins_interfaces(self): |
|
2660
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
105 |
self.failUnless(implements(CWUser, IWorkflowable)) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
106 |
class MyUser(CWUser): |
0 | 107 |
__implements__ = (IMileStone,) |
1358 | 108 |
self.vreg._loadedmods[__name__] = {} |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
109 |
self.vreg.register_appobject_class(MyUser) |
2660
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
110 |
self.vreg['etypes'].initialization_completed() |
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
111 |
MyUser_ = self.vreg['etypes'].etype_class('CWUser') |
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
112 |
self.failUnless(MyUser is MyUser_) |
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
113 |
self.failUnless(implements(MyUser_, IMileStone)) |
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
114 |
self.failUnless(implements(MyUser_, IWorkflowable)) |
0 | 115 |
|
116 |
||
117 |
class SpecializedEntityClassesTC(EnvBasedTC): |
|
118 |
||
119 |
def select_eclass(self, etype): |
|
120 |
# clear selector cache |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
121 |
clear_cache(self.vreg['etypes'], 'etype_class') |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
122 |
return self.vreg['etypes'].etype_class(etype) |
1642 | 123 |
|
0 | 124 |
def test_etype_class_selection_and_specialization(self): |
125 |
# no specific class for Subdivisions, the default one should be selected |
|
126 |
eclass = self.select_eclass('SubDivision') |
|
127 |
self.failUnless(eclass.__autogenerated__) |
|
128 |
#self.assertEquals(eclass.__bases__, (AnyEntity,)) |
|
129 |
# build class from most generic to most specific and make |
|
130 |
# sure the most specific is always selected |
|
1358 | 131 |
self.vreg._loadedmods[__name__] = {} |
0 | 132 |
for etype in ('Company', 'Division', 'SubDivision'): |
133 |
class Foo(AnyEntity): |
|
134 |
id = etype |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
135 |
self.vreg.register_appobject_class(Foo) |
0 | 136 |
eclass = self.select_eclass('SubDivision') |
137 |
if etype == 'SubDivision': |
|
138 |
self.failUnless(eclass is Foo) |
|
139 |
else: |
|
140 |
self.failUnless(eclass.__autogenerated__) |
|
141 |
self.assertEquals(eclass.__bases__, (Foo,)) |
|
142 |
# check Division eclass is still selected for plain Division entities |
|
143 |
eclass = self.select_eclass('Division') |
|
144 |
self.assertEquals(eclass.id, 'Division') |
|
1642 | 145 |
|
0 | 146 |
if __name__ == '__main__': |
147 |
unittest_main() |