40

I am trying to test my Django views. This view passes a QuerySet to the template:

def merchant_home(request, slug):
  merchant = Merchant.objects.get(slug=slug)
  product_list = merchant.products.all()
  return render_to_response('merchant_home.html',
                            {'merchant': merchant,
                            'product_list': product_list},
                            context_instance=RequestContext(request))

and test:

  def test(self):
    "Merchant home view should send merchant and merchant products to the template"
    merchant = Merchant.objects.create(name='test merchant')
    product = Product.objects.create(name='test product', price=100.00)
    merchant.products.add(product)

    test_client = Client()
    response = test_client.get('/' + merchant.slug)
    # self.assertListEqual(response.context['product_list'], merchant.products.all())
    self.assertQuerysetEqual(response.context['product_list'], merchant.products.all())

EDIT I am using self.assertQuerysetEqual instead of self.assertListEqual. Unfortunately this still doesn't work, and the terminal displays this: ['<Product: Product object>'] != [<Product: Product object>]


assertListEqual raises: 'QuerySet' object has no attribute 'difference' and assertEqual does not work either, although self.assertSetEqual(response.context['product_list'][0], merchant.products.all()[0]) does pass.

I assume this is because the QuerySets are different objects even though they contain the same model instances.

How do I test that two QuerySets contain the same data? I am even testing this correctly? This is my 4th day learning Django so I would like to know best practices, if possible. Thanks.

Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
jz999
  • 589
  • 1
  • 5
  • 13

6 Answers6

47

By default assertQuerysetEqual uses repr() on the first argument. This is why you were having issues with the strings in the queryset comparison.

To work around this you can override the transform argument with a lambda function that doesn't use repr():

self.assertQuerysetEqual(queryset_1, queryset_2, transform=lambda x: x)
Daniel
  • 3,115
  • 5
  • 28
  • 39
  • 1
    I found this also worked for comparing two dictionaries with the same keys and the same queryset values: `self.assertQuerysetEqual(dict_1, dict_2, transform=lambda x: x)` – Daniel Holmes Feb 19 '19 at 09:20
36

Use assertQuerysetEqual, which is built to compare the two querysets for you. You will need to subclass Django's django.test.TestCase for it to be available in your tests.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
girasquid
  • 15,121
  • 2
  • 48
  • 58
  • 17
    Awesome, this is helpful, though now my tests fails because of this: AssertionError: [''] != [] The only difference I see is that there are quotes around the first product object. They are both of class QuerySet – jz999 Jul 17 '13 at 19:47
11

I just had the same problem. The second argument of assertQuerysetEqual needs to be a list of the expected repr()s as strings. Here is an example from the Django test suite:

self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"], ordered=False)
sk1p
  • 6,645
  • 30
  • 35
4

An alternative, but not necessarily better, method might look like this (testing context in a view, for example) when using pytest:

all_the_things = Things.objects.all()
assert set(response.context_data['all_the_things']) == set(all_the_things)

This converts it to a set, which is directly comparable with another set. Be careful with the behaviour of set though, it might not be exactly what you want since it will remove duplicates and ignore the order of objects.

VisioN
  • 143,310
  • 32
  • 282
  • 281
Chris
  • 321
  • 3
  • 15
4

I ended up solving this issue using map to repr() each entry in the queryset inside the self.assertQuerysetEqual call, e.g.

self.assertQuerysetEqual(queryset_1, map(repr, queryset_2))
DrBuck
  • 822
  • 7
  • 22
-1

I found that using self.assertCountEqual(queryset1, queryset2) also solves the issue.

  • This doesn't add anything substantial to the existing answers. I recommend upvoting those answers instead. – ggorlen Jan 12 '21 at 23:17