Skip to content

Commit 124e358

Browse files
committed
add tests
1 parent 2b98c75 commit 124e358

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/integrations/prefect-slack/pyproject.toml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ classifiers = [
2222
"Programming Language :: Python :: 3.12",
2323
"Topic :: Software Development :: Libraries",
2424
]
25-
dependencies = [
26-
"aiohttp",
27-
"slack_sdk>=3.15.1",
28-
"prefect>=3.0.0rc1",
29-
]
25+
dependencies = ["aiohttp", "slack_sdk>=3.15.1", "prefect>=3.0.0rc1"]
3026
dynamic = ["version"]
3127

3228
[project.optional-dependencies]
@@ -74,7 +70,6 @@ fail_under = 80
7470
show_missing = true
7571

7672
[tool.pytest.ini_options]
73+
asyncio_default_fixture_loop_scope = "session"
7774
asyncio_mode = "auto"
78-
env = [
79-
"PREFECT_TEST_MODE=1",
80-
]
75+
env = ["PREFECT_TEST_MODE=1"]

src/integrations/prefect-slack/tests/test_credentials.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from unittest.mock import AsyncMock
1+
from unittest.mock import AsyncMock, MagicMock
22

33
import pytest
44
from prefect_slack import SlackCredentials, SlackWebhook
55
from slack_sdk.web.async_client import AsyncWebClient
6-
from slack_sdk.webhook.async_client import AsyncWebhookClient, WebhookResponse
6+
from slack_sdk.webhook.async_client import AsyncWebhookClient
7+
from slack_sdk.webhook.webhook_response import WebhookResponse
78

89

910
def test_slack_credentials():
@@ -63,3 +64,52 @@ async def test_slack_webhook_block_handles_raise_on_failure(
6364
with pytest.raises(NotificationError, match="Failed to send message: woops"):
6465
with block.raise_on_failure():
6566
await block.notify("hello", "world")
67+
68+
69+
def test_slack_webhook_sync_notify(monkeypatch):
70+
"""Test the sync notify path"""
71+
mock_client = MagicMock()
72+
mock_client.send.return_value = WebhookResponse(
73+
url="http://test", status_code=200, body="ok", headers={}
74+
)
75+
76+
webhook = SlackWebhook(url="http://test")
77+
monkeypatch.setattr(webhook, "get_client", MagicMock(return_value=mock_client))
78+
79+
webhook.notify("test message")
80+
mock_client.send.assert_called_once_with(text="test message")
81+
82+
83+
async def test_slack_webhook_async_notify(monkeypatch):
84+
"""Test the async notify path"""
85+
mock_client = MagicMock()
86+
mock_client.send = AsyncMock(
87+
return_value=WebhookResponse(
88+
url="http://test", status_code=200, body="ok", headers={}
89+
)
90+
)
91+
92+
webhook = SlackWebhook(url="http://test")
93+
monkeypatch.setattr(webhook, "get_client", MagicMock(return_value=mock_client))
94+
95+
await webhook.notify_async("test message")
96+
mock_client.send.assert_called_once_with(text="test message")
97+
98+
99+
@pytest.mark.parametrize("message", ["test message 1", "test message 2"])
100+
async def test_slack_webhook_notify_async_dispatch(monkeypatch, message):
101+
"""Test that async_dispatch properly handles both sync and async contexts"""
102+
103+
mock_response = WebhookResponse(
104+
url="http://test", status_code=200, body="ok", headers={}
105+
)
106+
107+
mock_client = MagicMock()
108+
mock_client.send = AsyncMock(return_value=mock_response)
109+
110+
webhook = SlackWebhook(url="http://test")
111+
monkeypatch.setattr(webhook, "get_client", lambda sync_client=False: mock_client)
112+
113+
# Test notification
114+
await webhook.notify(message)
115+
mock_client.send.assert_called_once_with(text=message)

0 commit comments

Comments
 (0)