Commit 088d3bc2 authored by Anssi Kääriäinen's avatar Anssi Kääriäinen
Browse files

Fixed #19462 -- Made assertQuerysetEqual detect undefined ordering

If there are more than one values to compare against and the qs isn't
ordered then assertQuerysetEqual will raise a ValueError.
parent 6ed6a18a
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -767,6 +767,12 @@ class TransactionTestCase(SimpleTestCase):
        items = six.moves.map(transform, qs)
        if not ordered:
            return self.assertEqual(set(items), set(values))
        values = list(values)
        # For example qs.iterator() could be passed as qs, but it does not
        # have 'ordered' attribute.
        if len(values) > 1 and hasattr(qs, 'ordered') and not qs.ordered:
            raise ValueError("Trying to compare non-ordered queryset "
                             "against more than one ordered values")
        return self.assertEqual(list(items), values)

    def assertNumQueries(self, num, func=None, *args, **kwargs):
+5 −0
Original line number Diff line number Diff line
@@ -23,6 +23,11 @@ Minor features
* Authentication backends can raise ``PermissionDenied`` to immediately fail
  the authentication chain.

* The ``assertQuerysetEqual()`` now checks for undefined order and raises
  ``ValueError`` if undefined order is spotted. The order is seen as
  undefined if the given ``QuerySet`` isn't ordered and there are more than
  one ordered values to compare against.

Backwards incompatible changes in 1.6
=====================================

+5 −0
Original line number Diff line number Diff line
@@ -1770,6 +1770,11 @@ your test suite.
	via an explicit ``order_by()`` call on the queryset prior to
	comparison.

    .. versionchanged:: 1.6
        The method now checks for undefined order and raises ``ValueError``
        if undefined order is spotted. The ordering is seen as undefined if
        the given ``qs`` isn't ordered and the comparison is against more
        than one ordered values.

.. method:: TestCase.assertNumQueries(num, func, *args, **kwargs)

+3 −1
Original line number Diff line number Diff line
@@ -158,6 +158,7 @@ class ExpressionsTests(TestCase):
                "Max Mustermann",
            ],
            lambda c: six.text_type(c.point_of_contact),
            ordered=False
        )

        c = Company.objects.all()[0]
@@ -170,7 +171,8 @@ class ExpressionsTests(TestCase):
                "Foobar Ltd.",
                "Test GmbH",
            ],
            lambda c: c.name
            lambda c: c.name,
            ordered=False
        )

        Company.objects.exclude(
+2 −1
Original line number Diff line number Diff line
@@ -77,7 +77,8 @@ class CustomField(TestCase):
                "12",
                "23",
            ],
            lambda m: str(m.data)
            lambda m: str(m.data),
            ordered=False
        )

    def test_field_subclassing(self):
Loading