From e61b539d5120092742a8e6b74b245c90a49695a2 Mon Sep 17 00:00:00 2001 From: alwx Date: Tue, 16 Nov 2021 10:00:10 +0100 Subject: [PATCH 1/4] A test for this particular case --- tests/core/test_migrate.py | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/core/test_migrate.py b/tests/core/test_migrate.py index 6b8395002d68..51095c04216c 100644 --- a/tests/core/test_migrate.py +++ b/tests/core/test_migrate.py @@ -440,6 +440,56 @@ def test_migrate_domain_format_with_custom_slot(tmp_path: Path, domain_out_file: } +def test_migrate_domain_with_no_requested_slot_for_from_entity_mappings(tmp_path: Path, domain_out_file: Path): + existing_domain_file = prepare_domain_path( + tmp_path, + """ + version: "3.0" + intents: + - greet + - affirm + - inform + entities: + - city + slots: + location: + type: text + influence_conversation: false + email: + type: text + influence_conversation: false + forms: + some_form: + location: + - entity: city + type: from_entity + - intent: something + type: from_text + """, + "domain.yml", + ) + + rasa.core.migrate.migrate_domain_format(existing_domain_file, domain_out_file) + + domain = Domain.from_path(domain_out_file) + assert domain + + migrated_domain = rasa.shared.utils.io.read_yaml_file(domain_out_file) + migrated_slots = migrated_domain.get("slots") + location_slot = migrated_slots.get("location") + mappings = location_slot.get("mappings") + assert mappings[0] == { + "entity": "city", + "type": "from_entity", + "conditions": [{"active_loop": "some_form"}] + } + assert mappings[1] == { + "intent": "something", + "type": "from_text", + "conditions": [{"active_loop": "some_form", "requested_slot": "location"}] + } + + def test_migrate_domain_format_duplicated_slots_in_forms( tmp_path: Path, domain_out_file: Path ): From 31ad794e8caa4d6c0699d287f0310be37d24b470 Mon Sep 17 00:00:00 2001 From: alwx Date: Tue, 16 Nov 2021 10:51:50 +0100 Subject: [PATCH 2/4] The bugfix --- rasa/core/migrate.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/rasa/core/migrate.py b/rasa/core/migrate.py index a19ca8ff8f2b..056bffd5410c 100644 --- a/rasa/core/migrate.py +++ b/rasa/core/migrate.py @@ -27,14 +27,14 @@ def _create_back_up( return original_content -def _update_mapping_condition( +def _get_updated_mapping_condition( condition: Dict[Text, Text], mapping: Dict[Text, Any], slot_name: Text ) -> Dict[Text, Text]: if mapping.get("type") not in [ str(SlotMapping.FROM_ENTITY), str(SlotMapping.FROM_TRIGGER_INTENT), ]: - condition.update({REQUESTED_SLOT: slot_name}) + return {**condition, REQUESTED_SLOT: slot_name} return condition @@ -52,20 +52,22 @@ def _get_updated_or_new_mappings( conditions = existing_mapping.pop("conditions", []) if existing_mapping in new_mappings: new_mappings.remove(existing_mapping) - - updated_condition = _update_mapping_condition( - condition, existing_mapping, slot_name + conditions.append( + _get_updated_mapping_condition(condition, existing_mapping, slot_name) ) - - conditions.append(updated_condition) existing_mapping.update({"conditions": conditions}) updated_mappings.append(existing_mapping) else: updated_mappings.append(mapping_copy) for mapping in new_mappings: - updated_condition = _update_mapping_condition(condition, mapping, slot_name) - mapping.update({"conditions": [updated_condition]}) + mapping.update( + { + "conditions": [ + _get_updated_mapping_condition(condition, mapping, slot_name) + ] + } + ) updated_mappings.append(mapping) return updated_mappings @@ -93,6 +95,7 @@ def _migrate_form_slots( updated_mappings = _get_updated_or_new_mappings( existing_mappings, mappings, condition, slot_name ) + print(">< Updated mappings", updated_mappings) slot_properties.update({"mappings": updated_mappings}) updated_slots[slot_name] = slot_properties From 639a643ce992e6cc04ea9f94d7869b929317ee1f Mon Sep 17 00:00:00 2001 From: alwx Date: Tue, 16 Nov 2021 10:53:50 +0100 Subject: [PATCH 3/4] Remove print statement --- rasa/core/migrate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rasa/core/migrate.py b/rasa/core/migrate.py index 056bffd5410c..d85ca7de68a2 100644 --- a/rasa/core/migrate.py +++ b/rasa/core/migrate.py @@ -95,7 +95,6 @@ def _migrate_form_slots( updated_mappings = _get_updated_or_new_mappings( existing_mappings, mappings, condition, slot_name ) - print(">< Updated mappings", updated_mappings) slot_properties.update({"mappings": updated_mappings}) updated_slots[slot_name] = slot_properties From 693cd6dc24b87cf9a049352079cefc705a476cfa Mon Sep 17 00:00:00 2001 From: alwx Date: Tue, 16 Nov 2021 10:54:14 +0100 Subject: [PATCH 4/4] Test code style update --- tests/core/test_migrate.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/core/test_migrate.py b/tests/core/test_migrate.py index 51095c04216c..780751ddb265 100644 --- a/tests/core/test_migrate.py +++ b/tests/core/test_migrate.py @@ -440,7 +440,9 @@ def test_migrate_domain_format_with_custom_slot(tmp_path: Path, domain_out_file: } -def test_migrate_domain_with_no_requested_slot_for_from_entity_mappings(tmp_path: Path, domain_out_file: Path): +def test_migrate_domain_with_no_requested_slot_for_from_entity_mappings( + tmp_path: Path, domain_out_file: Path +): existing_domain_file = prepare_domain_path( tmp_path, """ @@ -481,12 +483,12 @@ def test_migrate_domain_with_no_requested_slot_for_from_entity_mappings(tmp_path assert mappings[0] == { "entity": "city", "type": "from_entity", - "conditions": [{"active_loop": "some_form"}] + "conditions": [{"active_loop": "some_form"}], } assert mappings[1] == { "intent": "something", "type": "from_text", - "conditions": [{"active_loop": "some_form", "requested_slot": "location"}] + "conditions": [{"active_loop": "some_form", "requested_slot": "location"}], }