Skip to content

Commit

Permalink
WIP add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allcaps committed Aug 8, 2024
1 parent 20dffd1 commit 8b3cd7f
Showing 1 changed file with 195 additions and 4 deletions.
199 changes: 195 additions & 4 deletions tests/test_streamfield_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ def test_body_field_is_streamfield():
def test_streamfield_translate_char_block():
page = BlogPostPageFactory(
body=[
{"type": "heading", "value": "Hello heading", "id": str(uuid.uuid4())},
{"type": "heading", "value": "One Two Three", "id": str(uuid.uuid4())},
]
)
translation = page.copy_for_translation(LocaleFactory())
field = translation._meta.get_field("body")
assert isinstance(field.stream_block.child_blocks["heading"], blocks.CharBlock)
assert translation.body[0].value == "Uryyb urnqvat"
assert translation.body[0].value == "Bar Gjb Guerr"


def test_streamfield_translate_rich_text_block():
page = BlogPostPageFactory(
body=[
{
"type": "paragraph",
"value": "<p>Hello <em>richtext</em></p>",
"value": "<p>One <em>Two</em></p>",
"id": str(uuid.uuid4()),
},
]
Expand All @@ -44,4 +44,195 @@ def test_streamfield_translate_rich_text_block():
assert isinstance(
field.stream_block.child_blocks["paragraph"], blocks.RichTextBlock
)
assert str(translation.body[0].value) == "<p>Uryyb <em>evpugrkg</em></p>"
assert str(translation.body[0].value).strip() == "<p>Bar <em>Gjb</em></p>"


def test_streamfield_stream_block():
page = BlogPostPageFactory(
body=[
{
"type": "stream",
"value": [
{
"type": "paragraph",
"value": "One",
"id": str(uuid.uuid4()),
},
{
"type": "paragraph",
"value": "Two",
"id": str(uuid.uuid4()),
},
{
"type": "paragraph",
"value": "Three",
"id": str(uuid.uuid4()),
},
],
"id": str(uuid.uuid4()),
},
]
)
translation = page.copy_for_translation(LocaleFactory())
field = translation._meta.get_field("body")
assert isinstance(field.stream_block.child_blocks["stream"], blocks.StreamBlock)
assert str(translation.body[0].value) == "\n".join(
[
"""<div class="block-paragraph">Bar</div>""",
"""<div class="block-paragraph">Gjb</div>""",
"""<div class="block-paragraph">Guerr</div>""",
]
)


def test_streamfield_stream_nested_block():
page = BlogPostPageFactory(
body=[
{
"type": "stream_nested",
"value": [
{
"type": "stream",
"value": [
{
"type": "paragraph",
"value": "One",
"id": str(uuid.uuid4()),
},
{
"type": "paragraph",
"value": "Two",
"id": str(uuid.uuid4()),
},
{
"type": "paragraph",
"value": "Three",
"id": str(uuid.uuid4()),
},
],
"id": str(uuid.uuid4()),
}
],
"id": str(uuid.uuid4()),
}
]
)
translation = page.copy_for_translation(LocaleFactory())
field = translation._meta.get_field("body")
assert isinstance(field.stream_block.child_blocks["stream"], blocks.StreamBlock)
assert str(translation.body[0].value) == "".join(
[
"""<div class="block-stream">""",
"\n".join(
[
"""<div class="block-paragraph">Bar</div>""",
"""<div class="block-paragraph">Gjb</div>""",
"""<div class="block-paragraph">Guerr</div>""",
]
),
"""</div>""",
]
)


def test_streamfield_list_block():
page = BlogPostPageFactory(
body=[
{
"type": "list",
"value": [
{"type": "item", "value": "One", "id": str(uuid.uuid4())},
{"type": "item", "value": "Two", "id": str(uuid.uuid4())},
{"type": "item", "value": "Three", "id": str(uuid.uuid4())},
],
"id": str(uuid.uuid4()),
}
]
)
translation = page.copy_for_translation(LocaleFactory())
field = translation._meta.get_field("body")
assert isinstance(field.stream_block.child_blocks["stream"], blocks.StreamBlock)
assert list(translation.body[0].value) == [
"Bar",
"Gjb",
"Guerr",
]


def test_streamfield_list_nested_block():
page = BlogPostPageFactory(
body=[
{
"type": "list_nested",
"value": [
{
"type": "item",
"value": [
{
"type": "item",
"value": "One 1",
"id": str(uuid.uuid4()),
},
{
"type": "item",
"value": "Two 1",
"id": str(uuid.uuid4()),
},
{
"type": "item",
"value": "Three 1",
"id": str(uuid.uuid4()),
},
],
"id": str(uuid.uuid4()),
},
{
"type": "item",
"value": [
{
"type": "item",
"value": "One 2",
"id": str(uuid.uuid4()),
},
{
"type": "item",
"value": "Two 2",
"id": str(uuid.uuid4()),
},
{
"type": "item",
"value": "Three 2",
"id": str(uuid.uuid4()),
},
],
"id": str(uuid.uuid4()),
},
],
"id": str(uuid.uuid4()),
}
]
)
translation = page.copy_for_translation(LocaleFactory())
field = translation._meta.get_field("body")
assert isinstance(field.stream_block.child_blocks["stream"], blocks.StreamBlock)
assert [list(item) for item in translation.body[0].value] == [
[
"Bar 1",
"Gjb 1",
"Guerr 1",
],
[
"Bar 2",
"Gjb 2",
"Guerr 2",
],
]


# TODO: test_streamfield_image_struct_block
# TODO: test_streamfield_raw_block
# TODO: test_streamfield_blockquoteblock_block
# TODO: test_streamfield_page_block
# TODO: test_streamfield_document_block
# TODO: test_streamfield_image_chooser_block
# TODO: test_streamfield_snippet_block

0 comments on commit 8b3cd7f

Please sign in to comment.