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

Code before:
            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.order_by("id").count(), len(qs.order_by("id")))
        self.assertEqual(qs.extra(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.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"),
                    }

Review comment (suggestion):
I was about to change this to assertAlmostEqual just out of paranoia about the decimal value, when I realized I think the other test is sufficient. I think Simon suggested something along those lines. I'll remove it.

Code after:
            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.order_by("id").count(), len(qs.order_by("id")))
        self.assertEqual(qs.extra(order_by=["id"]).count(), len(qs.order_by("id")))

    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
        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