Skip to content

Commit

Permalink
fix: make Command an abstract class (#68)
Browse files Browse the repository at this point in the history
This ensures that a explicit NotImpletedError is raised if `handle` is not implemented, instead of a empty named exception.
  • Loading branch information
Era-Dorta authored Dec 11, 2024
1 parent 8da39b2 commit be4a8f0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
6 changes: 4 additions & 2 deletions signalbot/command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
from abc import ABC, abstractmethod

from .message import Message
from .context import Context
Expand Down Expand Up @@ -27,7 +28,7 @@ async def wrapper_triggered(*args, **kwargs):
return decorator_triggered


class Command:
class Command(ABC):
# optional
def setup(self):
pass
Expand All @@ -37,8 +38,9 @@ def describe(self) -> str:
return None

# overwrite
@abstractmethod
async def handle(self, context: Context):
raise NotImplementedError
pass

# helper method
# deprecated: please use @triggered
Expand Down
2 changes: 2 additions & 0 deletions signalbot/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .chat_testing import (
ChatTestCase,
DummyCommand,
SendMessagesMock,
ReceiveMessagesMock,
ReactMessageMock,
Expand All @@ -8,6 +9,7 @@

__all__ = [
"ChatTestCase",
"DummyCommand",
"SendMessagesMock",
"ReceiveMessagesMock",
"ReactMessageMock",
Expand Down
7 changes: 6 additions & 1 deletion signalbot/utils/chat_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import aiohttp
from unittest.mock import AsyncMock, MagicMock

from ..bot import SignalBot
from ..bot import SignalBot, Command, Context

from unittest.mock import patch

Expand Down Expand Up @@ -131,3 +131,8 @@ def _extract_responses(self):
for args in self.call_args_list:
results.append(args[0])
return results


class DummyCommand(Command):
async def handle(self, context: Context):
pass
17 changes: 11 additions & 6 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import asyncio
from unittest.mock import patch, AsyncMock
from signalbot import SignalBot, Command, SignalAPI
from signalbot.context import Context
from signalbot.utils import DummyCommand


class BotTestCase(unittest.IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -35,8 +37,8 @@ async def test_produce(self, mock):
)
self.signal_bot.listen(TestProducer.group_id, TestProducer.internal_id)
# Any two commands
self.signal_bot.register(Command())
self.signal_bot.register(Command())
self.signal_bot.register(DummyCommand())
self.signal_bot.register(DummyCommand())

await self.signal_bot._produce(1337)

Expand Down Expand Up @@ -99,13 +101,13 @@ def test_listen_valid_invalid_phone_number(self):

class TestRegisterCommand(BotTestCase):
def test_register_one_command(self):
self.signal_bot.register(Command())
self.signal_bot.register(DummyCommand())
self.assertEqual(len(self.signal_bot.commands), 1)

def test_register_three_commands(self):
self.signal_bot.register(Command())
self.signal_bot.register(Command())
self.signal_bot.register(Command())
self.signal_bot.register(DummyCommand())
self.signal_bot.register(DummyCommand())
self.signal_bot.register(DummyCommand())
self.assertEqual(len(self.signal_bot.commands), 3)

def test_register_calls_setup_of_command(self):
Expand All @@ -116,6 +118,9 @@ def __init__(self):
def setup(self):
self.state = True

def handle(self, context: Context):
pass

cmd = SomeTestCommand()
self.assertEqual(cmd.state, False)

Expand Down

0 comments on commit be4a8f0

Please sign in to comment.