Repository: wagtail/wagtail
File: wagtail/documents/tests/test_admin_views.py (Python)

Code before:
        )
        link = soup.select_one(f"a[href='{expected_url}']")
        self.assertIsNotNone(link)
        self.assertEqual(link.text.strip(), "Used 0 times")

    def test_order_by_usage_count(self):
        doc1 = models.Document.objects.create(title="Used twice document")
        doc2 = models.Document.objects.create(title="Used once document")
        with self.captureOnCommitCallbacks(execute=True):
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc2)

        cases = {
            "usage_count": [doc2, doc1],
            "-usage_count": [doc1, doc2],
        }
        for ordering, expected_order in cases.items():
            with self.subTest(ordering=ordering):
                response = self.client.get(
                    reverse("wagtaildocs:index"),
                    {"ordering": ordering},
                )
                self.assertEqual(response.status_code, 200)
                context = response.context
                self.assertSequenceEqual(
                    context["page_obj"].object_list,
                    expected_order,
                )

    def test_filter_by_usage_count(self):
        doc1 = models.Document.objects.create(title="Used twice document")
        doc2 = models.Document.objects.create(title="Used once document")
        doc3 = models.Document.objects.create(title="Unused document")
        with self.captureOnCommitCallbacks(execute=True):
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc2)

        response = self.client.get(
            reverse("wagtaildocs:index"),
            {"usage_count_min": "1"},
        )
        self.assertEqual(response.status_code, 200)
        self.assertCountEqual(
            response.context["page_obj"].object_list,
            [doc1, doc2],
        )

        response = self.client.get(
            reverse("wagtaildocs:index"),

Review comment (bug):
I'm not sure how useful adding `assertNumQueries` here is... That's the only test I can think of that would fail before/pass after the fix 🤔 

(Well, technically this one actually would pass before and after the fix, but the `assertNumQueries(12)` in the non-ordering case would fail because it previously didn't have that extra separate query.)

Code after:
        expected_url = reverse(
            "wagtaildocs:document_usage",
            args=(unused_document.pk,),
        )
        link = soup.select_one(f"a[href='{expected_url}']")
        self.assertIsNotNone(link)
        self.assertEqual(link.text.strip(), "Used 0 times")

    def test_order_by_usage_count(self):
        doc1 = models.Document.objects.create(title="Used twice document")
        doc2 = models.Document.objects.create(title="Used once document")
        with self.captureOnCommitCallbacks(execute=True):
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc2)

        cases = {
            "usage_count": [doc2, doc1],
            "-usage_count": [doc1, doc2],
        }
        for ordering, expected_order in cases.items():
            response = self.client.get(
                reverse("wagtaildocs:index"),
                {"ordering": ordering},
            )
            with self.subTest(ordering=ordering), self.assertNumQueries(11):
                response = self.client.get(
                    reverse("wagtaildocs:index"),
                    {"ordering": ordering},
                )
                self.assertEqual(response.status_code, 200)
                context = response.context
                self.assertSequenceEqual(
                    context["page_obj"].object_list,
                    expected_order,
                )

    def test_filter_by_usage_count(self):
        doc1 = models.Document.objects.create(title="Used twice document")
        doc2 = models.Document.objects.create(title="Used once document")
        doc3 = models.Document.objects.create(title="Unused document")
        with self.captureOnCommitCallbacks(execute=True):
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc1)
            VariousOnDeleteModel.objects.create(protected_document=doc2)

        response = self.client.get(
            reverse("wagtaildocs:index"),
            {"usage_count_min": "1"},
        )
        self.assertEqual(response.status_code, 200)