Skip to content

Commit

Permalink
singletone action upload in appendmode fix (#1609)
Browse files Browse the repository at this point in the history
* singletone action upload in appendmode fix

* test case

---------

Co-authored-by: spandan_mondal <[email protected]>
  • Loading branch information
hasinaxp and spandan_mondal authored Dec 4, 2024
1 parent 9d1d50e commit c3def34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions kairon/shared/data/action_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ def deserialize(bot: str, user: str, actions: Optional[dict] = None, other_colle
elif action_info.get('single_instance'):
if overwrite:
filtered_actions[action_type] = actions[action_type]
else:
existing_action = action_info.get("db_model").objects(bot=bot).first()
if not existing_action:
filtered_actions[action_type] = actions[action_type]
else:
new_actions = []
action_names = []
Expand Down
35 changes: 34 additions & 1 deletion tests/unit_test/data_processor/action_serializer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mongoengine import connect

from kairon import Utility
from kairon.shared.actions.data_objects import Actions, HttpActionConfig, PyscriptActionConfig
from kairon.shared.actions.data_objects import Actions, HttpActionConfig, PyscriptActionConfig, LiveAgentActionConfig
from kairon.shared.actions.models import ActionType, ActionParameterType, DbActionOperationType
from kairon.shared.callback.data_objects import CallbackConfig
from kairon.shared.data.data_validation import DataValidation
Expand Down Expand Up @@ -588,6 +588,39 @@ def test_action_serializer_deserialize():
assert callback.name == "cb1"


def test_action_serializer_deserialize_single_instance_append():
bot = "my_test_bot"
user = "test_user@test_user.com"

actions = {
"live_agent_action": [
{
'name': 'live_agent_action',
}
]
}

other_collections = {}

# Case : action doesn't exist
ActionSerializer.deserialize(bot, user, actions, other_collections)

actions_added = Actions.objects(bot=bot, user=user)
action_names = [action.name for action in actions_added]
print(action_names)
assert len(list(actions_added)) == 3
names = [action.name for action in actions_added]
assert "live_agent_action" in names

# Case : action already exists

ActionSerializer.deserialize(bot, user, actions, other_collections)
assert len(list(actions_added)) == 3
la_action_count = Actions.objects(bot=bot, user=user, name="live_agent_action").count()
assert la_action_count == 1
live_agent_action = LiveAgentActionConfig.objects(bot=bot, user=user).get()
assert live_agent_action.name == "live_agent_action"

def test_action_serializer_deserialize_overwrite():
bot = "my_test_bot"
user = "test_user@test_user.com"
Expand Down

0 comments on commit c3def34

Please sign in to comment.