Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
volta_cabral authored and jpfcabral committed Mar 20, 2024
1 parent 80f2332 commit eba2f1d
Show file tree
Hide file tree
Showing 10 changed files with 661 additions and 4 deletions.
4 changes: 2 additions & 2 deletions botgen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ def hears(self, pattern: str | list, handler: Callable, event: str = "message")

self._triggers[event].append(bot_trigger)

async def on(self, event: str, handler: Callable):
def on(self, event: str, handler: Callable):

if not event in self._events:
self._events[event] = []

self._events[event] = handler
self._events[event].append(handler)

async def spawn(
self, config: TurnContext | DialogContext = None, custom_adapter: BotAdapter = None
Expand Down
180 changes: 178 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ loguru = "^0.7.2"
pre-commit = "^3.6.0"
mkdocs = "^1.5.3"
mkdocs-material = "==8.*"
pytest = "^8.1.1"
pytest-cov = "^4.1.0"
pytest-asyncio = "^0.23.6"

[build-system]
requires = ["poetry-core"]
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/botgen/__init__.py
Empty file.
Empty file.
47 changes: 47 additions & 0 deletions tests/botgen/adapters/web_adapter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
from unittest.mock import AsyncMock, Mock
from botgen.adapters import WebAdapter
from botbuilder.schema import Activity, ConversationReference, ResourceResponse
from botbuilder.core import TurnContext

@pytest.fixture
def web_adapter():
return WebAdapter()

def test_activity_to_message(web_adapter):
activity = Activity(type="message", text="Test message")
message = web_adapter.activity_to_message(activity)
assert message.type == activity.type
assert message.text == activity.text

@pytest.mark.asyncio
async def test_send_activities_webhook(web_adapter):
context = Mock()
context.activity.channel_id = "websocket"
context.turn_state.get.return_value = None
activities = [Activity(type="message", text="Test message")]

with pytest.raises(NotImplementedError):
await web_adapter.send_activities(context, activities)

@pytest.mark.asyncio
async def test_update_activity(web_adapter):
context = Mock()
activity = Mock()
with pytest.raises(NotImplementedError):
await web_adapter.update_activity(context, activity)

@pytest.mark.asyncio
async def test_delete_activity(web_adapter):
context = Mock()
reference = Mock()
with pytest.raises(NotImplementedError):
await web_adapter.delete_activity(context, reference)

@pytest.mark.asyncio
async def test_process_activity(web_adapter):
request = Mock()
request.json = AsyncMock(return_value={"type": "message", "text": "Test message", "user": "user_id"})
logic_callback = AsyncMock()
response = await web_adapter.process_activity(request, logic_callback)
assert response == None
70 changes: 70 additions & 0 deletions tests/botgen/bot_worker_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest
from unittest.mock import Mock, AsyncMock
from botgen.bot_worker import BotWorker
from botbuilder.schema import Activity
from botbuilder.core import TurnContext

@pytest.fixture
def bot_worker():
# Mock Bot and config
bot_mock = Mock()
config = {"context": Mock()}
return BotWorker(bot_mock, config)

def test_get_controller(bot_worker):
controller = bot_worker.get_controller()
assert controller == bot_worker._controller

def test_get_config(bot_worker):
config = bot_worker.get_config()
assert config == bot_worker._config

@pytest.mark.asyncio
async def test_say(bot_worker):
# Mock the context's send_activity method
bot_worker._config["context"].send_activity = AsyncMock()

message = "Test message"
await bot_worker.say(message)

bot_worker._config["context"].send_activity.assert_called_once()

@pytest.mark.asyncio
async def test_reply(bot_worker):
# Mock methods and objects needed for reply
activity = Activity(type="message", text="Reply message", channel_data={})
bot_worker.ensure_message_format = AsyncMock(return_value=activity)
TurnContext.get_conversation_reference = Mock(return_value={"conversation": {"id": "123"}})
TurnContext.apply_conversation_reference = Mock(return_value=activity)
bot_worker.say = AsyncMock()

message_src = Mock()
message_src.incoming_message = Mock()
message_resp = "Replying to the message"

await bot_worker.reply(message_src, message_resp)

bot_worker.ensure_message_format.assert_called_once_with(message=message_resp)
TurnContext.get_conversation_reference.assert_called_once_with(message_src.incoming_message)
TurnContext.apply_conversation_reference.assert_called_once_with(activity, {"conversation": {"id": "123"}})
bot_worker.say.assert_called_once_with(activity)

@pytest.mark.asyncio
async def test_ensure_message_format_string(bot_worker):
message = "Test message"
activity = await bot_worker.ensure_message_format(message)
assert isinstance(activity, Activity)
assert activity.type == "message"
assert activity.text == message

@pytest.mark.asyncio
async def test_ensure_message_format_message_object(bot_worker):
message = Mock()
message.__dict__ = {"type": "test", "text": "Test message"}
activity = await bot_worker.ensure_message_format(message)
assert isinstance(activity, Activity)
assert activity.type == message.type
assert activity.text == message.text

if __name__ == '__main__':
pytest.main()
Loading

0 comments on commit eba2f1d

Please sign in to comment.