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

Add tests for registering ModelViewSetGroup

This commit is contained in:
Sage Abdullah 2023-08-15 14:54:11 +01:00
parent cbf2eb5379
commit 8c6a8befd9
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
3 changed files with 81 additions and 2 deletions

View File

@ -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/",
)

View File

@ -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",
),
)

View File

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