Skip to content

Commit

Permalink
Add RichTextBlockFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jams2 committed Aug 16, 2023
1 parent 437a6dc commit b8e73c5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/wagtail_factories/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"PageChooserBlockFactory",
"ImageChooserBlockFactory",
"DocumentChooserBlockFactory",
"RichTextBlockFactory",
]


Expand Down Expand Up @@ -216,6 +217,12 @@ class Meta:
model = blocks.IntegerBlock


@register_block_factory(blocks.RichTextBlock)
class RichTextBlockFactory(BlockFactory):
class Meta:
model = blocks.RichTextBlock


class ChooserBlockFactory(BlockFactory):
pass

Expand Down
15 changes: 15 additions & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
import wagtail_factories
from wagtail import rich_text
from wagtail.blocks import StructValue
from wagtail.documents.models import Document
from wagtail.images.models import Image
Expand Down Expand Up @@ -250,3 +251,17 @@ def test_chooser_block_strategy(Model, ModelChooserBlockFactory):
# Object is saved in database when the strategy is create
ModelChooserBlockFactory.create()
assert Model.objects.count() == objects_count + 1


@pytest.mark.django_db()
def test_rich_text_block_factory():
value = "<p>Foo bar</p>"
instance = MyTestPageWithStreamFieldFactory.build(
body__0="rich_text", body__1__rich_text=value
)
# Generated block value will be a RichText instance
assert isinstance(instance.body[0].value, rich_text.RichText)

# Declared value will be as declared (for now)
assert isinstance(instance.body[1].value, str)
assert instance.body[1].value == value
1 change: 1 addition & 0 deletions tests/testapp/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MyTestPageWithStreamFieldFactory(wagtail_factories.PageFactory):
"document": factory.SubFactory(
wagtail_factories.DocumentChooserBlockFactory
),
"rich_text": factory.SubFactory(wagtail_factories.RichTextBlockFactory),
}
)

Expand Down
35 changes: 33 additions & 2 deletions tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.0.10 on 2023-05-16 05:53
# Generated by Django 4.0.10 on 2023-08-16 08:18

import django.db.models.deletion
import wagtail.blocks
Expand All @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
("wagtailcore", "0030_index_on_pagerevision_created_at"),
("wagtailcore", "0078_referenceindex"),
]

operations = [
Expand Down Expand Up @@ -80,6 +80,12 @@ class Migration(migrations.Migration):
required=False
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
required=False
),
),
]
),
),
Expand All @@ -89,6 +95,7 @@ class Migration(migrations.Migration):
"document",
wagtail.documents.blocks.DocumentChooserBlock(),
),
("rich_text", wagtail.blocks.RichTextBlock()),
],
use_json_field=True,
),
Expand Down Expand Up @@ -158,6 +165,12 @@ class Migration(migrations.Migration):
required=False
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
required=False
),
),
]
),
),
Expand Down Expand Up @@ -282,6 +295,12 @@ class Migration(migrations.Migration):
required=False
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
required=False
),
),
]
),
),
Expand Down Expand Up @@ -356,6 +375,12 @@ class Migration(migrations.Migration):
required=False
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
required=False
),
),
]
),
),
Expand Down Expand Up @@ -437,6 +462,12 @@ class Migration(migrations.Migration):
required=False
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
required=False
),
),
]
),
),
Expand Down
2 changes: 2 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MyBlock(blocks.StructBlock):
item = MyBlockItem()
items = blocks.ListBlock(MyBlockItem)
image = ImageChooserBlock(required=False)
rich_text = blocks.RichTextBlock(required=False)


class SimpleStructBlock(blocks.StructBlock):
Expand All @@ -41,6 +42,7 @@ class MyTestPage(Page):
("page", blocks.PageChooserBlock()),
("image", ImageChooserBlock()),
("document", DocumentChooserBlock()),
("rich_text", blocks.RichTextBlock()),
],
use_json_field=True,
)
Expand Down

0 comments on commit b8e73c5

Please sign in to comment.