Factory boy classes for wagtail
Maintained. Last release 6 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install wagtail-factorieswagtail-factories
Factory boy classes for Wagtail CMS
Installation
pip install wagtail-factories
Usage
Documentation is still in progress, but see the tests for more examples.
import wagtail_factories
from wagtail import models as wt_models
from . import models
class MyCarouselItemFactory(wagtail_factories.StructBlockFactory):
label = 'my-label'
image = factory.SubFactory(
wagtail_factories.ImageChooserBlockFactory)
class Meta:
model = models.MyBlockItem
class MyCarouselFactory(wagtail_factories.StructBlockFactory):
title = "Carousel title"
items = wagtail_factories.ListBlockFactory(
MyCarouselItemFactory)
class Meta:
model = models.MyCarousel
class MyNewsPageFactory(wagtail_factories.PageFactory):
class Meta:
model = models.MyNewsPage
class MyNewsPageChooserBlockFactory(wagtail_factories.PageChooserBlockFactory):
page = factory.SubFactory(MyNewsPageFactory)
class MyTestPageFactory(wagtail_factories.PageFactory):
body = wagtail_factories.StreamFieldFactory({
'carousel': factory.SubFactory(MyCarouselFactory),
'news_page': factory.SubFactory(MyNewsPageChooserBlockFactory),
})
class Meta:
model = models.MyTestPage
def test_my_page():
root_page = wt_models.Page.get_first_root_node()
assert root_page is not None
my_page = MyTestPageFactory(
parent=root_page,
title="My great page",
body__0__carousel__items__0__label='Slide 1',
body__0__carousel__items__0__image__image__title='Image Slide 1',
body__0__carousel__items__1__label='Slide 2',
body__0__carousel__items__1__image__image__title='Image Slide 2',
body__0__carousel__items__2__label='Slide 3',
body__0__carousel__items__2__image__image__title='Image Slide 3',
body__1__news_page__page__title="News",
)
# Defaults defined on factory classes are propagated.
assert my_page.body[0].value["title"] == "Carousel title"
# Parameters are propagated.
assert my_page.title == "My great page"
assert my_page.body[0].value["items"][0].value["label"] == "Slide 1"
Using StreamBlockFactory
StreamBlockFactory can be used in conjunction with the other block
factory types to create complex, nested StreamValues, much like how
StreamBlock can be used to declare the blocks for a complex
StreamField.
First, define your StreamBlockFactory subclass, using
factory.SubFactory to wrap child block declarations. Be sure to
include your subclass as the model attribute on the inner
class.