diff options
| author | Simon Charette <charette.s@gmail.com> | 2021-04-24 01:07:18 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-04-28 20:27:42 +0200 |
| commit | d5add5d3a26f98e961dfbcad67bb04d936f2f332 (patch) | |
| tree | 8932d754cda985f7b46e3ead22fdb867a5c974fa /tests | |
| parent | 55cb3c8ac186f7664b38d66fb4c7e678fe524741 (diff) | |
[3.2.x] Fixed #32632, Fixed #32657 -- Removed flawed support for Subquery deconstruction.
Subquery deconstruction support required implementing complex and
expensive equality rules for sql.Query objects for little benefit as
the latter cannot themselves be made deconstructible to their reference
to model classes.
Making Expression @deconstructible and not BaseExpression allows
interested parties to conform to the "expression" API even if they are
not deconstructible as it's only a requirement for expressions allowed
in Model fields and meta options (e.g. constraints, indexes).
Thanks Phillip Cutter for the report.
This also fixes a performance regression in bbf141bcdc31f1324048af9233583a523ac54c94.
Backport of c8b659430556dca0b2fe27cf2ea0f8290dbafecd from main
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/queries/test_q.py | 19 | ||||
| -rw-r--r-- | tests/queries/test_query.py | 28 |
2 files changed, 15 insertions, 32 deletions
diff --git a/tests/queries/test_q.py b/tests/queries/test_q.py index 24a705f07f..d75751be63 100644 --- a/tests/queries/test_q.py +++ b/tests/queries/test_q.py @@ -1,4 +1,5 @@ -from django.db.models import Exists, F, OuterRef, Q +from django.db.models import BooleanField, Exists, F, OuterRef, Q +from django.db.models.expressions import RawSQL from django.test import SimpleTestCase from .models import Tag @@ -37,6 +38,16 @@ class QTests(SimpleTestCase): with self.assertRaisesMessage(TypeError, str(obj)): q & obj + def test_combine_negated_boolean_expression(self): + tagged = Tag.objects.filter(category=OuterRef('pk')) + tests = [ + Q() & ~Exists(tagged), + Q() | ~Exists(tagged), + ] + for q in tests: + with self.subTest(q=q): + self.assertIs(q.negated, True) + def test_deconstruct(self): q = Q(price__gt=F('discounted_price')) path, args, kwargs = q.deconstruct() @@ -88,10 +99,10 @@ class QTests(SimpleTestCase): self.assertEqual(kwargs, {}) def test_deconstruct_boolean_expression(self): - tagged = Tag.objects.filter(category=OuterRef('pk')) - q = Q(Exists(tagged)) + expr = RawSQL('1 = 1', BooleanField()) + q = Q(expr) _, args, kwargs = q.deconstruct() - self.assertEqual(args, (Exists(tagged),)) + self.assertEqual(args, (expr,)) self.assertEqual(kwargs, {}) def test_reconstruct(self): diff --git a/tests/queries/test_query.py b/tests/queries/test_query.py index 5db9d96163..523fa607f0 100644 --- a/tests/queries/test_query.py +++ b/tests/queries/test_query.py @@ -150,31 +150,3 @@ class TestQuery(SimpleTestCase): msg = 'Cannot filter against a non-conditional expression.' with self.assertRaisesMessage(TypeError, msg): query.build_where(Func(output_field=CharField())) - - def test_equality(self): - self.assertNotEqual( - Author.objects.all().query, - Author.objects.filter(item__name='foo').query, - ) - self.assertEqual( - Author.objects.filter(item__name='foo').query, - Author.objects.filter(item__name='foo').query, - ) - self.assertEqual( - Author.objects.filter(item__name='foo').query, - Author.objects.filter(Q(item__name='foo')).query, - ) - - def test_hash(self): - self.assertNotEqual( - hash(Author.objects.all().query), - hash(Author.objects.filter(item__name='foo').query) - ) - self.assertEqual( - hash(Author.objects.filter(item__name='foo').query), - hash(Author.objects.filter(item__name='foo').query), - ) - self.assertEqual( - hash(Author.objects.filter(item__name='foo').query), - hash(Author.objects.filter(Q(item__name='foo')).query), - ) |
