0

Im using coverage to test a view that exports a xls file using report builder code.

This is the view code :

from report_builder.models import Report
from report_utils.mixins import DataExportMixin, generate_filename
import re
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response

class DownloadFileView(DataExportMixin, View):

    #def dispatch(self, *args, **kwargs):
        #return super(DownloadFileView, self).dispatch(*args, **kwargs)

    def process_report(self, report_id, user_id,
                       file_type, to_response, queryset=None):
        report = get_object_or_404(Report, pk=report_id)
        user = User.objects.get(pk=user_id)
        if not queryset:
            queryset = report.get_query()

        display_fields = report.get_good_display_fields()
        #import pdb; pbb.set_trace()
        objects_list, message = self.report_to_list(
            queryset,
            display_fields,
            user,
            preview=False,)
        title = re.sub(r'\W+', '', report.name)[:30]
        header = []
        widths = []
        for field in display_fields:
            header.append(field.name)
            widths.append(field.width)

        if to_response:
            return self.list_to_xlsx_response(
                objects_list, title, header, widths)


    def get(self, request, *args, **kwargs):
        #report_id = 3
        report_id= kwargs["report_id"]
        file_type = 'xls'
        user_id = 2
        return self.process_report(
            report_id, user_id, file_type, to_response=True)

And this is the test i wrote for it :

class test_reports_download(TestCase):
    fixtures = ['test_data_cost_control_app.json']

    def test_reports_select_get(self):
        request = HttpRequest()
        request.method = 'GET'
        DownloadFileView.as_view()(request, report_id = 1, file_type = 'xls', user_id =2)

Coverage marks almost all view code as tested except for this 2 lines:

header.append(field.name) 
widths.append(field.width) 

Any idea why ?, i appreciate any help.

Thanks in advance

jsanchezs
  • 1,992
  • 3
  • 25
  • 51
  • Then `for field in display_fields` is not executed. In other words, `display_fields` might be empty, isn't it? – Shang Wang Feb 17 '16 at 22:54
  • Mm shouldn't be, i created a report record inside a fixtures json file so it could have data, also, other lines in view get tested succesfully. Any idea of what parameter or additional data should i write inside the test so it can pass ? – jsanchezs Feb 17 '16 at 22:59
  • @ShangWang did a pdb trace, report gets a report object indeed, but display_fields is empty as you said, any idea why ? report gets an existing and valid object =S – jsanchezs Feb 17 '16 at 23:03
  • donno, it's your code `display_fields = report.get_good_display_fields()`, so you might know it better than me. – Shang Wang Feb 17 '16 at 23:06

0 Answers0