How to Test in Django the Numbers of Querysets Returned by a CBV

For a real-estate app I have a search form and a class-based ListView that shows only the objects (properties) that match the search query from the form:

# views.py

from django.views.generic import ListView
from django.db.models import Q

from .models import Property
from .forms import PropertyForm

class ListPageView(ListView):
    template_name = 'property_list.html'
    model = Property

    def get_queryset(self):
        plz = self.request.GET.get('plz')
        rooms = self.request.GET.get('rooms')
        if plz and not rooms:
            object_list = Property.objects.filter(zip_code__exact=plz)
        elif rooms and not plz:
            object_list = Property.objects.filter(rooms__exact=rooms)
        elif plz and rooms:
            object_list = Property.objects.filter(
                Q(zip_code__icontains=plz) & Q(rooms__exact=rooms)
            )
        else:
            object_list = self.model.objects.none()

        return object_list

Tests

I wrote a test to check the logic of my view. I was able to write a test that checks if the queryset returns nothing:

from django.test import TestCase
from django.db.models import Q

from na_mi.models import Property


class ListPageViewTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        Property.objects.bulk_create(
            [Property(zip_code=4054,rooms=1),
             Property(zip_code=4132,rooms=4),
             Property(zip_code=4132,rooms=4),
             Property(zip_code=4132,rooms=3),
             Property(zip_code=4056,rooms=1)]
        )
   
    # tests if the queryset returns nothing   
    def test_view_no_match(self):
        response = self.client.get('/list/')
        self.assertContains(response, 'Sorry, no search result')

Question: How can I test my view if it returns the right number of querysets?

I want to have a test that ensures that the right number of querysets is returned.
Something like that (pseudo code).

    def test_view_show_zip_code_only(self):
        response = self.client.get('/list/?plz=4054&rooms=&title=/')
        self.assertContains(response.NUMBER_OF_QUERYSETS_RETURNED, '1')

Here it should only return one queryset, because only one database record that maches the query (plz=4054) exists in the database and nothing else is requested.

I found the TransactionTestCase.assertNumQueries(num, func, *args, **kwargs) function but I don’t want to test my queryset I want to test my view.

How to do that?

Okay. I found the solution, thanks to this answer on Stackoverflow.

I can test now the numbers of model instances my queryset returns:

    def test_query_plz_only(self):
        response = self.client.get('/list/?plz=4054&rooms=&title=/')
        object_list = response.context['object_list']
        self.assertEqual(object_list.count(), 1)

I’m now able to write test for the desired results and refactor and expand the logic of my ListView.

1 Like