diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-08-01 16:16:28 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-01 16:16:28 +0200 |
| commit | c9b9a52edc66be117c6e5b5214fa788a4d5db7a8 (patch) | |
| tree | 86bededa12bf9d2d6c441c0fe4fc1a35b0af796b /tests/aggregation/tests.py | |
| parent | 5a3725594faacc412e2d2b4ed160370228f1a118 (diff) | |
Fixed #34750 -- Fixed QuerySet.count() when grouping by unused multi-valued annotations.
Thanks Toan Vuong for the report.
Thanks Simon Charette for the review.
Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7.
Diffstat (limited to 'tests/aggregation/tests.py')
| -rw-r--r-- | tests/aggregation/tests.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index 9f2a7c8841..ad00afdcc1 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -2165,6 +2165,47 @@ class AggregateAnnotationPruningTests(TestCase): self.assertEqual(sql.count("select"), 2, "Subquery wrapping required") self.assertNotIn("authors_count", sql) + def test_unused_aliased_aggregate_and_annotation_reverse_fk(self): + Book.objects.create( + name="b3", + publisher=self.p2, + pages=1000, + rating=4.2, + price=50, + contact=self.a2, + pubdate=datetime.date.today(), + ) + qs = Publisher.objects.annotate( + total_pages=Sum("book__pages"), + good_book=Case( + When(book__rating__gt=4.0, then=Value(True)), + default=Value(False), + ), + ) + self.assertEqual(qs.count(), 3) + + def test_unused_aliased_aggregate_and_annotation_reverse_fk_grouped(self): + Book.objects.create( + name="b3", + publisher=self.p2, + pages=1000, + rating=4.2, + price=50, + contact=self.a2, + pubdate=datetime.date.today(), + ) + qs = ( + Publisher.objects.values("id", "name") + .annotate(total_pages=Sum("book__pages")) + .annotate( + good_book=Case( + When(book__rating__gt=4.0, then=Value(True)), + default=Value(False), + ) + ) + ) + self.assertEqual(qs.count(), 3) + def test_non_aggregate_annotation_pruned(self): with CaptureQueriesContext(connection) as ctx: Book.objects.annotate( |
