author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 09 Mar 2010 08:59:43 +0100 | |
changeset 4835 | 13b0b96d7982 |
parent 4491 | a0f48c31b58a |
child 5174 | 78438ad513ca |
child 5421 | 8167de96c523 |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3831
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
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 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2660
diff
changeset
|
14 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 15 |
|
16 |
from cubicweb import ValidationError |
|
17 |
from cubicweb.interfaces import IMileStone, IWorkflowable |
|
18 |
from cubicweb.entities import AnyEntity |
|
19 |
||
20 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2660
diff
changeset
|
21 |
class BaseEntityTC(CubicWebTC): |
0 | 22 |
|
23 |
def setup_database(self): |
|
24 |
self.member = self.create_user('member') |
|
1642 | 25 |
|
26 |
||
27 |
||
0 | 28 |
class MetadataTC(BaseEntityTC): |
29 |
||
30 |
def test_creator(self): |
|
31 |
self.login(u'member') |
|
4024
6a14cff373c3
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
32 |
entity = self.request().create_entity('Bookmark', title=u"hello", path=u'project/cubicweb') |
0 | 33 |
self.commit() |
1642 | 34 |
self.assertEquals(entity.creator.eid, self.member.eid) |
35 |
self.assertEquals(entity.dc_creator(), u'member') |
|
0 | 36 |
|
37 |
def test_type(self): |
|
1642 | 38 |
self.assertEquals(self.member.dc_type(), 'cwuser') |
0 | 39 |
|
1102
054dfee29bfc
some test for meta attributes (XXX should be in yams)
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
40 |
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
|
41 |
# 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
|
42 |
self.assertEquals(self.schema['CWUser'].meta_attributes(), {}) |
1642 | 43 |
self.assertEquals(dict((str(k), v) for k, v in self.schema['State'].meta_attributes().iteritems()), |
44 |
{'description_format': ('format', 'description')}) |
|
45 |
||
0 | 46 |
|
47 |
class EmailAddressTC(BaseEntityTC): |
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
email3 = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"').get_entity(0, 0) |
3831
5eb369d24e7d
benefit from the orm for clearer code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3830
diff
changeset
|
52 |
email1.set_relations(prefered_form=email2) |
3206
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
53 |
self.assertEquals(email1.prefered.eid, email2.eid) |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
54 |
self.assertEquals(email2.prefered.eid, email2.eid) |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
55 |
self.assertEquals(email3.prefered.eid, email3.eid) |
0 | 56 |
|
57 |
def test_mangling(self): |
|
3831
5eb369d24e7d
benefit from the orm for clearer code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3830
diff
changeset
|
58 |
email = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"').get_entity(0, 0) |
0 | 59 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne@philips.com') |
60 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne@philips.com') |
|
61 |
self.vreg.config.global_set_option('mangle-emails', True) |
|
62 |
self.assertEquals(email.display_address(), 'maarten.ter.huurne at philips dot com') |
|
63 |
self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne at philips dot com') |
|
3831
5eb369d24e7d
benefit from the orm for clearer code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3830
diff
changeset
|
64 |
email = self.execute('INSERT EmailAddress X: X address "syt"').get_entity(0, 0) |
0 | 65 |
self.assertEquals(email.display_address(), 'syt') |
66 |
self.assertEquals(email.printable_value('address'), 'syt') |
|
67 |
||
68 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1358
diff
changeset
|
69 |
class CWUserTC(BaseEntityTC): |
1642 | 70 |
|
0 | 71 |
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
|
72 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 73 |
e.complete() |
74 |
||
75 |
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
|
76 |
e = self.entity('CWUser X WHERE X login "admin"') |
0 | 77 |
self.failUnless(e.matching_groups('managers')) |
78 |
self.failIf(e.matching_groups('xyz')) |
|
79 |
self.failUnless(e.matching_groups(('xyz', 'managers'))) |
|
80 |
self.failIf(e.matching_groups(('xyz', 'abcd'))) |
|
81 |
||
3830
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
82 |
def test_dc_title_and_name(self): |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
83 |
e = self.entity('CWUser U WHERE U login "member"') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
84 |
self.assertEquals(e.dc_title(), 'member') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
85 |
self.assertEquals(e.name(), 'member') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
86 |
e.set_attributes(firstname=u'bouah') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
87 |
self.assertEquals(e.dc_title(), 'member') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
88 |
self.assertEquals(e.name(), u'bouah') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
89 |
e.set_attributes(surname=u'lôt') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
90 |
self.assertEquals(e.dc_title(), 'member') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
91 |
self.assertEquals(e.name(), u'bouah lôt') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
92 |
|
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
93 |
def test_allowed_massmail_keys(self): |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
94 |
e = self.entity('CWUser U WHERE U login "member"') |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
95 |
# Bytes/Password attributes should be omited |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
96 |
self.assertEquals(e.allowed_massmail_keys(), |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
97 |
set(('surname', 'firstname', 'login', 'last_login_time', |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
98 |
'creation_date', 'modification_date', 'cwuri', 'eid')) |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
99 |
) |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
100 |
|
0 | 101 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2660
diff
changeset
|
102 |
class InterfaceTC(CubicWebTC): |
0 | 103 |
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
class MyUser(CWUser): |
0 | 108 |
__implements__ = (IMileStone,) |
1358 | 109 |
self.vreg._loadedmods[__name__] = {} |
4491
a0f48c31b58a
kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
110 |
self.vreg.register(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.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
|
112 |
MyUser_ = self.vreg['etypes'].etype_class('CWUser') |
3206
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
113 |
# a copy is done systematically |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
self.failUnless(implements(MyUser_, IWorkflowable)) |
3206
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
117 |
# original class should not have beed modified, only the copy |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
118 |
self.failUnless(implements(MyUser, IMileStone)) |
5a51ac97f1d3
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
119 |
self.failIf(implements(MyUser, IWorkflowable)) |
0 | 120 |
|
121 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2660
diff
changeset
|
122 |
class SpecializedEntityClassesTC(CubicWebTC): |
0 | 123 |
|
124 |
def select_eclass(self, etype): |
|
125 |
# 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
|
126 |
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
|
127 |
return self.vreg['etypes'].etype_class(etype) |
1642 | 128 |
|
0 | 129 |
def test_etype_class_selection_and_specialization(self): |
130 |
# no specific class for Subdivisions, the default one should be selected |
|
131 |
eclass = self.select_eclass('SubDivision') |
|
132 |
self.failUnless(eclass.__autogenerated__) |
|
133 |
#self.assertEquals(eclass.__bases__, (AnyEntity,)) |
|
134 |
# build class from most generic to most specific and make |
|
135 |
# sure the most specific is always selected |
|
1358 | 136 |
self.vreg._loadedmods[__name__] = {} |
0 | 137 |
for etype in ('Company', 'Division', 'SubDivision'): |
138 |
class Foo(AnyEntity): |
|
3450
265331a0920c
use __regid__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
139 |
__regid__ = etype |
4491
a0f48c31b58a
kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
140 |
self.vreg.register(Foo) |
0 | 141 |
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
|
142 |
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
|
143 |
self.failIf(eclass is Foo) |
0 | 144 |
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
|
145 |
self.assertEquals(eclass.__bases__, (Foo,)) |
0 | 146 |
else: |
3140
1f64e9a5e1f8
update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
147 |
self.assertEquals(eclass.__bases__[0].__bases__, (Foo,)) |
0 | 148 |
# check Division eclass is still selected for plain Division entities |
149 |
eclass = self.select_eclass('Division') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
150 |
self.assertEquals(eclass.__regid__, 'Division') |
1642 | 151 |
|
0 | 152 |
if __name__ == '__main__': |
153 |
unittest_main() |