Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Khizov committed Dec 3, 2020
1 parent 9566db7 commit 5c6bb49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
9 changes: 6 additions & 3 deletions rasa/shared/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging
import os
import textwrap
from enum import Enum
from typing import (
Any,
Expand Down Expand Up @@ -1047,7 +1046,7 @@ def as_dict(self) -> Dict[Text, Any]:
KEY_INTENTS: self._transform_intents_for_file(),
KEY_ENTITIES: self._transform_entities_for_file(),
KEY_SLOTS: self._slot_definitions(),
KEY_RESPONSES: self.get_responses_with_multilines(self.templates),
KEY_RESPONSES: self.templates,
KEY_ACTIONS: self._custom_actions, # class names of the actions
KEY_FORMS: self.forms,
KEY_E2E_ACTIONS: self.action_texts,
Expand All @@ -1057,7 +1056,7 @@ def as_dict(self) -> Dict[Text, Any]:
def get_responses_with_multilines(
responses: Dict[Text, List[Dict[Text, Any]]]
) -> Dict[Text, List[Dict[Text, Any]]]:
"""Return `responses` with preserved multilines in the `text` key.
"""Returns `responses` with preserved multilines in the `text` key.
Args:
responses: Original `responses`.
Expand Down Expand Up @@ -1223,6 +1222,10 @@ def as_yaml(self, clean_before_dump: bool = False) -> Text:
domain_data.update(self.cleaned_domain())
else:
domain_data.update(self.as_dict())
if domain_data.get(KEY_RESPONSES, {}):
domain_data[KEY_RESPONSES] = self.get_responses_with_multilines(
domain_data[KEY_RESPONSES]
)

return rasa.shared.utils.io.dump_obj_as_yaml_to_string(
domain_data, should_preserve_key_order=True
Expand Down
40 changes: 40 additions & 0 deletions tests/shared/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,46 @@ def test_slot_order_is_preserved():
assert domain.as_yaml(clean_before_dump=True) == test_yaml


def test_slot_order_is_preserved_when_merging():

slot_1 = """
b:
type: text
influence_conversation: false
a:
type: text
influence_conversation: false"""

test_yaml_1 = f"""
slots:{slot_1}
"""

slot_2 = """
d:
type: text
influence_conversation: false
c:
type: text
influence_conversation: false"""

test_yaml_2 = f"""
slots:{slot_2}
"""

test_yaml_merged = f"""version: '{LATEST_TRAINING_DATA_FORMAT_VERSION}'
session_config:
session_expiration_time: 60
carry_over_slots_to_new_session: true
slots:{slot_2}{slot_1}
"""

domain_1 = Domain.from_yaml(test_yaml_1)
domain_2 = Domain.from_yaml(test_yaml_2)
domain_merged = domain_1.merge(domain_2)

assert domain_merged.as_yaml(clean_before_dump=True) == test_yaml_merged


def test_responses_text_multiline_is_preserved():
test_yaml = f"""version: '{LATEST_TRAINING_DATA_FORMAT_VERSION}'
session_config:
Expand Down

0 comments on commit 5c6bb49

Please sign in to comment.