test/unittest_entity.py
changeset 2968 0e3460341023
parent 2835 04034421b072
parent 2931 17224e90a1c4
child 3163 edfe43ceaa35
equal deleted inserted replaced
2902:dd9f2dd02f85 2968:0e3460341023
    74         user = self.user()
    74         user = self.user()
    75         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
    75         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
    76         e = self.entity('Any X WHERE X eid %(x)s', {'x':user.eid}, 'x')
    76         e = self.entity('Any X WHERE X eid %(x)s', {'x':user.eid}, 'x')
    77         self.assertEquals(e.use_email[0].address, "toto@logilab.org")
    77         self.assertEquals(e.use_email[0].address, "toto@logilab.org")
    78         self.assertEquals(e.use_email[0].eid, adeleid)
    78         self.assertEquals(e.use_email[0].eid, adeleid)
    79         usereid = self.execute('INSERT CWUser X: X login "toto", X upassword "toto", X in_group G, X in_state S '
    79         usereid = self.execute('INSERT CWUser X: X login "toto", X upassword "toto", X in_group G '
    80                                'WHERE G name "users", S name "activated"')[0][0]
    80                                'WHERE G name "users"')[0][0]
    81         e = self.entity('Any X WHERE X eid %(x)s', {'x':usereid}, 'x')
    81         e = self.entity('Any X WHERE X eid %(x)s', {'x':usereid}, 'x')
    82         e.copy_relations(user.eid)
    82         e.copy_relations(user.eid)
    83         self.failIf(e.use_email)
    83         self.failIf(e.use_email)
    84         self.failIf(e.primary_email)
    84         self.failIf(e.primary_email)
    85 
    85 
    86     def test_copy_with_non_initial_state(self):
    86     def test_copy_with_non_initial_state(self):
    87         user = self.user()
    87         user = self.user()
    88         eid = self.execute('INSERT CWUser X: X login "toto", X upassword %(pwd)s, X in_group G WHERE G name "users"',
    88         user = self.execute('INSERT CWUser X: X login "toto", X upassword %(pwd)s, X in_group G WHERE G name "users"',
    89                            {'pwd': 'toto'})[0][0]
    89                            {'pwd': 'toto'}).get_entity(0, 0)
    90         self.commit()
    90         self.commit()
    91         self.execute('SET X in_state S WHERE X eid %(x)s, S name "deactivated"', {'x': eid}, 'x')
    91         user.fire_transition('deactivate')
    92         self.commit()
    92         self.commit()
    93         eid2 = self.execute('INSERT CWUser X: X login "tutu", X upassword %(pwd)s', {'pwd': 'toto'})[0][0]
    93         eid2 = self.execute('INSERT CWUser X: X login "tutu", X upassword %(pwd)s', {'pwd': 'toto'})[0][0]
    94         e = self.entity('Any X WHERE X eid %(x)s', {'x': eid2}, 'x')
    94         e = self.entity('Any X WHERE X eid %(x)s', {'x': eid2}, 'x')
    95         e.copy_relations(eid)
    95         e.copy_relations(user.eid)
    96         self.commit()
    96         self.commit()
    97         e.clear_related_cache('in_state', 'subject')
    97         e.clear_related_cache('in_state', 'subject')
    98         self.assertEquals(e.state, 'activated')
    98         self.assertEquals(e.state, 'activated')
    99 
    99 
   100     def test_related_cache_both(self):
   100     def test_related_cache_both(self):
   130         peschema.subject_relation('connait').set_rproperty(peschema, peschema, 'cardinality', '11')
   130         peschema.subject_relation('connait').set_rproperty(peschema, peschema, 'cardinality', '11')
   131         peschema.subject_relation('evaluee').set_rproperty(peschema, Note.e_schema, 'cardinality', '1*')
   131         peschema.subject_relation('evaluee').set_rproperty(peschema, Note.e_schema, 'cardinality', '1*')
   132         seschema.subject_relation('evaluee').set_rproperty(seschema, Note.e_schema, 'cardinality', '1*')
   132         seschema.subject_relation('evaluee').set_rproperty(seschema, Note.e_schema, 'cardinality', '1*')
   133         # testing basic fetch_attrs attribute
   133         # testing basic fetch_attrs attribute
   134         self.assertEquals(Personne.fetch_rql(user),
   134         self.assertEquals(Personne.fetch_rql(user),
   135                           'Any X,AA,AB,AC ORDERBY AA ASC WHERE X is Personne, X nom AA, X prenom AB, X modification_date AC')
   135                           'Any X,AA,AB,AC ORDERBY AA ASC '
       
   136                           'WHERE X is Personne, X nom AA, X prenom AB, X modification_date AC')
   136         pfetch_attrs = Personne.fetch_attrs
   137         pfetch_attrs = Personne.fetch_attrs
   137         sfetch_attrs = Societe.fetch_attrs
   138         sfetch_attrs = Societe.fetch_attrs
   138         try:
   139         try:
   139             # testing unknown attributes
   140             # testing unknown attributes
   140             Personne.fetch_attrs = ('bloug', 'beep')
   141             Personne.fetch_attrs = ('bloug', 'beep')
   141             self.assertEquals(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   142             self.assertEquals(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   142             # testing one non final relation
   143             # testing one non final relation
   143             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   144             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   144             self.assertEquals(Personne.fetch_rql(user),
   145             self.assertEquals(Personne.fetch_rql(user),
   145                               'Any X,AA,AB,AC,AD ORDERBY AA ASC WHERE X is Personne, X nom AA, X prenom AB, X travaille AC, AC nom AD')
   146                               'Any X,AA,AB,AC,AD ORDERBY AA ASC '
       
   147                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   146             # testing two non final relations
   148             # testing two non final relations
   147             Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee')
   149             Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee')
   148             self.assertEquals(Personne.fetch_rql(user),
   150             self.assertEquals(Personne.fetch_rql(user),
   149                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC WHERE X is Personne, X nom AA, '
   151                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   150                               'X prenom AB, X travaille AC, AC nom AD, X evaluee AE, AE modification_date AF')
   152                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
       
   153                               'X evaluee AE?, AE modification_date AF')
   151             # testing one non final relation with recursion
   154             # testing one non final relation with recursion
   152             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   155             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   153             Societe.fetch_attrs = ('nom', 'evaluee')
   156             Societe.fetch_attrs = ('nom', 'evaluee')
   154             self.assertEquals(Personne.fetch_rql(user),
   157             self.assertEquals(Personne.fetch_rql(user),
   155                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC WHERE X is Personne, X nom AA, X prenom AB, '
   158                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   156                               'X travaille AC, AC nom AD, AC evaluee AE, AE modification_date AF'
   159                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
       
   160                               'AC evaluee AE?, AE modification_date AF'
   157                               )
   161                               )
   158             # testing symetric relation
   162             # testing symetric relation
   159             Personne.fetch_attrs = ('nom', 'connait')
   163             Personne.fetch_attrs = ('nom', 'connait')
   160             self.assertEquals(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AA ASC WHERE X is Personne, X nom AA, X connait AB')
   164             self.assertEquals(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AA ASC '
       
   165                               'WHERE X is Personne, X nom AA, X connait AB?')
   161             # testing optional relation
   166             # testing optional relation
   162             peschema.subject_relation('travaille').set_rproperty(peschema, seschema, 'cardinality', '?*')
   167             peschema.subject_relation('travaille').set_rproperty(peschema, seschema, 'cardinality', '?*')
   163             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   168             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   164             Societe.fetch_attrs = ('nom',)
   169             Societe.fetch_attrs = ('nom',)
   165             self.assertEquals(Personne.fetch_rql(user),
   170             self.assertEquals(Personne.fetch_rql(user),
   319         self.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"')
   324         self.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"')
   320         self.assertEquals(p1.evaluee[0].nom, "toto")
   325         self.assertEquals(p1.evaluee[0].nom, "toto")
   321         self.failUnless(not p1.reverse_evaluee)
   326         self.failUnless(not p1.reverse_evaluee)
   322 
   327 
   323     def test_complete_relation(self):
   328     def test_complete_relation(self):
   324         self.execute('SET RT add_permission G WHERE RT name "wf_info_for", G name "managers"')
       
   325         self.commit()
       
   326         session = self.session
   329         session = self.session
   327         try:
   330         eid = session.unsafe_execute(
   328             eid = session.unsafe_execute(
   331             'INSERT TrInfo X: X comment "zou", X wf_info_for U, X from_state S1, X to_state S2 '
   329                 'INSERT TrInfo X: X comment "zou", X wf_info_for U, X from_state S1, X to_state S2 '
   332             'WHERE U login "admin", S1 name "activated", S2 name "deactivated"')[0][0]
   330                 'WHERE U login "admin", S1 name "activated", S2 name "deactivated"')[0][0]
   333         trinfo = self.entity('Any X WHERE X eid %(x)s', {'x': eid}, 'x')
   331             trinfo = self.entity('Any X WHERE X eid %(x)s', {'x': eid}, 'x')
   334         trinfo.complete()
   332             trinfo.complete()
   335         self.failUnless(isinstance(trinfo['creation_date'], datetime))
   333             self.failUnless(trinfo.relation_cached('from_state', 'subject'))
   336         self.failUnless(trinfo.relation_cached('from_state', 'subject'))
   334             self.failUnless(trinfo.relation_cached('to_state', 'subject'))
   337         self.failUnless(trinfo.relation_cached('to_state', 'subject'))
   335             self.failUnless(trinfo.relation_cached('wf_info_for', 'subject'))
   338         self.failUnless(trinfo.relation_cached('wf_info_for', 'subject'))
   336             # check with a missing relation
   339         self.assertEquals(trinfo.by_transition, [])
   337             eid = session.unsafe_execute(
       
   338                 'INSERT TrInfo X: X comment "zou", X wf_info_for U,X to_state S2 '
       
   339                 'WHERE U login "admin", S2 name "activated"')[0][0]
       
   340             trinfo = self.entity('Any X WHERE X eid %(x)s', {'x': eid}, 'x')
       
   341             trinfo.complete()
       
   342             self.failUnless(isinstance(trinfo.creation_date, datetime))
       
   343             self.failUnless(trinfo.relation_cached('from_state', 'subject'))
       
   344             self.failUnless(trinfo.relation_cached('to_state', 'subject'))
       
   345             self.failUnless(trinfo.relation_cached('wf_info_for', 'subject'))
       
   346             self.assertEquals(trinfo.from_state, [])
       
   347         finally:
       
   348             self.rollback()
       
   349             self.execute('DELETE RT add_permission G WHERE RT name "wf_info_for", G name "managers"')
       
   350             self.commit()
       
   351 
   340 
   352     def test_request_cache(self):
   341     def test_request_cache(self):
   353         req = self.request()
   342         req = self.request()
   354         user = self.entity('CWUser X WHERE X login "admin"', req=req)
   343         user = self.entity('CWUser X WHERE X login "admin"', req=req)
   355         state = user.in_state[0]
   344         state = user.in_state[0]