From 8c6a8befd973774d0064c78a82df59e73dbd49eb Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Tue, 15 Aug 2023 14:54:11 +0100 Subject: [PATCH] Add tests for registering ModelViewSetGroup --- .../tests/viewsets/test_model_viewset.py | 36 +++++++++++++++++ wagtail/test/testapp/views.py | 40 ++++++++++++++++++- wagtail/test/testapp/wagtail_hooks.py | 7 +++- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 wagtail/admin/tests/viewsets/test_model_viewset.py diff --git a/wagtail/admin/tests/viewsets/test_model_viewset.py b/wagtail/admin/tests/viewsets/test_model_viewset.py new file mode 100644 index 0000000000..237047f199 --- /dev/null +++ b/wagtail/admin/tests/viewsets/test_model_viewset.py @@ -0,0 +1,36 @@ +from django.test import TestCase +from django.urls import reverse + +from wagtail.test.utils.wagtail_tests import WagtailTestUtils + + +class TestModelViewSetGroup(WagtailTestUtils, TestCase): + def setUp(self): + self.user = self.login() + + def test_menu_items(self): + response = self.client.get(reverse("wagtailadmin_home")) + self.assertEqual(response.status_code, 200) + # Menu label falls back to the title-cased app label + self.assertContains( + response, + '"name": "tests", "label": "Tests", "icon_name": "folder-open-inverse"', + ) + # Title-cased from verbose_name_plural + self.assertContains(response, "Json Stream Models") + self.assertContains(response, reverse("streammodel:index")) + self.assertEqual(reverse("streammodel:index"), "/admin/streammodel/") + # Set on class + self.assertContains(response, "JSON MinMaxCount StreamModel") + self.assertContains(response, reverse("minmaxcount_streammodel:index")) + self.assertEqual( + reverse("minmaxcount_streammodel:index"), + "/admin/minmaxcount-streammodel/", + ) + # Set on instance + self.assertContains(response, "JSON BlockCounts StreamModel") + self.assertContains(response, reverse("blockcounts_streammodel:index")) + self.assertEqual( + reverse("blockcounts_streammodel:index"), + "/admin/blockcounts/streammodel/", + ) diff --git a/wagtail/test/testapp/views.py b/wagtail/test/testapp/views.py index 36ccc03574..e36332c76b 100644 --- a/wagtail/test/testapp/views.py +++ b/wagtail/test/testapp/views.py @@ -12,8 +12,14 @@ from wagtail.admin import messages from wagtail.admin.auth import user_passes_test from wagtail.admin.views.generic import DeleteView, EditView, IndexView from wagtail.admin.viewsets.base import ViewSet, ViewSetGroup +from wagtail.admin.viewsets.model import ModelViewSet, ModelViewSetGroup from wagtail.contrib.forms.views import SubmissionsListView -from wagtail.test.testapp.models import ModelWithStringTypePrimaryKey +from wagtail.test.testapp.models import ( + JSONBlockCountsStreamModel, + JSONMinMaxCountStreamModel, + JSONStreamModel, + ModelWithStringTypePrimaryKey, +) def user_is_called_bob(user): @@ -152,3 +158,35 @@ class GreetingsViewSet(ViewSet): class MiscellaneousViewSetGroup(ViewSetGroup): items = (CalendarViewSet, GreetingsViewSet) menu_label = "Miscellaneous" + + +class JSONStreamModelViewSet(ModelViewSet): + name = "streammodel" + model = JSONStreamModel + exclude_form_fields = [] + icon = "rotate" + + +class JSONMinMaxCountStreamModelViewSet(ModelViewSet): + url_namespace = "minmaxcount_streammodel" + url_prefix = "minmaxcount-streammodel" + model = JSONMinMaxCountStreamModel + form_fields = ("body",) + icon = "reset" + menu_label = "JSON MinMaxCount StreamModel" + + +class JSONModelViewSetGroup(ModelViewSetGroup): + items = ( + JSONStreamModelViewSet, + JSONMinMaxCountStreamModelViewSet, + # Can be an instance instead of class + ModelViewSet( + model=JSONBlockCountsStreamModel, + exclude_form_fields=(), + icon="resubmit", + url_namespace="blockcounts_streammodel", + url_prefix="blockcounts/streammodel", + menu_label="JSON BlockCounts StreamModel", + ), + ) diff --git a/wagtail/test/testapp/wagtail_hooks.py b/wagtail/test/testapp/wagtail_hooks.py index a662fb258c..77f53ea19e 100644 --- a/wagtail/test/testapp/wagtail_hooks.py +++ b/wagtail/test/testapp/wagtail_hooks.py @@ -30,7 +30,7 @@ from wagtail.test.testapp.models import ( RevisableModel, VariousOnDeleteModel, ) -from wagtail.test.testapp.views import MiscellaneousViewSetGroup +from wagtail.test.testapp.views import JSONModelViewSetGroup, MiscellaneousViewSetGroup from .forms import FavouriteColourForm @@ -248,6 +248,11 @@ def register_viewsets(): return MiscellaneousViewSetGroup() +@hooks.register("register_admin_viewset") +def register_json_model_viewsets(): + return JSONModelViewSetGroup() + + class FullFeaturedSnippetFilterSet(WagtailFilterSet): class Meta: model = FullFeaturedSnippet