Skip to content

Commit f30dd71

Browse files
Improve unit tests around SayStream
1 parent cacf2ad commit f30dd71

File tree

5 files changed

+176
-22
lines changed

5 files changed

+176
-22
lines changed

slack_bolt/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .context.fail import Fail
1515
from .context.respond import Respond
1616
from .context.say import Say
17+
from .context.say_stream import SayStream
1718
from .kwargs_injection import Args
1819
from .listener import Listener
1920
from .listener_matcher import CustomListenerMatcher
@@ -42,6 +43,7 @@
4243
"Fail",
4344
"Respond",
4445
"Say",
46+
"SayStream",
4547
"Args",
4648
"Listener",
4749
"CustomListenerMatcher",

slack_bolt/context/say_stream/async_say_stream.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def __init__(
3232
async def __call__(
3333
self,
3434
*,
35-
buffer_size: Optional[int] = None,
3635
channel: Optional[str] = None,
3736
thread_ts: Optional[str] = None,
3837
recipient_team_id: Optional[str] = None,
@@ -52,16 +51,6 @@ async def __call__(
5251
if thread_ts is None:
5352
raise ValueError("say_stream without thread_ts here is unsupported")
5453

55-
if buffer_size:
56-
return await self.client.chat_stream(
57-
buffer_size=buffer_size,
58-
channel=channel,
59-
thread_ts=thread_ts,
60-
recipient_team_id=recipient_team_id or self.team_id,
61-
recipient_user_id=recipient_user_id or self.user_id,
62-
**kwargs,
63-
)
64-
6554
return await self.client.chat_stream(
6655
channel=channel,
6756
thread_ts=thread_ts,

slack_bolt/context/say_stream/say_stream.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def __init__(
3232
def __call__(
3333
self,
3434
*,
35-
buffer_size: Optional[int] = None,
3635
channel: Optional[str] = None,
3736
thread_ts: Optional[str] = None,
3837
recipient_team_id: Optional[str] = None,
@@ -52,16 +51,6 @@ def __call__(
5251
if thread_ts is None:
5352
raise ValueError("say_stream without thread_ts here is unsupported")
5453

55-
if buffer_size:
56-
return self.client.chat_stream(
57-
buffer_size=buffer_size,
58-
channel=channel,
59-
thread_ts=thread_ts,
60-
recipient_team_id=recipient_team_id or self.team_id,
61-
recipient_user_id=recipient_user_id or self.user_id,
62-
**kwargs,
63-
)
64-
6554
return self.client.chat_stream(
6655
channel=channel,
6756
thread_ts=thread_ts,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from unittest.mock import MagicMock
2+
3+
import pytest
4+
from slack_sdk import WebClient
5+
6+
from slack_bolt.context.say_stream.say_stream import SayStream
7+
from slack_bolt.warning import ExperimentalWarning
8+
9+
10+
class TestSayStream:
11+
def setup_method(self):
12+
self.mock_client = MagicMock(spec=WebClient)
13+
self.mock_client.chat_stream = MagicMock()
14+
15+
def test_missing_channel_raises(self):
16+
say_stream = SayStream(client=self.mock_client, channel_id=None, thread_ts="111.222")
17+
with pytest.warns(ExperimentalWarning):
18+
with pytest.raises(ValueError, match="channel"):
19+
say_stream()
20+
21+
def test_missing_thread_ts_raises(self):
22+
say_stream = SayStream(client=self.mock_client, channel_id="C111", thread_ts=None)
23+
with pytest.warns(ExperimentalWarning):
24+
with pytest.raises(ValueError, match="thread_ts"):
25+
say_stream()
26+
27+
def test_default_params(self):
28+
say_stream = SayStream(
29+
client=self.mock_client,
30+
channel_id="C111",
31+
thread_ts="111.222",
32+
team_id="T111",
33+
user_id="U111",
34+
)
35+
say_stream()
36+
37+
self.mock_client.chat_stream.assert_called_once_with(
38+
channel="C111",
39+
thread_ts="111.222",
40+
recipient_team_id="T111",
41+
recipient_user_id="U111",
42+
)
43+
44+
def test_parameter_overrides(self):
45+
say_stream = SayStream(
46+
client=self.mock_client,
47+
channel_id="C111",
48+
thread_ts="111.222",
49+
team_id="T111",
50+
user_id="U111",
51+
)
52+
say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222")
53+
54+
self.mock_client.chat_stream.assert_called_once_with(
55+
channel="C222",
56+
thread_ts="333.444",
57+
recipient_team_id="T222",
58+
recipient_user_id="U222",
59+
)
60+
61+
def test_buffer_size_passthrough(self):
62+
say_stream = SayStream(
63+
client=self.mock_client,
64+
channel_id="C111",
65+
thread_ts="111.222",
66+
)
67+
say_stream(buffer_size=100)
68+
69+
self.mock_client.chat_stream.assert_called_once_with(
70+
buffer_size=100,
71+
channel="C111",
72+
thread_ts="111.222",
73+
recipient_team_id=None,
74+
recipient_user_id=None,
75+
)
76+
77+
def test_experimental_warning(self):
78+
say_stream = SayStream(
79+
client=self.mock_client,
80+
channel_id="C111",
81+
thread_ts="111.222",
82+
)
83+
with pytest.warns(ExperimentalWarning, match="say_stream is experimental"):
84+
say_stream()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from unittest.mock import AsyncMock, MagicMock
2+
3+
import pytest
4+
from slack_sdk.web.async_client import AsyncWebClient
5+
6+
from slack_bolt.context.say_stream.async_say_stream import AsyncSayStream
7+
from slack_bolt.warning import ExperimentalWarning
8+
9+
10+
class TestAsyncSayStream:
11+
def setup_method(self):
12+
self.mock_client = MagicMock(spec=AsyncWebClient)
13+
self.mock_client.chat_stream = AsyncMock()
14+
15+
@pytest.mark.asyncio
16+
async def test_missing_channel_raises(self):
17+
say_stream = AsyncSayStream(client=self.mock_client, channel_id=None, thread_ts="111.222")
18+
with pytest.warns(ExperimentalWarning):
19+
with pytest.raises(ValueError, match="channel"):
20+
await say_stream()
21+
22+
@pytest.mark.asyncio
23+
async def test_missing_thread_ts_raises(self):
24+
say_stream = AsyncSayStream(client=self.mock_client, channel_id="C111", thread_ts=None)
25+
with pytest.warns(ExperimentalWarning):
26+
with pytest.raises(ValueError, match="thread_ts"):
27+
await say_stream()
28+
29+
@pytest.mark.asyncio
30+
async def test_default_params(self):
31+
say_stream = AsyncSayStream(
32+
client=self.mock_client,
33+
channel_id="C111",
34+
thread_ts="111.222",
35+
team_id="T111",
36+
user_id="U111",
37+
)
38+
await say_stream()
39+
40+
self.mock_client.chat_stream.assert_called_once_with(
41+
channel="C111",
42+
thread_ts="111.222",
43+
recipient_team_id="T111",
44+
recipient_user_id="U111",
45+
)
46+
47+
@pytest.mark.asyncio
48+
async def test_parameter_overrides(self):
49+
say_stream = AsyncSayStream(
50+
client=self.mock_client,
51+
channel_id="C111",
52+
thread_ts="111.222",
53+
team_id="T111",
54+
user_id="U111",
55+
)
56+
await say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222")
57+
58+
self.mock_client.chat_stream.assert_called_once_with(
59+
channel="C222",
60+
thread_ts="333.444",
61+
recipient_team_id="T222",
62+
recipient_user_id="U222",
63+
)
64+
65+
@pytest.mark.asyncio
66+
async def test_buffer_size_passthrough(self):
67+
say_stream = AsyncSayStream(
68+
client=self.mock_client,
69+
channel_id="C111",
70+
thread_ts="111.222",
71+
)
72+
await say_stream(buffer_size=100)
73+
74+
self.mock_client.chat_stream.assert_called_once_with(
75+
buffer_size=100,
76+
channel="C111",
77+
thread_ts="111.222",
78+
recipient_team_id=None,
79+
recipient_user_id=None,
80+
)
81+
82+
@pytest.mark.asyncio
83+
async def test_experimental_warning(self):
84+
say_stream = AsyncSayStream(
85+
client=self.mock_client,
86+
channel_id="C111",
87+
thread_ts="111.222",
88+
)
89+
with pytest.warns(ExperimentalWarning, match="say_stream is experimental"):
90+
await say_stream()

0 commit comments

Comments
 (0)