本文整理汇总了Python中spreadsheetresponsemixin.SpreadsheetResponseMixin.render_excel_response方法的典型用法代码示例。如果您正苦于以下问题:Python SpreadsheetResponseMixin.render_excel_response方法的具体用法?Python SpreadsheetResponseMixin.render_excel_response怎么用?Python SpreadsheetResponseMixin.render_excel_response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spreadsheetresponsemixin.SpreadsheetResponseMixin
的用法示例。
在下文中一共展示了SpreadsheetResponseMixin.render_excel_response方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RenderSetupTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import render_excel_response [as 别名]
class RenderSetupTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
MockModelFactory()
self.queryset = MockModel.objects.all()
self.mixin.queryset = self.queryset
def test_generate_data_is_called_once_with_fields_and_queryset(self):
self.mixin.generate_data = mock.MagicMock()
self.mixin.render_excel_response(queryset='test', fields='testfields')
self.mixin.generate_data.assert_called_once_with(queryset='test',
fields='testfields')
def test_if_no_headers_passed_generate_headers_called(self):
self.mixin.generate_headers = mock.MagicMock()
fields = (u'title',)
self.mixin.render_excel_response(fields=fields)
self.mixin.generate_headers.assert_called_once_with(self.mixin.data,
fields=fields)
示例2: RenderExcelResponseTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import render_excel_response [as 别名]
class RenderExcelResponseTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
MockModelFactory()
self.queryset = MockModel.objects.all()
self.mixin.queryset = self.queryset
def test_returns_httpresponse(self):
assert type(self.mixin.render_excel_response()) == HttpResponse
def test_returns_xlsx_content_type(self):
expected_content_type = \
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
response = self.mixin.render_excel_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.xlsx"'
response = self.mixin.render_excel_response()
actual_disposition = response._headers['content-disposition'][1]
assert actual_disposition == expected_disposition
def test_generate_xslx_is_called_with_data(self):
self.mixin.generate_xlsx = mock.MagicMock()
self.mixin.render_excel_response()
data = self.mixin.generate_data(self.queryset)
mut = self.mixin.generate_xlsx
assert mut.call_count == 1
assert list(mut.call_args.__getnewargs__()[0][1]['data']) == list(data)
def test_generate_xslx_is_called_with_headers(self):
self.mixin.generate_xlsx = mock.MagicMock()
headers = ('ColA', 'ColB')
self.mixin.render_excel_response(headers=headers)
mut = self.mixin.generate_xlsx
assert mut.call_count == 1
assert mut.call_args.__getnewargs__()[0][1]['headers'] == headers
def test_generate_xslx_is_called_with_response(self):
self.mixin.generate_xlsx = mock.MagicMock()
self.mixin.render_excel_response()
mut = self.mixin.generate_xlsx
assert mut.call_count == 1
assert type(mut.call_args.__getnewargs__()[0][1]['file']) \
== HttpResponse
示例3: RenderSetupTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import render_excel_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