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', )
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', )
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']
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']
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']
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!')
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!')