Skip to content

Commit 68c5c19

Browse files
authored
Merge pull request #21 from Point72/tkp/extract
Start on tests, ensure no exception from exception handler
2 parents b4391b6 + b749165 commit 68c5c19

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

csp_bot/bot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,11 @@ def extract_bot_commands(self, message: Message, channel: str, text: str, entiti
496496
raise
497497
except Exception:
498498
# Ignore
499-
log.exception(f"Error processing message: {message}")
499+
# NOTE: message itself may be malformed!
500+
try:
501+
log.exception(f"Error processing message: {message}")
502+
except Exception:
503+
log.exception("Error processing message (could not log message itself)")
500504

501505
def run_bot_command(self, command_instance: BotCommand) -> Optional[Union[List[Message], List[BotCommand]]]:
502506
# grab the important bits of the command

csp_bot/gateway/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .gateway import CspBotGateway, Gateway, GatewayChannels, GatewayModule
1+
from .gateway import *

csp_bot/gateway/gateway.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818

1919
log = getLogger(__name__)
2020

21+
__all__ = (
22+
"GatewayChannels",
23+
"GatewayModule",
24+
"GatewaySettings",
25+
"CspBotGateway",
26+
"Channels",
27+
"Gateway",
28+
"Module",
29+
"Settings",
30+
)
31+
2132

2233
class GatewayChannels(GatewayChannelsBase):
2334
messages_in: ts[Message] = None
@@ -72,5 +83,7 @@ def start(self, *args, **kwargs):
7283
super(CspBotGateway, self).start(*args, **kwargs)
7384

7485

86+
Channels = GatewayChannels
7587
Gateway = CspBotGateway
88+
Module = GatewayModule
7689
Settings = GatewaySettings

csp_bot/tests/conftest.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from unittest.mock import MagicMock, patch
2+
3+
import pytest
4+
from csp import ts
5+
from csp.impl.wiring import Edge
6+
from csp_adapter_discord import DiscordAdapterConfig
7+
from csp_adapter_slack import SlackAdapterConfig
8+
9+
from csp_bot import Bot, BotCommand, BotConfig, Channels, DiscordConfig, Message, SlackConfig
10+
11+
12+
@pytest.fixture(scope="session")
13+
def bot_config():
14+
return BotConfig(
15+
discord_config=DiscordConfig(
16+
bot_name="test_bot",
17+
adapter_config=DiscordAdapterConfig(
18+
token="1" * 72,
19+
),
20+
),
21+
slack_config=SlackConfig(
22+
bot_name="test_bot",
23+
adapter_config=SlackAdapterConfig(
24+
app_token="xapp-blerg",
25+
bot_token="xoxb-blerg",
26+
),
27+
),
28+
)
29+
30+
31+
@pytest.fixture(scope="session")
32+
def bot(bot_config):
33+
bot = Bot(config=bot_config)
34+
channels_mock = MagicMock(spec=Channels)
35+
36+
def side_effect(name):
37+
if name == "commands":
38+
return Edge(ts[BotCommand], None, 0)
39+
raise Exception(name)
40+
41+
channels_mock.get_channel.side_effect = side_effect
42+
with (
43+
patch("csp.unroll", return_value=Edge(ts[Message], None, 0)),
44+
patch("csp.flatten", return_value=Edge(ts[Message], None, 0)),
45+
patch("csp.timer", return_value=Edge(ts[Message], None, 0)),
46+
patch("csp_adapter_discord.adapter.DiscordAdapterManager.publish"),
47+
patch("csp_adapter_slack.adapter.SlackAdapterManager.publish"),
48+
):
49+
bot.connect(channels=channels_mock)
50+
yield bot

csp_bot/tests/test_bot.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from csp_bot import Bot, Message
2+
3+
4+
class TestBot:
5+
def test_extract_bot_commands(self, bot: Bot):
6+
# TODO
7+
# commands = bot.extract_bot_commands(
8+
# message=Message(
9+
# user="user",
10+
# msg="<@test_bot> /thanks <@user>",
11+
# channel="test_channel",
12+
# tags=["test_bot", "user"],
13+
# backend="slack",
14+
# ),
15+
# channel="test_channel",
16+
# text="<@test_bot> /thanks <@user>",
17+
# entities=["test_bot", "user"],
18+
# )
19+
# assert commands is not None
20+
...
21+
22+
def test_extract_bot_commands_ignore_message(self, bot):
23+
commands = bot.extract_bot_commands(
24+
message=Message(
25+
user="user",
26+
msg="ignore",
27+
channel="test_channel",
28+
tags=[],
29+
backend="slack",
30+
),
31+
channel="test_channel",
32+
text="ignore",
33+
entities=[],
34+
)
35+
assert commands is None
36+
37+
def test_extract_bot_commands_bad_message(self, bot):
38+
commands = bot.extract_bot_commands(
39+
message=Message(
40+
user="user",
41+
msg="\U00001010",
42+
channel="test_channel",
43+
tags=[],
44+
backend="slack",
45+
),
46+
channel="test_channel",
47+
text="\U00001010",
48+
entities=[],
49+
)
50+
assert commands is None
51+
52+
def test_run_bot_command(self, bot): ...

0 commit comments

Comments
 (0)