entities/test/unittest_base.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 27 Nov 2009 09:36:42 +0100
branchstable
changeset 3924 4347654979e8
parent 3831 5eb369d24e7d
child 3890 d7a270f50f54
child 4212 ab6573088b4a
permissions -rw-r--r--
don't type __maineid, it may not be an eid actually
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
from logilab.common.testlib import unittest_main
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
from logilab.common.decorators import clear_cache
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from logilab.common.interface import implements
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
from cubicweb.devtools.apptest import EnvBasedTC
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
from cubicweb import ValidationError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
from cubicweb.interfaces import IMileStone, IWorkflowable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
from cubicweb.entities import AnyEntity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from cubicweb.web.widgets import AutoCompletionWidget
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
class BaseEntityTC(EnvBasedTC):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    def setup_database(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
        self.member = self.create_user('member')
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    26
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    27
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    28
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
class MetadataTC(BaseEntityTC):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    def test_creator(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        self.login(u'member')
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    33
        entity = self.add_entity('Bookmark', title=u"hello", path=u'project/cubicweb')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        self.commit()
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    35
        self.assertEquals(entity.creator.eid, self.member.eid)
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    36
        self.assertEquals(entity.dc_creator(), u'member')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    def test_type(self):
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    39
        self.assertEquals(self.member.dc_type(), 'cwuser')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    44
        self.assertEquals(dict((str(k), v) for k, v in self.schema['State'].meta_attributes().iteritems()),
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    45
                          {'description_format': ('format', 'description')})
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    46
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
class EmailAddressTC(BaseEntityTC):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    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
    50
        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
    51
        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
    52
        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
    53
        email1.set_relations(prefered_form=email2)
3206
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
    54
        self.assertEquals(email1.prefered.eid, email2.eid)
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
    55
        self.assertEquals(email2.prefered.eid, email2.eid)
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
    56
        self.assertEquals(email3.prefered.eid, email3.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    def test_mangling(self):
3831
5eb369d24e7d benefit from the orm for clearer code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3830
diff changeset
    59
        email = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"').get_entity(0, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        self.assertEquals(email.display_address(), 'maarten.ter.huurne@philips.com')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        self.assertEquals(email.printable_value('address'), 'maarten.ter.huurne@philips.com')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        self.vreg.config.global_set_option('mangle-emails', True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        self.assertEquals(email.display_address(), 'maarten.ter.huurne at philips dot com')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        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
    65
        email = self.execute('INSERT EmailAddress X: X address "syt"').get_entity(0, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        self.assertEquals(email.display_address(), 'syt')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        self.assertEquals(email.printable_value('address'), 'syt')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1358
diff changeset
    70
class CWUserTC(BaseEntityTC):
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    71
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    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
    73
        e = self.entity('CWUser X WHERE X login "admin"')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        e.complete()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    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
    77
        e = self.entity('CWUser X WHERE X login "admin"')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        self.failUnless(e.matching_groups('managers'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        self.failIf(e.matching_groups('xyz'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        self.failUnless(e.matching_groups(('xyz', 'managers')))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        self.failIf(e.matching_groups(('xyz', 'abcd')))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
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
    83
    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
    84
        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
    85
        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
    86
        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
    87
        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
    88
        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
    89
        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
    90
        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
    91
        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
    92
        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
    93
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
    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
    95
        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
    96
        # 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
    97
        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
    98
                          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
    99
                               '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
   100
                          )
3b6bbb3a3c3e close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3316
diff changeset
   101
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
class InterfaceTC(EnvBasedTC):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    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
   106
        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
   107
        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
   108
        class MyUser(CWUser):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
            __implements__ = (IMileStone,)
1358
a7438eb2420c test update
sylvain.thenault@logilab.fr
parents: 1102
diff changeset
   110
        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
   111
        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
   112
        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
   113
        MyUser_ = self.vreg['etypes'].etype_class('CWUser')
3206
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
   114
        # a copy is done systematically
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
   115
        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
   116
        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
   117
        self.failUnless(implements(MyUser_, IWorkflowable))
3206
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
   118
        # original class should not have beed modified, only the copy
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
   119
        self.failUnless(implements(MyUser, IMileStone))
5a51ac97f1d3 tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3204
diff changeset
   120
        self.failIf(implements(MyUser, IWorkflowable))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
class SpecializedEntityClassesTC(EnvBasedTC):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
    def select_eclass(self, etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
        # 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
   127
        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
   128
        return self.vreg['etypes'].etype_class(etype)
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   129
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
    def test_etype_class_selection_and_specialization(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        # no specific class for Subdivisions, the default one should be selected
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        eclass = self.select_eclass('SubDivision')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        self.failUnless(eclass.__autogenerated__)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        #self.assertEquals(eclass.__bases__, (AnyEntity,))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        # build class from most generic to most specific and make
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        # sure the most specific is always selected
1358
a7438eb2420c test update
sylvain.thenault@logilab.fr
parents: 1102
diff changeset
   137
        self.vreg._loadedmods[__name__] = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        for etype in ('Company', 'Division', 'SubDivision'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
            class Foo(AnyEntity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
                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
   141
            self.vreg.register_appobject_class(Foo)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
            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
   143
            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
   144
            self.failIf(eclass is Foo)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
            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
   146
                self.assertEquals(eclass.__bases__, (Foo,))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
            else:
3140
1f64e9a5e1f8 update test now that etype_class systematically copy registry classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   148
                self.assertEquals(eclass.__bases__[0].__bases__, (Foo,))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        # check Division eclass is still selected for plain Division entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        eclass = self.select_eclass('Division')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        self.assertEquals(eclass.id, 'Division')
1642
12a98b17fb05 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   152
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
    unittest_main()