Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spandan_mondal committed Dec 2, 2024
1 parent 88e9da2 commit 3b8e43c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
5 changes: 3 additions & 2 deletions kairon/events/definitions/mail_channel_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from loguru import logger
from kairon import Utility
from kairon.events.definitions.base import EventsBase
from kairon.exceptions import AppException
from kairon.shared.channels.mail.processor import MailProcessor
from kairon.shared.constants import EventClass

Expand Down Expand Up @@ -35,7 +36,7 @@ def enqueue(self, **kwargs):
Utility.request_event_server(EventClass.email_channel_scheduler, payload)
except Exception as e:
logger.error(str(e))
raise e
raise AppException(e)

def execute(self, **kwargs):
"""
Expand All @@ -47,4 +48,4 @@ def execute(self, **kwargs):
MailProcessor.process_message_task(self.bot, mails)
except Exception as e:
logger.error(str(e))
raise e
raise AppException(e)
32 changes: 31 additions & 1 deletion tests/unit_test/chat/chat_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from unittest.mock import MagicMock, patch, AsyncMock

import ujson as json
import os
Expand Down Expand Up @@ -933,4 +934,33 @@ async def test_mongotracker_save(self):
data = list(store.client.get_database(config['db']).get_collection(bot).find({'type': 'flattened'}))
assert len(data) == 1
assert data[0]['tag'] == 'tracker_store'
assert data[0]['type'] == 'flattened'
assert data[0]['type'] == 'flattened'




@pytest.mark.asyncio
@patch("kairon.chat.utils.AgentProcessor.get_agent_without_cache")
@patch("kairon.chat.utils.ChatUtils.get_metadata")
async def test_process_messages_via_bot(mock_get_metadata, mock_get_agent_without_cache):
messages = ["/greet", "/bye"]
account = 1
bot = "test_bot"
user = "test_user"
is_integration_user = False
metadata = {"key": "value"}

mock_get_metadata.return_value = metadata
mock_model = MagicMock()
mock_get_agent_without_cache.return_value = mock_model
mock_model.handle_message = AsyncMock(side_effect=[{"text": "Hello"}, {"text": "Goodbye"}])
from kairon.chat.utils import ChatUtils

responses = await ChatUtils.process_messages_via_bot(messages, account, bot, user, is_integration_user, metadata)

assert len(responses) == 2
assert responses[0] == {"text": "Hello"}
assert responses[1] == {"text": "Goodbye"}
mock_get_metadata.assert_called_once_with(account, bot, is_integration_user, metadata)
mock_get_agent_without_cache.assert_called_once_with(bot, False)
assert mock_model.handle_message.call_count == 2
30 changes: 29 additions & 1 deletion tests/unit_test/events/definitions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,10 +1216,38 @@ def test_trigger_mail_channel_schedule_event_enqueue(self):
except AppException as e:
pytest.fail(f"Unexpected exception: {e}")

@responses.activate
def test_trigger_mail_channel_schedule_event_enqueue_exception(self):
from kairon.events.definitions.mail_channel_schedule import MailChannelScheduleEvent
from kairon.exceptions import AppException
from unittest.mock import patch

bot = "test_add_schedule_event"
user = "test_user"
url = f"http://localhost:5001/api/events/execute/{EventClass.email_channel_scheduler}?is_scheduled=False"
responses.add(
"POST", url,
json={"message": "test msg", "success": False, "error_code": 400, "data": None}
)
event = MailChannelScheduleEvent(bot, user)
with pytest.raises(AppException, match="Failed to trigger email_channel_scheduler event: test msg"):
event.enqueue()

@responses.activate
def test_trigger_mail_channel_schedule_event_execute(self):
from kairon.events.definitions.mail_channel_schedule import MailChannelScheduleEvent
try:
MailChannelScheduleEvent("", "").execute()
except AppException as e:
pytest.fail(f"Unexpected exception: {e}")
pytest.fail(f"Unexpected exception: {e}")

@responses.activate
def test_trigger_mail_channel_schedule_event_execute_exception(self):
from kairon.events.definitions.mail_channel_schedule import MailChannelScheduleEvent
from kairon.exceptions import AppException
from unittest.mock import patch

with patch("kairon.shared.channels.mail.processor.MailProcessor.process_message_task",
side_effect=Exception("Test")):
with pytest.raises(AppException, match="Test"):
MailChannelScheduleEvent("", "").execute(mails=["[email protected]"])

0 comments on commit 3b8e43c

Please sign in to comment.