Skip to content

Commit

Permalink
[FIX] Correct validation for SectionBlock (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklambourne authored Jun 25, 2023
1 parent 2c14271 commit a127525
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "slackblocks"
version = "0.9.6"
version = "0.9.7"
description = "Python wrapper for the Slack Blocks API"
authors = [
"Nicholas Lambourne <[email protected]>",
Expand Down
21 changes: 18 additions & 3 deletions slackblocks/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,27 @@ def __init__(
self,
text: Optional[TextLike] = None,
block_id: Optional[str] = None,
fields: Optional[List[Text]] = None,
fields: Optional[Union[TextLike, List[TextLike]]] = None,
accessory: Optional[Element] = None,
):
super().__init__(type_=BlockType.SECTION, block_id=block_id)
self.text = Text.to_text(text)
self.fields = fields
if not text and not fields:
raise InvalidUsageError(
"Must supply either `text` or `fields` or `both` to SectionBlock."
)
self.text = Text.to_text(text, max_length=3000, allow_none=True)
self.fields = coerce_to_list(
[
Text.to_text(field, max_length=2000, allow_none=False)
for field in coerce_to_list(fields, class_=(str, Text), allow_none=True)
]
if fields
else None,
class_=Text,
allow_none=True,
max_size=10,
)

self.accessory = accessory

def _resolve(self) -> Dict[str, Any]:
Expand Down
19 changes: 19 additions & 0 deletions test/samples/blocks/section_block_both_text_and_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "section",
"block_id": "fake_block_id",
"text": {
"type": "mrkdwn",
"text": "Hello"
},
"fields": [
{
"type": "mrkdwn",
"text": "Are you"
},
{
"type": "plain_text",
"text": "There?",
"emoji": true
}
]
}
15 changes: 15 additions & 0 deletions test/samples/blocks/section_block_empty_text_field_value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "section",
"block_id": "fake_block_id",
"fields": [
{
"type": "mrkdwn",
"text": "Highly"
},
{
"type": "plain_text",
"text": "Strung",
"emoji": true
}
]
}
10 changes: 10 additions & 0 deletions test/samples/blocks/section_block_single_field_value_coercion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "section",
"block_id": "fake_block_id",
"fields": [
{
"type": "mrkdwn",
"text": "Lowly"
}
]
}
99 changes: 83 additions & 16 deletions test/unit/test_blocks.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import pytest

from slackblocks import (
ActionsBlock,
CheckboxGroup,
ContextBlock,
DividerBlock,
HeaderBlock,
ImageBlock,
InvalidUsageError,
Option,
SectionBlock,
Text,
TextType,
)

from .utils import fetch_sample


def test_basic_section_block() -> None:
block = SectionBlock("Hello, world!", block_id="fake_block_id")
with open("test/samples/blocks/section_block_text_only.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(
path="test/samples/blocks/section_block_text_only.json"
) == repr(block)


def test_basic_section_fields() -> None:
Expand All @@ -24,20 +30,79 @@ def test_basic_section_fields() -> None:
fields=[Text(text="foo", type_=TextType.PLAINTEXT), Text(text="bar")],
block_id="fake_block_id",
)
with open("test/samples/blocks/section_block_fields.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(path="test/samples/blocks/section_block_fields.json") == repr(
block
)


def test_section_empty_text_field_value() -> None:
block = SectionBlock(
block_id="fake_block_id",
fields=[
Text("Highly", type_=TextType.MARKDOWN),
Text("Strung", type_=TextType.PLAINTEXT, emoji=True),
],
)
assert fetch_sample(
path="test/samples/blocks/section_block_empty_text_field_value.json"
) == repr(block)


def test_section_neither_fields_nor_text() -> None:
with pytest.raises(InvalidUsageError):
SectionBlock(
block_id="fake_block_id",
text=None,
fields=None,
)


def test_section_invalid_field_content() -> None:
with pytest.raises(InvalidUsageError):
SectionBlock(
block_id="fake_block_id",
fields=[
None,
],
)


def test_section_single_field_value_coercion() -> None:
block = SectionBlock(
block_id="fake_block_id",
fields="Lowly",
)
assert fetch_sample(
path="test/samples/blocks/section_block_single_field_value_coercion.json"
) == repr(block)


def test_section_both_text_and_fields() -> None:
block = SectionBlock(
text="Hello",
block_id="fake_block_id",
fields=[
Text("Are you", type_=TextType.MARKDOWN),
Text("There?", type_=TextType.PLAINTEXT, emoji=True),
],
)
assert fetch_sample(
path="test/samples/blocks/section_block_both_text_and_fields.json"
) == repr(block)


def test_basic_context_block() -> None:
block = ContextBlock(elements=[Text("Hello, world!")], block_id="fake_block_id")
with open("test/samples/blocks/context_block_text_only.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(
path="test/samples/blocks/context_block_text_only.json"
) == repr(block)


def test_basic_divider_block() -> None:
block = DividerBlock(block_id="fake_block_id")
with open("test/samples/blocks/divider_block_only.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(path="test/samples/blocks/divider_block_only.json") == repr(
block
)


def test_basic_image_block() -> None:
Expand All @@ -47,23 +112,24 @@ def test_basic_image_block() -> None:
title="image1",
block_id="fake_block_id",
)
with open("test/samples/blocks/image_block_only.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(path="test/samples/blocks/image_block_only.json") == repr(block)


def test_basic_header_block() -> None:
block = HeaderBlock(text="AloHa!", block_id="fake_block_id")
with open("test/samples/blocks/header_block_only.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(path="test/samples/blocks/header_block_only.json") == repr(
block
)


def test_basic_action_block() -> None:
block = ActionsBlock(
block_id="5d1d342f-d65c-4ac5-a2f5-690e48ef207e",
elements=[Option(text="Hi", value="Hi")],
)
with open("test/samples/blocks/actions_block_only.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(path="test/samples/blocks/actions_block_only.json") == repr(
block
)


def test_checkbox_action_block() -> None:
Expand All @@ -76,5 +142,6 @@ def test_checkbox_action_block() -> None:
block_id="fake_block_id",
elements=CheckboxGroup(action_id="actionId-0", options=options),
)
with open("test/samples/blocks/actions_block_checkboxes.json", "r") as expected:
assert repr(block) == expected.read()
assert fetch_sample(
path="test/samples/blocks/actions_block_checkboxes.json"
) == repr(block)

0 comments on commit a127525

Please sign in to comment.