summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2011-08-23 03:38:18 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2011-08-23 03:38:18 +0000
commit0686c6b0ee7e6099cdcc48317d2649dc19dc89c9 (patch)
tree1f3e583f1b55a9e49187b208f1ce472fa946f9cf
parent2664fa189614a4dbbfed5cb683148707ab568f47 (diff)
Add the ability to do unordered comparisons in assertQuerysetEqual.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16654 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/test/testcases.py4
-rw-r--r--docs/topics/testing.txt15
2 files changed, 14 insertions, 5 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 4a9b0c3ee8..71687f2a6e 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -597,7 +597,9 @@ class TransactionTestCase(SimpleTestCase):
msg_prefix + "Template '%s' was used unexpectedly in rendering"
" the response" % template_name)
- def assertQuerysetEqual(self, qs, values, transform=repr):
+ def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True):
+ if not ordered:
+ return self.assertEqual(set(map(transform, qs)), set(values))
return self.assertEqual(map(transform, qs), values)
def assertNumQueries(self, num, func=None, *args, **kwargs):
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index eef5b138b1..55c1868e13 100644
--- a/docs/topics/testing.txt
+++ b/docs/topics/testing.txt
@@ -1592,7 +1592,7 @@ your test suite.
``target_status_code`` will be the url and status code for the final
point of the redirect chain.
-.. method:: TestCase.assertQuerysetEqual(qs, values, transform=repr)
+.. method:: TestCase.assertQuerysetEqual(qs, values, transform=repr, ordered=True)
.. versionadded:: 1.3
@@ -1603,9 +1603,16 @@ your test suite.
each value is compared. Any other callable can be used if ``repr()`` doesn't
provide a unique or helpful comparison.
- The comparison is also ordering dependent. If ``qs`` doesn't provide an
- implicit ordering, you will need to apply a ``order_by()`` clause to your
- queryset to ensure that the test will pass reliably.
+ By default, the comparison is also ordering dependent. If ``qs`` doesn't
+ provide an implicit ordering, you can set the ``ordered`` parameter to
+ ``False``, which turns the comparison into a Python set comparison.
+
+ .. versionchanged:: 1.4
+ The ``ordered`` parameter is new in version 1.4. In earlier versions,
+ you would need to ensure the queryset is ordered consistently, possibly
+ via an explicit ``order_by()`` call on the queryset prior to
+ comparison.
+
.. method:: TestCase.assertNumQueries(num, func, *args, **kwargs)