From 466e7188a646c1fe5dfbdcd70e7b5ebb962da21d Mon Sep 17 00:00:00 2001 From: himanshu_gupta Date: Thu, 5 Dec 2024 16:31:57 +0530 Subject: [PATCH] Added default flag for slots and updated corresponding test cases --- kairon/shared/data/data_objects.py | 1 + kairon/shared/data/processor.py | 11 ++++-- tests/integration_test/services_test.py | 11 ++---- .../data_processor/data_processor_test.py | 37 +++++++++++++------ 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/kairon/shared/data/data_objects.py b/kairon/shared/data/data_objects.py index 81084069c..cc0da8a86 100644 --- a/kairon/shared/data/data_objects.py +++ b/kairon/shared/data/data_objects.py @@ -513,6 +513,7 @@ class Slots(Auditlog): status = BooleanField(default=True) influence_conversation = BooleanField(default=False) _has_been_set = BooleanField(default=False) + is_default = BooleanField(default=False) meta = {"indexes": [{"fields": ["bot", ("bot", "status", "name")]}]} diff --git a/kairon/shared/data/processor.py b/kairon/shared/data/processor.py index 9c6820a7a..74ead0d52 100644 --- a/kairon/shared/data/processor.py +++ b/kairon/shared/data/processor.py @@ -1675,6 +1675,7 @@ def add_system_required_slots(self, bot: Text, user: Text): bot, user, raise_exception_if_exists=False, + is_default = True ) for slot in [s for s in KaironSystemSlots if s.value not in non_conversational_slots]: @@ -1688,6 +1689,7 @@ def add_system_required_slots(self, bot: Text, user: Text): bot, user, raise_exception_if_exists=False, + is_default=True ) def fetch_slots(self, bot: Text, status=True): @@ -3407,6 +3409,7 @@ def add_complex_story(self, story: Dict, bot: Text, user: Text): bot, user, raise_exception_if_exists=False, + is_default=True ) return id @@ -3469,6 +3472,7 @@ def add_multiflow_story(self, story: Dict, bot: Text, user: Text): bot, user, raise_exception_if_exists=False, + is_default=True ) return id @@ -4728,7 +4732,7 @@ def list_all_actions(self, bot: str, with_doc_id: bool = True): action.pop("timestamp") yield action - def add_slot(self, slot_value: Dict, bot, user, raise_exception_if_exists=True): + def add_slot(self, slot_value: Dict, bot, user, raise_exception_if_exists=True, is_default = False): """ Adds slot if it doesn't exist, updates slot if it exists :param slot_value: slot data dict @@ -4766,6 +4770,7 @@ def add_slot(self, slot_value: Dict, bot, user, raise_exception_if_exists=True): slot.max_value = slot_value.get("max_value") slot.min_value = slot_value.get("min_value") + slot.is_default = is_default slot.user = user slot.bot = bot slot_id = slot.save().id.__str__() @@ -5264,9 +5269,7 @@ def get_existing_slots(bot: Text): :param status: active or inactive, default is active :return: list of slots """ - excluded_slots = list(KaironSystemSlots) - query = Q(bot=bot, status=True) & Q(name__nin=excluded_slots) - for slot in Slots.objects(query): + for slot in Slots.objects(bot=bot, status=True): slot = slot.to_mongo().to_dict() slot.pop("bot") slot.pop("user") diff --git a/tests/integration_test/services_test.py b/tests/integration_test/services_test.py index 3df07c8ef..f9be29a12 100644 --- a/tests/integration_test/services_test.py +++ b/tests/integration_test/services_test.py @@ -9699,18 +9699,13 @@ def test_get_slots(): headers={"Authorization": pytest.token_type + " " + pytest.access_token}, ) actual = response.json() - excluded_slots = list(KaironSystemSlots) - assert "data" in actual + assert len(actual["data"]) == 21 assert actual["success"] assert actual["error_code"] == 0 - - assert len(actual["data"]) == 21 - len(excluded_slots) - returned_slot_names = [slot["name"] for slot in actual["data"]] - for excluded_slot in excluded_slots: - assert excluded_slot not in returned_slot_names - assert Utility.check_empty_string(actual["message"]) + default_slots_count = sum(slot.get('is_default') for slot in actual["data"]) + assert default_slots_count == 14 def test_add_slots(): diff --git a/tests/unit_test/data_processor/data_processor_test.py b/tests/unit_test/data_processor/data_processor_test.py index 7535b68d5..22c2846ec 100644 --- a/tests/unit_test/data_processor/data_processor_test.py +++ b/tests/unit_test/data_processor/data_processor_test.py @@ -9348,21 +9348,36 @@ def test_get_slot(self): processor = MongoProcessor() slots = list(processor.get_existing_slots(bot)) expected = [ - {'name': 'date_time', 'type': 'text', 'influence_conversation': True, '_has_been_set': False}, - {'name': 'category', 'type': 'text', 'influence_conversation': False, '_has_been_set': False}, - {'name': 'file', 'type': 'text', 'influence_conversation': False, '_has_been_set': False}, - {'name': 'file_error', 'type': 'text', 'influence_conversation': False, '_has_been_set': False}, - {'name': 'file_text', 'type': 'text', 'influence_conversation': False, '_has_been_set': False}, - {'name': 'name', 'type': 'text', 'influence_conversation': True, '_has_been_set': False}, + {'name': 'kairon_action_response', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True}, + {'name': 'bot', 'type': 'any', 'initial_value': 'test', 'influence_conversation': False, + '_has_been_set': False, 'is_default': True}, + {'name': 'order', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True}, + {'name': 'payment', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True}, + {'name': 'flow_reply', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True}, + {'name': 'http_status_code', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True}, + {'name': 'image', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'audio', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'video', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'document', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'doc_url', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'longitude', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'latitude', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}, + {'name': 'date_time', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': False}, + {'name': 'category', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False}, + {'name': 'file', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False}, + {'name': 'file_error', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False}, + {'name': 'file_text', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False}, + {'name': 'name', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': False}, {'name': 'priority', 'type': 'categorical', 'values': ['low', 'medium', 'high', '__other__'], - 'influence_conversation': True, '_has_been_set': False}, + 'influence_conversation': True, '_has_been_set': False, 'is_default': False}, {'name': 'ticketid', 'type': 'float', 'initial_value': 1.0, 'max_value': 1.0, 'min_value': 0.0, - 'influence_conversation': True, '_has_been_set': False}, + 'influence_conversation': True, '_has_been_set': False, 'is_default': False}, {'name': 'age', 'type': 'float', 'max_value': 1.0, 'min_value': 0.0, 'influence_conversation': True, - '_has_been_set': False}, - {'name': 'occupation', 'type': 'text', 'influence_conversation': True, '_has_been_set': False}, + '_has_been_set': False, 'is_default': False}, + {'name': 'occupation', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': False}, + {'name': 'quick_reply', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True} ] - assert len(slots) == 10 + assert len(slots) == 24 assert not DeepDiff(slots, expected, ignore_order=True) def test_update_slot_add_value_intent_and_not_intent(self):