Refs #31235 -- Made assertQuerysetEqual() not call repr() on a queryset when compared to string values.
Per deprecation timeline.
This commit is contained in:
parent
75d6c4ae6d
commit
e2be307b3a
|
@ -6,7 +6,6 @@ import posixpath
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from copy import copy, deepcopy
|
from copy import copy, deepcopy
|
||||||
|
@ -42,7 +41,6 @@ from django.test.utils import (
|
||||||
CaptureQueriesContext, ContextList, compare_xml, modify_settings,
|
CaptureQueriesContext, ContextList, compare_xml, modify_settings,
|
||||||
override_settings,
|
override_settings,
|
||||||
)
|
)
|
||||||
from django.utils.deprecation import RemovedInDjango41Warning
|
|
||||||
from django.utils.functional import classproperty
|
from django.utils.functional import classproperty
|
||||||
from django.utils.version import PY310
|
from django.utils.version import PY310
|
||||||
from django.views.static import serve
|
from django.views.static import serve
|
||||||
|
@ -1059,23 +1057,6 @@ class TransactionTestCase(SimpleTestCase):
|
||||||
|
|
||||||
def assertQuerysetEqual(self, qs, values, transform=None, ordered=True, msg=None):
|
def assertQuerysetEqual(self, qs, values, transform=None, ordered=True, msg=None):
|
||||||
values = list(values)
|
values = list(values)
|
||||||
# RemovedInDjango41Warning.
|
|
||||||
if transform is None:
|
|
||||||
if (
|
|
||||||
values and isinstance(values[0], str) and
|
|
||||||
qs and not isinstance(qs[0], str)
|
|
||||||
):
|
|
||||||
# Transform qs using repr() if the first element of values is a
|
|
||||||
# string and the first element of qs is not (which would be the
|
|
||||||
# case if qs is a flattened values_list).
|
|
||||||
warnings.warn(
|
|
||||||
"In Django 4.1, repr() will not be called automatically "
|
|
||||||
"on a queryset when compared to string values. Set an "
|
|
||||||
"explicit 'transform' to silence this warning.",
|
|
||||||
category=RemovedInDjango41Warning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
transform = repr
|
|
||||||
items = qs
|
items = qs
|
||||||
if transform is not None:
|
if transform is not None:
|
||||||
items = map(transform, items)
|
items = map(transform, items)
|
||||||
|
|
|
@ -257,3 +257,6 @@ to remove usage of these features.
|
||||||
``django.core.validators.EmailValidator`` are removed.
|
``django.core.validators.EmailValidator`` are removed.
|
||||||
|
|
||||||
* The ``default_app_config`` application configuration variable is removed.
|
* The ``default_app_config`` application configuration variable is removed.
|
||||||
|
|
||||||
|
* ``TransactionTestCase.assertQuerysetEqual()`` no longer calls ``repr()`` on a
|
||||||
|
queryset when compared to string values.
|
||||||
|
|
|
@ -1700,13 +1700,6 @@ 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.
|
||||||
|
|
||||||
.. deprecated:: 3.2
|
|
||||||
|
|
||||||
If ``transform`` is not provided and ``values`` is a list of strings,
|
|
||||||
it's compared to a list produced by applying ``repr()`` to each member
|
|
||||||
of ``qs``. This behavior is deprecated and will be removed in Django
|
|
||||||
4.1. If you need it, explicitly set ``transform`` to ``repr``.
|
|
||||||
|
|
||||||
.. 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
|
||||||
|
|
|
@ -17,8 +17,8 @@ from django.forms import EmailField, IntegerField
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.test import (
|
from django.test import (
|
||||||
SimpleTestCase, TestCase, TransactionTestCase, ignore_warnings,
|
SimpleTestCase, TestCase, TransactionTestCase, skipIfDBFeature,
|
||||||
skipIfDBFeature, skipUnlessDBFeature,
|
skipUnlessDBFeature,
|
||||||
)
|
)
|
||||||
from django.test.html import HTMLParseError, parse_html
|
from django.test.html import HTMLParseError, parse_html
|
||||||
from django.test.utils import (
|
from django.test.utils import (
|
||||||
|
@ -26,7 +26,6 @@ from django.test.utils import (
|
||||||
override_settings, setup_test_environment,
|
override_settings, setup_test_environment,
|
||||||
)
|
)
|
||||||
from django.urls import NoReverseMatch, path, reverse, reverse_lazy
|
from django.urls import NoReverseMatch, path, reverse, reverse_lazy
|
||||||
from django.utils.deprecation import RemovedInDjango41Warning
|
|
||||||
from django.utils.log import DEFAULT_LOGGING
|
from django.utils.log import DEFAULT_LOGGING
|
||||||
|
|
||||||
from .models import Car, Person, PossessedCar
|
from .models import Car, Person, PossessedCar
|
||||||
|
@ -362,32 +361,6 @@ class AssertQuerysetEqualTests(TestCase):
|
||||||
self.assertIn(name, exception_msg)
|
self.assertIn(name, exception_msg)
|
||||||
|
|
||||||
|
|
||||||
class AssertQuerysetEqualDeprecationTests(TestCase):
|
|
||||||
@classmethod
|
|
||||||
def setUpTestData(cls):
|
|
||||||
cls.p1 = Person.objects.create(name='p1')
|
|
||||||
cls.p2 = Person.objects.create(name='p2')
|
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango41Warning)
|
|
||||||
def test_str_values(self):
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
Person.objects.all().order_by('name'),
|
|
||||||
[repr(self.p1), repr(self.p2)],
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_str_values_warning(self):
|
|
||||||
msg = (
|
|
||||||
"In Django 4.1, repr() will not be called automatically on a "
|
|
||||||
"queryset when compared to string values. Set an explicit "
|
|
||||||
"'transform' to silence this warning."
|
|
||||||
)
|
|
||||||
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
Person.objects.all().order_by('name'),
|
|
||||||
[repr(self.p1), repr(self.p2)],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='test_utils.urls')
|
@override_settings(ROOT_URLCONF='test_utils.urls')
|
||||||
class CaptureQueriesContextManagerTests(TestCase):
|
class CaptureQueriesContextManagerTests(TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue