# HG changeset patch # User Damien Garaud # Date 1407495907 -7200 # Node ID e48e5a597ccccae30505b77ecc8d9148c79bd946 # Parent 457efde98629bfadebeb5a5566a89daa5116cc9c [views] csvexport accept an empty rset (closes #4236928) When you tried to apply the 'csvexport' view on an empty rset, the view couldn't be selected and you got a HTTP 500 error. Also add two new test cases. diff -r 457efde98629 -r e48e5a597ccc web/test/unittest_views_csv.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/unittest_views_csv.py Fri Aug 08 13:05:07 2014 +0200 @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of CubicWeb. +# +# CubicWeb is free software: you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 2.1 of the License, or (at your option) +# any later version. +# +# CubicWeb is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with CubicWeb. If not, see . + +from cubicweb.devtools.testlib import CubicWebTC + + +class CSVExportViewsTC(CubicWebTC): + + def test_csvexport(self): + with self.admin_access.web_request() as req: + rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN ' + 'WHERE X in_group G, G name GN') + data = self.view('csvexport', rset, req=req) + self.assertEqual(req.headers_out.getRawHeaders('content-type'), + ['text/comma-separated-values;charset=UTF-8']) + expected_data = "String;COUNT(CWUser)\nguests;1\nmanagers;1" + self.assertMultiLineEqual(expected_data, data) + + def test_csvexport_on_empty_rset(self): + """Should return the CSV header. + """ + with self.admin_access.web_request() as req: + rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN ' + 'WHERE X in_group G, G name GN, X login "Miles"') + data = self.view('csvexport', rset, req=req) + self.assertEqual(req.headers_out.getRawHeaders('content-type'), + ['text/comma-separated-values;charset=UTF-8']) + expected_data = "String;COUNT(CWUser)" + self.assertMultiLineEqual(expected_data, data) + + +if __name__ == '__main__': + from logilab.common.testlib import unittest_main + unittest_main() diff -r 457efde98629 -r e48e5a597ccc web/test/unittest_viewselector.py --- a/web/test/unittest_viewselector.py Thu Sep 11 16:43:20 2014 +0200 +++ b/web/test/unittest_viewselector.py Fri Aug 08 13:05:07 2014 +0200 @@ -107,8 +107,11 @@ def test_possible_views_noresult(self): with self.admin_access.web_request() as req: rset = req.execute('Any X WHERE X eid 999999') - self.assertListEqual([('jsonexport', json.JsonRsetView)], - self.pviews(req, rset)) + self.assertListEqual(self.pviews(req, rset), + [('csvexport', csvexport.CSVRsetView), + ('ecsvexport', csvexport.CSVEntityView), + ('jsonexport', json.JsonRsetView), + ]) def test_possible_views_one_egroup(self): with self.admin_access.web_request() as req: @@ -214,6 +217,7 @@ rset = req.execute('Any N, X WHERE X in_group Y, Y name N') self.assertListEqual(self.pviews(req, rset), [('csvexport', csvexport.CSVRsetView), + ('ecsvexport', csvexport.CSVEntityView), ('jsonexport', json.JsonRsetView), ('rsetxml', xmlrss.XMLRsetView), ('table', tableview.RsetTableView), diff -r 457efde98629 -r e48e5a597ccc web/views/csvexport.py --- a/web/views/csvexport.py Thu Sep 11 16:43:20 2014 +0200 +++ b/web/views/csvexport.py Fri Aug 08 13:05:07 2014 +0200 @@ -21,6 +21,7 @@ _ = unicode from cubicweb.schema import display_name +from cubicweb.predicates import any_rset from cubicweb.uilib import UnicodeCSVWriter from cubicweb.view import EntityView, AnyRsetView @@ -47,6 +48,7 @@ class CSVRsetView(CSVMixIn, AnyRsetView): """dumps raw result set in CSV""" __regid__ = 'csvexport' + __select__ = any_rset() title = _('csv export') def call(self): @@ -78,6 +80,7 @@ contents) """ __regid__ = 'ecsvexport' + __select__ = any_rset() title = _('csv export (entities)') def call(self):