ソースを参照

Add tests for custom columns on snippets listing view

Sage Abdullah 2 年 前
コミット
c90dfc6fef

+ 36 - 0
wagtail/snippets/tests/test_snippets.py

@@ -598,6 +598,42 @@ class TestSnippetListViewWithFilterSet(TestCase, WagtailTestUtils):
         )
 
 
+class TestListViewWithCustomColumns(TestCase, WagtailTestUtils):
+    def setUp(self):
+        self.login()
+
+    @classmethod
+    def setUpTestData(cls):
+        FilterableSnippet.objects.create(text="From Indonesia", country_code="ID")
+        FilterableSnippet.objects.create(text="From the UK", country_code="UK")
+
+    def get(self, params={}):
+        return self.client.get(
+            reverse("wagtailsnippets_snippetstests_filterablesnippet:list"),
+            params,
+        )
+
+    def test_custom_columns(self):
+        response = self.get()
+        self.assertContains(response, "Text")
+        self.assertContains(response, "Country Code")
+        self.assertContains(response, "Custom Foo Column")
+        self.assertContains(response, "Updated")
+
+        self.assertContains(response, "Foo UK")
+
+        list_url = reverse("wagtailsnippets_snippetstests_filterablesnippet:list")
+        sort_country_code_url = list_url + "?ordering=country_code"
+
+        # One from the country code column, another from the custom foo column
+        self.assertContains(response, sort_country_code_url, count=2)
+
+        html = response.content.decode()
+
+        # The bulk actions column plus 4 columns defined in FilterableSnippetViewSet
+        self.assertTagInHTML("<th>", html, count=5, allow_extra_attrs=True)
+
+
 class TestSnippetCreateView(TestCase, WagtailTestUtils):
     def setUp(self):
         self.user = self.login()

+ 6 - 0
wagtail/test/snippets/models.py

@@ -80,6 +80,12 @@ class FilterableSnippet(index.Indexed, models.Model):
     def __str__(self):
         return self.text
 
+    def get_foo_country_code(self):
+        return f"Foo {self.country_code}"
+
+    get_foo_country_code.admin_order_field = "country_code"
+    get_foo_country_code.short_description = "Custom foo column"
+
 
 class FilterableSnippetFilterSet(WagtailFilterSet):
     class Meta:

+ 2 - 0
wagtail/test/snippets/views.py

@@ -1,6 +1,8 @@
+from wagtail.admin.ui.tables import UpdatedAtColumn
 from wagtail.snippets.views.snippets import SnippetViewSet
 from wagtail.test.snippets.models import FilterableSnippetFilterSet
 
 
 class FilterableSnippetViewSet(SnippetViewSet):
     filterset_class = FilterableSnippetFilterSet
+    list_display = ["text", "country_code", "get_foo_country_code", UpdatedAtColumn()]