From d17b0af9f6d3d23b632a15bd7b0dd881b954678b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 10 Jun 2022 16:22:20 +0100 Subject: [PATCH] Add tests for unique body classes --- justfile | 3 +++ website/common/models.py | 5 +++-- website/common/tests.py | 12 ++++++++++++ website/common/utils.py | 6 ++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 website/common/tests.py create mode 100644 website/common/utils.py diff --git a/justfile b/justfile index 990272c..8a58e72 100644 --- a/justfile +++ b/justfile @@ -23,3 +23,6 @@ install: @collectstatic: ./manage.py collectstatic --noinput -v2 + +test *ARGS: + ./manage.py test {{ ARGS }} diff --git a/website/common/models.py b/website/common/models.py index 1244229..86d1057 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -7,6 +7,7 @@ class BasePage(Page): class Meta: abstract = True + @classmethod @property - def body_class(self): - return "page-" + self._meta.db_table.replace("_", "-") + def body_class(cls): + return "page-" + cls._meta.db_table.replace("_", "-") diff --git a/website/common/tests.py b/website/common/tests.py new file mode 100644 index 0000000..121a10b --- /dev/null +++ b/website/common/tests.py @@ -0,0 +1,12 @@ +from django.test import SimpleTestCase +from .utils import get_page_models +from .models import BasePage + +class BasePageTestCase(SimpleTestCase): + def test_unique_body_classes(self): + body_classes = [page.body_class for page in get_page_models()] + self.assertEqual(len(body_classes), len(set(body_classes))) + + def test_pages_inherit_base_page(self): + for page_model in get_page_models(): + self.assertTrue(issubclass(page_model, BasePage), f"{page_model} does not inherit from {BasePage}.") diff --git a/website/common/utils.py b/website/common/utils.py new file mode 100644 index 0000000..a97dc91 --- /dev/null +++ b/website/common/utils.py @@ -0,0 +1,6 @@ +from wagtail.models import get_page_models as get_wagtail_page_models, Page + +def get_page_models(): + page_models = get_wagtail_page_models().copy() + page_models.remove(Page) + return page_models