author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 12 Oct 2009 13:16:08 +0200 | |
branch | stable |
changeset 3643 | 8513fd032d1f |
parent 3316 | c4c07aab1c39 |
child 3369 | 7b88d12b4ee2 |
child 3830 | 3b6bbb3a3c3e |
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 |
|
19 |
from cubicweb.web.widgets import AutoCompletionWidget |
|
20 |
||
21 |
||
22 |
class BaseEntityTC(EnvBasedTC): |
|
23 |
||
24 |
def setup_database(self): |
|
25 |
self.member = self.create_user('member') |
|
1642 | 26 |
|
27 |
||
28 |
||
0 | 29 |
class MetadataTC(BaseEntityTC): |
30 |
||
31 |
def test_creator(self): |
|
32 |
self.login(u'member') |
|
1642 | 33 |
entity = self.add_entity('Bookmark', title=u"hello", path=u'project/cubicweb') |
0 | 34 |
self.commit() |
1642 | 35 |
self.assertEquals(entity.creator.eid, self.member.eid) |
36 |
self.assertEquals(entity.dc_creator(), u'member') |
|
0 | 37 |
|
38 |
def test_type(self): |
|
1642 | 39 |
self.assertEquals(self.member.dc_type(), 'cwuser') |
0 | 40 |
|
1102
054dfee29bfc
some test for meta attributes (XXX should be in yams)
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
41 |
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
|
42 |
# 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
|
43 |
self.assertEquals(self.schema['CWUser'].meta_attributes(), {}) |
1642 | 44 |
self.assertEquals(dict((str(k), v) for k, v in self.schema['State'].meta_attributes().iteritems()), |
45 |
{'description_format': ('format', 'description')}) |
|
46 |
||
0 | 47 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
48 |
class CWUserTC(BaseEntityTC): |
0 | 49 |
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
|
50 |
e = self.entity('CWUser U WHERE U login "member"') |
0 | 51 |
self.assertEquals(e.dc_title(), 'member') |
52 |
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
|
53 |
self.execute(u'SET X firstname "bouah" WHERE X is CWUser, X login "member"') |
0 | 54 |
self.assertEquals(e.dc_title(), 'member') |
55 |
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
|
56 |
self.execute(u'SET X surname "lôt" WHERE X is CWUser, X login "member"') |
0 | 57 |
self.assertEquals(e.dc_title(), 'member') |
58 |
self.assertEquals(e.name(), u'bouah lôt') |
|
59 |
||
60 |
class EmailAddressTC(BaseEntityTC): |
|
61 |
def test_canonical_form(self): |
|
3204
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
62 |
email1 = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"').get_entity(0, 0) |
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
63 |
email2 = self.execute('INSERT EmailAddress X: X address "maarten@philips.com"').get_entity(0, 0) |
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
64 |
email3 = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"').get_entity(0, 0) |
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
65 |
self.execute('SET X prefered_form Y WHERE X eid %s, Y eid %s' % (email1.eid, email2.eid)) |
3206
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
66 |
self.assertEquals(email1.prefered.eid, email2.eid) |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
67 |
self.assertEquals(email2.prefered.eid, email2.eid) |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
68 |
self.assertEquals(email3.prefered.eid, email3.eid) |
0 | 69 |
|
70 |
def test_mangling(self): |
|
71 |
eid = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0] |
|
72 |
email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x') |
|
73 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne@philips.com') |
|
74 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne@philips.com') |
|
75 |
self.vreg.config.global_set_option('mangle-emails', True) |
|
76 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne at philips dot com') |
|
77 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne at philips dot com') |
|
78 |
eid = self.execute('INSERT EmailAddress X: X address "syt"')[0][0] |
|
79 |
email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x') |
|
80 |
self.assertEquals(email.display_address(), 'syt') |
|
81 |
self.assertEquals(email.printable_value('address'), 'syt') |
|
82 |
||
83 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
84 |
class CWUserTC(BaseEntityTC): |
1642 | 85 |
|
0 | 86 |
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
|
87 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 88 |
e.complete() |
89 |
||
90 |
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
|
91 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 92 |
self.failUnless(e.matching_groups('managers')) |
93 |
self.failIf(e.matching_groups('xyz')) |
|
94 |
self.failUnless(e.matching_groups(('xyz', 'managers'))) |
|
95 |
self.failIf(e.matching_groups(('xyz', 'abcd'))) |
|
96 |
||
97 |
||
98 |
class InterfaceTC(EnvBasedTC): |
|
99 |
||
100 |
def test_nonregr_subclasses_and_mixins_interfaces(self): |
|
3140
1f64e9a5e1f8
update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
101 |
CWUser = self.vreg['etypes'].etype_class('CWUser') |
2660
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
102 |
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
|
103 |
class MyUser(CWUser): |
0 | 104 |
__implements__ = (IMileStone,) |
1358 | 105 |
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
|
106 |
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
|
107 |
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
|
108 |
MyUser_ = self.vreg['etypes'].etype_class('CWUser') |
3206
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
109 |
# a copy is done systematically |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
110 |
self.failUnless(issubclass(MyUser_, MyUser)) |
2660
0d0924d75849
[test] update test: class initialization done in etype_class now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
111 |
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
|
112 |
self.failUnless(implements(MyUser_, IWorkflowable)) |
3206
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
113 |
# original class should not have beed modified, only the copy |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
114 |
self.failUnless(implements(MyUser, IMileStone)) |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
115 |
self.failIf(implements(MyUser, IWorkflowable)) |
0 | 116 |
|
117 |
||
118 |
class SpecializedEntityClassesTC(EnvBasedTC): |
|
119 |
||
120 |
def select_eclass(self, etype): |
|
121 |
# 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
|
122 |
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
|
123 |
return self.vreg['etypes'].etype_class(etype) |
1642 | 124 |
|
0 | 125 |
def test_etype_class_selection_and_specialization(self): |
126 |
# no specific class for Subdivisions, the default one should be selected |
|
127 |
eclass = self.select_eclass('SubDivision') |
|
128 |
self.failUnless(eclass.__autogenerated__) |
|
129 |
#self.assertEquals(eclass.__bases__, (AnyEntity,)) |
|
130 |
# build class from most generic to most specific and make |
|
131 |
# sure the most specific is always selected |
|
1358 | 132 |
self.vreg._loadedmods[__name__] = {} |
0 | 133 |
for etype in ('Company', 'Division', 'SubDivision'): |
134 |
class Foo(AnyEntity): |
|
135 |
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
|
136 |
self.vreg.register_appobject_class(Foo) |
0 | 137 |
eclass = self.select_eclass('SubDivision') |
3140
1f64e9a5e1f8
update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
138 |
self.failUnless(eclass.__autogenerated__) |
1f64e9a5e1f8
update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
139 |
self.failIf(eclass is Foo) |
0 | 140 |
if etype == 'SubDivision': |
3140
1f64e9a5e1f8
update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
141 |
self.assertEquals(eclass.__bases__, (Foo,)) |
0 | 142 |
else: |
3140
1f64e9a5e1f8
update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
143 |
self.assertEquals(eclass.__bases__[0].__bases__, (Foo,)) |
0 | 144 |
# check Division eclass is still selected for plain Division entities |
145 |
eclass = self.select_eclass('Division') |
|
146 |
self.assertEquals(eclass.id, 'Division') |
|
1642 | 147 |
|
0 | 148 |
if __name__ == '__main__': |
149 |
unittest_main() |