Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make command an abstract class #68

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading