web/test/unittest_views_csv.py
author Damien Garaud <damien.garaud@logilab.fr>
Fri, 08 Aug 2014 13:05:07 +0200
changeset 9977 e48e5a597ccc
child 10660 97f6b3d655fc
permissions -rw-r--r--
[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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9977
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     2
# copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     4
#
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     5
# This file is part of CubicWeb.
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     6
#
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    10
# any later version.
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    11
#
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    15
# details.
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    16
#
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    19
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    20
from cubicweb.devtools.testlib import CubicWebTC
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    21
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    22
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    23
class CSVExportViewsTC(CubicWebTC):
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    24
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    25
    def test_csvexport(self):
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    26
        with self.admin_access.web_request() as req:
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    27
            rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    28
                               'WHERE X in_group G, G name GN')
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    29
            data = self.view('csvexport', rset, req=req)
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    30
            self.assertEqual(req.headers_out.getRawHeaders('content-type'),
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    31
                             ['text/comma-separated-values;charset=UTF-8'])
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    32
            expected_data = "String;COUNT(CWUser)\nguests;1\nmanagers;1"
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    33
            self.assertMultiLineEqual(expected_data, data)
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    34
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    35
    def test_csvexport_on_empty_rset(self):
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    36
        """Should return the CSV header.
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    37
        """
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    38
        with self.admin_access.web_request() as req:
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    39
            rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    40
                               'WHERE X in_group G, G name GN, X login "Miles"')
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    41
            data = self.view('csvexport', rset, req=req)
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    42
            self.assertEqual(req.headers_out.getRawHeaders('content-type'),
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    43
                             ['text/comma-separated-values;charset=UTF-8'])
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    44
            expected_data = "String;COUNT(CWUser)"
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    45
            self.assertMultiLineEqual(expected_data, data)
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    46
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    47
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    48
if __name__ == '__main__':
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    49
    from logilab.common.testlib import unittest_main
e48e5a597ccc [views] csvexport accept an empty rset (closes #4236928)
Damien Garaud <damien.garaud@logilab.fr>
parents:
diff changeset
    50
    unittest_main()