test/unittest_entity.py
branchtls-sprint
changeset 1640 65b60f177eb1
parent 1398 5fe84a5f7035
child 1905 9c96dd7cc624
equal deleted inserted replaced
1639:375c857aa0f5 1640:65b60f177eb1
    13 ##         self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
    13 ##         self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
    14 ##         self.add_entity('Task', title=u'fait ca !', description=u'et plus vite', start=now())
    14 ##         self.add_entity('Task', title=u'fait ca !', description=u'et plus vite', start=now())
    15 ##         self.add_entity('Tag', name=u'x')
    15 ##         self.add_entity('Tag', name=u'x')
    16 ##         self.add_entity('Link', title=u'perdu', url=u'http://www.perdu.com',
    16 ##         self.add_entity('Link', title=u'perdu', url=u'http://www.perdu.com',
    17 ##                         embed=False)
    17 ##                         embed=False)
    18     
    18 
    19     def test_boolean_value(self):
    19     def test_boolean_value(self):
    20         e = self.etype_instance('CWUser')
    20         e = self.etype_instance('CWUser')
    21         self.failUnless(e)
    21         self.failUnless(e)
    22 
    22 
    23     def test_yams_inheritance(self):
    23     def test_yams_inheritance(self):
    35         self.assertEquals(e.has_eid(), False)
    35         self.assertEquals(e.has_eid(), False)
    36         e.eid = 0
    36         e.eid = 0
    37         self.assertEquals(e.has_eid(), True)
    37         self.assertEquals(e.has_eid(), True)
    38         e.eid = 2
    38         e.eid = 2
    39         self.assertEquals(e.has_eid(), True)
    39         self.assertEquals(e.has_eid(), True)
    40         
    40 
    41     def test_copy(self):
    41     def test_copy(self):
    42         self.add_entity('Tag', name=u'x')
    42         self.add_entity('Tag', name=u'x')
    43         p = self.add_entity('Personne', nom=u'toto')
    43         p = self.add_entity('Personne', nom=u'toto')
    44         oe = self.add_entity('Note', type=u'x')
    44         oe = self.add_entity('Note', type=u'x')
    45         self.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s',
    45         self.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s',
    48         e = self.add_entity('Note', type=u'z')
    48         e = self.add_entity('Note', type=u'z')
    49         e.copy_relations(oe.eid)
    49         e.copy_relations(oe.eid)
    50         self.assertEquals(len(e.ecrit_par), 1)
    50         self.assertEquals(len(e.ecrit_par), 1)
    51         self.assertEquals(e.ecrit_par[0].eid, p.eid)
    51         self.assertEquals(e.ecrit_par[0].eid, p.eid)
    52         self.assertEquals(len(e.reverse_tags), 0)
    52         self.assertEquals(len(e.reverse_tags), 0)
    53         
    53 
    54     def test_copy_with_nonmeta_composite_inlined(self):
    54     def test_copy_with_nonmeta_composite_inlined(self):
    55         p = self.add_entity('Personne', nom=u'toto')
    55         p = self.add_entity('Personne', nom=u'toto')
    56         oe = self.add_entity('Note', type=u'x')
    56         oe = self.add_entity('Note', type=u'x')
    57         self.schema['ecrit_par'].set_rproperty('Note', 'Personne', 'composite', 'subject')
    57         self.schema['ecrit_par'].set_rproperty('Note', 'Personne', 'composite', 'subject')
    58         self.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s',
    58         self.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s',
    59                      {'t': oe.eid, 'u': p.eid}, ('t','u'))
    59                      {'t': oe.eid, 'u': p.eid}, ('t','u'))
    60         e = self.add_entity('Note', type=u'z')
    60         e = self.add_entity('Note', type=u'z')
    61         e.copy_relations(oe.eid)
    61         e.copy_relations(oe.eid)
    62         self.failIf(e.ecrit_par)
    62         self.failIf(e.ecrit_par)
    63         self.failUnless(oe.ecrit_par)
    63         self.failUnless(oe.ecrit_par)
    64             
    64 
    65     def test_copy_with_composite(self):
    65     def test_copy_with_composite(self):
    66         user = self.user()
    66         user = self.user()
    67         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
    67         adeleid = self.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0]
    68         e = self.entity('Any X WHERE X eid %(x)s', {'x':user.eid}, 'x')
    68         e = self.entity('Any X WHERE X eid %(x)s', {'x':user.eid}, 'x')
    69         self.assertEquals(e.use_email[0].address, "toto@logilab.org")
    69         self.assertEquals(e.use_email[0].address, "toto@logilab.org")
    72                                'WHERE G name "users", S name "activated"')[0][0]
    72                                'WHERE G name "users", S name "activated"')[0][0]
    73         e = self.entity('Any X WHERE X eid %(x)s', {'x':usereid}, 'x')
    73         e = self.entity('Any X WHERE X eid %(x)s', {'x':usereid}, 'x')
    74         e.copy_relations(user.eid)
    74         e.copy_relations(user.eid)
    75         self.failIf(e.use_email)
    75         self.failIf(e.use_email)
    76         self.failIf(e.primary_email)
    76         self.failIf(e.primary_email)
    77         
    77 
    78     def test_copy_with_non_initial_state(self):
    78     def test_copy_with_non_initial_state(self):
    79         user = self.user()
    79         user = self.user()
    80         eid = self.execute('INSERT CWUser X: X login "toto", X upassword %(pwd)s, X in_group G WHERE G name "users"',
    80         eid = self.execute('INSERT CWUser X: X login "toto", X upassword %(pwd)s, X in_group G WHERE G name "users"',
    81                            {'pwd': 'toto'})[0][0]
    81                            {'pwd': 'toto'})[0][0]
    82         self.commit()
    82         self.commit()
   108             self.add_entity('Tag', name=tag)
   108             self.add_entity('Tag', name=tag)
   109         self.execute('SET X tags Y WHERE X is Tag, Y is Personne')
   109         self.execute('SET X tags Y WHERE X is Tag, Y is Personne')
   110         self.assertEquals(len(p.related('tags', 'object', limit=2)), 2)
   110         self.assertEquals(len(p.related('tags', 'object', limit=2)), 2)
   111         self.assertEquals(len(p.related('tags', 'object')), 4)
   111         self.assertEquals(len(p.related('tags', 'object')), 4)
   112 
   112 
   113         
   113 
   114     def test_fetch_rql(self):
   114     def test_fetch_rql(self):
   115         user = self.user()
   115         user = self.user()
   116         Personne = self.vreg.etype_class('Personne')
   116         Personne = self.vreg.etype_class('Personne')
   117         Societe = self.vreg.etype_class('Societe')
   117         Societe = self.vreg.etype_class('Societe')
   118         Note = self.vreg.etype_class('Note')
   118         Note = self.vreg.etype_class('Note')
   128         pfetch_attrs = Personne.fetch_attrs
   128         pfetch_attrs = Personne.fetch_attrs
   129         sfetch_attrs = Societe.fetch_attrs
   129         sfetch_attrs = Societe.fetch_attrs
   130         try:
   130         try:
   131             # testing unknown attributes
   131             # testing unknown attributes
   132             Personne.fetch_attrs = ('bloug', 'beep')
   132             Personne.fetch_attrs = ('bloug', 'beep')
   133             self.assertEquals(Personne.fetch_rql(user), 'Any X WHERE X is Personne')            
   133             self.assertEquals(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   134             # testing one non final relation
   134             # testing one non final relation
   135             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   135             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   136             self.assertEquals(Personne.fetch_rql(user),
   136             self.assertEquals(Personne.fetch_rql(user),
   137                               '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')
   137                               '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')
   138             # testing two non final relations
   138             # testing two non final relations
   177                           'X type AA, X modification_date AB')
   177                           'X type AA, X modification_date AB')
   178         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', ))
   178         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', ))
   179         # XXX
   179         # XXX
   180         self.assertEquals(aff.related_rql('evaluee'),
   180         self.assertEquals(aff.related_rql('evaluee'),
   181                           'Any X,AA ORDERBY Z DESC WHERE X modification_date Z, E eid %(x)s, E evaluee X, X modification_date AA')
   181                           'Any X,AA ORDERBY Z DESC WHERE X modification_date Z, E eid %(x)s, E evaluee X, X modification_date AA')
   182     
   182 
   183     def test_entity_unrelated(self):
   183     def test_entity_unrelated(self):
   184         p = self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   184         p = self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   185         e = self.add_entity('Tag', name=u'x')
   185         e = self.add_entity('Tag', name=u'x')
   186         related = [r.eid for r in e.tags]
   186         related = [r.eid for r in e.tags]
   187         self.failUnlessEqual(related, [])
   187         self.failUnlessEqual(related, [])
   196         e = self.add_entity('Tag', name=u'x')
   196         e = self.add_entity('Tag', name=u'x')
   197         self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   197         self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   198         self.add_entity('Personne', nom=u'di mascio', prenom=u'gwen')
   198         self.add_entity('Personne', nom=u'di mascio', prenom=u'gwen')
   199         self.assertEquals(len(e.unrelated('tags', 'Personne', 'subject', limit=1)),
   199         self.assertEquals(len(e.unrelated('tags', 'Personne', 'subject', limit=1)),
   200                           1)
   200                           1)
   201         
   201 
   202     def test_new_entity_unrelated(self):
   202     def test_new_entity_unrelated(self):
   203         e = self.etype_instance('CWUser')
   203         e = self.etype_instance('CWUser')
   204         unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')]
   204         unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')]
   205         # should be default groups but owners, i.e. managers, users, guests
   205         # should be default groups but owners, i.e. managers, users, guests
   206         self.assertEquals(len(unrelated), 3)
   206         self.assertEquals(len(unrelated), 3)
   207 
   207 
   208     def test_printable_value_string(self):
   208     def test_printable_value_string(self):
   209         e = self.add_entity('Card', title=u'rest test', content=u'du :eid:`1:*ReST*`',
   209         e = self.add_entity('Card', title=u'rest test', content=u'du :eid:`1:*ReST*`',
   210                             content_format=u'text/rest')
   210                             content_format=u'text/rest')
   211         self.assertEquals(e.printable_value('content'),
   211         self.assertEquals(e.printable_value('content'),
   212                           '<p>du <a class="reference" href="http://testing.fr/cubicweb/egroup/managers">*ReST*</a></p>\n')
   212                           '<p>du <a class="reference" href="http://testing.fr/cubicweb/cwgroup/managers">*ReST*</a></p>\n')
   213         e['content'] = 'du <em>html</em> <ref rql="CWUser X">users</ref>'
   213         e['content'] = 'du <em>html</em> <ref rql="CWUser X">users</ref>'
   214         e['content_format'] = 'text/html'
   214         e['content_format'] = 'text/html'
   215         self.assertEquals(e.printable_value('content'),
   215         self.assertEquals(e.printable_value('content'),
   216                           'du <em>html</em> <a href="http://testing.fr/cubicweb/view?rql=CWUser%20X">users</a>')
   216                           'du <em>html</em> <a href="http://testing.fr/cubicweb/view?rql=CWUser%20X">users</a>')
   217         e['content'] = 'du *texte*'
   217         e['content'] = 'du *texte*'
   235         if HAS_TAL:
   235         if HAS_TAL:
   236             e['content'] = '<h1 tal:content="self/title">titre</h1>'
   236             e['content'] = '<h1 tal:content="self/title">titre</h1>'
   237             e['content_format'] = 'text/cubicweb-page-template'
   237             e['content_format'] = 'text/cubicweb-page-template'
   238             self.assertEquals(e.printable_value('content'),
   238             self.assertEquals(e.printable_value('content'),
   239                               '<h1>zou</h1>')
   239                               '<h1>zou</h1>')
   240         
   240 
   241 
   241 
   242     def test_printable_value_bytes(self):
   242     def test_printable_value_bytes(self):
   243         e = self.add_entity('File', data=Binary('lambda x: 1'), data_format=u'text/x-python',
   243         e = self.add_entity('File', data=Binary('lambda x: 1'), data_format=u'text/x-python',
   244                             data_encoding=u'ascii', name=u'toto.py')
   244                             data_encoding=u'ascii', name=u'toto.py')
   245         from cubicweb.common import mttransforms
   245         from cubicweb.common import mttransforms
   252             self.assertEquals(e.printable_value('data'),
   252             self.assertEquals(e.printable_value('data'),
   253                               '''<pre class="python">
   253                               '''<pre class="python">
   254 <span style="color: #C00000;">lambda</span> <span style="color: #000000;">x</span><span style="color: #0000C0;">:</span> <span style="color: #0080C0;">1</span>
   254 <span style="color: #C00000;">lambda</span> <span style="color: #000000;">x</span><span style="color: #0000C0;">:</span> <span style="color: #0080C0;">1</span>
   255 </pre>
   255 </pre>
   256 ''')
   256 ''')
   257         
   257 
   258         e = self.add_entity('File', data=Binary('*héhéhé*'), data_format=u'text/rest',
   258         e = self.add_entity('File', data=Binary('*héhéhé*'), data_format=u'text/rest',
   259                             data_encoding=u'utf-8', name=u'toto.txt')
   259                             data_encoding=u'utf-8', name=u'toto.txt')
   260         self.assertEquals(e.printable_value('data'),
   260         self.assertEquals(e.printable_value('data'),
   261                           u'<p><em>héhéhé</em></p>\n')
   261                           u'<p><em>héhéhé</em></p>\n')
   262 
   262 
   288         self.assertEquals(e.printable_value('content'), e['content'])
   288         self.assertEquals(e.printable_value('content'), e['content'])
   289         e['content'] = u'<div>été</div>'
   289         e['content'] = u'<div>été</div>'
   290         self.assertEquals(e.printable_value('content'), e['content'])
   290         self.assertEquals(e.printable_value('content'), e['content'])
   291         e['content'] = u'été'
   291         e['content'] = u'été'
   292         self.assertEquals(e.printable_value('content'), e['content'])
   292         self.assertEquals(e.printable_value('content'), e['content'])
   293         
   293 
   294         
   294 
   295     def test_fulltextindex(self):
   295     def test_fulltextindex(self):
   296         e = self.etype_instance('File')
   296         e = self.etype_instance('File')
   297         e['name'] = 'an html file'
   297         e['name'] = 'an html file'
   298         e['description'] = 'du <em>html</em>'
   298         e['description'] = 'du <em>html</em>'
   299         e['description_format'] = 'text/html'
   299         e['description_format'] = 'text/html'
   300         e['data'] = Binary('some <em>data</em>')
   300         e['data'] = Binary('some <em>data</em>')
   301         e['data_format'] = 'text/html'
   301         e['data_format'] = 'text/html'
   302         e['data_encoding'] = 'ascii'
   302         e['data_encoding'] = 'ascii'
   303         self.assertEquals(set(e.get_words()), 
   303         self.assertEquals(set(e.get_words()),
   304                           set(['an', 'html', 'file', 'du', 'html', 'some', 'data']))
   304                           set(['an', 'html', 'file', 'du', 'html', 'some', 'data']))
   305 
   305 
   306         
   306 
   307     def test_nonregr_relation_cache(self):
   307     def test_nonregr_relation_cache(self):
   308         p1 = self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   308         p1 = self.add_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   309         p2 = self.add_entity('Personne', nom=u'toto')
   309         p2 = self.add_entity('Personne', nom=u'toto')
   310         self.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"')
   310         self.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"')
   311         self.assertEquals(p1.evaluee[0].nom, "toto")
   311         self.assertEquals(p1.evaluee[0].nom, "toto")
   312         self.failUnless(not p1.reverse_evaluee)
   312         self.failUnless(not p1.reverse_evaluee)
   313         
   313 
   314     def test_complete_relation(self):
   314     def test_complete_relation(self):
   315         self.execute('SET RT add_permission G WHERE RT name "wf_info_for", G name "managers"')
   315         self.execute('SET RT add_permission G WHERE RT name "wf_info_for", G name "managers"')
   316         self.commit()
   316         self.commit()
   317         try:
   317         try:
   318             eid = self.execute('INSERT TrInfo X: X comment "zou", X wf_info_for U,'
   318             eid = self.execute('INSERT TrInfo X: X comment "zou", X wf_info_for U,'
   380         self.assertEquals(metainf, {'source': {'adapter': 'native', 'uri': 'system'}, 'type': u'Note', 'extid': None})
   380         self.assertEquals(metainf, {'source': {'adapter': 'native', 'uri': 'system'}, 'type': u'Note', 'extid': None})
   381         self.assertEquals(note.absolute_url(), 'http://testing.fr/cubicweb/note/%s' % note.eid)
   381         self.assertEquals(note.absolute_url(), 'http://testing.fr/cubicweb/note/%s' % note.eid)
   382         metainf['source'] = metainf['source'].copy()
   382         metainf['source'] = metainf['source'].copy()
   383         metainf['source']['base-url']  = 'http://cubicweb2.com/'
   383         metainf['source']['base-url']  = 'http://cubicweb2.com/'
   384         self.assertEquals(note.absolute_url(), 'http://cubicweb2.com/note/%s' % note.eid)
   384         self.assertEquals(note.absolute_url(), 'http://cubicweb2.com/note/%s' % note.eid)
   385         
   385 
   386 if __name__ == '__main__':
   386 if __name__ == '__main__':
   387     from logilab.common.testlib import unittest_main
   387     from logilab.common.testlib import unittest_main
   388     unittest_main()
   388     unittest_main()
   389 
   389