Skip to content

Commit 9aaedf7

Browse files
improving asserts in unit tests
1 parent db99b51 commit 9aaedf7

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

tests/scenario_tests/test_events_say_stream.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
import time
33
from urllib.parse import quote
44

5-
import pytest
65
from slack_sdk.web import WebClient
76

87
from slack_bolt import App, BoltRequest, BoltContext
98
from slack_bolt.context.say_stream.say_stream import SayStream
109
from slack_bolt.middleware.assistant import Assistant
11-
from slack_bolt.warning import ExperimentalWarning
1210
from tests.mock_web_api_server import (
1311
setup_mock_web_api_server,
1412
cleanup_mock_web_api_server,
@@ -91,11 +89,14 @@ def test_say_stream_in_user_message(self):
9189
called = {"value": False}
9290

9391
@app.message("")
94-
def handle_user_message(say_stream: SayStream):
92+
def handle_user_message(say_stream: SayStream, context: BoltContext):
9593
assert say_stream is not None
9694
assert isinstance(say_stream, SayStream)
95+
assert say_stream == context.say_stream
9796
assert say_stream.channel == "C111"
9897
assert say_stream.thread_ts == "1610261659.001400"
98+
assert say_stream.recipient_team_id == context.team_id
99+
assert say_stream.recipient_user_id == context.user_id
99100
called["value"] = True
100101

101102
request = BoltRequest(body=user_message_event_payload, mode="socket_mode")
@@ -108,44 +109,35 @@ def test_say_stream_in_bot_message(self):
108109
called = {"value": False}
109110

110111
@app.message("")
111-
def handle_bot_message(say_stream: SayStream):
112+
def handle_bot_message(say_stream: SayStream, context: BoltContext):
112113
assert say_stream is not None
113114
assert isinstance(say_stream, SayStream)
115+
assert say_stream == context.say_stream
114116
assert say_stream.channel == "C111"
115117
assert say_stream.thread_ts == "1610261539.000900"
118+
assert say_stream.recipient_team_id == context.team_id
119+
assert say_stream.recipient_user_id == context.user_id
116120
called["value"] = True
117121

118122
request = BoltRequest(body=bot_message_event_payload, mode="socket_mode")
119123
response = app.dispatch(request)
120124
assert response.status == 200
121125
assert_target_called(called)
122126

123-
def test_say_stream_kwarg_emits_experimental_warning(self):
124-
app = App(client=self.web_client)
125-
called = {"value": False}
126-
127-
@app.event("app_mention")
128-
def handle_mention(say_stream: SayStream):
129-
with pytest.warns(ExperimentalWarning, match="say_stream is experimental"):
130-
say_stream()
131-
called["value"] = True
132-
133-
request = BoltRequest(body=app_mention_event_body, mode="socket_mode")
134-
response = app.dispatch(request)
135-
assert response.status == 200
136-
assert_target_called(called)
137-
138127
def test_say_stream_in_assistant_thread_started(self):
139128
app = App(client=self.web_client)
140129
assistant = Assistant()
141130
called = {"value": False}
142131

143132
@assistant.thread_started
144-
def start_thread(say_stream: SayStream):
133+
def start_thread(say_stream: SayStream, context: BoltContext):
145134
assert say_stream is not None
146135
assert isinstance(say_stream, SayStream)
136+
assert say_stream == context.say_stream
147137
assert say_stream.channel == "D111"
148138
assert say_stream.thread_ts == "1726133698.626339"
139+
assert say_stream.recipient_team_id == context.team_id
140+
assert say_stream.recipient_user_id == context.user_id
149141
called["value"] = True
150142

151143
app.assistant(assistant)
@@ -161,11 +153,14 @@ def test_say_stream_in_assistant_user_message(self):
161153
called = {"value": False}
162154

163155
@assistant.user_message
164-
def handle_user_message(say_stream: SayStream):
156+
def handle_user_message(say_stream: SayStream, context: BoltContext):
165157
assert say_stream is not None
166158
assert isinstance(say_stream, SayStream)
159+
assert say_stream == context.say_stream
167160
assert say_stream.channel == "D111"
168161
assert say_stream.thread_ts == "1726133698.626339"
162+
assert say_stream.recipient_team_id == context.team_id
163+
assert say_stream.recipient_user_id == context.user_id
169164
called["value"] = True
170165

171166
app.assistant(assistant)

tests/scenario_tests_async/test_events_say_stream.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from slack_bolt.context.async_context import AsyncBoltContext
1212
from slack_bolt.context.say_stream.async_say_stream import AsyncSayStream
1313
from slack_bolt.request.async_request import AsyncBoltRequest
14-
from slack_bolt.warning import ExperimentalWarning
1514
from tests.mock_web_api_server import (
1615
cleanup_mock_web_api_server_async,
1716
setup_mock_web_api_server_async,
@@ -97,11 +96,14 @@ async def test_say_stream_in_user_message(self):
9796
called = {"value": False}
9897

9998
@app.message("")
100-
async def handle_user_message(say_stream: AsyncSayStream):
99+
async def handle_user_message(say_stream: AsyncSayStream, context: AsyncBoltContext):
101100
assert say_stream is not None
102101
assert isinstance(say_stream, AsyncSayStream)
102+
assert say_stream == context.say_stream
103103
assert say_stream.channel == "C111"
104104
assert say_stream.thread_ts == "1610261659.001400"
105+
assert say_stream.recipient_team_id == context.team_id
106+
assert say_stream.recipient_user_id == context.user_id
105107
called["value"] = True
106108

107109
request = AsyncBoltRequest(body=user_message_event_payload, mode="socket_mode")
@@ -115,34 +117,21 @@ async def test_say_stream_in_bot_message(self):
115117
called = {"value": False}
116118

117119
@app.message("")
118-
async def handle_user_message(say_stream: AsyncSayStream):
120+
async def handle_user_message(say_stream: AsyncSayStream, context: AsyncBoltContext):
119121
assert say_stream is not None
120122
assert isinstance(say_stream, AsyncSayStream)
123+
assert say_stream == context.say_stream
121124
assert say_stream.channel == "C111"
122125
assert say_stream.thread_ts == "1610261539.000900"
126+
assert say_stream.recipient_team_id == context.team_id
127+
assert say_stream.recipient_user_id == context.user_id
123128
called["value"] = True
124129

125130
request = AsyncBoltRequest(body=bot_message_event_payload, mode="socket_mode")
126131
response = await app.async_dispatch(request)
127132
assert response.status == 200
128133
await assert_target_called(called)
129134

130-
@pytest.mark.asyncio
131-
async def test_say_stream_kwarg_emits_experimental_warning(self):
132-
app = AsyncApp(client=self.web_client)
133-
called = {"value": False}
134-
135-
@app.event("app_mention")
136-
async def handle_mention(say_stream: AsyncSayStream):
137-
with pytest.warns(ExperimentalWarning, match="say_stream is experimental"):
138-
await say_stream()
139-
called["value"] = True
140-
141-
request = AsyncBoltRequest(body=app_mention_event_body, mode="socket_mode")
142-
response = await app.async_dispatch(request)
143-
assert response.status == 200
144-
await assert_target_called(called)
145-
146135
@pytest.mark.asyncio
147136
async def test_say_stream_in_assistant_thread_started(self):
148137
app = AsyncApp(client=self.web_client)
@@ -153,8 +142,11 @@ async def test_say_stream_in_assistant_thread_started(self):
153142
async def start_thread(say_stream: AsyncSayStream, context: AsyncBoltContext):
154143
assert say_stream is not None
155144
assert isinstance(say_stream, AsyncSayStream)
145+
assert say_stream == context.say_stream
156146
assert say_stream.channel == "D111"
157147
assert say_stream.thread_ts == "1726133698.626339"
148+
assert say_stream.recipient_team_id == context.team_id
149+
assert say_stream.recipient_user_id == context.user_id
158150
called["value"] = True
159151

160152
app.assistant(assistant)
@@ -174,8 +166,11 @@ async def test_say_stream_in_assistant_user_message(self):
174166
async def handle_user_message(say_stream: AsyncSayStream, context: AsyncBoltContext):
175167
assert say_stream is not None
176168
assert isinstance(say_stream, AsyncSayStream)
169+
assert say_stream == context.say_stream
177170
assert say_stream.channel == "D111"
178171
assert say_stream.thread_ts == "1726133698.626339"
172+
assert say_stream.recipient_team_id == context.team_id
173+
assert say_stream.recipient_user_id == context.user_id
179174
called["value"] = True
180175

181176
app.assistant(assistant)

0 commit comments

Comments
 (0)