test/unittest_rset.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 10 Aug 2010 18:26:43 +0200
branchstable
changeset 6090 887c069d88df
parent 5752 b0bb553e3be4
child 6142 8bc6eac1fac1
child 6195 a0c4441e6ddf
permissions -rw-r--r--
[primary] backport _rel_label method from the default branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
# coding: utf-8
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     2
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     4
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     5
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     6
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    10
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    11
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    15
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    16
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3589
diff changeset
    19
"""unit tests for module cubicweb.utils
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1640
diff changeset
    20
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1640
diff changeset
    21
"""
4536
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    22
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    23
from urlparse import urlsplit
5740
46207f491a51 [rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5667
diff changeset
    24
import pickle
4536
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    25
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    26
from rql import parse
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
4850
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
    28
from logilab.common.testlib import TestCase, unittest_main, mock_object
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 572
diff changeset
    29
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
    30
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    33
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
def pprelcachedict(d):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    res = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    for k, (rset, related) in d.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        res[k] = sorted(v.eid for v in related)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    return sorted(res.items())
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    39
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
class AttrDescIteratorTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    """TestCase for cubicweb.rset.attr_desc_iterator"""
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    43
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    def test_relations_description(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        """tests relations_description() function"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        queries = {
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    47
            'Any U,L,M where U is CWUser, U login L, U mail M' : [(1, 'login', 'subject'), (2, 'mail', 'subject')],
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    48
            'Any U,L,M where U is CWUser, L is Foo, U mail M' : [(2, 'mail', 'subject')],
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
            'Any C,P where C is Company, C employs P' : [(1, 'employs', 'subject')],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
            'Any C,P where C is Company, P employed_by P' : [],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
            'Any C where C is Company, C employs P' : [],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
            }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        for rql, relations in queries.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
            result = list(attr_desc_iterator(parse(rql).children[0]))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
            self.assertEquals((rql, result), (rql, relations))
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    56
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    def test_relations_description_indexed(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        """tests relations_description() function"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        queries = {
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    60
            'Any C,U,P,L,M where C is Company, C employs P, U is CWUser, U login L, U mail M' :
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
            {0: [(2,'employs', 'subject')], 1: [(3,'login', 'subject'), (4,'mail', 'subject')]},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
            }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        for rql, results in queries.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
            for var_index, relations in results.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
                result = list(attr_desc_iterator(parse(rql).children[0], var_index))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
                self.assertEquals(result, relations)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
    70
class ResultSetTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    def setUp(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        super(ResultSetTC, self).setUp()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        self.rset = ResultSet([[12, 'adim'], [13, 'syt']],
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    75
                              'Any U,L where U is CWUser, U login L',
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    76
                              description=[['CWUser', 'String'], ['Bar', 'String']])
4850
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
    77
        self.rset.req = mock_object(vreg=self.vreg)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    def compare_urls(self, url1, url2):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        info1 = urlsplit(url1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        info2 = urlsplit(url2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        self.assertEquals(info1[:3], info2[:3])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        if info1[3] != info2[3]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
            params1 = dict(pair.split('=') for pair in info1[3].split('&'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
            params2 = dict(pair.split('=') for pair in info1[3].split('&'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
            self.assertDictEquals(params1, params2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
5740
46207f491a51 [rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5667
diff changeset
    88
    def test_pickle(self):
5747
d6ac0cd30fde [rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5740
diff changeset
    89
        del self.rset.req
5740
46207f491a51 [rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5667
diff changeset
    90
        self.assertEquals(len(pickle.dumps(self.rset)), 392)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    91
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    def test_build_url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        req = self.request()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        baseurl = req.base_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        self.compare_urls(req.build_url('view', vid='foo', rql='yo'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
                          '%sview?vid=foo&rql=yo' % baseurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
        self.compare_urls(req.build_url('view', _restpath='task/title/go'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
                          '%stask/title/go' % baseurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        #self.compare_urls(req.build_url('view', _restpath='/task/title/go'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        #                  '%stask/title/go' % baseurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        # empty _restpath should not crash
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        self.compare_urls(req.build_url('view', _restpath=''), baseurl)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   103
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   104
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    def test_resultset_build(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        """test basic build of a ResultSet"""
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   107
        rs = ResultSet([1,2,3], 'CWGroup X', description=['CWGroup', 'CWGroup', 'CWGroup'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        self.assertEquals(rs.rowcount, 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        self.assertEquals(rs.rows, [1,2,3])
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   110
        self.assertEquals(rs.description, ['CWGroup', 'CWGroup', 'CWGroup'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    def test_resultset_limit(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']],
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   115
                       'Any U,L where U is CWUser, U login L',
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   116
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        rs.req = self.request()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   118
        rs.vreg = self.vreg
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        self.assertEquals(rs.limit(2).rows, [[12000, 'adim'], [13000, 'syt']])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        rs2 = rs.limit(2, offset=1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        self.assertEquals(rs2.rows, [[13000, 'syt'], [14000, 'nico']])
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   123
        self.assertEquals(rs2.get_entity(0, 0).cw_row, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        self.assertEquals(rs.limit(2, offset=2).rows, [[14000, 'nico']])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        self.assertEquals(rs.limit(2, offset=3).rows, [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   126
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
    def test_resultset_filter(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']],
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   130
                       'Any U,L where U is CWUser, U login L',
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   131
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        rs.req = self.request()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   133
        rs.vreg = self.vreg
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        def test_filter(entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
            return entity.login != 'nico'
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   136
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        rs2 = rs.filtered_rset(test_filter)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        self.assertEquals(len(rs2), 2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        self.assertEquals([login for _, login in rs2], ['adim', 'syt'])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   140
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
    def test_resultset_transform(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        rs = ResultSet([[12, 'adim'], [13, 'syt'], [14, 'nico']],
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   143
                       'Any U,L where U is CWUser, U login L',
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   144
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
        rs.req = self.request()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        def test_transform(row, desc):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
            return row[1:], desc[1:]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        rs2 = rs.transformed_rset(test_transform)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        self.assertEquals(len(rs2), 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        self.assertEquals(list(rs2), [['adim'],['syt'],['nico']])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   152
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
    def test_resultset_sort(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']],
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   155
                       'Any U,L where U is CWUser, U login L',
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   156
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
        rs.req = self.request()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   158
        rs.vreg = self.vreg
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   159
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
        rs2 = rs.sorted_rset(lambda e:e['login'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
        self.assertEquals(len(rs2), 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
        self.assertEquals([login for _, login in rs2], ['adim', 'nico', 'syt'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
        # make sure rs is unchanged
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
        self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico'])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   165
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        rs2 = rs.sorted_rset(lambda e:e['login'], reverse=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
        self.assertEquals(len(rs2), 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
        self.assertEquals([login for _, login in rs2], ['syt', 'nico', 'adim'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
        # make sure rs is unchanged
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
        self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
        rs3 = rs.sorted_rset(lambda row: row[1], col=-1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        self.assertEquals(len(rs3), 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
        self.assertEquals([login for _, login in rs3], ['adim', 'nico', 'syt'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
        # make sure rs is unchanged
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
    def test_resultset_split(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
        rs = ResultSet([[12000, 'adim', u'Adim chez les pinguins'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
                        [12000, 'adim', u'Jardiner facile'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
                        [13000, 'syt',  u'Le carrelage en 42 leçons'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
                        [14000, 'nico', u'La tarte tatin en 15 minutes'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
                        [14000, 'nico', u"L'épluchage du castor commun"]],
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   184
                       'Any U, L, T WHERE U is CWUser, U login L,'\
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
                       'D created_by U, D title T',
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   186
                       description=[['CWUser', 'String', 'String']] * 5)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
        rs.req = self.request()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   188
        rs.vreg = self.vreg
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   189
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        rsets = rs.split_rset(lambda e:e['login'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
        self.assertEquals(len(rsets), 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
        self.assertEquals([login for _, login,_ in rsets[0]], ['adim', 'adim'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
        self.assertEquals([login for _, login,_ in rsets[1]], ['syt'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
        self.assertEquals([login for _, login,_ in rsets[2]], ['nico', 'nico'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        # make sure rs is unchanged
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        self.assertEquals([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico'])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   197
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        rsets = rs.split_rset(lambda e:e['login'], return_dict=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        self.assertEquals(len(rsets), 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
        self.assertEquals([login for _, login,_ in rsets['nico']], ['nico', 'nico'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        self.assertEquals([login for _, login,_ in rsets['adim']], ['adim', 'adim'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
        self.assertEquals([login for _, login,_ in rsets['syt']], ['syt'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
        # make sure rs is unchanged
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
        self.assertEquals([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
        rsets = rs.split_rset(lambda s: s.count('d'), col=2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
        self.assertEquals(len(rsets), 2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
        self.assertEquals([title for _, _, title in rsets[0]],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
                          [u"Adim chez les pinguins",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
                           u"Jardiner facile",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
                           u"L'épluchage du castor commun",])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
        self.assertEquals([title for _, _, title in rsets[1]],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
                          [u"Le carrelage en 42 leçons",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
                           u"La tarte tatin en 15 minutes",])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
        # make sure rs is unchanged
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
        self.assertEquals([title for _, _, title in rs],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
                          [u'Adim chez les pinguins',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
                           u'Jardiner facile',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
                           u'Le carrelage en 42 leçons',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
                           u'La tarte tatin en 15 minutes',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
                           u"L'épluchage du castor commun"])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   222
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    def test_cached_syntax_tree(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        """make sure syntax tree is cached"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
        rqlst1 = self.rset.syntax_tree()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
        rqlst2 = self.rset.syntax_tree()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
        self.assert_(rqlst1 is rqlst2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    def test_get_entity_simple(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   230
        self.request().create_entity('CWUser', login=u'adim', upassword='adim',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
                        surname=u'di mascio', firstname=u'adrien')
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
   232
        e = self.execute('Any X,T WHERE X login "adim", X surname T').get_entity(0, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
        self.assertEquals(e['surname'], 'di mascio')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        self.assertRaises(KeyError, e.__getitem__, 'firstname')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
        self.assertRaises(KeyError, e.__getitem__, 'creation_date')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   236
        self.assertEquals(pprelcachedict(e._cw_related_cache), [])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        e.complete()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
        self.assertEquals(e['firstname'], 'adrien')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   239
        self.assertEquals(pprelcachedict(e._cw_related_cache), [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   240
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
    def test_get_entity_advanced(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   242
        self.request().create_entity('Bookmark', title=u'zou', path=u'/view')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
        self.execute('SET X bookmarked_by Y WHERE X is Bookmark, Y login "anon"')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
        rset = self.execute('Any X,Y,XT,YN WHERE X bookmarked_by Y, X title XT, Y login YN')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   245
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
        e = rset.get_entity(0, 0)
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   247
        self.assertEquals(e.cw_row, 0)
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   248
        self.assertEquals(e.cw_col, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
        self.assertEquals(e['title'], 'zou')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
        self.assertRaises(KeyError, e.__getitem__, 'path')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   251
        self.assertEquals(e.view('text'), 'zou')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   252
        self.assertEquals(pprelcachedict(e._cw_related_cache), [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   253
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
        e = rset.get_entity(0, 1)
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   255
        self.assertEquals(e.cw_row, 0)
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   256
        self.assertEquals(e.cw_col, 1)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
        self.assertEquals(e['login'], 'anon')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
        self.assertRaises(KeyError, e.__getitem__, 'firstname')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   259
        self.assertEquals(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
                          [])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
        e.complete()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
        self.assertEquals(e['firstname'], None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        self.assertEquals(e.view('text'), 'anon')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   264
        self.assertEquals(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
                          [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   266
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
        self.assertRaises(NotAnEntity, rset.get_entity, 0, 2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
        self.assertRaises(NotAnEntity, rset.get_entity, 0, 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
    def test_get_entity_relation_cache_compt(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
        rset = self.execute('Any X,S WHERE X in_state S, X login "anon"')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
        e = rset.get_entity(0, 0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
        seid = self.execute('State X WHERE X name "activated"')[0][0]
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   274
        # for_user / in_group are prefetched in CWUser __init__, in_state should
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
        # be filed from our query rset
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   276
        self.assertEquals(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
                          [('in_state_subject', [seid])])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
    def test_get_entity_advanced_prefilled_cache(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   280
        e = self.request().create_entity('Bookmark', title=u'zou', path=u'path')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
        self.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
        rset = self.execute('Any X,U,S,XT,UL,SN WHERE X created_by U, U in_state S, '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
                            'X title XT, S name SN, U login UL, X eid %s' % e.eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
        e = rset.get_entity(0, 0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
        self.assertEquals(e['title'], 'zou')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   286
        self.assertEquals(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
                          [('created_by_subject', [5])])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
        # first level of recursion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
        u = e.created_by[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
        self.assertEquals(u['login'], 'admin')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
        self.assertRaises(KeyError, u.__getitem__, 'firstname')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
        # second level of recursion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
        s = u.in_state[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
        self.assertEquals(s['name'], 'activated')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
        self.assertRaises(KeyError, s.__getitem__, 'description')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   297
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
    def test_get_entity_cache_with_left_outer_join(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   299
        eid = self.execute('INSERT CWUser E: E login "joe", E upassword "joe", E in_group G '
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
                           'WHERE G name "users"')[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
        rset = self.execute('Any X,E WHERE X eid %(x)s, X primary_email E?', {'x': eid})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
        e = rset.get_entity(0, 0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
        # if any of the assertion below fails with a KeyError, the relation is not cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
        # related entities should be an empty list
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   305
        self.assertEquals(e._cw_relation_cache('primary_email', 'subject', True), ())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
        # related rset should be an empty rset
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   307
        cached = e._cw_relation_cache('primary_email', 'subject', False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
        self.assertIsInstance(cached, ResultSet)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
        self.assertEquals(cached.rowcount, 0)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   310
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
    def test_get_entity_union(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   313
        e = self.request().create_entity('Bookmark', title=u'manger', path=u'path')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
        rset = self.execute('Any X,N ORDERBY N WITH X,N BEING '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
                            '((Any X,N WHERE X is Bookmark, X title N)'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
                            ' UNION '
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   317
                            ' (Any X,N WHERE X is CWGroup, X name N))')
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   318
        expected = (('CWGroup', 'guests'), ('CWGroup', 'managers'),
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   319
                    ('Bookmark', 'manger'), ('CWGroup', 'owners'),
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   320
                    ('CWGroup', 'users'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
        for entity in rset.entities(): # test get_entity for each row actually
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   322
            etype, n = expected[entity.cw_row]
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   323
            self.assertEquals(entity.__regid__, etype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
            attr = etype == 'Bookmark' and 'title' or 'name'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
            self.assertEquals(entity[attr], n)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   326
572
9849fed789c9 test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   327
    def test_related_entity_optional(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   328
        e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path')
572
9849fed789c9 test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   329
        rset = self.execute('Any B,U,L WHERE B bookmarked_by U?, U login L')
9849fed789c9 test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   330
        entity, rtype = rset.related_entity(0, 2)
9849fed789c9 test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   331
        self.assertEquals(entity, None)
9849fed789c9 test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   332
        self.assertEquals(rtype, None)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   333
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
    def test_related_entity_union_subquery(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   335
        e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
        rset = self.execute('Any X,N ORDERBY N WITH X,N BEING '
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   337
                            '((Any X,N WHERE X is CWGroup, X name N)'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
                            ' UNION '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
                            ' (Any X,N WHERE X is Bookmark, X title N))')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
        entity, rtype = rset.related_entity(0, 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
        self.assertEquals(entity.eid, e.eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
        self.assertEquals(rtype, 'title')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
        entity, rtype = rset.related_entity(1, 1)
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3023
diff changeset
   344
        self.assertEquals(entity.__regid__, 'CWGroup')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
        self.assertEquals(rtype, 'name')
3016
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   346
        #
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
        rset = self.execute('Any X,N ORDERBY N WHERE X is Bookmark WITH X,N BEING '
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   348
                            '((Any X,N WHERE X is CWGroup, X name N)'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
                            ' UNION '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
                            ' (Any X,N WHERE X is Bookmark, X title N))')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
        entity, rtype = rset.related_entity(0, 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
        self.assertEquals(entity.eid, e.eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
        self.assertEquals(rtype, 'title')
3016
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   354
        #
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   355
        rset = self.execute('Any X,N ORDERBY N WITH N,X BEING '
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   356
                            '((Any N,X WHERE X is CWGroup, X name N)'
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   357
                            ' UNION '
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   358
                            ' (Any N,X WHERE X is Bookmark, X title N))')
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   359
        entity, rtype = rset.related_entity(0, 1)
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   360
        self.assertEquals(entity.eid, e.eid)
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   361
        self.assertEquals(rtype, 'title')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   362
4427
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   363
    def test_related_entity_trap_subquery(self):
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   364
        req = self.request()
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   365
        req.create_entity('Bookmark', title=u'test bookmark', path=u'')
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   366
        self.execute('SET B bookmarked_by U WHERE U login "admin"')
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   367
        rset = self.execute('Any B,T,L WHERE B bookmarked_by U, U login L '
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   368
                            'WITH B,T BEING (Any B,T WHERE B is Bookmark, B title T)')
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   369
        rset.related_entity(0, 2)
410c99a917fa fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   370
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
    def test_entities(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
        rset = self.execute('Any U,G WHERE U in_group G')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        # make sure we have at least one element
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
        self.failUnless(rset)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
        self.assertEquals(set(e.e_schema.type for e in rset.entities(0)),
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   376
                          set(['CWUser',]))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        self.assertEquals(set(e.e_schema.type for e in rset.entities(1)),
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   378
                          set(['CWGroup',]))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   380
    def test_printable_rql(self):
2635
c94df21f7ab2 F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2286
diff changeset
   381
        rset = self.execute(u'CWEType X WHERE X final FALSE')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
        self.assertEquals(rset.printable_rql(),
2635
c94df21f7ab2 F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2286
diff changeset
   383
                          'Any X WHERE X final FALSE, X is CWEType')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
    def test_searched_text(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
        rset = self.execute(u'Any X WHERE X has_text "foobar"')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
        self.assertEquals(rset.searched_text(), 'foobar')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
        rset = self.execute(u'Any X WHERE X has_text %(text)s', {'text' : 'foo'})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
        self.assertEquals(rset.searched_text(), 'foo')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   390
4939
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   391
    def test_union_limited_rql(self):
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   392
        rset = self.execute('(Any X,N WHERE X is Bookmark, X title N)'
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   393
                            ' UNION '
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   394
                            '(Any X,N WHERE X is CWGroup, X name N)')
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   395
        rset.limit(2, 10, inplace=True)
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   396
        self.assertEquals(rset.limited_rql(),
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   397
                          'Any A,B LIMIT 2 OFFSET 10 '
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   398
                          'WITH A,B BEING ('
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   399
                          '(Any X,N WHERE X is Bookmark, X title N) '
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   400
                          'UNION '
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   401
                          '(Any X,N WHERE X is CWGroup, X name N)'
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   402
                          ')')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   403
5667
04cbd80fd5dc [rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5426
diff changeset
   404
    def test_count_users_by_date(self):
04cbd80fd5dc [rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5426
diff changeset
   405
        rset = self.execute('Any D, COUNT(U) GROUPBY D WHERE U is CWUser, U creation_date D')
04cbd80fd5dc [rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5426
diff changeset
   406
        self.assertEquals(rset.related_entity(0,0), (None, None))
04cbd80fd5dc [rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5426
diff changeset
   407
5687
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   408
    def test_str(self):
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   409
        rset = self.execute('(Any X,N WHERE X is CWGroup, X name N)')
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   410
        self.assertIsInstance(str(rset), basestring)
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   411
        self.assertEquals(len(str(rset).splitlines()), 1)
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   412
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   413
    def test_repr(self):
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   414
        rset = self.execute('(Any X,N WHERE X is CWGroup, X name N)')
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   415
        self.assertIsInstance(repr(rset), basestring)
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   416
        self.assertTrue(len(repr(rset).splitlines()) > 1)
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   417
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   418
        rset = self.execute('(Any X WHERE X is CWGroup, X name "managers")')
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   419
        self.assertIsInstance(str(rset), basestring)
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   420
        self.assertEquals(len(str(rset).splitlines()), 1)
3ea39709b50e [rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5679
diff changeset
   421
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   423
    unittest_main()