Skip to content

Commit

Permalink
Test chat history config la flag (#1280)
Browse files Browse the repository at this point in the history
* changed conversation fetching for chat history and chat client to use flattened data properly

* added live_agent_enabled flag to chat client config

* added additional tests

---------

Co-authored-by: spandan.mondal <[email protected]>
  • Loading branch information
hasinaxp and spandan.mondal authored Jun 27, 2024
1 parent 1ba1177 commit 0584500
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/unit_test/data_processor/data_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
from kairon.shared.data.training_data_generation_processor import TrainingDataGenerationProcessor
from kairon.shared.data.utils import DataUtility
from kairon.shared.importer.processor import DataImporterLogProcessor
from kairon.shared.live_agent.live_agent import LiveAgentHandler
from kairon.shared.llm.gpt3 import GPT3FAQEmbedding
from kairon.shared.metering.constants import MetricType
from kairon.shared.metering.data_object import Metering
Expand Down Expand Up @@ -6509,6 +6510,22 @@ def _mock_bot_info(*args, **kwargs):
'/api/bot/.+/metric/user/logs/user_metrics'
], 'access-limit': ['/api/auth/.+/token/refresh']}

def test_get_chat_client_config_live_agent_enabled_false(self, monkeypatch):
def _mock_bot_info(*args, **kwargs):
return {
"_id": "9876543210", 'name': 'test_bot', 'account': 2, 'user': '[email protected]',
'status': True,
"metadata": {"source_bot_id": None}
}
def _mock_is_live_agent_service_available(*args, **kwargs):
return False
monkeypatch.setattr(AccountProcessor, 'get_bot', _mock_bot_info)
monkeypatch.setattr(LiveAgentHandler, 'is_live_agent_service_available', _mock_is_live_agent_service_available)
processor = MongoProcessor()
actual_config = processor.get_chat_client_config('test_bot', '[email protected]')
assert actual_config.config['live_agent_enabled'] == False


def test_save_chat_client_config_without_whitelisted_domain(self, monkeypatch):
def _mock_bot_info(*args, **kwargs):
return {'name': 'test', 'account': 1, 'user': '[email protected]', 'status': True}
Expand Down
34 changes: 34 additions & 0 deletions tests/unit_test/data_processor/history_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,40 @@ def test_flatten_conversation_range(self, mock_client):
assert f_count["conversation_data"] == []
assert message is None

@mock.patch('kairon.history.processor.MongoClient', autospec=True)
def test_flatten_conversation_with_data_no_ids(self, mock_client):
with open("./tests/testing_data/history/flattened_conversations.json", "r") as f:
conversations = json.load(f)
timestamp = time.time() - 2000
for conversation in conversations:
conversation['timestamp'] = timestamp
timestamp += 200
mock_client.return_value = mongomock.MongoClient(Utility.environment['tracker']['url'])
collection = mock_client().get_database().get_collection("tests_flattened")
collection.insert_many(conversations)
r_dict, message = HistoryProcessor.flatten_conversations("tests_flattened")

for conversation in r_dict["conversation_data"]:
assert not conversation.get("id")
assert not conversation.get("_id")
assert conversation.get("timestamp")

assert len(r_dict["conversation_data"]) == 11
assert message is None
r_dict["conversation_data"][0].pop('timestamp')
assert r_dict["conversation_data"][0] == {
'type': 'flattened', 'sender_id': '[email protected]',
'data': {
'user_input': 'Hi',
'intent': 'nlu_fallback',
'confidence': 0.7, 'action': ['utter_please_rephrase'],
'bot_response_text': 'Sorry, I did not get you!',
'bot_response_data': {
'elements': None, 'quick_replies': None, 'buttons': None, 'attachment': None,
'image': None, 'custom': None}
}
}

@mock.patch('kairon.history.processor.MongoClient', autospec=True)
def test_total_conversation_range_error(self, mock_client):
mock_client.side_effect = ServerSelectionTimeoutError("Failed to connect")
Expand Down

0 comments on commit 0584500

Please sign in to comment.