0

How to write an integration test of the following list view in django? I need an example.

class HomeView(ListView):
    model = Donor
    template_name = 'recipient/home.html'
    context_object_name = 'donors'
    paginate_by = 10

    def get_queryset(self):
        form = self.request.GET.get('q')
        if form:
            return Donor.objects.filter(
                Q(name__icontains=form) |
                Q(location__icontains=form) | 
                Q(sex__icontains=form) | 
                Q(age__icontains=form) | 
                Q(blood_group__icontains=form)
            ).order_by('id')
        queryset = Donor.objects.all().order_by('id')
        return queryset

    def get_context_data(self, **kwargs):
        kwargs['q'] = self.request.GET.get('q')
        return super().get_context_data(**kwargs)

1 Answer 1

0

Django rest framework provides a very convenient test framework.

In your test you would mock an API request to your endpoint and then check that the response of the request matches what you were expecting.

As an example. in your test you might want to create a few Donor instances to query, make the request and then see if the response contains the donors you were expecting. You can add filters to the request to test if they work for instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.