本文整理汇总了Python中spreadsheetresponsemixin.SpreadsheetResponseMixin.render_csv_response方法的典型用法代码示例。如果您正苦于以下问题:Python SpreadsheetResponseMixin.render_csv_response方法的具体用法?Python SpreadsheetResponseMixin.render_csv_response怎么用?Python SpreadsheetResponseMixin.render_csv_response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spreadsheetresponsemixin.SpreadsheetResponseMixin
的用法示例。
在下文中一共展示了SpreadsheetResponseMixin.render_csv_response方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RenderSetupTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import render_csv_response [as 别名]
class RenderSetupTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
self.mixin.generate_headers = mock.MagicMock()
self.mixin.queryset = MockModel.objects.all()
self.fields = (u'title',)
def test_if_no_self_queryset_raise_improperlyconfigured(self):
delattr(self.mixin, 'queryset')
with pytest.raises(NotImplementedError):
list(self.mixin.render_setup())
def test_get_fields_is_called_with_kwargs(self):
self.mixin.get_fields = mock.MagicMock()
self.mixin.render_setup(a=1, b=2)
self.mixin.get_fields.assert_called_once_with(a=1, b=2)
def test_generate_data_is_called_once_with_fields_and_queryset(self):
self.mixin.generate_data = mock.MagicMock()
qs = MockModel.objects.all()
self.mixin.render_excel_response(queryset=qs, fields=self.fields)
self.mixin.generate_data.assert_called_once_with(fields=self.fields)
def test_if_no_headers_passed_generate_headers_called(self):
self.mixin.render_excel_response(fields=self.fields)
self.mixin.generate_headers.assert_called_once_with(MockModel,
fields=self.fields)
def test_returns_attachment_with_correct_filename(self):
expected_disposition = 'attachment; filename="export.csv"'
response = self.mixin.render_csv_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
self.mixin.filename = 'data.dump'
expected_disposition = 'attachment; filename="data.dump"'
response = self.mixin.render_csv_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
expected_disposition = 'attachment; filename="data.dump"'
response = self.mixin.render_excel_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
delattr(self.mixin, 'filename')
self.mixin.filename_base = 'data.dump'
expected_disposition = 'attachment; filename="data.dump.csv"'
response = self.mixin.render_csv_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
expected_disposition = 'attachment; filename="data.dump.xlsx"'
response = self.mixin.render_excel_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
示例2: RenderCsvResponseTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import render_csv_response [as 别名]
class RenderCsvResponseTests(TestCase):
def setUp(self):
MockModelFactory()
self.queryset = MockModel.objects.all()
self.mixin = SpreadsheetResponseMixin()
self.mixin.queryset = self.queryset
def test_returns_httpresponse(self):
assert type(self.mixin.render_csv_response()) == HttpResponse
def test_returns_csv_content_type(self):
expected_content_type = 'text/csv'
response = self.mixin.render_csv_response()
actual_content_type = response._headers['content-type'][1]
assert actual_content_type == expected_content_type
def test_returns_attachment_content_disposition(self):
expected_disposition = 'attachment; filename="export.csv"'
response = self.mixin.render_csv_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
def test_generate_csv_is_called_with_data(self):
self.mixin.generate_csv = mock.MagicMock()
self.mixin.render_csv_response()
data = self.mixin.generate_data(self.queryset)
mut = self.mixin.generate_csv
assert mut.call_count == 1
assert list(mut.call_args.__getnewargs__()[0][1]['data']) == list(data)
def test_generate_csv_is_called_with_headers(self):
self.mixin.generate_csv = mock.MagicMock()
headers = ('ColA', 'ColB')
self.mixin.render_csv_response(headers=headers)
mut = self.mixin.generate_csv
assert mut.call_count == 1
assert mut.call_args.__getnewargs__()[0][1]['headers'] == headers
def test_generate_csv_is_called_with_response(self):
self.mixin.generate_csv = mock.MagicMock()
self.mixin.render_csv_response()
mut = self.mixin.generate_csv
assert mut.call_count == 1
assert type(mut.call_args.__getnewargs__()[0][1]['file']) \
== HttpResponse