Skip to content

Commit c8a50be

Browse files
feat: add has_been_called on complete & fail utility (#1390)
1 parent 1d4e86b commit c8a50be

File tree

10 files changed

+92
-0
lines changed

10 files changed

+92
-0
lines changed

slack_bolt/context/complete/async_complete.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class AsyncComplete:
88
client: AsyncWebClient
99
function_execution_id: Optional[str]
10+
_called: bool
1011

1112
def __init__(
1213
self,
@@ -15,6 +16,7 @@ def __init__(
1516
):
1617
self.client = client
1718
self.function_execution_id = function_execution_id
19+
self._called = False
1820

1921
async def __call__(self, outputs: Optional[Dict[str, Any]] = None) -> AsyncSlackResponse:
2022
"""Signal the successful completion of the custom function.
@@ -31,6 +33,15 @@ async def __call__(self, outputs: Optional[Dict[str, Any]] = None) -> AsyncSlack
3133
if self.function_execution_id is None:
3234
raise ValueError("complete is unsupported here as there is no function_execution_id")
3335

36+
self._called = True
3437
return await self.client.functions_completeSuccess(
3538
function_execution_id=self.function_execution_id, outputs=outputs or {}
3639
)
40+
41+
def has_been_called(self) -> bool:
42+
"""Check if this complete function has been called.
43+
44+
Returns:
45+
bool: True if the complete function has been called, False otherwise.
46+
"""
47+
return self._called

slack_bolt/context/complete/complete.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Complete:
88
client: WebClient
99
function_execution_id: Optional[str]
10+
_called: bool
1011

1112
def __init__(
1213
self,
@@ -15,6 +16,7 @@ def __init__(
1516
):
1617
self.client = client
1718
self.function_execution_id = function_execution_id
19+
self._called = False
1820

1921
def __call__(self, outputs: Optional[Dict[str, Any]] = None) -> SlackResponse:
2022
"""Signal the successful completion of the custom function.
@@ -31,4 +33,13 @@ def __call__(self, outputs: Optional[Dict[str, Any]] = None) -> SlackResponse:
3133
if self.function_execution_id is None:
3234
raise ValueError("complete is unsupported here as there is no function_execution_id")
3335

36+
self._called = True
3437
return self.client.functions_completeSuccess(function_execution_id=self.function_execution_id, outputs=outputs or {})
38+
39+
def has_been_called(self) -> bool:
40+
"""Check if this complete function has been called.
41+
42+
Returns:
43+
bool: True if the complete function has been called, False otherwise.
44+
"""
45+
return self._called

slack_bolt/context/fail/async_fail.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class AsyncFail:
88
client: AsyncWebClient
99
function_execution_id: Optional[str]
10+
_called: bool
1011

1112
def __init__(
1213
self,
@@ -15,6 +16,7 @@ def __init__(
1516
):
1617
self.client = client
1718
self.function_execution_id = function_execution_id
19+
self._called = False
1820

1921
async def __call__(self, error: str) -> AsyncSlackResponse:
2022
"""Signal that the custom function failed to complete.
@@ -31,4 +33,13 @@ async def __call__(self, error: str) -> AsyncSlackResponse:
3133
if self.function_execution_id is None:
3234
raise ValueError("fail is unsupported here as there is no function_execution_id")
3335

36+
self._called = True
3437
return await self.client.functions_completeError(function_execution_id=self.function_execution_id, error=error)
38+
39+
def has_been_called(self) -> bool:
40+
"""Check if this fail function has been called.
41+
42+
Returns:
43+
bool: True if the fail function has been called, False otherwise.
44+
"""
45+
return self._called

slack_bolt/context/fail/fail.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Fail:
88
client: WebClient
99
function_execution_id: Optional[str]
10+
_called: bool
1011

1112
def __init__(
1213
self,
@@ -15,6 +16,7 @@ def __init__(
1516
):
1617
self.client = client
1718
self.function_execution_id = function_execution_id
19+
self._called = False
1820

1921
def __call__(self, error: str) -> SlackResponse:
2022
"""Signal that the custom function failed to complete.
@@ -31,4 +33,13 @@ def __call__(self, error: str) -> SlackResponse:
3133
if self.function_execution_id is None:
3234
raise ValueError("fail is unsupported here as there is no function_execution_id")
3335

36+
self._called = True
3437
return self.client.functions_completeError(function_execution_id=self.function_execution_id, error=error)
38+
39+
def has_been_called(self) -> bool:
40+
"""Check if this fail function has been called.
41+
42+
Returns:
43+
bool: True if the fail function has been called, False otherwise.
44+
"""
45+
return self._called

tests/scenario_tests/test_function.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,20 @@ def reverse(body, event, context, client, complete, inputs):
300300
assert context.client.token == "xwfp-valid"
301301
assert client.token == "xwfp-valid"
302302
assert complete.client.token == "xwfp-valid"
303+
assert complete.has_been_called() is False
303304
complete(
304305
outputs={"reverseString": "olleh"},
305306
)
307+
assert complete.has_been_called() is True
306308

307309

308310
def reverse_error(body, event, fail):
309311
assert body == function_body
310312
assert event == function_body["event"]
311313
assert fail.function_execution_id == "Fx111"
314+
assert fail.has_been_called() is False
312315
fail(error="there was an error")
316+
assert fail.has_been_called() is True
313317

314318

315319
def complete_it(body, event, complete):

tests/scenario_tests_async/test_function.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,22 @@ async def reverse(body, event, client, context, complete, inputs):
312312
assert context.client.token == "xwfp-valid"
313313
assert client.token == "xwfp-valid"
314314
assert complete.client.token == "xwfp-valid"
315+
assert complete.has_been_called() is False
315316
await complete(
316317
outputs={"reverseString": "olleh"},
317318
)
319+
assert complete.has_been_called() is True
318320

319321

320322
async def reverse_error(body, event, fail):
321323
assert body == function_body
322324
assert event == function_body["event"]
323325
assert fail.function_execution_id == "Fx111"
326+
assert fail.has_been_called() is False
324327
await fail(
325328
error="there was an error",
326329
)
330+
assert fail.has_been_called() is True
327331

328332

329333
async def complete_it(body, event, complete):

tests/slack_bolt/context/test_complete.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@ def test_complete_no_function_execution_id(self):
3030

3131
with pytest.raises(ValueError):
3232
complete(outputs={"key": "value"})
33+
34+
def test_has_been_called_false_initially(self):
35+
complete = Complete(client=self.web_client, function_execution_id="fn1111")
36+
assert complete.has_been_called() is False
37+
38+
def test_has_been_called_true_after_complete(self):
39+
complete = Complete(client=self.web_client, function_execution_id="fn1111")
40+
complete(outputs={"key": "value"})
41+
assert complete.has_been_called() is True

tests/slack_bolt/context/test_fail.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@ def test_fail_no_function_execution_id(self):
3030

3131
with pytest.raises(ValueError):
3232
fail(error="there was an error")
33+
34+
def test_has_been_called_false_initially(self):
35+
fail = Fail(client=self.web_client, function_execution_id="fn1111")
36+
assert fail.has_been_called() is False
37+
38+
def test_has_been_called_true_after_fail(self):
39+
fail = Fail(client=self.web_client, function_execution_id="fn1111")
40+
fail(error="there was an error")
41+
assert fail.has_been_called() is True

tests/slack_bolt_async/context/test_async_complete.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ async def test_complete_no_function_execution_id(self):
3636

3737
with pytest.raises(ValueError):
3838
await complete(outputs={"key": "value"})
39+
40+
@pytest.mark.asyncio
41+
async def test_has_been_called_false_initially(self):
42+
complete = AsyncComplete(client=self.web_client, function_execution_id="fn1111")
43+
assert complete.has_been_called() is False
44+
45+
@pytest.mark.asyncio
46+
async def test_has_been_called_true_after_complete(self):
47+
complete = AsyncComplete(client=self.web_client, function_execution_id="fn1111")
48+
await complete(outputs={"key": "value"})
49+
assert complete.has_been_called() is True

tests/slack_bolt_async/context/test_async_fail.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ async def test_fail_no_function_execution_id(self):
3636

3737
with pytest.raises(ValueError):
3838
await fail(error="there was an error")
39+
40+
@pytest.mark.asyncio
41+
async def test_has_been_called_false_initially(self):
42+
fail = AsyncFail(client=self.web_client, function_execution_id="fn1111")
43+
assert fail.has_been_called() is False
44+
45+
@pytest.mark.asyncio
46+
async def test_has_been_called_true_after_fail(self):
47+
fail = AsyncFail(client=self.web_client, function_execution_id="fn1111")
48+
await fail(error="there was an error")
49+
assert fail.has_been_called() is True

0 commit comments

Comments
 (0)