1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
19 from cubicweb.devtools.testlib import CubicWebTC |
|
20 from cubicweb.web import facet |
|
21 |
|
22 |
|
23 class InsertAttrRelationTC(CubicWebTC): |
|
24 |
|
25 def parse(self, query): |
|
26 rqlst = self.vreg.parse(self.session, query) |
|
27 select = rqlst.children[0] |
|
28 return rqlst |
|
29 |
|
30 def _generate(self, rqlst, rel, role, attr): |
|
31 select = rqlst.children[0] |
|
32 filtered_variable = facet.get_filtered_variable(select) |
|
33 facet.prepare_select(select, filtered_variable) |
|
34 facet.insert_attr_select_relation(select, filtered_variable, |
|
35 rel, role, attr) |
|
36 return rqlst.as_string() |
|
37 |
|
38 @property |
|
39 def select(self): |
|
40 return self.parse(u'Any B,(NOW - CD),S,V,U,GROUP_CONCAT(TN),VN,P,CD,BMD ' |
|
41 'GROUPBY B,CD,S,V,U,VN,P,BMD ' |
|
42 'WHERE B in_state S, B creation_date CD, ' |
|
43 'B modification_date BMD, T? tags B, T name TN, ' |
|
44 'V? bookmarked_by B, V title VN, B created_by U?, ' |
|
45 'B in_group P, P name "managers"') |
|
46 |
|
47 def test_1(self): |
|
48 self.assertEqual(self._generate(self.select, 'in_state', 'subject', 'name'), |
|
49 'DISTINCT Any A,C ORDERBY C WHERE B in_group P, P name "managers", ' |
|
50 'B in_state A, B is CWUser, A name C') |
|
51 |
|
52 def test_2(self): |
|
53 self.assertEqual(self._generate(self.select, 'tags', 'object', 'name'), |
|
54 'DISTINCT Any A,C ORDERBY C WHERE B in_group P, P name "managers", ' |
|
55 'A tags B, B is CWUser, A name C') |
|
56 |
|
57 def test_3(self): |
|
58 self.assertEqual(self._generate(self.select, 'created_by', 'subject', 'login'), |
|
59 'DISTINCT Any A,C ORDERBY C WHERE B in_group P, P name "managers", ' |
|
60 'B created_by A, B is CWUser, A login C') |
|
61 |
|
62 def test_4(self): |
|
63 self.assertEqual(self._generate(self.parse(u'Any X WHERE X is CWUser'), 'created_by', 'subject', 'login'), |
|
64 "DISTINCT Any A,B ORDERBY B WHERE X is CWUser, X created_by A, A login B") |
|
65 |
|
66 def test_5(self): |
|
67 self.assertEqual(self._generate(self.parse(u'Any X,L WHERE X is CWUser, X login L'), 'created_by', 'subject', 'login'), |
|
68 "DISTINCT Any A,B ORDERBY B WHERE X is CWUser, X created_by A, A login B") |
|
69 |
|
70 def test_nonregr1(self): |
|
71 select = self.parse(u'Any T,V WHERE T bookmarked_by V?, ' |
|
72 'V in_state VS, VS name "published", T created_by U') |
|
73 self.assertEqual(self._generate(select, 'created_by', 'subject', 'login'), |
|
74 "DISTINCT Any A,B ORDERBY B WHERE T created_by U, " |
|
75 "T created_by A, T is Bookmark, A login B") |
|
76 |
|
77 def test_nonregr2(self): |
|
78 #'DISTINCT Any X,TMP,N WHERE P name TMP, X version_of P, P is Project, X is Version, not X in_state S,S name "published", X num N ORDERBY TMP,N' |
|
79 select = self.parse(u'DISTINCT Any V,TN,L ORDERBY TN,L WHERE T nom TN, V connait T, T is Personne, V is CWUser,' |
|
80 'NOT V in_state VS, VS name "published", V login L') |
|
81 rschema = self.schema['connait'] |
|
82 for rdefs in rschema.rdefs.values(): |
|
83 rdefs.cardinality = '++' |
|
84 try: |
|
85 self.assertEqual(self._generate(select, 'in_state', 'subject', 'name'), |
|
86 'DISTINCT Any A,B ORDERBY B WHERE V is CWUser, ' |
|
87 'NOT EXISTS(V in_state VS), VS name "published", ' |
|
88 'V in_state A, A name B') |
|
89 finally: |
|
90 for rdefs in rschema.rdefs.values(): |
|
91 rdefs.cardinality = '**' |
|
92 |
|
93 def test_nonregr3(self): |
|
94 #'DISTINCT Any X,TMP,N WHERE P name TMP, X version_of P, P is Project, X is Version, not X in_state S,S name "published", X num N ORDERBY TMP,N' |
|
95 select = self.parse(u'DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is CWUser, Y is Bookmark, X in_group A') |
|
96 self.assertEqual(self._generate(select, 'in_group', 'subject', 'name'), |
|
97 "DISTINCT Any B,C ORDERBY C WHERE X is CWUser, X in_group B, B name C") |
|
98 |
|
99 |
|
100 if __name__ == '__main__': |
|
101 from logilab.common.testlib import unittest_main |
|
102 unittest_main() |
|