Allowed a message to be passed to assertQuerysetEqual to make it consistent with other assert methods.

This commit is contained in:
Chris Bailey 2014-02-10 05:18:20 -05:00 committed by Tim Graham
parent 21f034165c
commit c5395eef76
2 changed files with 11 additions and 4 deletions

View File

@ -825,17 +825,17 @@ class TransactionTestCase(SimpleTestCase):
allow_cascade=self.available_apps is not None, allow_cascade=self.available_apps is not None,
inhibit_post_migrate=self.available_apps is not None) inhibit_post_migrate=self.available_apps is not None)
def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True): def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True, msg=None):
items = six.moves.map(transform, qs) items = six.moves.map(transform, qs)
if not ordered: if not ordered:
return self.assertEqual(set(items), set(values)) return self.assertEqual(set(items), set(values), msg=msg)
values = list(values) values = list(values)
# For example qs.iterator() could be passed as qs, but it does not # For example qs.iterator() could be passed as qs, but it does not
# have 'ordered' attribute. # have 'ordered' attribute.
if len(values) > 1 and hasattr(qs, 'ordered') and not qs.ordered: if len(values) > 1 and hasattr(qs, 'ordered') and not qs.ordered:
raise ValueError("Trying to compare non-ordered queryset " raise ValueError("Trying to compare non-ordered queryset "
"against more than one ordered values") "against more than one ordered values")
return self.assertEqual(list(items), values) return self.assertEqual(list(items), values, msg=msg)
def assertNumQueries(self, num, func=None, *args, **kwargs): def assertNumQueries(self, num, func=None, *args, **kwargs):
using = kwargs.pop("using", DEFAULT_DB_ALIAS) using = kwargs.pop("using", DEFAULT_DB_ALIAS)

View File

@ -1443,7 +1443,7 @@ your test suite.
Output in case of error can be customized with the ``msg`` argument. Output in case of error can be customized with the ``msg`` argument.
.. method:: TransactionTestCase.assertQuerysetEqual(qs, values, transform=repr, ordered=True) .. method:: TransactionTestCase.assertQuerysetEqual(qs, values, transform=repr, ordered=True, msg=None)
Asserts that a queryset ``qs`` returns a particular list of values ``values``. Asserts that a queryset ``qs`` returns a particular list of values ``values``.
@ -1456,6 +1456,8 @@ your test suite.
provide an implicit ordering, you can set the ``ordered`` parameter to provide an implicit ordering, you can set the ``ordered`` parameter to
``False``, which turns the comparison into a Python set comparison. ``False``, which turns the comparison into a Python set comparison.
Output in case of error can be customized with the ``msg`` argument.
.. versionchanged:: 1.6 .. versionchanged:: 1.6
The method now checks for undefined order and raises ``ValueError`` The method now checks for undefined order and raises ``ValueError``
@ -1463,6 +1465,11 @@ your test suite.
the given ``qs`` isn't ordered and the comparison is against more the given ``qs`` isn't ordered and the comparison is against more
than one ordered values. than one ordered values.
.. versionchanged:: 1.7
The method now accepts a ``msg`` parameter to allow customization of
error message
.. method:: TransactionTestCase.assertNumQueries(num, func, *args, **kwargs) .. method:: TransactionTestCase.assertNumQueries(num, func, *args, **kwargs)
Asserts that when ``func`` is called with ``*args`` and ``**kwargs`` that Asserts that when ``func`` is called with ``*args`` and ``**kwargs`` that