当前位置: 首页>>代码示例>>Python>>正文


Python SpreadsheetResponseMixin.generate_csv方法代码示例

本文整理汇总了Python中spreadsheetresponsemixin.SpreadsheetResponseMixin.generate_csv方法的典型用法代码示例。如果您正苦于以下问题:Python SpreadsheetResponseMixin.generate_csv方法的具体用法?Python SpreadsheetResponseMixin.generate_csv怎么用?Python SpreadsheetResponseMixin.generate_csv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在spreadsheetresponsemixin.SpreadsheetResponseMixin的用法示例。


在下文中一共展示了SpreadsheetResponseMixin.generate_csv方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GenerateCsvTests

# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_csv [as 别名]
class GenerateCsvTests(TestCase):
    def setUp(self):
        self.data = (('row1col1', 'row1col2'), ('row2col1', 'row2col2'))
        self.mixin = SpreadsheetResponseMixin()

    def test_if_no_file_passed_in_stringio_is_returned(self):
        generated_csv = self.mixin.generate_csv(self.data)
        assert type(generated_csv) == type(StringIO())

    def test_if_file_is_passed_it_is_returned_with_content_added(self):
        given_content = mock.MagicMock()
        returned_file = self.mixin.generate_csv(self.data, file=given_content)
        assert returned_file == given_content

    def test_adds_row_of_data(self):
        expected_string = \
            'row1col1,row1col2\r\nrow2col1,row2col2\r\n'
        generated_csv = self.mixin.generate_csv(self.data)
        assert generated_csv.getvalue() == expected_string

    def test_inserts_headers_if_provided(self):
        headers = ('ColA', 'ColB')
        expected_string = \
            'ColA,ColB\r\nrow1col1,row1col2\r\nrow2col1,row2col2\r\n'
        generated_csv = self.mixin.generate_csv(self.data, headers)
        assert generated_csv.getvalue() == expected_string

    def test_handles_data_with_commas_correctly_by_putting_quotes(self):
        data = (('Title is, boo', '2'), )
        expected_string = \
            '"Title is, boo",2\r\n'
        generated_csv = self.mixin.generate_csv(data)
        assert generated_csv.getvalue() == expected_string

    def test_handles_data_with_quotes_correctly(self):
        data = (('Title is, "boo"', '2'), )
        expected_string = \
            '"Title is, ""boo""",2\r\n'
        generated_csv = self.mixin.generate_csv(data)
        assert generated_csv.getvalue() == expected_string

    def test_handles_data_in_unicode_correctly(self):
        data = ((u'Bumblebee is čmrlj', '2'), )
        expected_string = u'Bumblebee is čmrlj,2\r\n'.encode('utf-8')
        generated_csv = self.mixin.generate_csv(data)
        assert generated_csv.getvalue() == expected_string
开发者ID:ashwoods,项目名称:django-spreadsheetresponsemixin,代码行数:48,代码来源:test_views.py


注:本文中的spreadsheetresponsemixin.SpreadsheetResponseMixin.generate_csv方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。