entities/test/unittest_base.py
branchtls-sprint
changeset 1398 5fe84a5f7035
parent 1358 a7438eb2420c
child 1642 12a98b17fb05
equal deleted inserted replaced
1397:6cbc7bc8ea6d 1398:5fe84a5f7035
     8 from cubicweb.devtools.apptest import EnvBasedTC
     8 from cubicweb.devtools.apptest import EnvBasedTC
     9 
     9 
    10 from cubicweb import ValidationError
    10 from cubicweb import ValidationError
    11 from cubicweb.interfaces import IMileStone, IWorkflowable
    11 from cubicweb.interfaces import IMileStone, IWorkflowable
    12 from cubicweb.entities import AnyEntity
    12 from cubicweb.entities import AnyEntity
    13 from cubicweb.entities.authobjs import EUser
    13 from cubicweb.entities.authobjs import CWUser
    14 from cubicweb.web.widgets import AutoCompletionWidget
    14 from cubicweb.web.widgets import AutoCompletionWidget
    15 
    15 
    16 
    16 
    17 class BaseEntityTC(EnvBasedTC):
    17 class BaseEntityTC(EnvBasedTC):
    18 
    18 
    34         self.assertEquals(self.member.dc_type(), 'euser')
    34         self.assertEquals(self.member.dc_type(), 'euser')
    35         
    35         
    36 
    36 
    37     def test_entity_meta_attributes(self):
    37     def test_entity_meta_attributes(self):
    38         # XXX move to yams
    38         # XXX move to yams
    39         self.assertEquals(self.schema['EUser'].meta_attributes(), {})
    39         self.assertEquals(self.schema['CWUser'].meta_attributes(), {})
    40         self.assertEquals(dict((str(k), v) for k, v in self.schema['Card'].meta_attributes().iteritems()),
    40         self.assertEquals(dict((str(k), v) for k, v in self.schema['Card'].meta_attributes().iteritems()),
    41                           {'content_format': ('format', 'content')})
    41                           {'content_format': ('format', 'content')})
    42         
    42         
    43 
    43 
    44 class EUserTC(BaseEntityTC):
    44 class CWUserTC(BaseEntityTC):
    45     def test_dc_title_and_name(self):
    45     def test_dc_title_and_name(self):
    46         e = self.entity('EUser U WHERE U login "member"')
    46         e = self.entity('CWUser U WHERE U login "member"')
    47         self.assertEquals(e.dc_title(), 'member')
    47         self.assertEquals(e.dc_title(), 'member')
    48         self.assertEquals(e.name(), 'member')
    48         self.assertEquals(e.name(), 'member')
    49         self.execute(u'SET X firstname "bouah" WHERE X is EUser, X login "member"')
    49         self.execute(u'SET X firstname "bouah" WHERE X is CWUser, X login "member"')
    50         self.assertEquals(e.dc_title(), 'member')
    50         self.assertEquals(e.dc_title(), 'member')
    51         self.assertEquals(e.name(), u'bouah')
    51         self.assertEquals(e.name(), u'bouah')
    52         self.execute(u'SET X surname "lôt" WHERE X is EUser, X login "member"')
    52         self.execute(u'SET X surname "lôt" WHERE X is CWUser, X login "member"')
    53         self.assertEquals(e.dc_title(), 'member')
    53         self.assertEquals(e.dc_title(), 'member')
    54         self.assertEquals(e.name(), u'bouah lôt')
    54         self.assertEquals(e.name(), u'bouah lôt')
    55 
    55 
    56     
    56     
    57 class StateAndTransitionsTC(BaseEntityTC):
    57 class StateAndTransitionsTC(BaseEntityTC):
    58         
    58         
    59     def test_transitions(self):
    59     def test_transitions(self):
    60         user = self.entity('EUser X')
    60         user = self.entity('CWUser X')
    61         e = self.entity('State S WHERE S name "activated"')
    61         e = self.entity('State S WHERE S name "activated"')
    62         trs = list(e.transitions(user))
    62         trs = list(e.transitions(user))
    63         self.assertEquals(len(trs), 1)
    63         self.assertEquals(len(trs), 1)
    64         self.assertEquals(trs[0].name, u'deactivate')
    64         self.assertEquals(trs[0].name, u'deactivate')
    65         self.assertEquals(trs[0].destination().name, u'deactivated')
    65         self.assertEquals(trs[0].destination().name, u'deactivated')
    69         self.login('member')
    69         self.login('member')
    70         # fetch the entity using the new session
    70         # fetch the entity using the new session
    71         e = self.entity('State S WHERE S name "activated"')
    71         e = self.entity('State S WHERE S name "activated"')
    72         trs = list(e.transitions(user))
    72         trs = list(e.transitions(user))
    73         self.assertEquals(len(trs), 0)
    73         self.assertEquals(len(trs), 0)
    74         user = self.entity('EUser X')
    74         user = self.entity('CWUser X')
    75         self.assert_(not user.can_pass_transition('deactivate'))
    75         self.assert_(not user.can_pass_transition('deactivate'))
    76         self.assert_(not user.can_pass_transition('activate'))
    76         self.assert_(not user.can_pass_transition('activate'))
    77         
    77         
    78     def test_transitions_with_dest_specfied(self):
    78     def test_transitions_with_dest_specfied(self):
    79         user = self.entity('EUser X')
    79         user = self.entity('CWUser X')
    80         e = self.entity('State S WHERE S name "activated"')
    80         e = self.entity('State S WHERE S name "activated"')
    81         e2 = self.entity('State S WHERE S name "deactivated"')
    81         e2 = self.entity('State S WHERE S name "deactivated"')
    82         trs = list(e.transitions(user, e2.eid))
    82         trs = list(e.transitions(user, e2.eid))
    83         self.assertEquals(len(trs), 1)
    83         self.assertEquals(len(trs), 1)
    84         self.assertEquals(trs[0].name, u'deactivate')
    84         self.assertEquals(trs[0].name, u'deactivate')
   130         state1 = self.add_entity('State', name=u'state1')
   130         state1 = self.add_entity('State', name=u'state1')
   131         state2 = self.add_entity('State', name=u'state2')
   131         state2 = self.add_entity('State', name=u'state2')
   132         state3 = self.add_entity('State', name=u'state3')
   132         state3 = self.add_entity('State', name=u'state3')
   133         tr1 = self.add_entity('Transition', name=u'tr1')
   133         tr1 = self.add_entity('Transition', name=u'tr1')
   134         tr2 = self.add_entity('Transition', name=u'tr2')
   134         tr2 = self.add_entity('Transition', name=u'tr2')
   135         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is EEType, Y name "Card"' %
   135         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "Card"' %
   136                       (state1.eid, state2.eid))
   136                       (state1.eid, state2.eid))
   137         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is EEType, Y name "Bookmark"' %
   137         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "Bookmark"' %
   138                       (state1.eid, state3.eid))
   138                       (state1.eid, state3.eid))
   139         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Card"' % tr1.eid)
   139         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Card"' % tr1.eid)
   140         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Bookmark"' % tr2.eid)
   140         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Bookmark"' % tr2.eid)
   141         self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' %
   141         self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' %
   142                       (state1.eid, tr1.eid))
   142                       (state1.eid, tr1.eid))
   169         """
   169         """
   170         state1 = self.add_entity('State', name=u'state1')
   170         state1 = self.add_entity('State', name=u'state1')
   171         state2 = self.add_entity('State', name=u'state2')
   171         state2 = self.add_entity('State', name=u'state2')
   172         tr1 = self.add_entity('Transition', name=u'tr1')
   172         tr1 = self.add_entity('Transition', name=u'tr1')
   173         tr2 = self.add_entity('Transition', name=u'tr2')
   173         tr2 = self.add_entity('Transition', name=u'tr2')
   174         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is EEType, Y name "Card"' %
   174         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "Card"' %
   175                       (state1.eid, state2.eid))
   175                       (state1.eid, state2.eid))
   176         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is EEType, Y name "Bookmark"' %
   176         self.execute('SET X state_of Y WHERE X eid in (%s, %s), Y is CWEType, Y name "Bookmark"' %
   177                       (state1.eid, state2.eid))
   177                       (state1.eid, state2.eid))
   178         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Card"' % tr1.eid)
   178         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Card"' % tr1.eid)
   179         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Bookmark"' % tr2.eid)
   179         self.execute('SET X transition_of Y WHERE X eid %s, Y name "Bookmark"' % tr2.eid)
   180         self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' %
   180         self.execute('SET X allowed_transition Y WHERE X eid %s, Y eid %s' %
   181                       (state1.eid, tr1.eid))
   181                       (state1.eid, tr1.eid))
   223         email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x')
   223         email = self.entity('Any X WHERE X eid %(x)s', {'x':eid}, 'x')
   224         self.assertEquals(email.display_address(), 'syt')
   224         self.assertEquals(email.display_address(), 'syt')
   225         self.assertEquals(email.printable_value('address'), 'syt')
   225         self.assertEquals(email.printable_value('address'), 'syt')
   226 
   226 
   227 
   227 
   228 class EUserTC(BaseEntityTC):
   228 class CWUserTC(BaseEntityTC):
   229     
   229     
   230     def test_complete(self):
   230     def test_complete(self):
   231         e = self.entity('EUser X WHERE X login "admin"')
   231         e = self.entity('CWUser X WHERE X login "admin"')
   232         e.complete()
   232         e.complete()
   233 
   233 
   234         
   234         
   235     def test_matching_groups(self):
   235     def test_matching_groups(self):
   236         e = self.entity('EUser X WHERE X login "admin"')
   236         e = self.entity('CWUser X WHERE X login "admin"')
   237         self.failUnless(e.matching_groups('managers'))
   237         self.failUnless(e.matching_groups('managers'))
   238         self.failIf(e.matching_groups('xyz'))
   238         self.failIf(e.matching_groups('xyz'))
   239         self.failUnless(e.matching_groups(('xyz', 'managers')))
   239         self.failUnless(e.matching_groups(('xyz', 'managers')))
   240         self.failIf(e.matching_groups(('xyz', 'abcd')))
   240         self.failIf(e.matching_groups(('xyz', 'abcd')))
   241 
   241 
   249         e.change_state(activatedeid, u'activate 1')
   249         e.change_state(activatedeid, u'activate 1')
   250         self.commit()
   250         self.commit()
   251         e.change_state(deactivatedeid, u'deactivate 2')
   251         e.change_state(deactivatedeid, u'deactivate 2')
   252         self.commit()
   252         self.commit()
   253         # get a fresh user to avoid potential cache issues
   253         # get a fresh user to avoid potential cache issues
   254         e = self.entity('EUser X WHERE X eid %s' % e.eid)
   254         e = self.entity('CWUser X WHERE X eid %s' % e.eid)
   255         self.assertEquals([tr.comment for tr in e.reverse_wf_info_for],
   255         self.assertEquals([tr.comment for tr in e.reverse_wf_info_for],
   256                           [None, 'deactivate 1', 'activate 1', 'deactivate 2'])
   256                           [None, 'deactivate 1', 'activate 1', 'deactivate 2'])
   257         self.assertEquals(e.latest_trinfo().comment, 'deactivate 2')
   257         self.assertEquals(e.latest_trinfo().comment, 'deactivate 2')
   258 
   258 
   259 
   259 
   260 class InterfaceTC(EnvBasedTC):
   260 class InterfaceTC(EnvBasedTC):
   261 
   261 
   262     def test_nonregr_subclasses_and_mixins_interfaces(self):
   262     def test_nonregr_subclasses_and_mixins_interfaces(self):
   263         class MyUser(EUser):
   263         class MyUser(CWUser):
   264             __implements__ = (IMileStone,)
   264             __implements__ = (IMileStone,)
   265         self.vreg._loadedmods[__name__] = {}
   265         self.vreg._loadedmods[__name__] = {}
   266         self.vreg.register_vobject_class(MyUser)
   266         self.vreg.register_vobject_class(MyUser)
   267         self.failUnless(implements(EUser, IWorkflowable))
   267         self.failUnless(implements(CWUser, IWorkflowable))
   268         self.failUnless(implements(MyUser, IMileStone))
   268         self.failUnless(implements(MyUser, IMileStone))
   269         self.failUnless(implements(MyUser, IWorkflowable))
   269         self.failUnless(implements(MyUser, IWorkflowable))
   270 
   270 
   271 
   271 
   272 class SpecializedEntityClassesTC(EnvBasedTC):
   272 class SpecializedEntityClassesTC(EnvBasedTC):