Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Apr 6, 2024
1 parent 7419c13 commit 5b1c9d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
24 changes: 22 additions & 2 deletions wagtail/tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
from wagtail.blocks.field_block import FieldBlockAdapter
from wagtail.blocks.list_block import ListBlockAdapter, ListBlockValidationError
from wagtail.blocks.static_block import StaticBlockAdapter
from wagtail.blocks.stream_block import StreamBlockAdapter, StreamBlockValidationError
from wagtail.blocks.stream_block import (
StreamBlockAdapter,
StreamBlockValidationError,
StreamValue,
)
from wagtail.blocks.struct_block import StructBlockAdapter, StructBlockValidationError
from wagtail.models import Page
from wagtail.rich_text import RichText
Expand Down Expand Up @@ -206,7 +210,8 @@ class CustomBlock(blocks.FieldBlock):
def test_form_handling_is_independent_of_serialisation(self):
class Base64EncodingCharBlock(blocks.CharBlock):
"""A CharBlock with a deliberately perverse JSON (de)serialisation format
so that it visibly blows up if we call to_python / get_prep_value where we shouldn't"""
so that it visibly blows up if we call to_python / get_prep_value where we shouldn't
"""

def to_python(self, jsonish_value):
# decode as base64 on the way out of the JSON serialisation
Expand Down Expand Up @@ -3033,6 +3038,7 @@ def test_initialisation(self):
)

self.assertEqual(list(block.child_blocks.keys()), ["heading", "paragraph"])
self.assertIs(block.value_class, StreamValue)

def test_initialisation_with_binary_string_names(self):
# migrations will sometimes write out names as binary strings, just to keep us on our toes
Expand All @@ -3045,6 +3051,20 @@ def test_initialisation_with_binary_string_names(self):

self.assertEqual(list(block.child_blocks.keys()), [b"heading", b"paragraph"])

def test_initialisation_with_custom_value_class(self):
class CustomStreamValue(StreamValue):
pass

block = blocks.StreamBlock(
[
("heading", blocks.CharBlock()),
("paragraph", blocks.CharBlock()),
],
value_class=CustomStreamValue,
)

self.assertIs(block.value_class, CustomStreamValue)

def test_initialisation_from_subclass(self):
class ArticleBlock(blocks.StreamBlock):
heading = blocks.CharBlock()
Expand Down
23 changes: 23 additions & 0 deletions wagtail/tests/test_streamfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,29 @@ def test_can_assign_as_list(self):
self.assertIsInstance(fetched_body[0].value, RichText)
self.assertEqual(fetched_body[0].value.source, "<h2>hello world</h2>")

def test_custom_value_class(self):
class CustomStreamValue(StreamValue):
pass

body_field = JSONStreamModel._meta.get_field("body")

body_field.stream_block.value_class = CustomStreamValue
try:
self.json_body.body = [("rich_text", RichText("<h2>hello world</h2>"))]
self.json_body.save()

# As above, the body should now be a stream consisting of a single rich_text block
fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
# The 'value_class' of the underlying stream_block should be respected
self.assertIsInstance(fetched_body, CustomStreamValue)
# CustomStreamValue is functionally equivalent to StreamValue, so the same value
# transformation should have taken place
self.assertEqual(len(fetched_body), 1)
self.assertIsInstance(fetched_body[0].value, RichText)
self.assertEqual(fetched_body[0].value.source, "<h2>hello world</h2>")
finally:
body_field.stream_block.value_class = StreamValue

def test_can_append(self):
self.json_body.body.append(("text", "bar"))
self.json_body.save()
Expand Down

0 comments on commit 5b1c9d8

Please sign in to comment.