本文整理汇总了Python中spreadsheetresponsemixin.SpreadsheetResponseMixin.generate_data方法的典型用法代码示例。如果您正苦于以下问题:Python SpreadsheetResponseMixin.generate_data方法的具体用法?Python SpreadsheetResponseMixin.generate_data怎么用?Python SpreadsheetResponseMixin.generate_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spreadsheetresponsemixin.SpreadsheetResponseMixin
的用法示例。
在下文中一共展示了SpreadsheetResponseMixin.generate_data方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GenerateHeadersTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [as 别名]
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', )
示例2: RenderExcelResponseTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [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_get_filename_called_with_csv_parameter(self):
self.mixin.get_filename = mock.MagicMock()
self.mixin.render_excel_response()
self.mixin.get_filename.assert_called_once_with(extension='xlsx')
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: RenderCsvResponseTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [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_get_filename_called_with_csv_parameter(self):
self.mixin.get_filename = mock.MagicMock()
self.mixin.render_csv_response()
self.mixin.get_filename.assert_called_once_with(extension='csv')
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
示例4: GenerateHeadersTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [as 别名]
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', )
示例5: GenerateHeadersTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [as 别名]
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!')
示例6: GenerateDataTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [as 别名]
class GenerateDataTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
self.mock = MockModelFactory()
self.mock2 = MockModelFactory()
self.queryset = MockModel.objects.all()
def test_assertion_error_raised_if_not_a_queryset_is_sent(self):
with pytest.raises(AssertionError):
self.mixin.generate_data([1])
def test_if_queryset_is_none_gets_self_queryset(self):
self.mixin.queryset = mock.Mock(spec=QuerySet)
self.mixin.generate_data()
self.mixin.queryset.values_list.assert_called_once_with()
def test_if_no_self_queryset_raise_improperlyconfigured(self):
with pytest.raises(NotImplementedError):
self.mixin.generate_data()
def test_returns_values_list_qs_if_queryset(self):
expected_list = self.queryset.values_list()
actual_list = self.mixin.generate_data(self.queryset)
assert list(actual_list) == list(expected_list)
def test_returns_values_list_if_qs_is_values_queryset(self):
expected_list = MockModel.objects.all().values_list()
actual_list = self.mixin.generate_data(self.queryset.values())
assert list(actual_list) == list(expected_list)
def test_returns_self_if_qs_is_values_list_queryset(self):
values_list_queryset = MockModel.objects.all().values_list()
actual_list = self.mixin.generate_data(values_list_queryset)
assert list(actual_list) == list(values_list_queryset)
def test_uses_specified_fields(self):
fields = ('title',)
values_list_queryset = MockModel.objects.all().values_list()
# We expect it to be filtered by title,
# even though full value_list is passed
expected_list = MockModel.objects.all().values_list(*fields)
actual_list = self.mixin.generate_data(values_list_queryset, fields)
assert list(actual_list) == list(expected_list)
def test_reverse_ordering_when_fields_specified(self):
fields = ('title', 'id')
actual_list = self.mixin.generate_data(self.queryset, fields)
assert actual_list[0] == (self.mock.title, self.mock.id)
示例7: GenerateDataTests
# 需要导入模块: from spreadsheetresponsemixin import SpreadsheetResponseMixin [as 别名]
# 或者: from spreadsheetresponsemixin.SpreadsheetResponseMixin import generate_data [as 别名]
class GenerateDataTests(TestCase):
def setUp(self):
self.mixin = SpreadsheetResponseMixin()
self.author = MockAuthorFactory()
self.mock = MockModelFactory(author=self.author)
self.mock2 = MockModelFactory(author=self.author)
self.queryset = MockModel.objects.all()
def test_assertion_error_raised_if_not_a_queryset_is_sent(self):
with pytest.raises(AssertionError):
self.mixin.queryset = [1]
list(self.mixin.generate_data())
def test_if_queryset_is_none_gets_self_queryset(self):
self.mixin.queryset = MockModel.objects.all()
self.assertSequenceEqual(MockModel.objects.values_list(),
list(self.mixin.generate_data()))
def test_returns_values_list_qs_if_queryset(self):
self.mixin.queryset = self.queryset
expected_list = self.queryset.values_list()
actual_list = self.mixin.generate_data()
assert list(actual_list) == list(expected_list)
def test_returns_values_list_if_qs_is_values_queryset(self):
self.mixin.queryset = self.queryset.values()
expected_list = MockModel.objects.all().values_list()
actual_list = self.mixin.generate_data()
assert list(actual_list) == list(expected_list)
def test_returns_self_if_qs_is_values_list_queryset(self):
values_list_queryset = MockModel.objects.all().values_list()
self.mixin.queryset = values_list_queryset
actual_list = self.mixin.generate_data()
assert list(actual_list) == list(values_list_queryset)
def test_uses_specified_fields(self):
fields = ('title',)
values_list_queryset = MockModel.objects.all().values_list()
self.mixin.queryset = values_list_queryset
# We expect it to be filtered by title,
# even though full value_list is passed
expected_list = MockModel.objects.all().values_list(*fields)
actual_list = self.mixin.generate_data(fields)
assert list(actual_list) == list(expected_list)
def test_follows_foreign_key_with_model_queryset(self):
fields = ('title', 'author__name')
queryset = MockModel.objects.all()
self.mixin.queryset = queryset
expected_list = [
(self.mock.title, self.author.name),
(self.mock2.title, self.author.name),
]
actual_list = self.mixin.generate_data(fields)
assert list(actual_list) == list(expected_list)
def test_allows_calculated_field_values(self):
fields = ('title', 'author__name', 'calculated')
queryset = MockModel.objects.all()
self.mixin.queryset = queryset
expected_list = [
(self.mock.title, self.author.name, u'whee %d' % self.mock.id),
(self.mock2.title, self.author.name, u'whee %d' % self.mock2.id),
]
self.mixin.calculated = lambda values: 'whee %d' % values[0]
self.mixin.calculated.fields = ['id']
actual_list = self.mixin.generate_data(fields)
self.assertEqual(list(actual_list), expected_list)
def test_follows_foreign_key_with_values_list_queryset(self):
fields = ('title', 'author__name')
values_list_queryset = MockModel.objects.all().values_list()
self.mixin.queryset = values_list_queryset
expected_list = [
(self.mock.title, self.author.name),
(self.mock2.title, self.author.name),
]
actual_list = self.mixin.generate_data(fields)
assert list(actual_list) == list(expected_list)
def test_reverse_ordering_when_fields_specified(self):
fields = ('title', 'id')
self.mixin.queryset = self.queryset
actual_list = self.mixin.generate_data(fields)
assert list(actual_list)[0] == (self.mock.title, self.mock.id)
def test_allows_evaluation_using_models(self):
fields = ('title', 'author__name', 'calculated')
self.mixin.queryset = self.queryset
expected_list = [
(self.mock.title, self.author.name, u'whee %d' % self.mock.id),
(self.mock2.title, self.author.name, u'whee %d' % self.mock2.id),
]
self.mixin.use_models = True
self.mixin.calculated = lambda model: 'whee %d' % model.id
#.........这里部分代码省略.........