test/unittest_rset.py
author Julien Cristau <julien.cristau@logilab.fr>
Tue, 16 Apr 2013 16:51:18 +0200
branchstable
changeset 8953 041804bc48e9
parent 8735 5567a5117aeb
child 8900 010a59e12d89
permissions -rw-r--r--
[server] add a looping task to free unused cnxsets capture by dead pyro When a pyro client disconnects, we're not necessarily notified so they can hang on to their session/cnxset forever. Don't let them do that. The hacky looping task is only installed when pyro is enabled. This optional activation save this very hacky patch from being rejected. closes #2825380
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
7395
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
     2
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
     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/>.
6366
1806148d6ce8 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6225 6340
diff changeset
    19
"""unit tests for module cubicweb.utils"""
4536
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    20
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    21
from urlparse import urlsplit
5740
46207f491a51 [rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5667
diff changeset
    22
import pickle
4536
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    23
02d0803a60b8 cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4461
diff changeset
    24
from rql import parse
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
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
    26
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
    27
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
    28
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    31
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
def pprelcachedict(d):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    res = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    for k, (rset, related) in d.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        res[k] = sorted(v.eid for v in related)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    return sorted(res.items())
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    37
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
class AttrDescIteratorTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    """TestCase for cubicweb.rset.attr_desc_iterator"""
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    41
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    def test_relations_description(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
        """tests relations_description() function"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        queries = {
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    45
            '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
    46
            '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
    47
            '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
    48
            'Any C,P where C is Company, P employed_by P' : [],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
            'Any C where C is Company, C employs P' : [],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
            }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        for rql, relations in queries.items():
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
    52
            result = list(attr_desc_iterator(parse(rql).children[0], 0, 0))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
    53
            self.assertEqual((rql, result), (rql, relations))
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    54
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
    def test_relations_description_indexed(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        """tests relations_description() function"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        queries = {
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    58
            '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
    59
            {0: [(2,'employs', 'subject')], 1: [(3,'login', 'subject'), (4,'mail', 'subject')]},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
            }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        for rql, results in queries.items():
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
    62
            for idx, relations in results.items():
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
    63
                result = list(attr_desc_iterator(parse(rql).children[0], idx, idx))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
    64
                self.assertEqual(result, relations)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
7395
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    66
    def test_subquery_callfunc(self):
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    67
        rql = ('Any A,B,C,COUNT(D) GROUPBY A,B,C WITH A,B,C,D BEING '
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    68
               '(Any YEAR(CD), MONTH(CD), S, X WHERE X is CWUser, X creation_date CD, X in_state S)')
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    69
        rqlst = parse(rql)
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    70
        select, col = rqlst.locate_subquery(2, 'CWUser', None)
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    71
        result = list(attr_desc_iterator(select, col, 2))
09ffcc04bd21 [rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7298
diff changeset
    72
        self.assertEqual(result, [])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
8085
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    74
    def test_subquery_callfunc_2(self):
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    75
        rql = ('Any X,S,L WHERE X in_state S WITH X, L BEING (Any X,MAX(L) GROUPBY X WHERE X is CWUser, T wf_info_for X, T creation_date L)')
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    76
        rqlst = parse(rql)
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    77
        select, col = rqlst.locate_subquery(0, 'CWUser', None)
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    78
        result = list(attr_desc_iterator(select, col, 0))
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    79
        self.assertEqual(result, [(1, 'in_state', 'subject')])
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
    80
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
    82
class ResultSetTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    def setUp(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        super(ResultSetTC, self).setUp()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
        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
    87
                              '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
    88
                              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
    89
        self.rset.req = mock_object(vreg=self.vreg)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    def compare_urls(self, url1, url2):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        info1 = urlsplit(url1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        info2 = urlsplit(url2)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
    94
        self.assertEqual(info1[:3], info2[:3])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        if info1[3] != info2[3]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
            params1 = dict(pair.split('=') for pair in info1[3].split('&'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
            params2 = dict(pair.split('=') for pair in info1[3].split('&'))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
    98
            self.assertDictEqual(params1, params2)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
5740
46207f491a51 [rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5667
diff changeset
   100
    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
   101
        del self.rset.req
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   102
        self.assertEqual(len(pickle.dumps(self.rset)), 392)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   103
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    def test_build_url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        req = self.request()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        baseurl = req.base_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        self.compare_urls(req.build_url('view', vid='foo', rql='yo'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
                          '%sview?vid=foo&rql=yo' % baseurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        self.compare_urls(req.build_url('view', _restpath='task/title/go'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
                          '%stask/title/go' % baseurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
        #self.compare_urls(req.build_url('view', _restpath='/task/title/go'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        #                  '%stask/title/go' % baseurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        # empty _restpath should not crash
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        self.compare_urls(req.build_url('view', _restpath=''), baseurl)
8595
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   115
        self.assertNotIn('https', req.build_url('view', vid='foo', rql='yo',
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   116
                                                  __secure__=True))
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   117
        try:
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   118
            self.config.global_set_option('https-url', 'https://testing.fr/')
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   119
            self.assertTrue('https', req.build_url('view', vid='foo', rql='yo',
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   120
                                                     __secure__=True))
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   121
            self.compare_urls(req.build_url('view', vid='foo', rql='yo',
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   122
                                            __secure__=True),
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   123
                              '%sview?vid=foo&rql=yo' % req.base_url(secure=True))
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   124
        finally:
01e1e44f1eb3 [server] implement base_url with secure=True (closes #2508638)
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 8087
diff changeset
   125
            self.config.global_set_option('https-url', None)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   126
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   127
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   128
    def test_build(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        """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
   130
        rs = ResultSet([1,2,3], 'CWGroup X', description=['CWGroup', 'CWGroup', 'CWGroup'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   131
        self.assertEqual(rs.rowcount, 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   132
        self.assertEqual(rs.rows, [1,2,3])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   133
        self.assertEqual(rs.description, ['CWGroup', 'CWGroup', 'CWGroup'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   136
    def test_limit(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        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
   138
                       '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
   139
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        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
   141
        rs.vreg = self.vreg
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   142
        self.assertEqual(rs.limit(2).rows, [[12000, 'adim'], [13000, 'syt']])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        rs2 = rs.limit(2, offset=1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   144
        self.assertEqual(rs2.rows, [[13000, 'syt'], [14000, 'nico']])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   145
        self.assertEqual(rs2.get_entity(0, 0).cw_row, 0)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   146
        self.assertEqual(rs.limit(2, offset=2).rows, [[14000, 'nico']])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   147
        self.assertEqual(rs.limit(2, offset=3).rows, [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   148
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   149
    def test_limit_2(self):
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   150
        req = self.request()
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   151
        # drop user from cache for the sake of this test
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   152
        req.drop_entity_cache(req.user.eid)
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   153
        rs = req.execute('Any E,U WHERE E is CWEType, E created_by U')
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   154
        # get entity on row 9. This will fill its created_by relation cache,
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   155
        # with cwuser on row 9 as well
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   156
        e1 = rs.get_entity(9, 0)
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   157
        # get entity on row 10. This will fill its created_by relation cache,
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   158
        # with cwuser built on row 9
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   159
        e2 = rs.get_entity(10, 0)
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   160
        # limit result set from row 10
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   161
        rs.limit(1, 10, inplace=True)
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   162
        # get back eid
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   163
        e = rs.get_entity(0, 0)
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   164
        self.assertTrue(e2 is e)
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   165
        # rs.limit has properly removed cwuser for request cache, but it's
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   166
        # still referenced by e/e2 relation cache
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   167
        u = e.created_by[0]
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   168
        # now ensure this doesn't trigger IndexError because cwuser.cw_row is 9
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   169
        # while now rset has only one row
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   170
        u.cw_rset[u.cw_row]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   172
    def test_filter(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        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
   174
                       '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
   175
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        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
   177
        rs.vreg = self.vreg
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        def test_filter(entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
            return entity.login != 'nico'
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   180
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
        rs2 = rs.filtered_rset(test_filter)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   182
        self.assertEqual(len(rs2), 2)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   183
        self.assertEqual([login for _, login in rs2], ['adim', 'syt'])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   184
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   185
    def test_transform(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
        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
   187
                       '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
   188
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
        rs.req = self.request()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        def test_transform(row, desc):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
            return row[1:], desc[1:]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
        rs2 = rs.transformed_rset(test_transform)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   194
        self.assertEqual(len(rs2), 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   195
        self.assertEqual(list(rs2), [['adim'],['syt'],['nico']])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   196
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   197
    def test_sort(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        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
   199
                       '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
   200
                       description=[['CWUser', 'String']] * 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        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
   202
        rs.vreg = self.vreg
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   203
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   204
        rs2 = rs.sorted_rset(lambda e:e.cw_attr_cache['login'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   205
        self.assertEqual(len(rs2), 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   206
        self.assertEqual([login for _, login in rs2], ['adim', 'nico', 'syt'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
        # make sure rs is unchanged
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   208
        self.assertEqual([login for _, login in rs], ['adim', 'syt', 'nico'])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   209
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   210
        rs2 = rs.sorted_rset(lambda e:e.cw_attr_cache['login'], reverse=True)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   211
        self.assertEqual(len(rs2), 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   212
        self.assertEqual([login for _, login in rs2], ['syt', 'nico', 'adim'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        # make sure rs is unchanged
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   214
        self.assertEqual([login for _, login in rs], ['adim', 'syt', 'nico'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
        rs3 = rs.sorted_rset(lambda row: row[1], col=-1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   217
        self.assertEqual(len(rs3), 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   218
        self.assertEqual([login for _, login in rs3], ['adim', 'nico', 'syt'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
        # make sure rs is unchanged
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   220
        self.assertEqual([login for _, login in rs], ['adim', 'syt', 'nico'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
8048
10a0f73d834d [rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   222
    def test_split(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
        rs = ResultSet([[12000, 'adim', u'Adim chez les pinguins'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
                        [12000, 'adim', u'Jardiner facile'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
                        [13000, 'syt',  u'Le carrelage en 42 leçons'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
                        [14000, 'nico', u'La tarte tatin en 15 minutes'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
                        [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
   228
                       'Any U, L, T WHERE U is CWUser, U login L,'\
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
                       '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
   230
                       description=[['CWUser', 'String', 'String']] * 5)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
        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
   232
        rs.vreg = self.vreg
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   233
        rsets = rs.split_rset(lambda e:e.cw_attr_cache['login'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   234
        self.assertEqual(len(rsets), 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   235
        self.assertEqual([login for _, login,_ in rsets[0]], ['adim', 'adim'])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   236
        self.assertEqual([login for _, login,_ in rsets[1]], ['syt'])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   237
        self.assertEqual([login for _, login,_ in rsets[2]], ['nico', 'nico'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
        # make sure rs is unchanged
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   239
        self.assertEqual([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico'])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   240
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   241
        rsets = rs.split_rset(lambda e:e.cw_attr_cache['login'], return_dict=True)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   242
        self.assertEqual(len(rsets), 3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   243
        self.assertEqual([login for _, login,_ in rsets['nico']], ['nico', 'nico'])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   244
        self.assertEqual([login for _, login,_ in rsets['adim']], ['adim', 'adim'])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   245
        self.assertEqual([login for _, login,_ in rsets['syt']], ['syt'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
        # make sure rs is unchanged
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   247
        self.assertEqual([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
        rsets = rs.split_rset(lambda s: s.count('d'), col=2)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   250
        self.assertEqual(len(rsets), 2)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   251
        self.assertEqual([title for _, _, title in rsets[0]],
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
                          [u"Adim chez les pinguins",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
                           u"Jardiner facile",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
                           u"L'épluchage du castor commun",])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   255
        self.assertEqual([title for _, _, title in rsets[1]],
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
                          [u"Le carrelage en 42 leçons",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
                           u"La tarte tatin en 15 minutes",])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
        # make sure rs is unchanged
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   259
        self.assertEqual([title for _, _, title in rs],
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
                          [u'Adim chez les pinguins',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
                           u'Jardiner facile',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
                           u'Le carrelage en 42 leçons',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
                           u'La tarte tatin en 15 minutes',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
                           u"L'épluchage du castor commun"])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   265
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
    def test_cached_syntax_tree(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
        """make sure syntax tree is cached"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
        rqlst1 = self.rset.syntax_tree()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
        rqlst2 = self.rset.syntax_tree()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
        self.assert_(rqlst1 is rqlst2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
    def test_get_entity_simple(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   273
        self.request().create_entity('CWUser', login=u'adim', upassword='adim',
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   274
                                     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
   275
        e = self.execute('Any X,T WHERE X login "adim", X surname T').get_entity(0, 0)
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   276
        self.assertEqual(e.cw_attr_cache['surname'], 'di mascio')
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   277
        self.assertRaises(KeyError, e.cw_attr_cache.__getitem__, 'firstname')
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   278
        self.assertRaises(KeyError, e.cw_attr_cache.__getitem__, 'creation_date')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   279
        self.assertEqual(pprelcachedict(e._cw_related_cache), [])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
        e.complete()
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   281
        self.assertEqual(e.cw_attr_cache['firstname'], 'adrien')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   282
        self.assertEqual(pprelcachedict(e._cw_related_cache), [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   283
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
    def test_get_entity_advanced(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   285
        self.request().create_entity('Bookmark', title=u'zou', path=u'/view')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
        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
   287
        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
   288
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
        e = rset.get_entity(0, 0)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   290
        self.assertEqual(e.cw_row, 0)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   291
        self.assertEqual(e.cw_col, 0)
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   292
        self.assertEqual(e.cw_attr_cache['title'], 'zou')
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   293
        self.assertRaises(KeyError, e.cw_attr_cache.__getitem__, 'path')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   294
        self.assertEqual(e.view('text'), 'zou')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   295
        self.assertEqual(pprelcachedict(e._cw_related_cache), [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   296
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
        e = rset.get_entity(0, 1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   298
        self.assertEqual(e.cw_row, 0)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   299
        self.assertEqual(e.cw_col, 1)
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   300
        self.assertEqual(e.cw_attr_cache['login'], 'anon')
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   301
        self.assertRaises(KeyError, e.cw_attr_cache.__getitem__, 'firstname')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   302
        self.assertEqual(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
                          [])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
        e.complete()
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   305
        self.assertEqual(e.cw_attr_cache['firstname'], None)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   306
        self.assertEqual(e.view('text'), 'anon')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   307
        self.assertEqual(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
                          [])
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   309
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   310
        self.assertRaises(NotAnEntity, rset.get_entity, 0, 2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
        self.assertRaises(NotAnEntity, rset.get_entity, 0, 3)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
    def test_get_entity_relation_cache_compt(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
        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
   315
        e = rset.get_entity(0, 0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
        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
   317
        # 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
   318
        # be filed from our query rset
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   319
        self.assertEqual(pprelcachedict(e._cw_related_cache),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
                          [('in_state_subject', [seid])])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
    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
   323
        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
   324
        self.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
        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
   326
                            '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
   327
        e = rset.get_entity(0, 0)
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   328
        self.assertEqual(e.cw_attr_cache['title'], 'zou')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   329
        self.assertEqual(pprelcachedict(e._cw_related_cache),
6415
b0b0f097a72d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6367
diff changeset
   330
                          [('created_by_subject', [self.user().eid])])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
        # first level of recursion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
        u = e.created_by[0]
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   333
        self.assertEqual(u.cw_attr_cache['login'], 'admin')
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   334
        self.assertRaises(KeyError, u.cw_attr_cache.__getitem__, 'firstname')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
        # second level of recursion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
        s = u.in_state[0]
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   337
        self.assertEqual(s.cw_attr_cache['name'], 'activated')
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   338
        self.assertRaises(KeyError, s.cw_attr_cache.__getitem__, 'description')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   340
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
    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
   342
        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
   343
                           'WHERE G name "users"')[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
        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
   345
        e = rset.get_entity(0, 0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
        # 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
   347
        # related entities should be an empty list
8735
5567a5117aeb [entity] ensure the .related(entities=False) parameter is honored all the way down (closes #2755994)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8595
diff changeset
   348
        self.assertEqual(e._cw_related_cache['primary_email_subject'][True], ())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
        # related rset should be an empty rset
8735
5567a5117aeb [entity] ensure the .related(entities=False) parameter is honored all the way down (closes #2755994)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8595
diff changeset
   350
        cached = e._cw_related_cache['primary_email_subject'][False]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
        self.assertIsInstance(cached, ResultSet)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   352
        self.assertEqual(cached.rowcount, 0)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   353
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   355
    def test_get_entity_union(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   356
        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
   357
        rset = self.execute('Any X,N ORDERBY N WITH X,N BEING '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
                            '((Any X,N WHERE X is Bookmark, X title N)'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
                            ' UNION '
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   360
                            ' (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
   361
        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
   362
                    ('Bookmark', 'manger'), ('CWGroup', 'owners'),
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 692
diff changeset
   363
                    ('CWGroup', 'users'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
        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
   365
            etype, n = expected[entity.cw_row]
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   366
            self.assertEqual(entity.__regid__, etype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
            attr = etype == 'Bookmark' and 'title' or 'name'
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
   368
            self.assertEqual(entity.cw_attr_cache[attr], n)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   369
572
9849fed789c9 test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   370
    def test_related_entity_optional(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   371
        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
   372
        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
   373
        entity, rtype = rset.related_entity(0, 2)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   374
        self.assertEqual(entity, None)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   375
        self.assertEqual(rtype, None)
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   376
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   377
    def test_related_entity_union_subquery_1(self):
4461
a35c76ffed92 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4427
diff changeset
   378
        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
   379
        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
   380
                            '((Any X,N WHERE X is CWGroup, X name N)'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
                            ' UNION '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
                            ' (Any X,N WHERE X is Bookmark, X title N))')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
        entity, rtype = rset.related_entity(0, 1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   384
        self.assertEqual(entity.eid, e.eid)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   385
        self.assertEqual(rtype, 'title')
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   386
        self.assertEqual(entity.title, 'aaaa')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
        entity, rtype = rset.related_entity(1, 1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   388
        self.assertEqual(entity.__regid__, 'CWGroup')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   389
        self.assertEqual(rtype, 'name')
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   390
        self.assertEqual(entity.name, 'guests')
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   391
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   392
    def test_related_entity_union_subquery_2(self):
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   393
        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
   394
        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
   395
                            '((Any X,N WHERE X is CWGroup, X name N)'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
                            ' UNION '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
                            ' (Any X,N WHERE X is Bookmark, X title N))')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
        entity, rtype = rset.related_entity(0, 1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   399
        self.assertEqual(entity.eid, e.eid)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   400
        self.assertEqual(rtype, 'title')
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   401
        self.assertEqual(entity.title, 'aaaa')
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   402
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   403
    def test_related_entity_union_subquery_3(self):
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   404
        e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path')
3016
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   405
        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
   406
                            '((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
   407
                            ' UNION '
5787d1cc8106 [rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2635
diff changeset
   408
                            ' (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
   409
        entity, rtype = rset.related_entity(0, 1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   410
        self.assertEqual(entity.eid, e.eid)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   411
        self.assertEqual(rtype, 'title')
6915
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   412
        self.assertEqual(entity.title, 'aaaa')
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   413
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   414
    def test_related_entity_union_subquery_4(self):
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   415
        e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path')
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   416
        rset = self.execute('Any X,X, N ORDERBY N WITH X,N BEING '
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   417
                            '((Any X,N WHERE X is CWGroup, X name N)'
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   418
                            ' UNION '
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   419
                            ' (Any X,N WHERE X is Bookmark, X title N))')
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   420
        entity, rtype = rset.related_entity(0, 2)
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   421
        self.assertEqual(entity.eid, e.eid)
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   422
        self.assertEqual(rtype, 'title')
99eb71b311e4 [rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6857
diff changeset
   423
        self.assertEqual(entity.title, 'aaaa')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   424
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
   425
    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
   426
        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
   427
        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
   428
        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
   429
        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
   430
                            '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
   431
        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
   432
6195
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   433
    def test_related_entity_subquery_outerjoin(self):
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   434
        rset = self.execute('Any X,S,L WHERE X in_state S '
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   435
                            'WITH X, L BEING (Any X,MAX(L) GROUPBY X '
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   436
                            'WHERE X is CWUser, T? wf_info_for X, T creation_date L)')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   437
        self.assertEqual(len(rset), 2)
6195
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   438
        rset.related_entity(0, 1)
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   439
        rset.related_entity(0, 2)
a0c4441e6ddf [rset] add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5752
diff changeset
   440
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
    def test_entities(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   442
        rset = self.execute('Any U,G WHERE U in_group G')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
        # make sure we have at least one element
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   444
        self.assertTrue(rset)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   445
        self.assertEqual(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
   446
                          set(['CWUser',]))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   447
        self.assertEqual(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
   448
                          set(['CWGroup',]))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
6857
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   450
    def test_iter_rows_with_entities(self):
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   451
        rset = self.execute('Any U,UN,G,GN WHERE U in_group G, U login UN, G name GN')
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   452
        # make sure we have at least one element
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7395
diff changeset
   453
        self.assertTrue(rset)
6857
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   454
        out = list(rset.iter_rows_with_entities())[0]
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   455
        self.assertEqual( out[0].login, out[1] )
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   456
        self.assertEqual( out[2].name, out[3] )
8d2062387134 [rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6415
diff changeset
   457
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   458
    def test_printable_rql(self):
2635
c94df21f7ab2 F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2286
diff changeset
   459
        rset = self.execute(u'CWEType X WHERE X final FALSE')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   460
        self.assertEqual(rset.printable_rql(),
2635
c94df21f7ab2 F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2286
diff changeset
   461
                          'Any X WHERE X final FALSE, X is CWEType')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
    def test_searched_text(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
        rset = self.execute(u'Any X WHERE X has_text "foobar"')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   465
        self.assertEqual(rset.searched_text(), 'foobar')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
        rset = self.execute(u'Any X WHERE X has_text %(text)s', {'text' : 'foo'})
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   467
        self.assertEqual(rset.searched_text(), 'foo')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   468
4939
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   469
    def test_union_limited_rql(self):
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   470
        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
   471
                            ' UNION '
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   472
                            '(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
   473
        rset.limit(2, 10, inplace=True)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   474
        self.assertEqual(rset.limited_rql(),
4939
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   475
                          '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
   476
                          'WITH A,B BEING ('
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   477
                          '(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
   478
                          'UNION '
349af486f5ed fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4536
diff changeset
   479
                          '(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
   480
                          ')')
1640
65b60f177eb1 fix tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   481
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
   482
    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
   483
        rset = self.execute('Any D, COUNT(U) GROUPBY D WHERE U is CWUser, U creation_date D')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   484
        self.assertEqual(rset.related_entity(0,0), (None, None))
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
   485
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
   486
    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
   487
        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
   488
        self.assertIsInstance(str(rset), basestring)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   489
        self.assertEqual(len(str(rset).splitlines()), 1)
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
   490
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
   491
    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
   492
        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
   493
        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
   494
        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
   495
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
   496
        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
   497
        self.assertIsInstance(str(rset), basestring)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6195
diff changeset
   498
        self.assertEqual(len(str(rset).splitlines()), 1)
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
   499
8085
51929d531aff [rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8048
diff changeset
   500
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   501
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
    unittest_main()