web/test/unittest_views_baseviews.py
changeset 9838 14640aeb5ddc
parent 9523 cd5738fc440f
child 10726 bb91f581c389
--- a/web/test/unittest_views_baseviews.py	Wed Jul 02 15:11:00 2014 +0200
+++ b/web/test/unittest_views_baseviews.py	Tue Jun 03 11:00:50 2014 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -22,7 +22,6 @@
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.utils import json
 from cubicweb.view import StartupView, TRANSITIONAL_DOCTYPE
-from cubicweb.web.htmlwidgets import TableWidget
 from cubicweb.web.views import vid_from_rset
 
 def loadjson(value):
@@ -31,90 +30,85 @@
 class VidFromRsetTC(CubicWebTC):
 
     def test_no_rset(self):
-        req = self.request()
-        self.assertEqual(vid_from_rset(req, None, self.schema), 'index')
+        with self.admin_access.web_request() as req:
+            self.assertEqual(vid_from_rset(req, None, self.schema), 'index')
 
     def test_no_entity(self):
-        req = self.request()
-        rset = self.execute('Any X WHERE X login "blabla"')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'noresult')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X WHERE X login "blabla"')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'noresult')
 
     def test_one_entity(self):
-        req = self.request()
-        rset = self.execute('Any X WHERE X login "admin"')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
-        rset = self.execute('Any X, L WHERE X login "admin", X login L')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
-        req.search_state = ('pasnormal',)
-        rset = self.execute('Any X WHERE X login "admin"')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'outofcontext-search')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X WHERE X login "admin"')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
+            rset = req.execute('Any X, L WHERE X login "admin", X login L')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
+            req.search_state = ('pasnormal',)
+            rset = req.execute('Any X WHERE X login "admin"')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'outofcontext-search')
 
     def test_one_entity_eid(self):
-        req = self.request()
-        rset = self.execute('Any X WHERE X eid 1')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X WHERE X eid 1')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
 
     def test_more_than_one_entity_same_type(self):
-        req = self.request()
-        rset = self.execute('Any X WHERE X is CWUser')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist')
-        rset = self.execute('Any X, L WHERE X login L')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X WHERE X is CWUser')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist')
+            rset = req.execute('Any X, L WHERE X login L')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist')
 
     def test_more_than_one_entity_diff_type(self):
-        req = self.request()
-        rset = self.execute('Any X WHERE X is IN (CWUser, CWGroup)')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'list')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X WHERE X is IN (CWUser, CWGroup)')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'list')
 
     def test_more_than_one_entity_by_row(self):
-        req = self.request()
-        rset = self.execute('Any X, G WHERE X in_group G')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X, G WHERE X in_group G')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
 
     def test_more_than_one_entity_by_row_2(self):
-        req = self.request()
-        rset = self.execute('Any X, GN WHERE X in_group G, G name GN')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X, GN WHERE X in_group G, G name GN')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
 
     def test_aggregat(self):
-        req = self.request()
-        rset = self.execute('Any X, COUNT(T) GROUPBY X WHERE X is T')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
-        rset = self.execute('Any MAX(X) WHERE X is CWUser')
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
+        with self.admin_access.web_request() as req:
+            rset = req.execute('Any X, COUNT(T) GROUPBY X WHERE X is T')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
+            rset = req.execute('Any MAX(X) WHERE X is CWUser')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
 
     def test_subquery(self):
-        rset = self.execute(
+        with self.admin_access.web_request() as req:
+            rset = req.execute(
 'DISTINCT Any X,N ORDERBY N '
 'WITH X,N BEING ('
 '     (DISTINCT Any P,N WHERE P is CWUser, P login N)'
 '       UNION'
 '     (DISTINCT Any W,N WHERE W is CWGroup, W name N))')
-        req = self.request()
-        self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
+            self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
 
 
 class TableViewTC(CubicWebTC):
 
-    def _prepare_entity(self):
-        req = self.request()
+    def _prepare_entity(self, req):
         e = req.create_entity("State", name=u'<toto>', description=u'loo"ong blabla')
-        rset = req.execute('Any X, D, CD, NOW - CD WHERE X is State, X description D, X creation_date CD, X eid %(x)s',
+        rset = req.execute('Any X, D, CD, NOW - CD WHERE X is State, '
+                           'X description D, X creation_date CD, X eid %(x)s',
                            {'x': e.eid})
         view = self.vreg['views'].select('table', req, rset=rset)
         return e, rset, view
 
-    def test_headers(self):
-        self.skipTest('implement me')
-
     def test_sortvalue(self):
-        e, _, view = self._prepare_entity()
-        colrenderers = view.build_column_renderers()[:3]
-        self.assertListEqual([renderer.sortvalue(0) for renderer in colrenderers],
-                             [u'<toto>', u'loo"ong blabla', e.creation_date])
-        # XXX sqlite does not handle Interval correctly
-        # value = loadjson(view.sortvalue(0, 3))
-        # self.assertAlmostEquals(value, rset.rows[0][3].seconds)
+        with self.admin_access.web_request() as req:
+            e, _, view = self._prepare_entity(req)
+            colrenderers = view.build_column_renderers()[:3]
+            self.assertListEqual([renderer.sortvalue(0) for renderer in colrenderers],
+                                 [u'<toto>', u'loo"ong blabla', e.creation_date])
 
 
 class HTMLStreamTests(CubicWebTC):
@@ -129,11 +123,15 @@
             def call(self):
                 self._cw.set_doctype('<!DOCTYPE html>')
 
-        with self.temporary_appobjects(MyView):
-            html_source = self.view('my-view').source
-            source_lines = [line.strip() for line in html_source.splitlines(False)
-                            if line.strip()]
-            self.assertListEqual(['<!DOCTYPE html>', '<html xmlns:cubicweb="http://www.cubicweb.org" lang="en">'], source_lines[:2])
+        with self.admin_access.web_request() as req:
+            with self.temporary_appobjects(MyView):
+                html_source = self.view('my-view', req=req).source
+                source_lines = [line.strip()
+                                for line in html_source.splitlines(False)
+                                if line.strip()]
+                self.assertListEqual(['<!DOCTYPE html>',
+                                      '<html xmlns:cubicweb="http://www.cubicweb.org" lang="en">'],
+                                     source_lines[:2])
 
     def test_set_doctype_no_reset_xmldecl(self):
         """
@@ -147,12 +145,16 @@
                 self._cw.set_doctype(html_doctype)
                 self._cw.main_stream.set_htmlattrs([('lang', 'cz')])
 
-        with self.temporary_appobjects(MyView):
-            html_source = self.view('my-view').source
-            source_lines = [line.strip() for line in html_source.splitlines(False)
-                            if line.strip()]
-            self.assertListEqual([html_doctype, '<html xmlns:cubicweb="http://www.cubicweb.org" lang="cz">', '<head>'],
-                                 source_lines[:3])
+        with self.admin_access.web_request() as req:
+            with self.temporary_appobjects(MyView):
+                html_source = self.view('my-view', req=req).source
+                source_lines = [line.strip()
+                                for line in html_source.splitlines(False)
+                                if line.strip()]
+                self.assertListEqual([html_doctype,
+                                      '<html xmlns:cubicweb="http://www.cubicweb.org" lang="cz">',
+                                      '<head>'],
+                                     source_lines[:3])
 
 if __name__ == '__main__':
     unittest_main()