web/test/unittest_facet.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 07 Jul 2011 11:33:39 +0200
changeset 7624 ce020f90fb8e
parent 7618 5395007c415c
child 7629 58f11f7dad68
permissions -rw-r--r--
[facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7615
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
     1
from logilab.common.date import datetime2ticks
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
from cubicweb.devtools.testlib import CubicWebTC
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
from cubicweb.web import facet
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
class BaseFacetTC(CubicWebTC):
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
7624
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
     7
    def prepare_rqlst(self, rql='CWUser X', baserql='Any X WHERE X is CWUser',
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
     8
                      preparedrql='DISTINCT Any  WHERE X is CWUser',
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
     9
                      mainvar='X'):
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
        req = self.request()
7624
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
    11
        rset = self.execute(rql)
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
        rqlst = rset.syntax_tree().copy()
7624
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
    13
        filtered_variable, baserql = facet.init_facets(rset, rqlst.children[0],
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
    14
                                                       mainvar=mainvar)
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
    15
        self.assertEqual(filtered_variable.name, mainvar)
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
    16
        self.assertEqual(baserql, baserql)
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
    17
        self.assertEqual(rqlst.as_string(), preparedrql)
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    18
        return req, rset, rqlst, filtered_variable
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    20
    def _in_group_facet(self, cls=facet.RelationFacet, no_relation=False):
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    21
        req, rset, rqlst, filtered_variable = self.prepare_rqlst()
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    22
        cls.no_relation = no_relation
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    23
        f = cls(req, rset=rset, select=rqlst.children[0],
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    24
                filtered_variable=filtered_variable)
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    25
        f.__regid__ = 'in_group'
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
        f.rtype = 'in_group'
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
        f.role = 'subject'
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
        f.target_attr = 'name'
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
        guests, managers = [eid for eid, in self.execute('CWGroup G ORDERBY GN '
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
                                                         'WHERE G name GN, G name IN ("guests", "managers")')]
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    31
        groups = [eid for eid, in self.execute('CWGroup G ORDERBY GN '
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    32
                                               'WHERE G name GN, G name IN ("guests", "managers")')]
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    33
        return f, groups
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    34
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    35
    def test_relation_simple(self):
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    36
        f, (guests, managers) = self._in_group_facet()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
    37
        self.assertEqual(f.vocabulary(),
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    38
                      [(u'guests', guests), (u'managers', managers)])
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        # ensure rqlst is left unmodified
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    40
        self.assertEqual(f.select.as_string(), 'DISTINCT Any  WHERE X is CWUser')
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
        #rqlst = rset.syntax_tree()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
    42
        self.assertEqual(f.possible_values(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
                          [str(guests), str(managers)])
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
        # ensure rqlst is left unmodified
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    45
        self.assertEqual(f.select.as_string(), 'DISTINCT Any  WHERE X is CWUser')
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    46
        f._cw.form[f.__regid__] = str(guests)
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
        f.add_rql_restrictions()
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
        # selection is cluttered because rqlst has been prepared for facet (it
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
        # is not in real life)
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    50
        self.assertEqual(f.select.as_string(),
7618
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
    51
                         'DISTINCT Any  WHERE X is CWUser, X in_group D, D eid %s' % guests)
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    53
    def test_relation_optional_rel(self):
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    54
        req = self.request()
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    55
        rset = self.execute('Any X,GROUP_CONCAT(GN) GROUPBY X '
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    56
                            'WHERE X in_group G?, G name GN, NOT G name "users"')
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    57
        rqlst = rset.syntax_tree().copy()
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    58
        select = rqlst.children[0]
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    59
        filtered_variable, baserql = facet.init_facets(rset, select)
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    60
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    61
        f = facet.RelationFacet(req, rset=rset,
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    62
                                select=select,
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    63
                                filtered_variable=filtered_variable)
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    64
        f.rtype = 'in_group'
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    65
        f.role = 'subject'
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    66
        f.target_attr = 'name'
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    67
        guests, managers = [eid for eid, in self.execute('CWGroup G ORDERBY GN '
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    68
                                                         'WHERE G name GN, G name IN ("guests", "managers")')]
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
    69
        self.assertEqual(f.vocabulary(),
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    70
                          [(u'guests', guests), (u'managers', managers)])
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    71
        # ensure rqlst is left unmodified
7601
9030e8a4481e [facets] should remove groupby from base rqlst. Closes #1796196
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6435
diff changeset
    72
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X in_group G?, G name GN, NOT G name "users"')
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    73
        #rqlst = rset.syntax_tree()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
    74
        self.assertEqual(sorted(f.possible_values()),
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    75
                          [str(guests), str(managers)])
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    76
        # ensure rqlst is left unmodified
7601
9030e8a4481e [facets] should remove groupby from base rqlst. Closes #1796196
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6435
diff changeset
    77
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X in_group G?, G name GN, NOT G name "users"')
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    78
        req.form[f.__regid__] = str(guests)
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    79
        f.add_rql_restrictions()
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    80
        # selection is cluttered because rqlst has been prepared for facet (it
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    81
        # is not in real life)
7601
9030e8a4481e [facets] should remove groupby from base rqlst. Closes #1796196
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6435
diff changeset
    82
        self.assertEqual(f.select.as_string(),
9030e8a4481e [facets] should remove groupby from base rqlst. Closes #1796196
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6435
diff changeset
    83
                          'DISTINCT Any  WHERE X in_group G?, G name GN, NOT G name "users", X in_group D, D eid %s' % guests)
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
    84
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    85
    def test_relation_no_relation_1(self):
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    86
        f, (guests, managers) = self._in_group_facet(no_relation=True)
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    87
        self.assertEqual(f.vocabulary(),
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    88
                          [(u'guests', guests), (u'managers', managers)])
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    89
        self.assertEqual(f.possible_values(),
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    90
                          [str(guests), str(managers)])
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    91
        f._cw.create_entity('CWUser', login=u'hop', upassword='toto')
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    92
        self.assertEqual(f.vocabulary(),
6417
d2361abe8505 [test] fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6380
diff changeset
    93
                          [(u'<no relation>', ''), (u'guests', guests), (u'managers', managers)])
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    94
        self.assertEqual(f.possible_values(),
6417
d2361abe8505 [test] fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6380
diff changeset
    95
                          [str(guests), str(managers), ''])
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    96
        f._cw.form[f.__regid__] = ''
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    97
        f.add_rql_restrictions()
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
    98
        self.assertEqual(f.select.as_string(),
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    99
                          'DISTINCT Any  WHERE X is CWUser, NOT X in_group G')
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   100
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   101
    def test_relation_no_relation_2(self):
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   102
        f, (guests, managers) = self._in_group_facet(no_relation=True)
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   103
        f._cw.form[f.__regid__] = ['', guests]
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   104
        f.select.save_state()
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   105
        f.add_rql_restrictions()
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   106
        self.assertEqual(f.select.as_string(),
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   107
                          'DISTINCT Any  WHERE X is CWUser, (NOT X in_group B) OR (X in_group A, A eid %s)' % guests)
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   108
        f.select.recover()
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   109
        self.assertEqual(f.select.as_string(),
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   110
                          'DISTINCT Any  WHERE X is CWUser')
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   111
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   112
6338
b520b86113e3 add test case demonstrating #1251910
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6119
diff changeset
   113
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   114
    def test_relationattribute(self):
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   115
        f, (guests, managers) = self._in_group_facet(cls=facet.RelationAttributeFacet)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
   116
        self.assertEqual(f.vocabulary(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
                          [(u'guests', u'guests'), (u'managers', u'managers')])
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
        # ensure rqlst is left unmodified
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   119
        self.assertEqual(f.select.as_string(), 'DISTINCT Any  WHERE X is CWUser')
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
        #rqlst = rset.syntax_tree()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
   121
        self.assertEqual(f.possible_values(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   122
                          ['guests', 'managers'])
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   123
        # ensure rqlst is left unmodified
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   124
        self.assertEqual(f.select.as_string(), 'DISTINCT Any  WHERE X is CWUser')
6380
63d5dbaef999 [facets] support for `no_relation` on RelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   125
        f._cw.form[f.__regid__] = 'guests'
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   126
        f.add_rql_restrictions()
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
        # selection is cluttered because rqlst has been prepared for facet (it
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
        # is not in real life)
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   129
        self.assertEqual(f.select.as_string(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
                          "DISTINCT Any  WHERE X is CWUser, X in_group E, E name 'guests'")
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
7615
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   132
    def test_daterange(self):
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   133
        req, rset, rqlst, filtered_variable = self.prepare_rqlst()
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   134
        f = facet.DateRangeFacet(req, rset=rset,
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   135
                                 select=rqlst.children[0],
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   136
                                 filtered_variable=filtered_variable)
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   137
        f.rtype = 'creation_date'
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   138
        dates = self.execute('Any CD ORDERBY CD WHERE X is CWUser, X creation_date CD')
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   139
        self.assertEqual(f.vocabulary(),
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   140
                          [(str(dates[0][0]), dates[0][0]),
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   141
                           (str(dates[1][0]), dates[1][0])])
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   142
        # ensure rqlst is left unmodified
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   143
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   144
        #rqlst = rset.syntax_tree()
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   145
        self.assertEqual(f.possible_values(),
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   146
                          [str(dates[0][0]), str(dates[1][0])])
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   147
        # ensure rqlst is left unmodified
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   148
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   149
        req.form['%s_inf' % f.__regid__] = str(datetime2ticks(dates[0][0]))
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   150
        req.form['%s_sup' % f.__regid__] = str(datetime2ticks(dates[0][0]))
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   151
        f.add_rql_restrictions()
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   152
        # selection is cluttered because rqlst has been prepared for facet (it
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   153
        # is not in real life)
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   154
        self.assertEqual(f.select.as_string(),
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   155
                          'DISTINCT Any  WHERE X is CWUser, X creation_date >= "%s", '
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   156
                         'X creation_date <= "%s"'
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   157
                         % (dates[0][0].strftime('%Y/%m/%d'),
e5ad51352eb3 [facet] fix argument inversion in date range facet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7612
diff changeset
   158
                            dates[0][0].strftime('%Y/%m/%d')))
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
    def test_attribute(self):
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   161
        req, rset, rqlst, filtered_variable = self.prepare_rqlst()
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
        f = facet.AttributeFacet(req, rset=rset,
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   163
                                 select=rqlst.children[0],
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   164
                                 filtered_variable=filtered_variable)
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
        f.rtype = 'login'
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
   166
        self.assertEqual(f.vocabulary(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
                          [(u'admin', u'admin'), (u'anon', u'anon')])
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
        # ensure rqlst is left unmodified
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
   169
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
        #rqlst = rset.syntax_tree()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
   171
        self.assertEqual(f.possible_values(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   172
                          ['admin', 'anon'])
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
        # ensure rqlst is left unmodified
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6338
diff changeset
   174
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   175
        req.form[f.__regid__] = 'admin'
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   176
        f.add_rql_restrictions()
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   177
        # selection is cluttered because rqlst has been prepared for facet (it
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
        # is not in real life)
7605
48abeac162fd [facets] refactor / cleanup facet api: more consistent variable naming and easier to reuse function. Closes #1796804
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7601
diff changeset
   179
        self.assertEqual(f.select.as_string(),
6119
b217635d3b28 new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   180
                          "DISTINCT Any  WHERE X is CWUser, X login 'admin'")
6433
95c40961df21 [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   181
7618
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   182
    def test_rql_path_eid(self):
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   183
        req, rset, rqlst, filtered_variable = self.prepare_rqlst()
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   184
        facet.RQLPathFacet.path = [('X created_by U'), ('U owned_by O'), ('O login OL')]
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   185
        f = facet.RQLPathFacet(req, rset=rset,
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   186
                               select=rqlst.children[0],
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   187
                               filtered_variable=filtered_variable)
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   188
        f.filter_variable = 'O'
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   189
        f.label_variable = 'OL'
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   190
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   191
        self.assertEqual(f.vocabulary(), [(u'admin', self.user().eid),])
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   192
        # ensure rqlst is left unmodified
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   193
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   194
        #rqlst = rset.syntax_tree()
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   195
        self.assertEqual(f.possible_values(),
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   196
                          [str(self.user().eid),])
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   197
        # ensure rqlst is left unmodified
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   198
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   199
        req.form[f.__regid__] = '1'
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   200
        f.add_rql_restrictions()
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   201
        # selection is cluttered because rqlst has been prepared for facet (it
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   202
        # is not in real life)
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   203
        self.assertEqual(f.select.as_string(),
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   204
                         "DISTINCT Any  WHERE X is CWUser, X created_by F, F owned_by G, G eid 1")
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   205
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   206
    def test_rql_path_attr(self):
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   207
        req, rset, rqlst, filtered_variable = self.prepare_rqlst()
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   208
        facet.RQLPathFacet.path = [('X created_by U'), ('U owned_by O'), ('O login OL')]
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   209
        f = facet.RQLPathFacet(req, rset=rset,
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   210
                               select=rqlst.children[0],
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   211
                               filtered_variable=filtered_variable)
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   212
        f.filter_variable = 'OL'
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   213
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   214
        self.assertEqual(f.vocabulary(), [(u'admin', 'admin'),])
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   215
        # ensure rqlst is left unmodified
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   216
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   217
        #rqlst = rset.syntax_tree()
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   218
        self.assertEqual(f.possible_values(), ['admin',])
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   219
        # ensure rqlst is left unmodified
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   220
        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   221
        req.form[f.__regid__] = 'admin'
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   222
        f.add_rql_restrictions()
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   223
        # selection is cluttered because rqlst has been prepared for facet (it
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   224
        # is not in real life)
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   225
        self.assertEqual(f.select.as_string(),
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   226
                         "DISTINCT Any  WHERE X is CWUser, X created_by G, G owned_by H, H login 'admin'")
5395007c415c [facet] closes #1806931: new facet type, based on arbitrary rql path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7615
diff changeset
   227
7624
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   228
    def prepareg_aggregat_rqlst(self):
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   229
        return self.prepare_rqlst(
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   230
            'Any 1, COUNT(X) WHERE X is CWUser, X creation_date XD, '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   231
            'X modification_date XM, Y creation_date YD, Y is CWGroup '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   232
            'HAVING DAY(XD)>=DAY(YD) AND DAY(XM)<=DAY(YD)', mainvar='X',
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   233
            baserql='DISTINCT Any  WHERE X is CWUser, X creation_date XD, '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   234
            'X modification_date XM, Y creation_date YD, Y is CWGroup '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   235
            'HAVING DAY(XD) >= DAY(YD), DAY(XM) <= DAY(YD)',
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   236
            preparedrql='DISTINCT Any  WHERE X is CWUser, X creation_date XD, '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   237
            'X modification_date XM, Y creation_date YD, Y is CWGroup '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   238
            'HAVING DAY(XD) >= DAY(YD), DAY(XM) <= DAY(YD)')
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   239
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   240
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   241
    def test_aggregat_query_cleanup_select(self):
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   242
        req, rset, rqlst, filtered_variable = self.prepareg_aggregat_rqlst()
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   243
        select = rqlst.children[0]
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   244
        facet.cleanup_select(select, filtered_variable=filtered_variable)
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   245
        self.assertEqual(select.as_string(),
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   246
                         'DISTINCT Any  WHERE X is CWUser, X creation_date XD, '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   247
                         'X modification_date XM, Y creation_date YD, Y is CWGroup '
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   248
                         'HAVING DAY(XD) >= DAY(YD), DAY(XM) <= DAY(YD)')
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   249
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   250
    def test_aggregat_query_rql_path(self):
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   251
        req, rset, rqlst, filtered_variable = self.prepareg_aggregat_rqlst()
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   252
        facet.RQLPathFacet.path = [('X created_by U'), ('U owned_by O'), ('O login OL')]
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   253
        f = facet.RQLPathFacet(req, rset=rset,
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   254
                               select=rqlst.children[0],
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   255
                               filtered_variable=filtered_variable)
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   256
        f.filter_variable = 'OL'
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   257
        self.assertEqual(f.vocabulary(), [(u'admin', u'admin')])
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   258
        self.assertEqual(f.possible_values(), ['admin'])
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   259
        req.form[f.__regid__] = 'admin'
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   260
        f.add_rql_restrictions()
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   261
        self.assertEqual(f.select.as_string(),
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   262
                         "DISTINCT Any  WHERE X is CWUser, X creation_date XD, "
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   263
                         "X modification_date XM, Y creation_date YD, Y is CWGroup, "
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   264
                         "X created_by G, G owned_by H, H login 'admin' "
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   265
                         "HAVING DAY(XD) >= DAY(YD), DAY(XM) <= DAY(YD)")
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   266
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   267
    def test_aggregat_query_attribute(self):
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   268
        req, rset, rqlst, filtered_variable = self.prepareg_aggregat_rqlst()
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   269
        f = facet.AttributeFacet(req, rset=rset,
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   270
                                 select=rqlst.children[0],
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   271
                                 filtered_variable=filtered_variable)
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   272
        f.rtype = 'login'
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   273
        self.assertEqual(f.vocabulary(),
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   274
                          [(u'admin', u'admin'), (u'anon', u'anon')])
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   275
        self.assertEqual(f.possible_values(),
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   276
                          ['admin', 'anon'])
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   277
        req.form[f.__regid__] = 'admin'
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   278
        f.add_rql_restrictions()
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   279
        self.assertEqual(f.select.as_string(),
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   280
                          "DISTINCT Any  WHERE X is CWUser, X creation_date XD, "
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   281
                          "X modification_date XM, Y creation_date YD, Y is CWGroup, X login 'admin' "
ce020f90fb8e [facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7618
diff changeset
   282
                          "HAVING DAY(XD) >= DAY(YD), DAY(XM) <= DAY(YD)")
6433
95c40961df21 [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   283
95c40961df21 [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   284
if __name__ == '__main__':
95c40961df21 [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   285
    from logilab.common.testlib import unittest_main
95c40961df21 [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   286
    unittest_main()