0

我使用覆盖来测试使用报告生成器代码导出 xls 文件的视图。

这是视图代码:

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)

这是我为它写的测试:

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 将几乎所有视图代码都标记为已测试,除了这 2 行:

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

知道为什么吗?我感谢任何帮助。

提前致谢

4

0 回答 0