Repository: wagtail/wagtail
File: wagtail/snippets/tests/test_viewset.py (Python)

Code before:
        with self.assertRaises(ImproperlyConfigured) as cm:
            register_snippet(SnippetViewSet)
        message = str(cm.exception)
        self.assertIn("ModelViewSet", message)
        self.assertIn(
            "must define a `model` attribute or pass a `model` argument",
            message,
        )


class TestIncorrectConverter(SimpleTestCase):
    def test_unknown_converter(self):
        class BadViewSet(SnippetViewSet):
            model = AdvertTag
            url_converter = "foo"

        with self.assertRaises(ImproperlyConfigured) as cm:
            register_snippet(BadViewSet)
        message = str(cm.exception)
        self.assertEqual(
            "BadViewSet is using an unknown URL converter",
            message,
        )


class TestSnippetConverter(TestCase):
    def setUp(self):
        super().setUp()
        self.viewset = AdvertWithCustomUUIDPrimaryKey.snippet_viewset

    def test_cannot_reverse(self):
        edit_url = f"{self.viewset.url_namespace}:edit"
        with self.assertRaises(NoReverseMatch):
            reverse(edit_url, kwargs={"pk": 123})

    def test_404_on_pk_not_uuids(self):
        response = self.client.get(f"{self.viewset.url_prefix}/edit/123/")
        self.assertEqual(response.status_code, 404)


class BaseSnippetViewSetTests(WagtailTestUtils, TestCase):
    model = None

    def setUp(self):
        self.user = self.login()

    def get_url(self, url_name, args=()):
        return reverse(self.model.snippet_viewset.get_url_name(url_name), args=args)


class TestCustomIcon(BaseSnippetViewSetTests):

Review comment (suggestion):
Can probably merge this with `TestAdminURLs` a few lines below. We'll also need the same tests for `ModelViewSet`

Code after:
        with self.assertRaises(ImproperlyConfigured) as cm:
            register_snippet(SnippetViewSet)
        message = str(cm.exception)
        self.assertIn("ModelViewSet", message)
        self.assertIn(
            "must define a `model` attribute or pass a `model` argument",
            message,
        )


class TestIncorrectConverter(SimpleTestCase):
    def test_unknown_converter(self):
        class BadViewSet(SnippetViewSet):
            model = AdvertTag
            pk_path_converter = "foo"

        with self.assertRaises(ImproperlyConfigured) as cm:
            register_snippet(BadViewSet)
        message = str(cm.exception)
        self.assertEqual(
            "BadViewSet.pk_path_converter is not a registered path converter",
            message,
        )


class BaseSnippetViewSetTests(WagtailTestUtils, TestCase):
    model = None

    def setUp(self):
        self.user = self.login()

    def get_url(self, url_name, args=()):
        return reverse(self.model.snippet_viewset.get_url_name(url_name), args=args)


class TestCustomIcon(BaseSnippetViewSetTests):
    model = FullFeaturedSnippet

    def setUp(self):
        super().setUp()
        self.object = self.model.objects.create(text="test snippet with custom icon")
        self.revision_1 = self.object.save_revision()
        self.revision_1.publish()
        self.object.text = "test snippet with custom icon (updated)"
        self.revision_2 = self.object.save_revision()

    def test_get_views(self):
        pk = quote(self.object.pk)
        views = [
            ("list", [], "headers/slim_header.html"),
            ("add", [], "headers/slim_header.html"),