本文整理汇总了Python中spreadsheetresponsemixin.SpreadsheetResponseMixin类的典型用法代码示例。如果您正苦于以下问题:Python SpreadsheetResponseMixin类的具体用法?Python SpreadsheetResponseMixin怎么用?Python SpreadsheetResponseMixin使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SpreadsheetResponseMixin类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GenerateXlsxTests
class GenerateXlsxTests(TestCase):
def setUp(self):
self.data = (('row1col1', 'row1col2'), ('row2col1', 'row2col2'))
self.mixin = SpreadsheetResponseMixin()
def _get_sheet(self, wb):
return wb.get_active_sheet()
def test_returns_workbook_if_no_file_passed(self):
assert type(self.mixin.generate_xlsx(self.data)) == Workbook
def test_if_file_is_passed_it_is_returned_with_content_added(self):
given_content = StringIO()
assert given_content.getvalue() == ''
self.mixin.generate_xlsx(self.data, file=given_content)
assert given_content.getvalue() > ''
def test_adds_row_of_data(self):
wb = self.mixin.generate_xlsx(self.data)
ws = self._get_sheet(wb)
assert ws.cell('A1').value == 'row1col1'
assert ws.cell('B2').value == 'row2col2'
def test_inserts_headers_if_provided(self):
headers = ('ColA', 'ColB')
wb = self.mixin.generate_xlsx(self.data, headers)
ws = self._get_sheet(wb)
assert ws.cell('A1').value == 'ColA'
assert ws.cell('B2').value == 'row1col2'
示例2: GetRenderMethodTest
class GetRenderMethodTest(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
def test_raise_notimplemented_for_unknown_format(self):
with pytest.raises(NotImplementedError):
self.mixin.get_render_method('doc')
def test_returns_excel_response_method_for_excel_format(self):
expected_render_method = self.mixin.render_excel_response
assert self.mixin.get_render_method('excel') == expected_render_method
def test_csv_response_method_for_csv_format(self):
expected_render_method = self.mixin.render_csv_response
assert self.mixin.get_render_method('csv') == expected_render_method
示例3: GetFormatTest
class GetFormatTest(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
def test_get_format_from_export_format_kwarg(self):
format = 'excel'
assert self.mixin.get_format(format=format) == format
def test_get_format_from_export_format_attribute(self):
format = 'csv'
self.mixin.format = format
assert self.mixin.get_format() == format
def test_raise_notimplemented_if_export_format_not_supplied(self):
with pytest.raises(NotImplementedError):
self.mixin.get_format()
示例4: RenderExcelResponseTests
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
示例5: GenerateHeadersTests
class GenerateHeadersTests(TestCase):
def setUp(self):
MockModelFactory()
self.mixin = SpreadsheetResponseMixin()
self.data = self.mixin.generate_data(MockModel.objects.all())
def test_get_fields_defined_on_view(self):
fields = ('title', 'summary')
self.mixin.fields = fields
assert fields == self.mixin.get_fields()
def test_get_fields_from_kwargs(self):
fields = ('title', 'summary')
assert fields == self.mixin.get_fields(fields=fields)
def test_get_fields_from_model(self):
model = MockModelFactory()
self.mixin.model = model
assert ['id', 'title'] == self.mixin.get_fields()
def test_get_fields_from_queryset(self):
self.mixin.queryset = MockModel.objects.all()
assert ['id', 'title'] == self.mixin.get_fields()
def test_generate_headers_gets_headers_from_model_name(self):
assert self.mixin.generate_headers(self.data) == (u'Id', u'Title')
def test_generate_headers_only_returns_fields_if_fields_is_passed(self):
fields = ('title',)
assert self.mixin.generate_headers(self.data,
fields=fields) == (u'Title', )
示例6: RenderCsvResponseTests
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
示例7: GetFilenameTest
class GetFilenameTest(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
def test_from_filename_kwarg(self):
filename = 'filename.kwarg'
assert self.mixin.get_filename(filename=filename) == filename
def test_from_filename_attribute(self):
filename = 'filename.attr'
self.mixin.filename = filename
assert self.mixin.get_filename() == filename
def test_default_return_with_no_extension_provided(self):
assert self.mixin.get_filename() == 'export.out'
def test_default_return_with_extension_provided(self):
assert self.mixin.get_filename(extension='blob') == 'export.blob'
示例8: GenerateHeadersTests
class GenerateHeadersTests(TestCase):
def setUp(self):
MockModelFactory()
self.mixin = SpreadsheetResponseMixin()
self.data = self.mixin.generate_data(MockModel.objects.all())
def test_generate_headers_gets_headers_from_model_name(self):
assert self.mixin.generate_headers(self.data) == (u'Id', u'Title')
def test_generate_headers_keeps_fields_order(self):
fields = ('title', 'id')
headers = self.mixin.generate_headers(self.data, fields=fields)
assert headers == (u'Title', u'Id')
def test_generate_headers_only_returns_fields_if_fields_is_passed(self):
fields = ('title',)
assert self.mixin.generate_headers(self.data,
fields=fields) == (u'Title', )
示例9: RenderSetupTests
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)
示例10: GetFieldsTests
class GetFieldsTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
def test_if_fields_defined_on_view(self):
fields = ('title', 'summary')
self.mixin.fields = fields
assert self.mixin.get_fields() == fields
def test_get_fields_from_kwargs(self):
fields = ('title', 'summary')
assert self.mixin.get_fields(fields=fields) == fields
def test_get_fields_from_model(self):
self.mixin.model = MockModel
assert self.mixin.get_fields() == ['id', 'title']
def test_get_fields_from_queryset(self):
self.mixin.queryset = MockModel.objects.all()
assert self.mixin.get_fields() == ['id', 'title']
示例11: GetFieldsTests
class GetFieldsTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
def test_if_fields_defined_on_view(self):
fields = ('title', 'summary')
self.mixin.fields = fields
assert self.mixin.get_fields() == fields
def test_get_fields_from_kwargs(self):
fields = ('title', 'summary')
assert self.mixin.get_fields(fields=fields) == fields
def test_get_fields_from_queryset(self):
self.mixin.queryset = MockModel.objects.all()
assert self.mixin.get_fields() == ['id', 'title', 'author']
def test_get_fields_from_values_list_queryset(self):
self.mixin.queryset = MockModel.objects.all().values_list()
assert self.mixin.get_fields() == ['id', 'title', 'author_id']
def test_get_fields_from_values_list_queryset_with_specific_fields(self):
self.mixin.queryset = MockModel.objects.all().values_list('author', 'title')
assert self.mixin.get_fields() == ['author', 'title']
示例12: GenerateCsvTests
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
示例13: GenerateHeadersTests
class GenerateHeadersTests(TestCase):
def setUp(self):
MockModelFactory()
self.mixin = SpreadsheetResponseMixin()
self.mixin.queryset = MockModel.objects.all()
self.data = self.mixin.generate_data()
def test_generate_headers_gets_headers_from_model_name(self):
fields = self.mixin.get_fields(model=MockModel)
assert self.mixin.generate_headers(MockModel, fields) == (u'Id', u'Title', u'Author')
def test_generate_headers_keeps_fields_order(self):
fields = ('title', 'id')
headers = self.mixin.generate_headers(MockModel, fields=fields)
assert headers == (u'Title', u'Id')
def test_generate_headers_only_returns_fields_if_fields_is_passed(self):
fields = ('title',)
assert self.mixin.generate_headers(MockModel,
fields=fields) == (u'Title', )
def test_generate_headers_follows_foreign_keys(self):
fields = ('title', 'author__name')
headers = self.mixin.generate_headers(MockModel, fields)
assert headers == (u'Title', u'Author Name')
def test_generate_headers_with_calculated_fields(self):
fields = ('title', 'author__name', 'calculate_this')
self.mixin.calculate_this = lambda values: 'whee %d' % values[0]
headers = self.mixin.generate_headers(MockModel, fields)
assert headers == (u'Title', u'Author Name', u'Calculate This')
def test_generate_headers_with_calculated_fields_with_verbose_names(self):
fields = ('title', 'author__name', 'calculate_this')
self.mixin.calculate_this = lambda values: 'whee %d' % values[0]
self.mixin.calculate_this.verbose_name = 'Whee!'
headers = self.mixin.generate_headers(MockModel, fields)
assert headers == (u'Title', u'Author Name', u'Whee!')
示例14: setUp
def setUp(self):
self.data = (('row1col1', 'row1col2'), ('row2col1', 'row2col2'))
self.mixin = SpreadsheetResponseMixin()
示例15: RenderSetupTests
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