-
Notifications
You must be signed in to change notification settings - Fork 857
feat(models): add data_table Block Kit block #1900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zimeg
wants to merge
8
commits into
main
Choose a base branch
from
chore/block-kit-data-table-wt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb7c204
feat(models): add data_table Block Kit block
zimeg 15bedc7
Merge remote-tracking branch 'origin/main' into chore/block-kit-data-…
zimeg 9342f94
feat(models): add RawNumberObject + type data_table rows for cell obj…
zimeg a58f126
docs(models): drop data-table-block URL from RawNumberObject docstring
zimeg 96eecbb
docs(models): use terse block_id line in DataTableBlock docstring
zimeg 6ef7ed9
docs(models): match rows description to docs verbatim in DataTableBlock
zimeg e1cfb35
refactor(models): order RawNumberObject before RawTextObject
zimeg b4d3a3e
test(models): order data_table tests to mirror source class order
zimeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| ContainerBlock, | ||
| ContextActionsBlock, | ||
| ContextBlock, | ||
| DataTableBlock, | ||
| DividerBlock, | ||
| FileBlock, | ||
| HeaderBlock, | ||
|
|
@@ -26,6 +27,7 @@ | |
| OverflowMenuElement, | ||
| PlainTextObject, | ||
| PlanBlock, | ||
| RawNumberObject, | ||
| RawTextObject, | ||
| RichTextBlock, | ||
| RichTextElementParts, | ||
|
|
@@ -1358,6 +1360,40 @@ def test_parsing_empty_block_elements(self): | |
| self.assertIsNotNone(block_dict["elements"][3].get("elements")) | ||
|
|
||
|
|
||
| # ---------------------------------------------- | ||
| # RawNumberObject | ||
| # ---------------------------------------------- | ||
|
|
||
|
|
||
| class RawNumberObjectTests(unittest.TestCase): | ||
| def test_basic_creation(self): | ||
| """Test basic RawNumberObject creation""" | ||
| obj = RawNumberObject(value=120, text="120") | ||
| expected = {"type": "raw_number", "value": 120, "text": "120"} | ||
| self.assertDictEqual(expected, obj.to_dict()) | ||
|
|
||
| def test_float_value(self): | ||
| """Test RawNumberObject accepts a float value""" | ||
| obj = RawNumberObject(value=3.14, text="3.14") | ||
| expected = {"type": "raw_number", "value": 3.14, "text": "3.14"} | ||
| self.assertDictEqual(expected, obj.to_dict()) | ||
|
|
||
| def test_text_length_validation_min(self): | ||
| """Test that empty text fails validation""" | ||
| with self.assertRaises(SlackObjectFormationError): | ||
| RawNumberObject(value=0, text="").to_dict() | ||
|
|
||
| def test_text_length_validation_at_min(self): | ||
| """Test that text with 1 character passes validation""" | ||
| obj = RawNumberObject(value=1, text="1") | ||
| obj.to_dict() # Should not raise | ||
|
|
||
| def test_attributes(self): | ||
| """Test that RawNumberObject only has value, text, and type attributes""" | ||
| obj = RawNumberObject(value=1, text="1") | ||
| self.assertEqual(obj.attributes, {"value", "text", "type"}) | ||
|
|
||
|
|
||
| # ---------------------------------------------- | ||
| # RawTextObject | ||
| # ---------------------------------------------- | ||
|
|
@@ -1923,3 +1959,99 @@ def test_single_card(self): | |
| def test_empty_elements_validation(self): | ||
| with self.assertRaises(SlackObjectFormationError): | ||
| CarouselBlock(elements=[]).validate_json() | ||
|
|
||
|
|
||
| class DataTableBlockTests(unittest.TestCase): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📇 note: These tests follow |
||
| def test_document(self): | ||
| """Test basic data table block from Slack documentation example""" | ||
| input = { | ||
| "type": "data_table", | ||
| "caption": "Quarterly sales by region", | ||
| "rows": [ | ||
| [{"type": "raw_text", "text": "Region"}, {"type": "raw_text", "text": "Sales"}], | ||
| [{"type": "raw_text", "text": "West"}, {"type": "raw_number", "value": 120, "text": "120"}], | ||
| [{"type": "raw_text", "text": "East"}, {"type": "raw_number", "value": 95, "text": "95"}], | ||
| ], | ||
| } | ||
| self.assertDictEqual(input, DataTableBlock(**input).to_dict()) | ||
| self.assertDictEqual(input, Block.parse(input).to_dict()) | ||
|
|
||
| def test_all_fields(self): | ||
| """Test data table block with every optional field set""" | ||
| input = { | ||
| "type": "data_table", | ||
| "block_id": "data-table-123", | ||
| "caption": "User directory", | ||
| "page_size": 25, | ||
| "row_header_column_index": 1, | ||
| "rows": [ | ||
| [{"type": "raw_text", "text": "ID"}, {"type": "raw_text", "text": "Name"}], | ||
| [{"type": "raw_number", "value": 1, "text": "1"}, {"type": "raw_text", "text": "Alice"}], | ||
| ], | ||
| } | ||
| self.assertDictEqual(input, DataTableBlock(**input).to_dict()) | ||
| self.assertDictEqual(input, Block.parse(input).to_dict()) | ||
|
|
||
| def test_with_rich_text(self): | ||
| """Test data table block with rich_text cells""" | ||
| input = { | ||
| "type": "data_table", | ||
| "caption": "Links", | ||
| "rows": [ | ||
| [{"type": "raw_text", "text": "Site"}], | ||
| [ | ||
| { | ||
| "type": "rich_text", | ||
| "elements": [ | ||
| { | ||
| "type": "rich_text_section", | ||
| "elements": [{"text": "Slack", "type": "link", "url": "https://slack.com"}], | ||
| } | ||
| ], | ||
| }, | ||
| ], | ||
| ], | ||
| } | ||
| self.assertDictEqual(input, DataTableBlock(**input).to_dict()) | ||
| self.assertDictEqual(input, Block.parse(input).to_dict()) | ||
|
|
||
| def test_with_cell_object_helpers(self): | ||
| """Test data table block built from RawTextObject and RawNumberObject helpers""" | ||
| block = DataTableBlock( | ||
| caption="Quarterly sales by region", | ||
| rows=[ | ||
| [RawTextObject(text="Region").to_dict(), RawTextObject(text="Sales").to_dict()], | ||
| [RawTextObject(text="West").to_dict(), RawNumberObject(value=120, text="120").to_dict()], | ||
| [RawTextObject(text="East").to_dict(), RawNumberObject(value=95, text="95").to_dict()], | ||
| ], | ||
| ) | ||
| expected = { | ||
| "type": "data_table", | ||
| "caption": "Quarterly sales by region", | ||
| "rows": [ | ||
| [{"type": "raw_text", "text": "Region"}, {"type": "raw_text", "text": "Sales"}], | ||
| [{"type": "raw_text", "text": "West"}, {"type": "raw_number", "value": 120, "text": "120"}], | ||
| [{"type": "raw_text", "text": "East"}, {"type": "raw_number", "value": 95, "text": "95"}], | ||
| ], | ||
| } | ||
| self.assertDictEqual(expected, block.to_dict()) | ||
|
|
||
| def test_rows_validation(self): | ||
| """Test that empty rows fail validation""" | ||
| with self.assertRaises(SlackObjectFormationError): | ||
| DataTableBlock(caption="empty", rows=[]).to_dict() | ||
|
|
||
| def test_caption_required(self): | ||
| """Test that DataTableBlock requires a caption argument""" | ||
| with self.assertRaises(TypeError): | ||
| DataTableBlock(rows=[[{"type": "raw_text", "text": "A"}]]) | ||
|
|
||
| def test_page_size_validation(self): | ||
| """Test that page_size outside the allowed range fails validation""" | ||
| rows = [[{"type": "raw_text", "text": "A"}], [{"type": "raw_text", "text": "B"}]] | ||
| with self.assertRaises(SlackObjectFormationError): | ||
| DataTableBlock(caption="too small", rows=rows, page_size=0).to_dict() | ||
| with self.assertRaises(SlackObjectFormationError): | ||
| DataTableBlock(caption="too big", rows=rows, page_size=101).to_dict() | ||
| # A valid page_size should pass | ||
| DataTableBlock(caption="ok", rows=rows, page_size=50).to_dict() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎲 note: This and the
RawTextObjectfollowing might later be moved to tests for composition objects perhaps?