0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Add tests for passing dict list_filter and filterset_class

This commit is contained in:
Sage Abdullah 2023-08-22 12:22:52 +01:00
parent 1fa124fe82
commit fb9abace01
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
4 changed files with 162 additions and 78 deletions

View File

@ -157,11 +157,21 @@ class TestCustomColumns(WagtailTestUtils, TestCase):
class TestListFilter(WagtailTestUtils, TestCase):
cases = {
"list": ("feature_complete_toy", "release_date", "Release date"),
"dict": ("fctoy_alt1", "name__icontains", "Name contains"),
"filterset_class": (
"fctoy-alt2",
"release_date__year__lte",
"Release date year is less than or equal to",
),
}
def setUp(self):
self.user = self.login()
def get(self, params=None):
return self.client.get(reverse("feature_complete_toy:index"), params)
def get(self, url_namespace, params=None):
return self.client.get(reverse(f"{url_namespace}:index"), params)
@classmethod
def setUpTestData(cls):
@ -176,84 +186,123 @@ class TestListFilter(WagtailTestUtils, TestCase):
def test_unfiltered_no_results(self):
FeatureCompleteToy.objects.all().delete()
response = self.get()
self.assertContains(response, "There are no feature complete toys to display")
self.assertContains(
response,
'<label class="w-field__label" for="id_release_date" id="id_release_date-label">Release date</label>',
html=True,
)
self.assertContains(
response,
'<input type="text" name="release_date" autocomplete="off" id="id_release_date">',
html=True,
)
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
for case, (url_namespace, lookup, label_text) in self.cases.items():
with self.subTest(case=case):
response = self.get(url_namespace)
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(
response,
"There are no feature complete toys to display",
)
self.assertNotContains(
response,
"No feature complete toys match your query",
)
self.assertNotContains(response, "Buzz Lightyear")
self.assertNotContains(response, "Forky")
soup = self.get_soup(response.content)
label = soup.select_one(f"label#id_{lookup}-label")
self.assertIsNotNone(label)
self.assertEqual(label.text.strip(), label_text)
input = soup.select_one(f"input#id_{lookup}")
self.assertIsNotNone(input)
def test_unfiltered_with_results(self):
response = self.get()
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(response, "Buzz Lightyear")
self.assertContains(response, "Forky")
self.assertNotContains(response, "There are 2 matches")
self.assertContains(
response,
'<label class="w-field__label" for="id_release_date" id="id_release_date-label">Release date</label>',
html=True,
)
self.assertContains(
response,
'<input type="text" name="release_date" autocomplete="off" id="id_release_date">',
html=True,
)
for case, (url_namespace, lookup, label_text) in self.cases.items():
with self.subTest(case=case):
response = self.get(url_namespace)
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(response, "Buzz Lightyear")
self.assertContains(response, "Forky")
self.assertNotContains(response, "There are 2 matches")
self.assertNotContains(
response,
"There are no feature complete toys to display",
)
self.assertNotContains(
response,
"No feature complete toys match your query",
)
soup = self.get_soup(response.content)
label = soup.select_one(f"label#id_{lookup}-label")
self.assertIsNotNone(label)
self.assertEqual(label.text.strip(), label_text)
input = soup.select_one(f"input#id_{lookup}")
self.assertIsNotNone(input)
def test_empty_filter_with_results(self):
response = self.get({"release_date": ""})
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(response, "Buzz Lightyear")
self.assertContains(response, "Forky")
self.assertNotContains(response, "There are 2 matches")
self.assertContains(
response,
'<label class="w-field__label" for="id_release_date" id="id_release_date-label">Release date</label>',
html=True,
)
self.assertContains(
response,
'<input type="text" name="release_date" value="" autocomplete="off" id="id_release_date">',
html=True,
)
for case, (url_namespace, lookup, label_text) in self.cases.items():
with self.subTest(case=case):
response = self.get(url_namespace, {lookup: ""})
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(response, "Buzz Lightyear")
self.assertContains(response, "Forky")
self.assertNotContains(response, "There are 2 matches")
self.assertNotContains(
response,
"No feature complete toys match your query",
)
soup = self.get_soup(response.content)
label = soup.select_one(f"label#id_{lookup}-label")
self.assertIsNotNone(label)
self.assertEqual(label.text.strip(), label_text)
input = soup.select_one(f"input#id_{lookup}")
self.assertIsNotNone(input)
self.assertFalse(input.attrs.get("value"))
def test_filtered_no_results(self):
response = self.get({"release_date": "1970-01-01"})
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(
response,
"No feature complete toys match your query",
)
self.assertContains(
response,
'<label class="w-field__label" for="id_release_date" id="id_release_date-label">Release date</label>',
html=True,
)
self.assertContains(
response,
'<input type="text" name="release_date" value="1970-01-01" autocomplete="off" id="id_release_date">',
html=True,
)
lookup_values = {
"release_date": "1999-09-09",
"name__icontains": "Woody",
"release_date__year__lte": "1990",
}
for case, (url_namespace, lookup, label_text) in self.cases.items():
with self.subTest(case=case):
value = lookup_values[lookup]
response = self.get(url_namespace, {lookup: value})
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(
response,
"No feature complete toys match your query",
)
self.assertNotContains(response, "Buzz Lightyear")
self.assertNotContains(response, "Forky")
self.assertNotContains(response, "There are 2 matches")
soup = self.get_soup(response.content)
label = soup.select_one(f"label#id_{lookup}-label")
self.assertIsNotNone(label)
self.assertEqual(label.text.strip(), label_text)
input = soup.select_one(f"input#id_{lookup}")
self.assertIsNotNone(input)
self.assertEqual(input.attrs.get("value"), value)
def test_filtered_with_results(self):
response = self.get({"release_date": "1995-11-19"})
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(response, "Buzz Lightyear")
self.assertContains(response, "There is 1 match")
self.assertContains(
response,
'<label class="w-field__label" for="id_release_date" id="id_release_date-label">Release date</label>',
html=True,
)
self.assertContains(
response,
'<input type="text" name="release_date" value="1995-11-19" autocomplete="off" id="id_release_date">',
html=True,
)
lookup_values = {
"release_date": "1995-11-19",
"name__icontains": "Ightyear",
"release_date__year__lte": "2017",
}
for case, (url_namespace, lookup, label_text) in self.cases.items():
with self.subTest(case=case):
value = lookup_values[lookup]
response = self.get(url_namespace, {lookup: value})
self.assertTemplateUsed(response, "wagtailadmin/shared/filters.html")
self.assertContains(response, "Buzz Lightyear")
self.assertContains(response, "There is 1 match")
self.assertNotContains(response, "Forky")
self.assertNotContains(
response,
"No feature complete toys match your query",
)
soup = self.get_soup(response.content)
label = soup.select_one(f"label#id_{lookup}-label")
self.assertIsNotNone(label)
self.assertEqual(label.text.strip(), label_text)
input = soup.select_one(f"input#id_{lookup}")
self.assertIsNotNone(input)
self.assertEqual(input.attrs.get("value"), value)

View File

@ -2149,3 +2149,6 @@ class FeatureCompleteToy(models.Model):
if (lowered := self.name.lower()) == lowered[::-1]:
return None
return False
def __str__(self):
return f"{self.name} ({self.release_date})"

View File

@ -10,6 +10,7 @@ from django.utils.translation import gettext_lazy
from wagtail.admin import messages
from wagtail.admin.auth import user_passes_test
from wagtail.admin.filters import WagtailFilterSet
from wagtail.admin.ui.tables import BooleanColumn, UpdatedAtColumn
from wagtail.admin.views.generic import DeleteView, EditView, IndexView
from wagtail.admin.viewsets.base import ViewSet, ViewSetGroup
@ -201,8 +202,39 @@ class FeatureCompleteToyViewSet(ModelViewSet):
menu_label = "Feature Complete Toys"
icon = "media"
exclude_form_fields = ()
add_to_admin_menu = True
template_prefix = "customprefix/"
index_template_name = "tests/fctoy_index.html"
list_display = ["name", BooleanColumn("is_cool"), UpdatedAtColumn()]
list_filter = ["name", "release_date"]
class FCToyAlt1ViewSet(ModelViewSet):
model = FeatureCompleteToy
icon = "media"
list_filter = {"name": ["icontains"]}
form_fields = ["name"]
menu_label = "FC Toys Alt 1"
class FCToyCustomFilterSet(WagtailFilterSet):
class Meta:
model = FeatureCompleteToy
fields = {"release_date": ["year__lte"]}
class ToyViewSetGroup(ModelViewSetGroup):
menu_label = "Toys"
menu_icon = "media"
items = (
FeatureCompleteToyViewSet,
FCToyAlt1ViewSet(name="fctoy_alt1"),
ModelViewSet(
name="fctoy-alt2",
menu_label="FC Toys Alt 2",
model=FeatureCompleteToy,
icon="media",
filterset_class=FCToyCustomFilterSet,
exclude_form_fields=(),
),
)

View File

@ -31,9 +31,9 @@ from wagtail.test.testapp.models import (
VariousOnDeleteModel,
)
from wagtail.test.testapp.views import (
FeatureCompleteToyViewSet,
JSONModelViewSetGroup,
MiscellaneousViewSetGroup,
ToyViewSetGroup,
)
from .forms import FavouriteColourForm
@ -259,7 +259,7 @@ def register_json_model_viewsets():
@hooks.register("register_admin_viewset")
def register_toy_viewset():
return FeatureCompleteToyViewSet()
return ToyViewSetGroup()
class FullFeaturedSnippetFilterSet(WagtailFilterSet):