async def test_simple_story(tmpdir: Path, default_domain: Domain, input_md_file: Text, input_yaml_file: Text): original_md_reader = MarkdownStoryReader(default_domain, None, False, input_md_file, is_used_for_training=False) original_md_story_steps = original_md_reader.read_from_file(input_md_file) assert not YAMLStoryWriter.stories_contain_loops(original_md_story_steps) original_yaml_reader = YAMLStoryReader(default_domain, None, False) original_yaml_story_steps = original_yaml_reader.read_from_file( input_yaml_file) target_story_filename = tmpdir / "test.yml" writer = YAMLStoryWriter() writer.dump(target_story_filename, original_md_story_steps) processed_yaml_reader = YAMLStoryReader(default_domain, None, False) processed_yaml_story_steps = processed_yaml_reader.read_from_file( target_story_filename) assert len(processed_yaml_story_steps) == len(original_yaml_story_steps) for processed_step, original_step in zip(processed_yaml_story_steps, original_yaml_story_steps): assert len(processed_step.events) == len(original_step.events)
def test_raises_exception_missing_intent_in_rules(file: Text, domain: Domain): reader = YAMLStoryReader(domain) with pytest.warns(UserWarning) as warning: reader.read_from_file(file) assert "Missing intent value" in warning[0].message.args[0]
async def test_story_with_retrieval_intent_warns(file: Text, warning: Optional["Warning"]): reader = YAMLStoryReader(is_used_for_training=False) with pytest.warns(warning) as record: reader.read_from_file(file) assert len(record) == (1 if warning else 0)
def test_read_from_file_skip_validation(monkeypatch: MonkeyPatch): yaml_file = "data/test_wrong_yaml_stories/wrong_yaml.yml" reader = YAMLStoryReader() monkeypatch.setattr( sys.modules["rasa.shared.utils.io"], rasa.shared.utils.io.read_yaml.__name__, Mock(return_value={}), ) with pytest.raises(YamlException): _ = reader.read_from_file(yaml_file, skip_validation=False) assert reader.read_from_file(yaml_file, skip_validation=True) == []
async def test_simple_story(tmpdir: Path, domain: Domain, input_yaml_file: Text): original_yaml_reader = YAMLStoryReader(domain, None) original_yaml_story_steps = original_yaml_reader.read_from_file( input_yaml_file) target_story_filename = tmpdir / "test.yml" writer = YAMLStoryWriter() writer.dump(target_story_filename, original_yaml_story_steps) processed_yaml_reader = YAMLStoryReader(domain, None) processed_yaml_story_steps = processed_yaml_reader.read_from_file( target_story_filename) assert len(processed_yaml_story_steps) == len(original_yaml_story_steps) for processed_step, original_step in zip(processed_yaml_story_steps, original_yaml_story_steps): assert len(processed_step.events) == len(original_step.events)
async def test_story_start_checkpoint_is_skipped(domain: Domain): input_yaml_file = "data/test_yaml_stories/stories.yml" original_yaml_reader = YAMLStoryReader(domain, None) original_yaml_story_steps = original_yaml_reader.read_from_file( input_yaml_file) yaml_text = YAMLStoryWriter().dumps(original_yaml_story_steps) assert STORY_START not in yaml_text
def test_yaml_writer_stories_to_yaml(domain: Domain): reader = YAMLStoryReader(domain, None) writer = YAMLStoryWriter() steps = reader.read_from_file( "data/test_yaml_stories/simple_story_with_only_end.yml") result = writer.stories_to_yaml(steps) assert isinstance(result, OrderedDict) assert "stories" in result assert len(result["stories"]) == 1
def test_yaml_writer_dumps_rules(input_yaml_file: Text, tmpdir: Path, domain: Domain): original_yaml_reader = YAMLStoryReader(domain, None) original_yaml_story_steps = original_yaml_reader.read_from_file( input_yaml_file) dump = YAMLStoryWriter().dumps(original_yaml_story_steps) # remove the version string dump = "\n".join(dump.split("\n")[1:]) with open(input_yaml_file) as original_file: assert dump == original_file.read()
async def test_forms_are_converted(domain: Domain): original_yaml_reader = YAMLStoryReader(domain, None) original_yaml_story_steps = original_yaml_reader.read_from_file( "data/test_yaml_stories/stories_form.yml") assert YAMLStoryWriter.stories_contain_loops(original_yaml_story_steps) writer = YAMLStoryWriter() with pytest.warns(None) as record: writer.dumps(original_yaml_story_steps) assert len(record) == 0
def _dump_rules(path: Path, new_rules: List[StoryStep]) -> None: existing_rules = [] if path.exists(): rules_reader = YAMLStoryReader() existing_rules = rules_reader.read_from_file(path) _backup(path) if existing_rules: rasa.shared.utils.cli.print_info( f"Found existing rules in the output file '{path}'. The new rules will " f"be appended to the existing rules.") rules_writer = YAMLStoryWriter() rules_writer.dump(path, existing_rules + new_rules)
async def convert_and_write(cls, source_path: Path, output_path: Path) -> None: """Migrate retrieval intent responses to the new 2.0 format in stories. Before 2.0, retrieval intent responses needed to start with `respond_`. Now, they need to start with `utter_`. Args: source_path: the source YAML stories file. output_path: Path to the output directory. """ reader = YAMLStoryReader() story_steps = reader.read_from_file(source_path) for story_step in story_steps: for event in story_step.events: if isinstance(event, ActionExecuted): event.action_name = normalize_utter_action( event.action_name) output_file = cls.generate_path_for_converted_training_data_file( source_path, output_path) YAMLStoryWriter().dump(output_file, story_steps)
def test_generating_trackers( default_model_storage: ModelStorage, default_execution_context: ExecutionContext, config: Dict[Text, Any], expected_trackers: int, ): reader = YAMLStoryReader() steps = reader.read_from_file("data/test_yaml_stories/stories.yml") component = TrainingTrackerProvider.create( { **TrainingTrackerProvider.get_default_config(), **config }, default_model_storage, Resource("xy"), default_execution_context, ) trackers = component.generate_trackers(story_graph=StoryGraph(steps), domain=Domain.empty()) assert len(trackers) == expected_trackers assert all(isinstance(t, TrackerWithCachedStates) for t in trackers)