summaryrefslogtreecommitdiff
path: root/tests/aggregation_regress/tests.py
diff options
context:
space:
mode:
authorVojtech Bocek <vojtech.bocek@avast.com>2019-07-29 16:40:27 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-09-09 14:04:46 +0200
commitb1d37fea8fd7904c2b2d11c91393cf1b989314b6 (patch)
tree09fcbf33ded256df5cb2d82c85e513be5c81c365 /tests/aggregation_regress/tests.py
parent10d5e439e92da3881f5c32151f3a89f264c9cfd8 (diff)
Fixed #28107 -- Added DatabaseFeatures.allows_group_by_selected_pks_on_model() to allow enabling optimization for unmanaged models.
Diffstat (limited to 'tests/aggregation_regress/tests.py')
-rw-r--r--tests/aggregation_regress/tests.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py
index 4142643020..6234d3c590 100644
--- a/tests/aggregation_regress/tests.py
+++ b/tests/aggregation_regress/tests.py
@@ -1333,6 +1333,33 @@ class AggregationTests(TestCase):
self.assertIn(field.name, grouping[index + 1][0])
assertQuerysetResults(queryset)
+ @skipUnlessDBFeature('allows_group_by_selected_pks')
+ def test_aggregate_unmanaged_model_as_tables(self):
+ qs = Book.objects.select_related('contact').annotate(num_authors=Count('authors'))
+ # Force treating unmanaged models as tables.
+ with mock.patch(
+ 'django.db.connection.features.allows_group_by_selected_pks_on_model',
+ return_value=True,
+ ):
+ with mock.patch.object(Book._meta, 'managed', False), \
+ mock.patch.object(Author._meta, 'managed', False):
+ _, _, grouping = qs.query.get_compiler(using='default').pre_sql_setup()
+ self.assertEqual(len(grouping), 2)
+ self.assertIn('id', grouping[0][0])
+ self.assertIn('id', grouping[1][0])
+ self.assertQuerysetEqual(
+ qs.order_by('name'),
+ [
+ ('Artificial Intelligence: A Modern Approach', 2),
+ ('Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp', 1),
+ ('Practical Django Projects', 1),
+ ('Python Web Development with Django', 3),
+ ('Sams Teach Yourself Django in 24 Hours', 1),
+ ('The Definitive Guide to Django: Web Development Done Right', 2),
+ ],
+ attrgetter('name', 'num_authors'),
+ )
+
def test_reverse_join_trimming(self):
qs = Author.objects.annotate(Count('book_contact_set__contact'))
self.assertIn(' JOIN ', str(qs.query))