Add assertions for subpage, parent page types to test helper

Two new assertions have been added: `assertAllowedSubpageTypes` and
`assertAllowedParentPageTypes`. They both take a Page class, and a set
of Page classes, and compares the allowed subpage / parent page models
for the Page class to the set passed in.
pull/1796/merge
Tim Heap 2015-12-01 15:31:37 +11:00 zatwierdzone przez Matt Westcott
rodzic 6947eab55b
commit 36cb270885
3 zmienionych plików z 69 dodań i 16 usunięć

Wyświetl plik

@ -403,8 +403,11 @@ class BusinessIndex(Page):
class BusinessSubIndex(Page):
""" Can be placed under BusinessIndex, and have BusinessChild children """
subpage_types = ['tests.BusinessChild']
parent_page_types = ['tests.BusinessIndex']
# BusinessNowherePage is 'incorrectly' added here as a possible child.
# The rules on BusinessNowherePage prevent it from being a child here though.
subpage_types = ['tests.BusinessChild', 'tests.BusinessNowherePage']
parent_page_types = ['tests.BusinessIndex', 'tests.BusinessChild']
class BusinessChild(Page):

Wyświetl plik

@ -6,14 +6,11 @@ from contextlib import contextmanager
import django
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils import six
from django.utils.text import slugify
from wagtail.wagtailcore.models import Page
class WagtailTestUtils(object):
def login(self):
@ -71,13 +68,7 @@ class WagtailPageTests(WagtailTestUtils, TestCase):
self.login()
def _testCanCreateAt(self, parent_model, child_model):
child_ct = ContentType.objects.get_for_model(child_model)
parent_ct = ContentType.objects.get_for_model(parent_model)
return all([
child_ct in parent_model.allowed_subpage_types(),
# Anything can be created under a Page
parent_model is Page or parent_ct in child_model.allowed_parent_page_types()])
return child_model in parent_model.allowed_subpage_models()
def assertCanCreateAt(self, parent_model, child_model, msg=None):
"""
@ -145,3 +136,31 @@ class WagtailPageTests(WagtailTestUtils, TestCase):
child_model._meta.app_label, child_model._meta.model_name,
response.redirect_chain))
raise self.failureException(msg)
def assertAllowedSubpageTypes(self, parent_model, child_models, msg=None):
"""
Test that the only page types that can be created under
``parent_model`` are ``child_models``.
The list of allowed child models may differ from those set in
``Page.subpage_types``, if the child models have set
``Page.parent_page_types``.
"""
self.assertEqual(
set(parent_model.allowed_subpage_models()),
set(child_models),
msg=msg)
def assertAllowedParentPageTypes(self, child_model, parent_models, msg=None):
"""
Test that the only page types that ``child_model`` can be created under
are ``parent_models``.
The list of allowed parent models may differ from those set in
``Page.parent_page_types``, if the parent models have set
``Page.subpage_types``.
"""
self.assertEqual(
set(child_model.allowed_parent_page_models()),
set(parent_models),
msg=msg)

Wyświetl plik

@ -2,10 +2,13 @@ from __future__ import absolute_import, unicode_literals
import json
from django.utils import six
from wagtail.tests.testapp.models import (
BusinessChild, EventIndex, EventPage, SimplePage, StreamPage)
BusinessChild, BusinessIndex, BusinessNowherePage, BusinessSubIndex,
EventIndex, EventPage, SimplePage, StreamPage)
from wagtail.tests.utils import WagtailPageTests
from wagtail.wagtailcore.models import Page, Site
from wagtail.wagtailcore.models import PAGE_MODEL_CLASSES, Page, Site
class TestWagtailPageTests(WagtailPageTests):
@ -14,6 +17,12 @@ class TestWagtailPageTests(WagtailPageTests):
site = Site.objects.get(is_default_site=True)
self.root = site.root_page.specific
def assertRaisesRegex(self, *args, **kwargs):
if six.PY3:
return super(TestWagtailPageTests, self).assertRaisesRegex(*args, **kwargs)
else:
return self.assertRaisesRegexp(*args, **kwargs)
def test_assert_can_create_at(self):
# It should be possible to create an EventPage under an EventIndex,
self.assertCanCreateAt(EventIndex, EventPage)
@ -47,11 +56,33 @@ class TestWagtailPageTests(WagtailPageTests):
simple_page = SimplePage(title='Simple Page', slug='simple')
self.root.add_child(instance=simple_page)
# This should raise an error, as a BusinessChild can not be created under a SimplePage
with self.assertRaisesRegexp(AssertionError, r'Can not create a tests.businesschild under a tests.simplepage'):
with self.assertRaisesRegex(AssertionError, r'Can not create a tests.businesschild under a tests.simplepage'):
self.assertCanCreate(simple_page, BusinessChild, {})
def test_assert_can_create_validation_error(self):
# This should raise some validation errors, complaining about missing
# title and slug fields
with self.assertRaisesRegexp(AssertionError, r'\bslug:\n[\s\S]*\btitle:\n'):
with self.assertRaisesRegex(AssertionError, r'\bslug:\n[\s\S]*\btitle:\n'):
self.assertCanCreate(self.root, SimplePage, {})
def test_assert_allowed_subpage_types(self):
self.assertAllowedSubpageTypes(BusinessIndex, {BusinessChild, BusinessSubIndex})
self.assertAllowedSubpageTypes(BusinessChild, {})
# The only page types that have rules are the Business pages. As such,
# everything can be created under the Page model except some of the
# Business pages
all_but_business = set(PAGE_MODEL_CLASSES) - {BusinessSubIndex, BusinessChild, BusinessNowherePage}
self.assertAllowedSubpageTypes(Page, all_but_business)
with self.assertRaises(AssertionError):
self.assertAllowedSubpageTypes(BusinessSubIndex, {BusinessSubIndex, BusinessChild})
def test_assert_allowed_parent_page_types(self):
self.assertAllowedParentPageTypes(BusinessChild, {BusinessIndex, BusinessSubIndex})
self.assertAllowedParentPageTypes(BusinessSubIndex, {BusinessIndex})
# The only page types that have rules are the Business pages. As such,
# a BusinessIndex can be created everywhere except under the other
# Business pages.
all_but_business = set(PAGE_MODEL_CLASSES) - {BusinessSubIndex, BusinessChild, BusinessIndex}
self.assertAllowedParentPageTypes(BusinessIndex, all_but_business)
with self.assertRaises(AssertionError):
self.assertAllowedParentPageTypes(BusinessSubIndex, {BusinessSubIndex, BusinessIndex})