Repository: django/django
File: tests/aggregation_regress/tests.py (Python)

Code before:
        Book.objects.create(
            isbn="113235613",
            name=self.b4.name,
            pages=self.b4.pages,
            rating=4.0,
            price=Decimal("39.69"),
            contact=self.a5,
            publisher=self.p3,
            pubdate=datetime.date(2018, 11, 3),
        )
        qs = Book.objects.values("contact__name", "publisher__name").annotate(
            publications=Count("id")
        )
        self.assertEqual(qs.count(), 6)
        self.assertEqual(
            qs.aggregate(Avg("rating"))["rating__avg"], (12.5 + 3 * 4.0) / 6
        )
        # before ticket 26434 had been solved .count() was returning also 6
        self.assertEqual(qs.order_by("id").count(), 7)
        # before ticket 26434 had been solved .aggregate(Avg(...))
        # was returning also (12.5 + 3 * 4.0) / 6)
        self.assertEqual(
            qs.order_by("id").aggregate(Avg("rating"))["rating__avg"],
            (12.5 + 4 * 4.0) / 7,
        )

    def test_annotation_with_value(self):
        values = (
            Book.objects.filter(
                name="Practical Django Projects",
            )
            .annotate(
                discount_price=F("price") * 2,
            )
            .values(
                "discount_price",
            )
            .annotate(sum_discount=Sum("discount_price"))
        )
        with self.assertNumQueries(1) as ctx:
            self.assertSequenceEqual(
                values,
                [
                    {
                        "discount_price": Decimal("59.38"),
                        "sum_discount": Decimal("59.38"),
                    }
                ],
            )
        if connection.features.allows_group_by_select_index:
            self.assertIn("GROUP BY 1", ctx[0]["sql"])

Review comment (suggestion):
Similar story here and you can drop the ticket reference as it's _baked_ into the commit message and easily referenceable through blame.

```suggestion
        self.assertEqual(
            qs.order_by("id").aggregate(Avg("rating"))["rating__avg"],
            (12.5 + 4 * 4.0) / 7,
        )

```

Code after:
                discount_price=F("price") * 2,
            )
            .values(
                "discount_price",
            )
            .annotate(sum_discount=Sum("discount_price"))
        )
        with self.assertNumQueries(1) as ctx:
            self.assertSequenceEqual(
                values,
                [
                    {
                        "discount_price": Decimal("59.38"),
                        "sum_discount": Decimal("59.38"),
                    }
                ],
            )
        if connection.features.allows_group_by_select_index:
            self.assertIn("GROUP BY 1", ctx[0]["sql"])

    def test_aggregates_in_where_clause(self):
        """
        Regression test for #12822: DatabaseError: aggregates not allowed in
        WHERE clause

        The subselect works and returns results equivalent to a
        query with the IDs listed.

        Before the corresponding fix for this bug, this test passed in 1.1 and
        failed in 1.2-beta (trunk).
        """
        qs = Book.objects.values("contact").annotate(Max("id"))
        qs = qs.order_by("contact").values_list("id__max", flat=True)
        # don't do anything with the queryset (qs) before including it as a
        # subquery
        books = Book.objects.order_by("id")
        qs1 = books.filter(id__in=qs)
        qs2 = books.filter(id__in=list(qs))
        self.assertEqual(list(qs1), list(qs2))

    def test_aggregates_in_where_clause_pre_eval(self):
        """
        Regression test for #12822: DatabaseError: aggregates not allowed in
        WHERE clause

        Same as the above test, but evaluates the queryset for the subquery
        before it's used as a subquery.

        Before the corresponding fix for this bug, this test failed in both
        1.1 and 1.2-beta (trunk).
        """