test/unittest_rset.py
changeset 9347 bd841d6ae723
parent 9331 c6f54ed6b60a
child 9582 46ed25d38fe2
equal deleted inserted replaced
9344:4da3ef764395 9347:bd841d6ae723
    25 
    25 
    26 from logilab.common.testlib import TestCase, unittest_main, mock_object
    26 from logilab.common.testlib import TestCase, unittest_main, mock_object
    27 
    27 
    28 from cubicweb.devtools.testlib import CubicWebTC
    28 from cubicweb.devtools.testlib import CubicWebTC
    29 from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator
    29 from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator
       
    30 
       
    31 from cubicweb import NoResultError, MultipleResultsError
    30 
    32 
    31 
    33 
    32 def pprelcachedict(d):
    34 def pprelcachedict(d):
    33     res = {}
    35     res = {}
    34     for k, (rset, related) in d.items():
    36     for k, (rset, related) in d.items():
   366             etype, n = expected[entity.cw_row]
   368             etype, n = expected[entity.cw_row]
   367             self.assertEqual(entity.cw_etype, etype)
   369             self.assertEqual(entity.cw_etype, etype)
   368             attr = etype == 'Bookmark' and 'title' or 'name'
   370             attr = etype == 'Bookmark' and 'title' or 'name'
   369             self.assertEqual(entity.cw_attr_cache[attr], n)
   371             self.assertEqual(entity.cw_attr_cache[attr], n)
   370 
   372 
       
   373     def test_one(self):
       
   374         self.request().create_entity('CWUser', login=u'cdevienne',
       
   375                                      upassword=u'cdevienne',
       
   376                                      surname=u'de Vienne',
       
   377                                      firstname=u'Christophe')
       
   378         e = self.execute('Any X WHERE X login "cdevienne"').one()
       
   379 
       
   380         self.assertEqual(e.surname, u'de Vienne')
       
   381 
       
   382         e = self.execute(
       
   383             'Any X, N WHERE X login "cdevienne", X surname N').one()
       
   384         self.assertEqual(e.surname, u'de Vienne')
       
   385 
       
   386         e = self.execute(
       
   387             'Any N, X WHERE X login "cdevienne", X surname N').one(col=1)
       
   388         self.assertEqual(e.surname, u'de Vienne')
       
   389 
       
   390     def test_one_no_rows(self):
       
   391         with self.assertRaises(NoResultError):
       
   392             self.execute('Any X WHERE X login "patanok"').one()
       
   393 
       
   394     def test_one_multiple_rows(self):
       
   395         self.request().create_entity(
       
   396             'CWUser', login=u'cdevienne', upassword=u'cdevienne',
       
   397             surname=u'de Vienne', firstname=u'Christophe')
       
   398 
       
   399         self.request().create_entity(
       
   400             'CWUser', login=u'adim', upassword='adim', surname=u'di mascio',
       
   401             firstname=u'adrien')
       
   402 
       
   403         with self.assertRaises(MultipleResultsError):
       
   404             self.execute('Any X WHERE X is CWUser').one()
       
   405 
   371     def test_related_entity_optional(self):
   406     def test_related_entity_optional(self):
   372         e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path')
   407         e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path')
   373         rset = self.execute('Any B,U,L WHERE B bookmarked_by U?, U login L')
   408         rset = self.execute('Any B,U,L WHERE B bookmarked_by U?, U login L')
   374         entity, rtype = rset.related_entity(0, 2)
   409         entity, rtype = rset.related_entity(0, 2)
   375         self.assertEqual(entity, None)
   410         self.assertEqual(entity, None)