Skip to content

Commit 55ea797

Browse files
fix python 3.7 test issues
1 parent a4a63c8 commit 55ea797

File tree

2 files changed

+73
-66
lines changed

2 files changed

+73
-66
lines changed

tests/slack_bolt/context/test_say_stream.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
1-
from unittest.mock import MagicMock
2-
31
import pytest
42
from slack_sdk import WebClient
53

64
from slack_bolt.context.say_stream.say_stream import SayStream
75
from slack_bolt.warning import ExperimentalWarning
6+
from tests.mock_web_api_server import cleanup_mock_web_api_server, setup_mock_web_api_server
87

98

109
class TestSayStream:
1110
def setup_method(self):
12-
self.mock_client = MagicMock(spec=WebClient)
13-
self.mock_client.chat_stream = MagicMock()
11+
setup_mock_web_api_server(self)
12+
valid_token = "xoxb-valid"
13+
mock_api_server_base_url = "http://localhost:8888"
14+
self.web_client = WebClient(token=valid_token, base_url=mock_api_server_base_url)
15+
16+
def teardown_method(self):
17+
cleanup_mock_web_api_server(self)
1418

1519
def test_missing_channel_raises(self):
16-
say_stream = SayStream(client=self.mock_client, channel_id=None, thread_ts="111.222")
20+
say_stream = SayStream(client=self.web_client, channel_id=None, thread_ts="111.222")
1721
with pytest.warns(ExperimentalWarning):
1822
with pytest.raises(ValueError, match="channel"):
1923
say_stream()
2024

2125
def test_missing_thread_ts_raises(self):
22-
say_stream = SayStream(client=self.mock_client, channel_id="C111", thread_ts=None)
26+
say_stream = SayStream(client=self.web_client, channel_id="C111", thread_ts=None)
2327
with pytest.warns(ExperimentalWarning):
2428
with pytest.raises(ValueError, match="thread_ts"):
2529
say_stream()
2630

2731
def test_default_params(self):
2832
say_stream = SayStream(
29-
client=self.mock_client,
33+
client=self.web_client,
3034
channel_id="C111",
3135
thread_ts="111.222",
3236
team_id="T111",
3337
user_id="U111",
3438
)
35-
say_stream()
39+
stream = say_stream()
3640

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-
)
41+
assert stream._stream_args == {
42+
"channel": "C111",
43+
"thread_ts": "111.222",
44+
"recipient_team_id": "T111",
45+
"recipient_user_id": "U111",
46+
"task_display_mode": None,
47+
}
4348

4449
def test_parameter_overrides(self):
4550
say_stream = SayStream(
46-
client=self.mock_client,
51+
client=self.web_client,
4752
channel_id="C111",
4853
thread_ts="111.222",
4954
team_id="T111",
5055
user_id="U111",
5156
)
52-
say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222")
57+
stream = say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222")
5358

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-
)
59+
assert stream._stream_args == {
60+
"channel": "C222",
61+
"thread_ts": "333.444",
62+
"recipient_team_id": "T222",
63+
"recipient_user_id": "U222",
64+
"task_display_mode": None,
65+
}
6066

6167
def test_buffer_size_passthrough(self):
6268
say_stream = SayStream(
63-
client=self.mock_client,
69+
client=self.web_client,
6470
channel_id="C111",
6571
thread_ts="111.222",
6672
)
67-
say_stream(buffer_size=100)
73+
stream = say_stream(buffer_size=100)
6874

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-
)
75+
assert stream._buffer_size == 100
7676

7777
def test_experimental_warning(self):
7878
say_stream = SayStream(
79-
client=self.mock_client,
79+
client=self.web_client,
8080
channel_id="C111",
8181
thread_ts="111.222",
8282
)

tests/slack_bolt_async/context/test_async_say_stream.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,95 @@
1-
from unittest.mock import AsyncMock, MagicMock
2-
31
import pytest
42
from slack_sdk.web.async_client import AsyncWebClient
53

64
from slack_bolt.context.say_stream.async_say_stream import AsyncSayStream
75
from slack_bolt.warning import ExperimentalWarning
6+
from tests.mock_web_api_server import (
7+
cleanup_mock_web_api_server,
8+
setup_mock_web_api_server,
9+
)
10+
from tests.utils import remove_os_env_temporarily, restore_os_env
811

912

1013
class TestAsyncSayStream:
11-
def setup_method(self):
12-
self.mock_client = MagicMock(spec=AsyncWebClient)
13-
self.mock_client.chat_stream = AsyncMock()
14+
@pytest.fixture(scope="function", autouse=True)
15+
def setup_teardown(self):
16+
old_os_env = remove_os_env_temporarily()
17+
setup_mock_web_api_server(self)
18+
valid_token = "xoxb-valid"
19+
mock_api_server_base_url = "http://localhost:8888"
20+
try:
21+
self.web_client = AsyncWebClient(token=valid_token, base_url=mock_api_server_base_url)
22+
yield # run the test here
23+
finally:
24+
cleanup_mock_web_api_server(self)
25+
restore_os_env(old_os_env)
1426

1527
@pytest.mark.asyncio
1628
async def test_missing_channel_raises(self):
17-
say_stream = AsyncSayStream(client=self.mock_client, channel_id=None, thread_ts="111.222")
29+
say_stream = AsyncSayStream(client=self.web_client, channel_id=None, thread_ts="111.222")
1830
with pytest.warns(ExperimentalWarning):
1931
with pytest.raises(ValueError, match="channel"):
2032
await say_stream()
2133

2234
@pytest.mark.asyncio
2335
async def test_missing_thread_ts_raises(self):
24-
say_stream = AsyncSayStream(client=self.mock_client, channel_id="C111", thread_ts=None)
36+
say_stream = AsyncSayStream(client=self.web_client, channel_id="C111", thread_ts=None)
2537
with pytest.warns(ExperimentalWarning):
2638
with pytest.raises(ValueError, match="thread_ts"):
2739
await say_stream()
2840

2941
@pytest.mark.asyncio
3042
async def test_default_params(self):
3143
say_stream = AsyncSayStream(
32-
client=self.mock_client,
44+
client=self.web_client,
3345
channel_id="C111",
3446
thread_ts="111.222",
3547
team_id="T111",
3648
user_id="U111",
3749
)
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-
)
50+
stream = await say_stream()
51+
assert stream._stream_args == {
52+
"channel": "C111",
53+
"thread_ts": "111.222",
54+
"recipient_team_id": "T111",
55+
"recipient_user_id": "U111",
56+
"task_display_mode": None,
57+
}
4658

4759
@pytest.mark.asyncio
4860
async def test_parameter_overrides(self):
4961
say_stream = AsyncSayStream(
50-
client=self.mock_client,
62+
client=self.web_client,
5163
channel_id="C111",
5264
thread_ts="111.222",
5365
team_id="T111",
5466
user_id="U111",
5567
)
56-
await say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222")
68+
stream = await say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222")
5769

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-
)
70+
assert stream._stream_args == {
71+
"channel": "C222",
72+
"thread_ts": "333.444",
73+
"recipient_team_id": "T222",
74+
"recipient_user_id": "U222",
75+
"task_display_mode": None,
76+
}
6477

6578
@pytest.mark.asyncio
6679
async def test_buffer_size_passthrough(self):
6780
say_stream = AsyncSayStream(
68-
client=self.mock_client,
81+
client=self.web_client,
6982
channel_id="C111",
7083
thread_ts="111.222",
7184
)
72-
await say_stream(buffer_size=100)
85+
stream = await say_stream(buffer_size=100)
7386

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-
)
87+
assert stream._buffer_size == 100
8188

8289
@pytest.mark.asyncio
8390
async def test_experimental_warning(self):
8491
say_stream = AsyncSayStream(
85-
client=self.mock_client,
92+
client=self.web_client,
8693
channel_id="C111",
8794
thread_ts="111.222",
8895
)

0 commit comments

Comments
 (0)