server/test/unittest_querier.py
changeset 8542 7e264ce34cd4
parent 8349 fdb796435d7b
child 8623 9f7481a05383
equal deleted inserted replaced
8541:5b6bc27ece6e 8542:7e264ce34cd4
    27 
    27 
    28 from cubicweb import QueryError, Unauthorized, Binary
    28 from cubicweb import QueryError, Unauthorized, Binary
    29 from cubicweb.server.sqlutils import SQL_PREFIX
    29 from cubicweb.server.sqlutils import SQL_PREFIX
    30 from cubicweb.server.utils import crypt_password
    30 from cubicweb.server.utils import crypt_password
    31 from cubicweb.server.sources.native import make_schema
    31 from cubicweb.server.sources.native import make_schema
       
    32 from cubicweb.server.querier import manual_build_descr, _make_description
    32 from cubicweb.devtools import get_test_db_handler, TestServerConfiguration
    33 from cubicweb.devtools import get_test_db_handler, TestServerConfiguration
    33 from cubicweb.devtools.testlib import CubicWebTC
    34 from cubicweb.devtools.testlib import CubicWebTC
    34 from cubicweb.devtools.repotest import tuplify, BaseQuerierTC
    35 from cubicweb.devtools.repotest import tuplify, BaseQuerierTC
    35 from unittest_session import Variable
       
    36 
    36 
    37 class FixedOffset(tzinfo):
    37 class FixedOffset(tzinfo):
    38     def __init__(self, hours=0):
    38     def __init__(self, hours=0):
    39         self.hours = hours
    39         self.hours = hours
    40     def utcoffset(self, dt):
    40     def utcoffset(self, dt):
    83 def tearDownClass(cls, *args):
    83 def tearDownClass(cls, *args):
    84     global repo, cnx
    84     global repo, cnx
    85     cnx.close()
    85     cnx.close()
    86     repo.shutdown()
    86     repo.shutdown()
    87     del repo, cnx
    87     del repo, cnx
       
    88 
       
    89 
       
    90 class Variable:
       
    91     def __init__(self, name):
       
    92         self.name = name
       
    93         self.children = []
       
    94 
       
    95     def get_type(self, solution, args=None):
       
    96         return solution[self.name]
       
    97     def as_string(self):
       
    98         return self.name
       
    99 
       
   100 class Function:
       
   101     def __init__(self, name, varname):
       
   102         self.name = name
       
   103         self.children = [Variable(varname)]
       
   104     def get_type(self, solution, args=None):
       
   105         return 'Int'
       
   106 
       
   107 class MakeDescriptionTC(TestCase):
       
   108     def test_known_values(self):
       
   109         solution = {'A': 'Int', 'B': 'CWUser'}
       
   110         self.assertEqual(_make_description((Function('max', 'A'), Variable('B')), {}, solution),
       
   111                           ['Int','CWUser'])
    88 
   112 
    89 
   113 
    90 class UtilsTC(BaseQuerierTC):
   114 class UtilsTC(BaseQuerierTC):
    91     setUpClass = classmethod(setUpClass)
   115     setUpClass = classmethod(setUpClass)
    92     tearDownClass = classmethod(tearDownClass)
   116     tearDownClass = classmethod(tearDownClass)
   240         rset = self.execute('Any %(x)s', {'x': 'str'})
   264         rset = self.execute('Any %(x)s', {'x': 'str'})
   241         self.assertEqual(rset.description[0][0], 'String')
   265         self.assertEqual(rset.description[0][0], 'String')
   242         rset = self.execute('Any %(x)s', {'x': u'str'})
   266         rset = self.execute('Any %(x)s', {'x': u'str'})
   243         self.assertEqual(rset.description[0][0], 'String')
   267         self.assertEqual(rset.description[0][0], 'String')
   244 
   268 
       
   269     def test_build_descr1(self):
       
   270         rset = self.execute('(Any U,L WHERE U login L) UNION (Any G,N WHERE G name N, G is CWGroup)')
       
   271         rset.req = self.transaction
       
   272         orig_length = len(rset)
       
   273         rset.rows[0][0] = 9999999
       
   274         description = manual_build_descr(rset.req, rset.syntax_tree(), None, rset.rows)
       
   275         self.assertEqual(len(description), orig_length - 1)
       
   276         self.assertEqual(len(rset.rows), orig_length - 1)
       
   277         self.assertNotEqual(rset.rows[0][0], 9999999)
       
   278 
       
   279     def test_build_descr2(self):
       
   280         rset = self.execute('Any X,Y WITH X,Y BEING ((Any G,NULL WHERE G is CWGroup) UNION (Any U,G WHERE U in_group G))')
       
   281         for x, y in rset.description:
       
   282             if y is not None:
       
   283                 self.assertEqual(y, 'CWGroup')
       
   284 
       
   285     def test_build_descr3(self):
       
   286         rset = self.execute('(Any G,NULL WHERE G is CWGroup) UNION (Any U,G WHERE U in_group G)')
       
   287         for x, y in rset.description:
       
   288             if y is not None:
       
   289                 self.assertEqual(y, 'CWGroup')
       
   290 
   245 
   291 
   246 class QuerierTC(BaseQuerierTC):
   292 class QuerierTC(BaseQuerierTC):
   247     setUpClass = classmethod(setUpClass)
   293     setUpClass = classmethod(setUpClass)
   248     tearDownClass = classmethod(tearDownClass)
   294     tearDownClass = classmethod(tearDownClass)
   249 
   295