Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestCase that automatically test PageFactory subclasses #61

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/wagtail_factories/autotest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os

from django.contrib.auth import get_user_model
from django.http import HttpRequest
from django.test import TestCase
from wagtail.models import Page
from wagtail.admin.views.pages import EditView

import wagtail_factories


class PageFeaturesTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = get_user_model().objects.create_superuser("superuser")
cls.default_request = cls.get_request("/")

@classmethod
def get_request(cls, path: str, method: str = "GET") -> HttpRequest:
request = HttpRequest()
request.method = method
request.path = path
request.user = get_user_model().objects.create_superuser("superuser")

def assertCanRenderPageWithoutErrors(self, page: Page, route_path: str = "/"):
full_path = os.path.join(page.get_url(self.default_request), route_path)
request = self.get_request(full_path)
try:
view, args, kwargs = page.resolve_subpage(route_path)
except AttributeError:
page.serve(request)
else:
page.serve(request, view, args, kwargs)

def assertCanEditPageWithoutErrors(self, page: Page):
path = f"/admin/pages/{page.id}/edit/"
request = self.get_request(path)
EditView.as_view()(request, page.id)
post_request = self.get_request(path, method="POST")
EditView.as_view()(post_request, page.id)

def assertCanPreviewPageWithoutErrors(self, page: Page, preview_mode: str = ""):
path = f"/admin/pages/{page.id}/edit/preview/?mode={preview_mode}"
request = self.get_request(path)
preview_request = page.make_preview_request(request, preview_mode)
page.serve_preview(preview_request, preview_mode)


class PageAutoTestCase(PageFeaturesTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.factories_to_test = [
f for f in wagtail_factories.get_page_factories() if f._meta.autotest
]

def test_can_render_pages_without_errors(self):
for factory in self.factories_to_test:
page = factory()
for path in page.get_route_paths(page):
with self.subTest(f"Page type: {type(page)}, Route: {path}"):
self.assertCanRenderPageWithoutErrors(page, path)

def test_can_edit_pages_without_errors(self):
for factory in self.factories_to_test:
page = factory()
with self.subTest(f"Page type: {type(page)}"):
self.assertCanEditPageWithoutErrors(page)

def test_can_preview_pages_without_errors(self):
for factory in self.factories_to_test:
page = factory()
for mode, label in page.preview_modes:
with self.subTest(f"Page type: {type(page)}, Mode: {label}"):
self.assertCanPreviewPageWithoutErrors(page, mode)
30 changes: 29 additions & 1 deletion src/wagtail_factories/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import factory
from django.utils.text import slugify
from factory import errors, utils
from factory.base import FactoryOptions, OptionDefault
from factory.declarations import ParameteredAttribute
from wagtail.images import get_image_model

Expand All @@ -12,10 +13,12 @@
# Wagtail<3.0
from wagtail.core.models import Collection, Page, Site

from factory.base import FactoryMetaClass
from factory.django import DjangoModelFactory
from wagtail.documents import get_document_model

__all__ = [
"get_page_factories",
"CollectionFactory",
"ImageFactory",
"PageFactory",
Expand All @@ -25,6 +28,13 @@
logger = logging.getLogger(__file__)


PAGE_FACTORIES = []


def get_page_factories():
return PAGE_FACTORIES


class ParentNodeFactory(ParameteredAttribute):

EXTEND_CONTAINERS = True
Expand Down Expand Up @@ -115,12 +125,30 @@ class Meta:
model = Collection


class PageFactory(MP_NodeFactory):
class PageFactoryMetaClass(FactoryMetaClass):
def __new__(mcs, class_name, bases, attrs):
new_class = super().__new__(mcs, class_name, bases, attrs)
PAGE_FACTORIES.append(new_class)
return new_class


class PageFactoryOptions(FactoryOptions):
def _build_default_options(self):
return super()._build_default_options() + [
OptionDefault("autotest", True, inherit=False),
]


class PageFactory(MP_NodeFactory, metaclass=PageFactoryMetaClass):

title = "Test page"
slug = factory.LazyAttribute(lambda obj: slugify(obj.title))

_options_class = PageFactoryOptions

class Meta:
model = Page
autotest = False


class CollectionMemberFactory(DjangoModelFactory):
Expand Down
1 change: 1 addition & 0 deletions tests/test_autotest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from wagtail_factories.autotest import PageAutoTestCase # noqa
15 changes: 14 additions & 1 deletion tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import pytest

import wagtail_factories
from tests.testapp.factories import MyTestPageFactory, MyTestPageGetOrCreateFactory
from tests.testapp.factories import (
MyTestPageFactory,
MyTestPageGetOrCreateFactory,
MyTestPageWithStreamFieldFactory,
)

try:
from wagtail.models import Page, Site
Expand Down Expand Up @@ -166,3 +170,12 @@ def test_document_add_to_collection():
collection__parent=root_collection, collection__name="new"
)
assert document.collection.name == "new"


def test_get_page_facories():
result = wagtail_factories.get_page_factories()
assert result == [
MyTestPageFactory,
MyTestPageGetOrCreateFactory,
MyTestPageWithStreamFieldFactory,
]