|
1 # copyright 2003-2014 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 logilab.common.testlib import unittest_main |
|
20 from logilab.mtconverter import html_unescape |
|
21 |
|
22 from cubicweb.devtools.testlib import CubicWebTC |
|
23 from cubicweb.utils import json |
|
24 from cubicweb.view import StartupView, TRANSITIONAL_DOCTYPE |
|
25 from cubicweb.web.views import vid_from_rset |
|
26 |
|
27 def loadjson(value): |
|
28 return json.loads(html_unescape(value)) |
|
29 |
|
30 class VidFromRsetTC(CubicWebTC): |
|
31 |
|
32 def test_no_rset(self): |
|
33 with self.admin_access.web_request() as req: |
|
34 self.assertEqual(vid_from_rset(req, None, self.schema), 'index') |
|
35 |
|
36 def test_no_entity(self): |
|
37 with self.admin_access.web_request() as req: |
|
38 rset = req.execute('Any X WHERE X login "blabla"') |
|
39 self.assertEqual(vid_from_rset(req, rset, self.schema), 'noresult') |
|
40 |
|
41 def test_one_entity(self): |
|
42 with self.admin_access.web_request() as req: |
|
43 rset = req.execute('Any X WHERE X login "admin"') |
|
44 self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary') |
|
45 rset = req.execute('Any X, L WHERE X login "admin", X login L') |
|
46 self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary') |
|
47 req.search_state = ('pasnormal',) |
|
48 rset = req.execute('Any X WHERE X login "admin"') |
|
49 self.assertEqual(vid_from_rset(req, rset, self.schema), 'outofcontext-search') |
|
50 |
|
51 def test_one_entity_eid(self): |
|
52 with self.admin_access.web_request() as req: |
|
53 rset = req.execute('Any X WHERE X eid 1') |
|
54 self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary') |
|
55 |
|
56 def test_more_than_one_entity_same_type(self): |
|
57 with self.admin_access.web_request() as req: |
|
58 rset = req.execute('Any X WHERE X is CWUser') |
|
59 self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist') |
|
60 rset = req.execute('Any X, L WHERE X login L') |
|
61 self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist') |
|
62 |
|
63 def test_more_than_one_entity_diff_type(self): |
|
64 with self.admin_access.web_request() as req: |
|
65 rset = req.execute('Any X WHERE X is IN (CWUser, CWGroup)') |
|
66 self.assertEqual(vid_from_rset(req, rset, self.schema), 'list') |
|
67 |
|
68 def test_more_than_one_entity_by_row(self): |
|
69 with self.admin_access.web_request() as req: |
|
70 rset = req.execute('Any X, G WHERE X in_group G') |
|
71 self.assertEqual(vid_from_rset(req, rset, self.schema), 'table') |
|
72 |
|
73 def test_more_than_one_entity_by_row_2(self): |
|
74 with self.admin_access.web_request() as req: |
|
75 rset = req.execute('Any X, GN WHERE X in_group G, G name GN') |
|
76 self.assertEqual(vid_from_rset(req, rset, self.schema), 'table') |
|
77 |
|
78 def test_aggregat(self): |
|
79 with self.admin_access.web_request() as req: |
|
80 rset = req.execute('Any X, COUNT(T) GROUPBY X WHERE X is T') |
|
81 self.assertEqual(vid_from_rset(req, rset, self.schema), 'table') |
|
82 rset = req.execute('Any MAX(X) WHERE X is CWUser') |
|
83 self.assertEqual(vid_from_rset(req, rset, self.schema), 'table') |
|
84 |
|
85 def test_subquery(self): |
|
86 with self.admin_access.web_request() as req: |
|
87 rset = req.execute( |
|
88 'DISTINCT Any X,N ORDERBY N ' |
|
89 'WITH X,N BEING (' |
|
90 ' (DISTINCT Any P,N WHERE P is CWUser, P login N)' |
|
91 ' UNION' |
|
92 ' (DISTINCT Any W,N WHERE W is CWGroup, W name N))') |
|
93 self.assertEqual(vid_from_rset(req, rset, self.schema), 'table') |
|
94 |
|
95 |
|
96 class TableViewTC(CubicWebTC): |
|
97 |
|
98 def _prepare_entity(self, req): |
|
99 e = req.create_entity("State", name=u'<toto>', description=u'loo"ong blabla') |
|
100 rset = req.execute('Any X, D, CD, NOW - CD WHERE X is State, ' |
|
101 'X description D, X creation_date CD, X eid %(x)s', |
|
102 {'x': e.eid}) |
|
103 view = self.vreg['views'].select('table', req, rset=rset) |
|
104 return e, rset, view |
|
105 |
|
106 def test_sortvalue(self): |
|
107 with self.admin_access.web_request() as req: |
|
108 e, _, view = self._prepare_entity(req) |
|
109 colrenderers = view.build_column_renderers()[:3] |
|
110 self.assertListEqual([renderer.sortvalue(0) for renderer in colrenderers], |
|
111 [u'<toto>', u'loo"ong blabla', e.creation_date]) |
|
112 |
|
113 |
|
114 class HTMLStreamTests(CubicWebTC): |
|
115 |
|
116 def test_set_doctype_reset_xmldecl(self): |
|
117 """ |
|
118 tests `cubicweb.web.request.CubicWebRequestBase.set_doctype` |
|
119 with xmldecl reset |
|
120 """ |
|
121 class MyView(StartupView): |
|
122 __regid__ = 'my-view' |
|
123 def call(self): |
|
124 self._cw.set_doctype('<!DOCTYPE html>') |
|
125 |
|
126 with self.admin_access.web_request() as req: |
|
127 with self.temporary_appobjects(MyView): |
|
128 html_source = self.view('my-view', req=req).source |
|
129 source_lines = [line.strip() |
|
130 for line in html_source.splitlines(False) |
|
131 if line.strip()] |
|
132 self.assertListEqual([b'<!DOCTYPE html>', |
|
133 b'<html xmlns:cubicweb="http://www.cubicweb.org" lang="en">'], |
|
134 source_lines[:2]) |
|
135 |
|
136 def test_set_doctype_no_reset_xmldecl(self): |
|
137 """ |
|
138 tests `cubicweb.web.request.CubicWebRequestBase.set_doctype` |
|
139 with no xmldecl reset |
|
140 """ |
|
141 html_doctype = TRANSITIONAL_DOCTYPE.strip() |
|
142 class MyView(StartupView): |
|
143 __regid__ = 'my-view' |
|
144 def call(self): |
|
145 self._cw.set_doctype(html_doctype) |
|
146 self._cw.main_stream.set_htmlattrs([('lang', 'cz')]) |
|
147 |
|
148 with self.admin_access.web_request() as req: |
|
149 with self.temporary_appobjects(MyView): |
|
150 html_source = self.view('my-view', req=req).source |
|
151 source_lines = [line.strip() |
|
152 for line in html_source.splitlines(False) |
|
153 if line.strip()] |
|
154 self.assertListEqual([html_doctype.encode('ascii'), |
|
155 b'<html xmlns:cubicweb="http://www.cubicweb.org" lang="cz">', |
|
156 b'<head>'], |
|
157 source_lines[:3]) |
|
158 |
|
159 if __name__ == '__main__': |
|
160 unittest_main() |