Skip to content

Commit 1fb7355

Browse files
committed
test: confirm chunks parse as expected json values
1 parent 783ada5 commit 1fb7355

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import unittest
2+
3+
from slack_sdk.models.messages.chunk import MarkdownTextChunk, TaskUpdateChunk, URLSource
4+
5+
6+
class MarkdownTextChunkTests(unittest.TestCase):
7+
def test_json(self):
8+
self.assertDictEqual(
9+
MarkdownTextChunk(text="greetings!").to_dict(),
10+
{
11+
"type": "markdown_text",
12+
"text": "greetings!",
13+
},
14+
)
15+
16+
17+
class TaskUpdateChunkTests(unittest.TestCase):
18+
def test_json(self):
19+
self.assertDictEqual(
20+
TaskUpdateChunk(id="001", title="Waiting...", status="pending").to_dict(),
21+
{
22+
"type": "task_update",
23+
"id": "001",
24+
"title": "Waiting...",
25+
"status": "pending",
26+
},
27+
)
28+
self.assertDictEqual(
29+
TaskUpdateChunk(
30+
id="002",
31+
title="Wondering...",
32+
status="in_progress",
33+
details="- Gathering information...",
34+
).to_dict(),
35+
{
36+
"type": "task_update",
37+
"id": "002",
38+
"title": "Wondering...",
39+
"status": "in_progress",
40+
"details": "- Gathering information...",
41+
},
42+
)
43+
self.assertDictEqual(
44+
TaskUpdateChunk(
45+
id="003",
46+
title="Answering...",
47+
status="complete",
48+
output="Found a solution",
49+
sources=[
50+
URLSource(
51+
text="The Free Encyclopedia",
52+
url="https://wikipedia.org",
53+
icon_url="https://example.com/globe.png",
54+
),
55+
],
56+
).to_dict(),
57+
{
58+
"type": "task_update",
59+
"id": "003",
60+
"title": "Answering...",
61+
"status": "complete",
62+
"output": "Found a solution",
63+
"sources": [
64+
{
65+
"type": "url",
66+
"text": "The Free Encyclopedia",
67+
"url": "https://wikipedia.org",
68+
"icon_url": "https://example.com/globe.png",
69+
},
70+
],
71+
},
72+
)

tests/slack_sdk/web/test_internal_utils.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
import unittest
33
from io import BytesIO
44
from pathlib import Path
5-
from typing import Dict, Sequence, Union
5+
from typing import Dict
66

7-
import pytest
87

98
from slack_sdk.models.attachments import Attachment
109
from slack_sdk.models.blocks import Block, DividerBlock
10+
from slack_sdk.models.messages.chunk import MarkdownTextChunk, TaskUpdateChunk
1111
from slack_sdk.web.internal_utils import (
1212
_build_unexpected_body_error_message,
13+
_get_url,
14+
_next_cursor_is_present,
1315
_parse_web_class_objects,
1416
_to_v2_file_upload_item,
15-
_next_cursor_is_present,
16-
_get_url,
1717
)
1818

1919

@@ -57,6 +57,20 @@ def test_can_parse_sequence_of_attachments(self):
5757
for attachment in kwargs["attachments"]:
5858
assert isinstance(attachment, Dict)
5959

60+
def test_can_parse_sequence_of_chunks(self):
61+
for chunks in [
62+
[MarkdownTextChunk(text="fiz"), TaskUpdateChunk(id="001", title="baz", status="complete")], # list
63+
(
64+
MarkdownTextChunk(text="fiz"),
65+
TaskUpdateChunk(id="001", title="baz", status="complete"),
66+
), # tuple
67+
]:
68+
kwargs = {"chunks": chunks}
69+
_parse_web_class_objects(kwargs)
70+
assert kwargs["chunks"]
71+
for chunks in kwargs["chunks"]:
72+
assert isinstance(chunks, Dict)
73+
6074
def test_can_parse_str_blocks(self):
6175
input = json.dumps([Block(block_id="42").to_dict(), Block(block_id="24").to_dict()])
6276
kwargs = {"blocks": input}
@@ -71,6 +85,15 @@ def test_can_parse_str_attachments(self):
7185
assert isinstance(kwargs["attachments"], str)
7286
assert input == kwargs["attachments"]
7387

88+
def test_can_parse_str_chunks(self):
89+
input = json.dumps(
90+
[MarkdownTextChunk(text="fiz").to_dict(), TaskUpdateChunk(id="001", title="baz", status="complete").to_dict()]
91+
)
92+
kwargs = {"chunks": input}
93+
_parse_web_class_objects(kwargs)
94+
assert isinstance(kwargs["chunks"], str)
95+
assert input == kwargs["chunks"]
96+
7497
def test_can_parse_user_auth_blocks(self):
7598
kwargs = {
7699
"channel": "C12345",

0 commit comments

Comments
 (0)