diff options
| author | Simon Charette <charette.s@gmail.com> | 2020-08-12 23:16:22 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-08-13 14:10:36 +0200 |
| commit | 51297a92324976a704279b567ec4f80bb92d7b60 (patch) | |
| tree | 4acb395cf9be8778dff721015aeba4cc11654c20 /tests | |
| parent | 7f4c9222dfe2f28ff8a7ffc56c28ccbadf19cf6f (diff) | |
Fixed #31792 -- Made Exists() reuse QuerySet.exists() optimizations.
The latter is already optimized to limit the number of results, avoid
selecting unnecessary fields, and drop ordering if possible without
altering the semantic of the query.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/expressions/tests.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 2f392a51e6..c15204ce33 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -22,7 +22,7 @@ from django.db.models.functions import ( from django.db.models.sql import constants from django.db.models.sql.datastructures import Join from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature -from django.test.utils import Approximate, isolate_apps +from django.test.utils import Approximate, CaptureQueriesContext, isolate_apps from django.utils.functional import SimpleLazyObject from .models import ( @@ -1738,6 +1738,26 @@ class ValueTests(TestCase): Value(object()).output_field +class ExistsTests(TestCase): + def test_optimizations(self): + with CaptureQueriesContext(connection) as context: + list(Experiment.objects.values(exists=Exists( + Experiment.objects.order_by('pk'), + )).order_by()) + captured_queries = context.captured_queries + self.assertEqual(len(captured_queries), 1) + captured_sql = captured_queries[0]['sql'] + self.assertNotIn( + connection.ops.quote_name(Experiment._meta.pk.column), + captured_sql, + ) + self.assertIn( + connection.ops.limit_offset_sql(None, 1), + captured_sql, + ) + self.assertNotIn('ORDER BY', captured_sql) + + class FieldTransformTests(TestCase): @classmethod |
