Login

assertQuerysetEqual

Author:
coleifer
Posted:
May 7, 2010
Language:
Python
Version:
1.1
Score:
3 (after 3 ratings)

I often find myself testing that the queryset returned by a method contains the instances I expect. I use a custom method, assertQuerysetEqual(), to test the equality of two querysets or lists::

def test_some_values(self):
    qs = get_user_list()
    self.assertQuerysetEqual(qs, [normal_user, super_user])

Makes it easy to test small querysets against lists whose values are known and expected.

1
2
3
4
5
6
7
8
9
from django.test import TestCase

class BaseTestCase(TestCase):
    def assertQuerysetEqual(self, qs1, qs2):
        pk = lambda o: o.pk
        return self.assertEqual(
            list(sorted(qs1, key=pk)),
            list(sorted(qs2, key=pk))
        )

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.