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

Code before:
        for attr, value in kwargs.items():
            self.assertEqual(getattr(obj, attr), value)

    def test_count_preserve_group_by(self):
        # new release of the same book
        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.count(), len(qs))
        # 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 .count() was not equal to len(qs)
        self.assertEqual(qs.order_by("id").count(), len(qs.order_by("id")))

    def test_aggregate_preserve_group_by(self):
        # new release of the same book
        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,

Review comment (suggestion):
Counts are aggregates, and this patch doesn't introduce specialization for `Count`, so I think just having the prior test with count is sufficient.

Code after:
        for attr, value in kwargs.items():
            self.assertEqual(getattr(obj, attr), value)

    def test_count_preserve_group_by(self):
        # new release of the same book
        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(), Book.objects.count() - 1)
        self.assertEqual(qs.order_by("id").count(), Book.objects.count())
        self.assertEqual(qs.extra(order_by=["id"]).count(), Book.objects.count())

    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"])

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