test/unittest_entity.py
changeset 6366 1806148d6ce8
parent 6173 eb386e473044
parent 6340 470d8e828fda
child 6415 b0b0f097a72d
equal deleted inserted replaced
6333:e3994fcc21c3 6366:1806148d6ce8
    38         e2 = self.vreg['etypes'].etype_class('SubNote')(self.request())
    38         e2 = self.vreg['etypes'].etype_class('SubNote')(self.request())
    39         self.assertIs(e.__class__, e2.__class__)
    39         self.assertIs(e.__class__, e2.__class__)
    40 
    40 
    41     def test_has_eid(self):
    41     def test_has_eid(self):
    42         e = self.vreg['etypes'].etype_class('CWUser')(self.request())
    42         e = self.vreg['etypes'].etype_class('CWUser')(self.request())
    43         self.assertEquals(e.eid, None)
    43         self.assertEqual(e.eid, None)
    44         self.assertEquals(e.has_eid(), False)
    44         self.assertEqual(e.has_eid(), False)
    45         e.eid = 'X'
    45         e.eid = 'X'
    46         self.assertEquals(e.has_eid(), False)
    46         self.assertEqual(e.has_eid(), False)
    47         e.eid = 0
    47         e.eid = 0
    48         self.assertEquals(e.has_eid(), True)
    48         self.assertEqual(e.has_eid(), True)
    49         e.eid = 2
    49         e.eid = 2
    50         self.assertEquals(e.has_eid(), True)
    50         self.assertEqual(e.has_eid(), True)
    51 
    51 
    52     def test_copy(self):
    52     def test_copy(self):
    53         req = self.request()
    53         req = self.request()
    54         req.create_entity('Tag', name=u'x')
    54         req.create_entity('Tag', name=u'x')
    55         p = req.create_entity('Personne', nom=u'toto')
    55         p = req.create_entity('Personne', nom=u'toto')
    57         self.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s',
    57         self.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s',
    58                      {'t': oe.eid, 'u': p.eid})
    58                      {'t': oe.eid, 'u': p.eid})
    59         self.execute('SET TAG tags X WHERE X eid %(x)s', {'x': oe.eid})
    59         self.execute('SET TAG tags X WHERE X eid %(x)s', {'x': oe.eid})
    60         e = req.create_entity('Note', type=u'z')
    60         e = req.create_entity('Note', type=u'z')
    61         e.copy_relations(oe.eid)
    61         e.copy_relations(oe.eid)
    62         self.assertEquals(len(e.ecrit_par), 1)
    62         self.assertEqual(len(e.ecrit_par), 1)
    63         self.assertEquals(e.ecrit_par[0].eid, p.eid)
    63         self.assertEqual(e.ecrit_par[0].eid, p.eid)
    64         self.assertEquals(len(e.reverse_tags), 1)
    64         self.assertEqual(len(e.reverse_tags), 1)
    65         # check meta-relations are not copied, set on commit
    65         # check meta-relations are not copied, set on commit
    66         self.assertEquals(len(e.created_by), 0)
    66         self.assertEqual(len(e.created_by), 0)
    67 
    67 
    68     def test_copy_with_nonmeta_composite_inlined(self):
    68     def test_copy_with_nonmeta_composite_inlined(self):
    69         req = self.request()
    69         req = self.request()
    70         p = req.create_entity('Personne', nom=u'toto')
    70         p = req.create_entity('Personne', nom=u'toto')
    71         oe = req.create_entity('Note', type=u'x')
    71         oe = req.create_entity('Note', type=u'x')
    79 
    79 
    80     def test_copy_with_composite(self):
    80     def test_copy_with_composite(self):
    81         user = self.user()
    81         user = self.user()
    82         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
    82         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
    83         e = self.execute('Any X WHERE X eid %(x)s', {'x': user.eid}).get_entity(0, 0)
    83         e = self.execute('Any X WHERE X eid %(x)s', {'x': user.eid}).get_entity(0, 0)
    84         self.assertEquals(e.use_email[0].address, "toto@logilab.org")
    84         self.assertEqual(e.use_email[0].address, "toto@logilab.org")
    85         self.assertEquals(e.use_email[0].eid, adeleid)
    85         self.assertEqual(e.use_email[0].eid, adeleid)
    86         usereid = self.execute('INSERT CWUser X: X login "toto", X upassword "toto", X in_group G '
    86         usereid = self.execute('INSERT CWUser X: X login "toto", X upassword "toto", X in_group G '
    87                                'WHERE G name "users"')[0][0]
    87                                'WHERE G name "users"')[0][0]
    88         e = self.execute('Any X WHERE X eid %(x)s', {'x': usereid}).get_entity(0, 0)
    88         e = self.execute('Any X WHERE X eid %(x)s', {'x': usereid}).get_entity(0, 0)
    89         e.copy_relations(user.eid)
    89         e.copy_relations(user.eid)
    90         self.failIf(e.use_email)
    90         self.failIf(e.use_email)
   100         eid2 = self.execute('INSERT CWUser X: X login "tutu", X upassword %(pwd)s', {'pwd': 'toto'})[0][0]
   100         eid2 = self.execute('INSERT CWUser X: X login "tutu", X upassword %(pwd)s', {'pwd': 'toto'})[0][0]
   101         e = self.execute('Any X WHERE X eid %(x)s', {'x': eid2}).get_entity(0, 0)
   101         e = self.execute('Any X WHERE X eid %(x)s', {'x': eid2}).get_entity(0, 0)
   102         e.copy_relations(user.eid)
   102         e.copy_relations(user.eid)
   103         self.commit()
   103         self.commit()
   104         e.cw_clear_relation_cache('in_state', 'subject')
   104         e.cw_clear_relation_cache('in_state', 'subject')
   105         self.assertEquals(e.cw_adapt_to('IWorkflowable').state, 'activated')
   105         self.assertEqual(e.cw_adapt_to('IWorkflowable').state, 'activated')
   106 
   106 
   107     def test_related_cache_both(self):
   107     def test_related_cache_both(self):
   108         user = self.execute('Any X WHERE X eid %(x)s', {'x':self.user().eid}).get_entity(0, 0)
   108         user = self.execute('Any X WHERE X eid %(x)s', {'x':self.user().eid}).get_entity(0, 0)
   109         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
   109         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
   110         self.commit()
   110         self.commit()
   111         self.assertEquals(user._cw_related_cache, {})
   111         self.assertEqual(user._cw_related_cache, {})
   112         email = user.primary_email[0]
   112         email = user.primary_email[0]
   113         self.assertEquals(sorted(user._cw_related_cache), ['primary_email_subject'])
   113         self.assertEqual(sorted(user._cw_related_cache), ['primary_email_subject'])
   114         self.assertEquals(email._cw_related_cache.keys(), ['primary_email_object'])
   114         self.assertEqual(email._cw_related_cache.keys(), ['primary_email_object'])
   115         groups = user.in_group
   115         groups = user.in_group
   116         self.assertEquals(sorted(user._cw_related_cache), ['in_group_subject', 'primary_email_subject'])
   116         self.assertEqual(sorted(user._cw_related_cache), ['in_group_subject', 'primary_email_subject'])
   117         for group in groups:
   117         for group in groups:
   118             self.failIf('in_group_subject' in group._cw_related_cache, group._cw_related_cache.keys())
   118             self.failIf('in_group_subject' in group._cw_related_cache, group._cw_related_cache.keys())
   119 
   119 
   120     def test_related_limit(self):
   120     def test_related_limit(self):
   121         req = self.request()
   121         req = self.request()
   122         p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   122         p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   123         for tag in u'abcd':
   123         for tag in u'abcd':
   124             req.create_entity('Tag', name=tag)
   124             req.create_entity('Tag', name=tag)
   125         self.execute('SET X tags Y WHERE X is Tag, Y is Personne')
   125         self.execute('SET X tags Y WHERE X is Tag, Y is Personne')
   126         self.assertEquals(len(p.related('tags', 'object', limit=2)), 2)
   126         self.assertEqual(len(p.related('tags', 'object', limit=2)), 2)
   127         self.assertEquals(len(p.related('tags', 'object')), 4)
   127         self.assertEqual(len(p.related('tags', 'object')), 4)
   128 
   128 
   129 
   129 
   130     def test_fetch_rql(self):
   130     def test_fetch_rql(self):
   131         user = self.user()
   131         user = self.user()
   132         Personne = self.vreg['etypes'].etype_class('Personne')
   132         Personne = self.vreg['etypes'].etype_class('Personne')
   137         peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '1*'
   137         peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '1*'
   138         peschema.subjrels['connait'].rdef(peschema, peschema).cardinality = '11'
   138         peschema.subjrels['connait'].rdef(peschema, peschema).cardinality = '11'
   139         peschema.subjrels['evaluee'].rdef(peschema, Note.e_schema).cardinality = '1*'
   139         peschema.subjrels['evaluee'].rdef(peschema, Note.e_schema).cardinality = '1*'
   140         seschema.subjrels['evaluee'].rdef(seschema, Note.e_schema).cardinality = '1*'
   140         seschema.subjrels['evaluee'].rdef(seschema, Note.e_schema).cardinality = '1*'
   141         # testing basic fetch_attrs attribute
   141         # testing basic fetch_attrs attribute
   142         self.assertEquals(Personne.fetch_rql(user),
   142         self.assertEqual(Personne.fetch_rql(user),
   143                           'Any X,AA,AB,AC ORDERBY AA ASC '
   143                           'Any X,AA,AB,AC ORDERBY AA ASC '
   144                           'WHERE X is Personne, X nom AA, X prenom AB, X modification_date AC')
   144                           'WHERE X is Personne, X nom AA, X prenom AB, X modification_date AC')
   145         pfetch_attrs = Personne.fetch_attrs
   145         pfetch_attrs = Personne.fetch_attrs
   146         sfetch_attrs = Societe.fetch_attrs
   146         sfetch_attrs = Societe.fetch_attrs
   147         try:
   147         try:
   148             # testing unknown attributes
   148             # testing unknown attributes
   149             Personne.fetch_attrs = ('bloug', 'beep')
   149             Personne.fetch_attrs = ('bloug', 'beep')
   150             self.assertEquals(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   150             self.assertEqual(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   151             # testing one non final relation
   151             # testing one non final relation
   152             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   152             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   153             self.assertEquals(Personne.fetch_rql(user),
   153             self.assertEqual(Personne.fetch_rql(user),
   154                               'Any X,AA,AB,AC,AD ORDERBY AA ASC '
   154                               'Any X,AA,AB,AC,AD ORDERBY AA ASC '
   155                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   155                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   156             # testing two non final relations
   156             # testing two non final relations
   157             Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee')
   157             Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee')
   158             self.assertEquals(Personne.fetch_rql(user),
   158             self.assertEqual(Personne.fetch_rql(user),
   159                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   159                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   160                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   160                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   161                               'X evaluee AE?, AE modification_date AF')
   161                               'X evaluee AE?, AE modification_date AF')
   162             # testing one non final relation with recursion
   162             # testing one non final relation with recursion
   163             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   163             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   164             Societe.fetch_attrs = ('nom', 'evaluee')
   164             Societe.fetch_attrs = ('nom', 'evaluee')
   165             self.assertEquals(Personne.fetch_rql(user),
   165             self.assertEqual(Personne.fetch_rql(user),
   166                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   166                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   167                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   167                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   168                               'AC evaluee AE?, AE modification_date AF'
   168                               'AC evaluee AE?, AE modification_date AF'
   169                               )
   169                               )
   170             # testing symmetric relation
   170             # testing symmetric relation
   171             Personne.fetch_attrs = ('nom', 'connait')
   171             Personne.fetch_attrs = ('nom', 'connait')
   172             self.assertEquals(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AA ASC '
   172             self.assertEqual(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AA ASC '
   173                               'WHERE X is Personne, X nom AA, X connait AB?')
   173                               'WHERE X is Personne, X nom AA, X connait AB?')
   174             # testing optional relation
   174             # testing optional relation
   175             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '?*'
   175             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '?*'
   176             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   176             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   177             Societe.fetch_attrs = ('nom',)
   177             Societe.fetch_attrs = ('nom',)
   178             self.assertEquals(Personne.fetch_rql(user),
   178             self.assertEqual(Personne.fetch_rql(user),
   179                               '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')
   179                               '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')
   180             # testing relation with cardinality > 1
   180             # testing relation with cardinality > 1
   181             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '**'
   181             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '**'
   182             self.assertEquals(Personne.fetch_rql(user),
   182             self.assertEqual(Personne.fetch_rql(user),
   183                               'Any X,AA,AB ORDERBY AA ASC WHERE X is Personne, X nom AA, X prenom AB')
   183                               'Any X,AA,AB ORDERBY AA ASC WHERE X is Personne, X nom AA, X prenom AB')
   184             # XXX test unauthorized attribute
   184             # XXX test unauthorized attribute
   185         finally:
   185         finally:
   186             Personne.fetch_attrs = pfetch_attrs
   186             Personne.fetch_attrs = pfetch_attrs
   187             Societe.fetch_attrs = sfetch_attrs
   187             Societe.fetch_attrs = sfetch_attrs
   193         self.failUnless(issubclass(self.vreg['etypes'].etype_class('SubNote'), Note))
   193         self.failUnless(issubclass(self.vreg['etypes'].etype_class('SubNote'), Note))
   194         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', 'type'))
   194         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', 'type'))
   195         Note.fetch_attrs, Note.fetch_order = fetch_config(('type',))
   195         Note.fetch_attrs, Note.fetch_order = fetch_config(('type',))
   196         SubNote.fetch_attrs, SubNote.fetch_order = fetch_config(('type',))
   196         SubNote.fetch_attrs, SubNote.fetch_order = fetch_config(('type',))
   197         p = self.request().create_entity('Personne', nom=u'pouet')
   197         p = self.request().create_entity('Personne', nom=u'pouet')
   198         self.assertEquals(p.cw_related_rql('evaluee'),
   198         self.assertEqual(p.cw_related_rql('evaluee'),
   199                           'Any X,AA,AB ORDERBY AA ASC WHERE E eid %(x)s, E evaluee X, '
   199                           'Any X,AA,AB ORDERBY AA ASC WHERE E eid %(x)s, E evaluee X, '
   200                           'X type AA, X modification_date AB')
   200                           'X type AA, X modification_date AB')
   201         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', ))
   201         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', ))
   202         # XXX
   202         # XXX
   203         self.assertEquals(p.cw_related_rql('evaluee'),
   203         self.assertEqual(p.cw_related_rql('evaluee'),
   204                           'Any X,AA ORDERBY AA DESC '
   204                           'Any X,AA ORDERBY AA DESC '
   205                           'WHERE E eid %(x)s, E evaluee X, X modification_date AA')
   205                           'WHERE E eid %(x)s, E evaluee X, X modification_date AA')
   206 
   206 
   207         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   207         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   208         self.assertEquals(tag.cw_related_rql('tags', 'subject'),
   208         self.assertEqual(tag.cw_related_rql('tags', 'subject'),
   209                           'Any X,AA ORDERBY AA DESC '
   209                           'Any X,AA ORDERBY AA DESC '
   210                           'WHERE E eid %(x)s, E tags X, X modification_date AA')
   210                           'WHERE E eid %(x)s, E tags X, X modification_date AA')
   211         self.assertEquals(tag.cw_related_rql('tags', 'subject', ('Personne',)),
   211         self.assertEqual(tag.cw_related_rql('tags', 'subject', ('Personne',)),
   212                           'Any X,AA,AB ORDERBY AA ASC '
   212                           'Any X,AA,AB ORDERBY AA ASC '
   213                           'WHERE E eid %(x)s, E tags X, X is IN (Personne), X nom AA, '
   213                           'WHERE E eid %(x)s, E tags X, X is IN (Personne), X nom AA, '
   214                           'X modification_date AB')
   214                           'X modification_date AB')
   215 
   215 
   216     def test_related_rql_ambigous_cant_use_fetch_order(self):
   216     def test_related_rql_ambigous_cant_use_fetch_order(self):
   217         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   217         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   218         for ttype in self.schema['tags'].objects():
   218         for ttype in self.schema['tags'].objects():
   219             self.vreg['etypes'].etype_class(ttype).fetch_attrs = ('modification_date',)
   219             self.vreg['etypes'].etype_class(ttype).fetch_attrs = ('modification_date',)
   220         self.assertEquals(tag.cw_related_rql('tags', 'subject'),
   220         self.assertEqual(tag.cw_related_rql('tags', 'subject'),
   221                           'Any X,AA ORDERBY AA DESC '
   221                           'Any X,AA ORDERBY AA DESC '
   222                           'WHERE E eid %(x)s, E tags X, X modification_date AA')
   222                           'WHERE E eid %(x)s, E tags X, X modification_date AA')
   223 
   223 
   224     def test_unrelated_rql_security_1(self):
   224     def test_unrelated_rql_security_1(self):
   225         user = self.request().user
   225         user = self.request().user
   226         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   226         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   227         self.assertEquals(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   227         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   228                           'WHERE NOT S use_email O, S eid %(x)s, O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   228                           'WHERE NOT S use_email O, S eid %(x)s, O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   229         self.create_user('toto')
   229         self.create_user('toto')
   230         self.login('toto')
   230         self.login('toto')
   231         user = self.request().user
   231         user = self.request().user
   232         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   232         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   233         self.assertEquals(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   233         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   234                           'WHERE NOT S use_email O, S eid %(x)s, O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   234                           'WHERE NOT S use_email O, S eid %(x)s, O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   235         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   235         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   236         self.assertRaises(Unauthorized, user.cw_unrelated_rql, 'use_email', 'EmailAddress', 'subject')
   236         self.assertRaises(Unauthorized, user.cw_unrelated_rql, 'use_email', 'EmailAddress', 'subject')
   237         self.login('anon')
   237         self.login('anon')
   238         user = self.request().user
   238         user = self.request().user
   239         self.assertRaises(Unauthorized, user.cw_unrelated_rql, 'use_email', 'EmailAddress', 'subject')
   239         self.assertRaises(Unauthorized, user.cw_unrelated_rql, 'use_email', 'EmailAddress', 'subject')
   240 
   240 
   241     def test_unrelated_rql_security_2(self):
   241     def test_unrelated_rql_security_2(self):
   242         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   242         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   243         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   243         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   244         self.assertEquals(rql, 'Any S,AA,AB,AC,AD ORDERBY AA ASC '
   244         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA ASC '
   245                           'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, S login AA, S firstname AB, S surname AC, S modification_date AD')
   245                           'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, S login AA, S firstname AB, S surname AC, S modification_date AD')
   246         #rql = email.cw_unrelated_rql('use_email', 'Person', 'object')[0]
   246         #rql = email.cw_unrelated_rql('use_email', 'Person', 'object')[0]
   247         #self.assertEquals(rql, '')
   247         #self.assertEqual(rql, '')
   248         self.login('anon')
   248         self.login('anon')
   249         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   249         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   250         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   250         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   251         self.assertEquals(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   251         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   252                           'WHERE NOT EXISTS(S use_email O), O eid %(x)s, S is CWUser, S login AA, S firstname AB, S surname AC, S modification_date AD, '
   252                           'WHERE NOT EXISTS(S use_email O), O eid %(x)s, S is CWUser, S login AA, S firstname AB, S surname AC, S modification_date AD, '
   253                           'A eid %(B)s, EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   253                           'A eid %(B)s, EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   254         #rql = email.cw_unrelated_rql('use_email', 'Person', 'object')[0]
   254         #rql = email.cw_unrelated_rql('use_email', 'Person', 'object')[0]
   255         #self.assertEquals(rql, '')
   255         #self.assertEqual(rql, '')
   256 
   256 
   257     def test_unrelated_rql_security_nonexistant(self):
   257     def test_unrelated_rql_security_nonexistant(self):
   258         self.login('anon')
   258         self.login('anon')
   259         email = self.vreg['etypes'].etype_class('EmailAddress')(self.request())
   259         email = self.vreg['etypes'].etype_class('EmailAddress')(self.request())
   260         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   260         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   261         self.assertEquals(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   261         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   262                           'WHERE S is CWUser, S login AA, S firstname AB, S surname AC, S modification_date AD, '
   262                           'WHERE S is CWUser, S login AA, S firstname AB, S surname AC, S modification_date AD, '
   263                           'A eid %(B)s, EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   263                           'A eid %(B)s, EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   264 
   264 
   265     def test_unrelated_base(self):
   265     def test_unrelated_base(self):
   266         req = self.request()
   266         req = self.request()
   278     def test_unrelated_limit(self):
   278     def test_unrelated_limit(self):
   279         req = self.request()
   279         req = self.request()
   280         e = req.create_entity('Tag', name=u'x')
   280         e = req.create_entity('Tag', name=u'x')
   281         req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   281         req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   282         req.create_entity('Personne', nom=u'thenault', prenom=u'sylvain')
   282         req.create_entity('Personne', nom=u'thenault', prenom=u'sylvain')
   283         self.assertEquals(len(e.unrelated('tags', 'Personne', 'subject', limit=1)),
   283         self.assertEqual(len(e.unrelated('tags', 'Personne', 'subject', limit=1)),
   284                           1)
   284                           1)
   285 
   285 
   286     def test_unrelated_security(self):
   286     def test_unrelated_security(self):
   287         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   287         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   288         rset = email.unrelated('use_email', 'CWUser', 'object')
   288         rset = email.unrelated('use_email', 'CWUser', 'object')
   289         self.assertEquals([x.login for x in rset.entities()], [u'admin', u'anon'])
   289         self.assertEqual([x.login for x in rset.entities()], [u'admin', u'anon'])
   290         user = self.request().user
   290         user = self.request().user
   291         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   291         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   292         self.assertEquals([x.address for x in rset.entities()], [u'hop'])
   292         self.assertEqual([x.address for x in rset.entities()], [u'hop'])
   293         self.create_user('toto')
   293         self.create_user('toto')
   294         self.login('toto')
   294         self.login('toto')
   295         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   295         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   296         rset = email.unrelated('use_email', 'CWUser', 'object')
   296         rset = email.unrelated('use_email', 'CWUser', 'object')
   297         self.assertEquals([x.login for x in rset.entities()], ['toto'])
   297         self.assertEqual([x.login for x in rset.entities()], ['toto'])
   298         user = self.request().user
   298         user = self.request().user
   299         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   299         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   300         self.assertEquals([x.address for x in rset.entities()], ['hop'])
   300         self.assertEqual([x.address for x in rset.entities()], ['hop'])
   301         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   301         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   302         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   302         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   303         self.assertEquals([x.address for x in rset.entities()], [])
   303         self.assertEqual([x.address for x in rset.entities()], [])
   304         self.login('anon')
   304         self.login('anon')
   305         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   305         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   306         rset = email.unrelated('use_email', 'CWUser', 'object')
   306         rset = email.unrelated('use_email', 'CWUser', 'object')
   307         self.assertEquals([x.login for x in rset.entities()], [])
   307         self.assertEqual([x.login for x in rset.entities()], [])
   308         user = self.request().user
   308         user = self.request().user
   309         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   309         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   310         self.assertEquals([x.address for x in rset.entities()], [])
   310         self.assertEqual([x.address for x in rset.entities()], [])
   311 
   311 
   312     def test_unrelated_new_entity(self):
   312     def test_unrelated_new_entity(self):
   313         e = self.vreg['etypes'].etype_class('CWUser')(self.request())
   313         e = self.vreg['etypes'].etype_class('CWUser')(self.request())
   314         unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')]
   314         unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')]
   315         # should be default groups but owners, i.e. managers, users, guests
   315         # should be default groups but owners, i.e. managers, users, guests
   316         self.assertEquals(len(unrelated), 3)
   316         self.assertEqual(len(unrelated), 3)
   317 
   317 
   318     def test_printable_value_string(self):
   318     def test_printable_value_string(self):
   319         e = self.request().create_entity('Card', title=u'rest test', content=u'du :eid:`1:*ReST*`',
   319         e = self.request().create_entity('Card', title=u'rest test', content=u'du :eid:`1:*ReST*`',
   320                             content_format=u'text/rest')
   320                             content_format=u'text/rest')
   321         self.assertEquals(e.printable_value('content'),
   321         self.assertEqual(e.printable_value('content'),
   322                           '<p>du <a class="reference" href="http://testing.fr/cubicweb/cwgroup/guests">*ReST*</a></p>\n')
   322                           '<p>du <a class="reference" href="http://testing.fr/cubicweb/cwgroup/guests">*ReST*</a></p>\n')
   323         e.cw_attr_cache['content'] = 'du <em>html</em> <ref rql="CWUser X">users</ref>'
   323         e.cw_attr_cache['content'] = 'du <em>html</em> <ref rql="CWUser X">users</ref>'
   324         e.cw_attr_cache['content_format'] = 'text/html'
   324         e.cw_attr_cache['content_format'] = 'text/html'
   325         self.assertEquals(e.printable_value('content'),
   325         self.assertEqual(e.printable_value('content'),
   326                           'du <em>html</em> <a href="http://testing.fr/cubicweb/view?rql=CWUser%20X">users</a>')
   326                           'du <em>html</em> <a href="http://testing.fr/cubicweb/view?rql=CWUser%20X">users</a>')
   327         e.cw_attr_cache['content'] = 'du *texte*'
   327         e.cw_attr_cache['content'] = 'du *texte*'
   328         e.cw_attr_cache['content_format'] = 'text/plain'
   328         e.cw_attr_cache['content_format'] = 'text/plain'
   329         self.assertEquals(e.printable_value('content'),
   329         self.assertEqual(e.printable_value('content'),
   330                           '<p>\ndu *texte*<br/>\n</p>')
   330                           '<p>\ndu *texte*<br/>\n</p>')
   331         e.cw_attr_cache['title'] = 'zou'
   331         e.cw_attr_cache['title'] = 'zou'
   332         e.cw_attr_cache['content'] = '''\
   332         e.cw_attr_cache['content'] = '''\
   333 a title
   333 a title
   334 =======
   334 =======
   335 du :eid:`1:*ReST*`'''
   335 du :eid:`1:*ReST*`'''
   336         e.cw_attr_cache['content_format'] = 'text/rest'
   336         e.cw_attr_cache['content_format'] = 'text/rest'
   337         self.assertEquals(e.printable_value('content', format='text/plain'),
   337         self.assertEqual(e.printable_value('content', format='text/plain'),
   338                           e.cw_attr_cache['content'])
   338                           e.cw_attr_cache['content'])
   339 
   339 
   340         e.cw_attr_cache['content'] = u'<b>yo (zou éà ;)</b>'
   340         e.cw_attr_cache['content'] = u'<b>yo (zou éà ;)</b>'
   341         e.cw_attr_cache['content_format'] = 'text/html'
   341         e.cw_attr_cache['content_format'] = 'text/html'
   342         self.assertEquals(e.printable_value('content', format='text/plain').strip(),
   342         self.assertEqual(e.printable_value('content', format='text/plain').strip(),
   343                           u'**yo (zou éà ;)**')
       
   344         if HAS_TAL:
   343         if HAS_TAL:
   345             e.cw_attr_cache['content'] = '<h1 tal:content="self/title">titre</h1>'
   344             e.cw_attr_cache['content'] = '<h1 tal:content="self/title">titre</h1>'
   346             e.cw_attr_cache['content_format'] = 'text/cubicweb-page-template'
   345             e.cw_attr_cache['content_format'] = 'text/cubicweb-page-template'
   347             self.assertEquals(e.printable_value('content'),
   346             self.assertEqual(e.printable_value('content'),
   348                               '<h1>zou</h1>')
   347                               '<h1>zou</h1>')
   349 
   348 
   350 
   349 
   351     def test_printable_value_bytes(self):
   350     def test_printable_value_bytes(self):
   352         req = self.request()
   351         req = self.request()
   354                             data_encoding=u'ascii', data_name=u'toto.py')
   353                             data_encoding=u'ascii', data_name=u'toto.py')
   355         from cubicweb import mttransforms
   354         from cubicweb import mttransforms
   356         if mttransforms.HAS_PYGMENTS_TRANSFORMS:
   355         if mttransforms.HAS_PYGMENTS_TRANSFORMS:
   357             import pygments
   356             import pygments
   358             if tuple(int(i) for i in pygments.__version__.split('.')[:2]) >= (1, 3):
   357             if tuple(int(i) for i in pygments.__version__.split('.')[:2]) >= (1, 3):
   359                 self.assertEquals(e.printable_value('data'),
   358                 self.assertEqual(e.printable_value('data'),
   360                                   '''<div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mi">1</span>
   359                                   '''<div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mi">1</span>
   361 </pre></div>
   360 </pre></div>
   362 ''')
   361 ''')
   363             else:
   362             else:
   364                 self.assertEquals(e.printable_value('data'),
   363                 self.assertEqual(e.printable_value('data'),
   365                                   '''<div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mf">1</span>
   364                                   '''<div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mf">1</span>
   366 </pre></div>
   365 </pre></div>
   367 ''')
   366 ''')
   368         else:
   367         else:
   369             self.assertEquals(e.printable_value('data'),
   368             self.assertEqual(e.printable_value('data'),
   370                               '''<pre class="python">
   369                               '''<pre class="python">
   371 <span style="color: #C00000;">lambda</span> <span style="color: #000000;">x</span><span style="color: #0000C0;">:</span> <span style="color: #0080C0;">1</span>
   370 <span style="color: #C00000;">lambda</span> <span style="color: #000000;">x</span><span style="color: #0000C0;">:</span> <span style="color: #0080C0;">1</span>
   372 </pre>
   371 </pre>
   373 ''')
   372 ''')
   374 
   373 
   375         e = req.create_entity('File', data=Binary('*héhéhé*'), data_format=u'text/rest',
   374         e = req.create_entity('File', data=Binary('*héhéhé*'), data_format=u'text/rest',
   376                             data_encoding=u'utf-8', data_name=u'toto.txt')
   375                             data_encoding=u'utf-8', data_name=u'toto.txt')
   377         self.assertEquals(e.printable_value('data'),
   376         self.assertEqual(e.printable_value('data'),
   378                           u'<p><em>héhéhé</em></p>\n')
   377                           u'<p><em>héhéhé</em></p>\n')
   379 
   378 
   380     def test_printable_value_bad_html(self):
   379     def test_printable_value_bad_html(self):
   381         """make sure we don't crash if we try to render invalid XHTML strings"""
   380         """make sure we don't crash if we try to render invalid XHTML strings"""
   382         req = self.request()
   381         req = self.request()
   383         e = req.create_entity('Card', title=u'bad html', content=u'<div>R&D<br>',
   382         e = req.create_entity('Card', title=u'bad html', content=u'<div>R&D<br>',
   384                             content_format=u'text/html')
   383                             content_format=u'text/html')
   385         tidy = lambda x: x.replace('\n', '')
   384         tidy = lambda x: x.replace('\n', '')
   386         self.assertEquals(tidy(e.printable_value('content')),
   385         self.assertEqual(tidy(e.printable_value('content')),
   387                           '<div>R&amp;D<br/></div>')
   386                           '<div>R&amp;D<br/></div>')
   388         e.cw_attr_cache['content'] = u'yo !! R&D <div> pas fermé'
   387         e.cw_attr_cache['content'] = u'yo !! R&D <div> pas fermé'
   389         self.assertEquals(tidy(e.printable_value('content')),
   388         self.assertEqual(tidy(e.printable_value('content')),
   390                           u'yo !! R&amp;D <div> pas fermé</div>')
   389                           u'yo !! R&amp;D <div> pas fermé</div>')
   391         e.cw_attr_cache['content'] = u'R&D'
   390         e.cw_attr_cache['content'] = u'R&D'
   392         self.assertEquals(tidy(e.printable_value('content')), u'R&amp;D')
   391         self.assertEqual(tidy(e.printable_value('content')), u'R&amp;D')
   393         e.cw_attr_cache['content'] = u'R&D;'
   392         e.cw_attr_cache['content'] = u'R&D;'
   394         self.assertEquals(tidy(e.printable_value('content')), u'R&amp;D;')
   393         self.assertEqual(tidy(e.printable_value('content')), u'R&amp;D;')
   395         e.cw_attr_cache['content'] = u'yo !! R&amp;D <div> pas fermé'
   394         e.cw_attr_cache['content'] = u'yo !! R&amp;D <div> pas fermé'
   396         self.assertEquals(tidy(e.printable_value('content')),
   395         self.assertEqual(tidy(e.printable_value('content')),
   397                           u'yo !! R&amp;D <div> pas fermé</div>')
   396                           u'yo !! R&amp;D <div> pas fermé</div>')
   398         e.cw_attr_cache['content'] = u'été <div> été'
   397         e.cw_attr_cache['content'] = u'été <div> été'
   399         self.assertEquals(tidy(e.printable_value('content')),
   398         self.assertEqual(tidy(e.printable_value('content')),
   400                           u'été <div> été</div>')
       
   401         e.cw_attr_cache['content'] = u'C&apos;est un exemple s&eacute;rieux'
   399         e.cw_attr_cache['content'] = u'C&apos;est un exemple s&eacute;rieux'
   402         self.assertEquals(tidy(e.printable_value('content')),
   400         self.assertEqual(tidy(e.printable_value('content')),
       
   401         e['content'] = u'C&apos;est un exemple s&eacute;rieux'
       
   402         self.assertEqual(tidy(e.printable_value('content')),
   403                           u"C'est un exemple sérieux")
   403                           u"C'est un exemple sérieux")
   404         # make sure valid xhtml is left untouched
   404         # make sure valid xhtml is left untouched
   405         e.cw_attr_cache['content'] = u'<div>R&amp;D<br/></div>'
   405         e.cw_attr_cache['content'] = u'<div>R&amp;D<br/></div>'
   406         self.assertEquals(e.printable_value('content'), e.cw_attr_cache['content'])
   406         self.assertEqual(e.printable_value('content'), e.cw_attr_cache['content'])
   407         e.cw_attr_cache['content'] = u'<div>été</div>'
   407         e.cw_attr_cache['content'] = u'<div>été</div>'
   408         self.assertEquals(e.printable_value('content'), e.cw_attr_cache['content'])
   408         self.assertEqual(e.printable_value('content'), e.cw_attr_cache['content'])
   409         e.cw_attr_cache['content'] = u'été'
   409         e.cw_attr_cache['content'] = u'été'
   410         self.assertEquals(e.printable_value('content'), e.cw_attr_cache['content'])
   410         self.assertEqual(e.printable_value('content'), e.cw_attr_cache['content'])
   411         e.cw_attr_cache['content'] = u'hop\r\nhop\nhip\rmomo'
   411         e.cw_attr_cache['content'] = u'hop\r\nhop\nhip\rmomo'
   412         self.assertEquals(e.printable_value('content'), u'hop\nhop\nhip\nmomo')
   412         self.assertEqual(e.printable_value('content'), u'hop\nhop\nhip\nmomo')
   413 
   413 
   414     def test_printable_value_bad_html_ms(self):
   414     def test_printable_value_bad_html_ms(self):
   415         self.skip('fix soup2xhtml to handle this test')
   415         self.skipTest('fix soup2xhtml to handle this test')
   416         req = self.request()
   416         req = self.request()
   417         e = req.create_entity('Card', title=u'bad html', content=u'<div>R&D<br>',
   417         e = req.create_entity('Card', title=u'bad html', content=u'<div>R&D<br>',
   418                             content_format=u'text/html')
   418                             content_format=u'text/html')
   419         tidy = lambda x: x.replace('\n', '')
   419         tidy = lambda x: x.replace('\n', '')
   420         e.cw_attr_cache['content'] = u'<div x:foo="bar">ms orifice produces weird html</div>'
   420         e.cw_attr_cache['content'] = u'<div x:foo="bar">ms orifice produces weird html</div>'
   421         self.assertEquals(tidy(e.printable_value('content')),
   421         self.assertEqual(tidy(e.printable_value('content')),
   422                           u'<div>ms orifice produces weird html</div>')
   422                           u'<div>ms orifice produces weird html</div>')
   423         import tidy as tidymod # apt-get install python-tidy
   423         import tidy as tidymod # apt-get install python-tidy
   424         tidy = lambda x: str(tidymod.parseString(x.encode('utf-8'),
   424         tidy = lambda x: str(tidymod.parseString(x.encode('utf-8'),
   425                                                  **{'drop_proprietary_attributes': True,
   425                                                  **{'drop_proprietary_attributes': True,
   426                                                     'output_xhtml': True,
   426                                                     'output_xhtml': True,
   427                                                     'show_body_only' : True,
   427                                                     'show_body_only' : True,
   428                                                     'quote-nbsp' : False,
   428                                                     'quote-nbsp' : False,
   429                                                     'char_encoding' : 'utf8'})).decode('utf-8').strip()
   429                                                     'char_encoding' : 'utf8'})).decode('utf-8').strip()
   430         self.assertEquals(tidy(e.printable_value('content')),
   430         self.assertEqual(tidy(e.printable_value('content')),
   431                           u'<div>ms orifice produces weird html</div>')
   431                           u'<div>ms orifice produces weird html</div>')
   432 
   432 
   433 
   433 
   434     def test_fulltextindex(self):
   434     def test_fulltextindex(self):
   435         e = self.vreg['etypes'].etype_class('File')(self.request())
   435         e = self.vreg['etypes'].etype_class('File')(self.request())
   438         e.cw_attr_cache['data'] = Binary('some <em>data</em>')
   438         e.cw_attr_cache['data'] = Binary('some <em>data</em>')
   439         e.cw_attr_cache['data_name'] = 'an html file'
   439         e.cw_attr_cache['data_name'] = 'an html file'
   440         e.cw_attr_cache['data_format'] = 'text/html'
   440         e.cw_attr_cache['data_format'] = 'text/html'
   441         e.cw_attr_cache['data_encoding'] = 'ascii'
   441         e.cw_attr_cache['data_encoding'] = 'ascii'
   442         e._cw.transaction_data = {} # XXX req should be a session
   442         e._cw.transaction_data = {} # XXX req should be a session
   443         self.assertEquals(e.cw_adapt_to('IFTIndexable').get_words(),
   443         self.assertEqual(e.cw_adapt_to('IFTIndexable').get_words(),
   444                           {'C': [u'du', u'html', 'an', 'html', 'file', u'some', u'data']})
   444                           {'C': [u'du', u'html', 'an', 'html', 'file', u'some', u'data']})
   445 
   445 
   446 
   446 
   447     def test_nonregr_relation_cache(self):
   447     def test_nonregr_relation_cache(self):
   448         req = self.request()
   448         req = self.request()
   449         p1 = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   449         p1 = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   450         p2 = req.create_entity('Personne', nom=u'toto')
   450         p2 = req.create_entity('Personne', nom=u'toto')
   451         self.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"')
   451         self.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"')
   452         self.assertEquals(p1.evaluee[0].nom, "toto")
   452         self.assertEqual(p1.evaluee[0].nom, "toto")
   453         self.failUnless(not p1.reverse_evaluee)
   453         self.failUnless(not p1.reverse_evaluee)
   454 
   454 
   455     def test_complete_relation(self):
   455     def test_complete_relation(self):
   456         session = self.session
   456         session = self.session
   457         eid = session.execute(
   457         eid = session.execute(
   461         trinfo.complete()
   461         trinfo.complete()
   462         self.failUnless(isinstance(trinfo.cw_attr_cache['creation_date'], datetime))
   462         self.failUnless(isinstance(trinfo.cw_attr_cache['creation_date'], datetime))
   463         self.failUnless(trinfo.cw_relation_cached('from_state', 'subject'))
   463         self.failUnless(trinfo.cw_relation_cached('from_state', 'subject'))
   464         self.failUnless(trinfo.cw_relation_cached('to_state', 'subject'))
   464         self.failUnless(trinfo.cw_relation_cached('to_state', 'subject'))
   465         self.failUnless(trinfo.cw_relation_cached('wf_info_for', 'subject'))
   465         self.failUnless(trinfo.cw_relation_cached('wf_info_for', 'subject'))
   466         self.assertEquals(trinfo.by_transition, ())
   466         self.assertEqual(trinfo.by_transition, ())
   467 
   467 
   468     def test_request_cache(self):
   468     def test_request_cache(self):
   469         req = self.request()
   469         req = self.request()
   470         user = self.execute('CWUser X WHERE X login "admin"', req=req).get_entity(0, 0)
   470         user = self.execute('CWUser X WHERE X login "admin"', req=req).get_entity(0, 0)
   471         state = user.in_state[0]
   471         state = user.in_state[0]
   473         self.failUnless(state is samestate)
   473         self.failUnless(state is samestate)
   474 
   474 
   475     def test_rest_path(self):
   475     def test_rest_path(self):
   476         req = self.request()
   476         req = self.request()
   477         note = req.create_entity('Note', type=u'z')
   477         note = req.create_entity('Note', type=u'z')
   478         self.assertEquals(note.rest_path(), 'note/%s' % note.eid)
   478         self.assertEqual(note.rest_path(), 'note/%s' % note.eid)
   479         # unique attr
   479         # unique attr
   480         tag = req.create_entity('Tag', name=u'x')
   480         tag = req.create_entity('Tag', name=u'x')
   481         self.assertEquals(tag.rest_path(), 'tag/x')
   481         self.assertEqual(tag.rest_path(), 'tag/x')
   482         # test explicit rest_attr
   482         # test explicit rest_attr
   483         person = req.create_entity('Personne', prenom=u'john', nom=u'doe')
   483         person = req.create_entity('Personne', prenom=u'john', nom=u'doe')
   484         self.assertEquals(person.rest_path(), 'personne/doe')
   484         self.assertEqual(person.rest_path(), 'personne/doe')
   485         # ambiguity test
   485         # ambiguity test
   486         person2 = req.create_entity('Personne', prenom=u'remi', nom=u'doe')
   486         person2 = req.create_entity('Personne', prenom=u'remi', nom=u'doe')
   487         person.clear_all_caches()
   487         person.clear_all_caches()
   488         self.assertEquals(person.rest_path(), 'personne/eid/%s' % person.eid)
   488         self.assertEqual(person.rest_path(), 'personne/eid/%s' % person.eid)
   489         self.assertEquals(person2.rest_path(), 'personne/eid/%s' % person2.eid)
   489         self.assertEqual(person2.rest_path(), 'personne/eid/%s' % person2.eid)
   490         # unique attr with None value (wikiid in this case)
   490         # unique attr with None value (wikiid in this case)
   491         card1 = req.create_entity('Card', title=u'hop')
   491         card1 = req.create_entity('Card', title=u'hop')
   492         self.assertEquals(card1.rest_path(), 'card/eid/%s' % card1.eid)
   492         self.assertEqual(card1.rest_path(), 'card/eid/%s' % card1.eid)
   493         # don't use rest if we have /, ? or & in the path (breaks mod_proxy)
   493         # don't use rest if we have /, ? or & in the path (breaks mod_proxy)
   494         card2 = req.create_entity('Card', title=u'pod', wikiid=u'zo/bi')
   494         card2 = req.create_entity('Card', title=u'pod', wikiid=u'zo/bi')
   495         self.assertEquals(card2.rest_path(), 'card/eid/%d' % card2.eid)
   495         self.assertEqual(card2.rest_path(), 'card/eid/%d' % card2.eid)
   496         card3 = req.create_entity('Card', title=u'pod', wikiid=u'zo&bi')
   496         card3 = req.create_entity('Card', title=u'pod', wikiid=u'zo&bi')
   497         self.assertEquals(card3.rest_path(), 'card/eid/%d' % card3.eid)
   497         self.assertEqual(card3.rest_path(), 'card/eid/%d' % card3.eid)
   498         card4 = req.create_entity('Card', title=u'pod', wikiid=u'zo?bi')
   498         card4 = req.create_entity('Card', title=u'pod', wikiid=u'zo?bi')
   499         self.assertEquals(card4.rest_path(), 'card/eid/%d' % card4.eid)
   499         self.assertEqual(card4.rest_path(), 'card/eid/%d' % card4.eid)
   500         
   500         
   501 
   501 
   502     def test_set_attributes(self):
   502     def test_set_attributes(self):
   503         req = self.request()
   503         req = self.request()
   504         person = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   504         person = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   505         self.assertEquals(person.prenom, u'adrien')
   505         self.assertEqual(person.prenom, u'adrien')
   506         self.assertEquals(person.nom, u'di mascio')
   506         self.assertEqual(person.nom, u'di mascio')
   507         person.set_attributes(prenom=u'sylvain', nom=u'thénault')
   507         person.set_attributes(prenom=u'sylvain', nom=u'thénault')
   508         person = self.execute('Personne P').get_entity(0, 0) # XXX retreival needed ?
   508         person = self.execute('Personne P').get_entity(0, 0) # XXX retreival needed ?
   509         self.assertEquals(person.prenom, u'sylvain')
   509         self.assertEqual(person.prenom, u'sylvain')
   510         self.assertEquals(person.nom, u'thénault')
   510         self.assertEqual(person.nom, u'thénault')
   511 
   511 
   512     def test_metainformation_and_external_absolute_url(self):
   512     def test_metainformation_and_external_absolute_url(self):
   513         req = self.request()
   513         req = self.request()
   514         note = req.create_entity('Note', type=u'z')
   514         note = req.create_entity('Note', type=u'z')
   515         metainf = note.cw_metainformation()
   515         metainf = note.cw_metainformation()
   516         self.assertEquals(metainf, {'source': {'adapter': 'native', 'uri': 'system'}, 'type': u'Note', 'extid': None})
   516         self.assertEqual(metainf, {'source': {'adapter': 'native', 'uri': 'system'}, 'type': u'Note', 'extid': None})
   517         self.assertEquals(note.absolute_url(), 'http://testing.fr/cubicweb/note/%s' % note.eid)
   517         self.assertEqual(note.absolute_url(), 'http://testing.fr/cubicweb/note/%s' % note.eid)
   518         metainf['source'] = metainf['source'].copy()
   518         metainf['source'] = metainf['source'].copy()
   519         metainf['source']['base-url']  = 'http://cubicweb2.com/'
   519         metainf['source']['base-url']  = 'http://cubicweb2.com/'
   520         metainf['extid']  = 1234
   520         metainf['extid']  = 1234
   521         self.assertEquals(note.absolute_url(), 'http://cubicweb2.com/note/1234')
   521         self.assertEqual(note.absolute_url(), 'http://cubicweb2.com/note/1234')
   522 
   522 
   523     def test_absolute_url_empty_field(self):
   523     def test_absolute_url_empty_field(self):
   524         req = self.request()
   524         req = self.request()
   525         card = req.create_entity('Card', wikiid=u'', title=u'test')
   525         card = req.create_entity('Card', wikiid=u'', title=u'test')
   526         self.assertEquals(card.absolute_url(),
   526         self.assertEqual(card.absolute_url(),
   527                           'http://testing.fr/cubicweb/card/eid/%s' % card.eid)
   527                           'http://testing.fr/cubicweb/card/eid/%s' % card.eid)
   528 
   528 
   529     def test_create_entity(self):
   529     def test_create_entity(self):
   530         req = self.request()
   530         req = self.request()
   531         p1 = req.create_entity('Personne', nom=u'fayolle', prenom=u'alexandre')
   531         p1 = req.create_entity('Personne', nom=u'fayolle', prenom=u'alexandre')
   533         note = req.create_entity('Note', type=u'z')
   533         note = req.create_entity('Note', type=u'z')
   534         req = self.request()
   534         req = self.request()
   535         p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien',
   535         p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien',
   536                               connait=p1, evaluee=[p1, p2],
   536                               connait=p1, evaluee=[p1, p2],
   537                               reverse_ecrit_par=note)
   537                               reverse_ecrit_par=note)
   538         self.assertEquals(p.nom, 'di mascio')
   538         self.assertEqual(p.nom, 'di mascio')
   539         self.assertEquals([c.nom for c in p.connait], ['fayolle'])
   539         self.assertEqual([c.nom for c in p.connait], ['fayolle'])
   540         self.assertEquals(sorted([c.nom for c in p.evaluee]), ['campeas', 'fayolle'])
   540         self.assertEqual(sorted([c.nom for c in p.evaluee]), ['campeas', 'fayolle'])
   541         self.assertEquals([c.type for c in p.reverse_ecrit_par], ['z'])
   541         self.assertEqual([c.type for c in p.reverse_ecrit_par], ['z'])
   542 
   542 
   543 
   543 
   544 
   544 
   545 if __name__ == '__main__':
   545 if __name__ == '__main__':
   546     from logilab.common.testlib import unittest_main
   546     from logilab.common.testlib import unittest_main