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_yaml_file, unfold_or_utterances=False, ) original_md_story_steps = await 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 = await 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 = await 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_is_yaml_file(): valid_yaml_file = "data/test_yaml_stories/stories.yml" valid_markdown_file = "data/test_stories/stories.md" assert YAMLStoryReader.is_yaml_story_file(valid_yaml_file) assert not YAMLStoryReader.is_yaml_story_file(valid_markdown_file)
def test_read_mixed_training_data_file(default_domain: Domain): training_data_file = "data/test_mixed_yaml_training_data/training_data.yml" reader = YAMLStoryReader(default_domain) yaml_content = rasa.shared.utils.io.read_yaml_file(training_data_file) with pytest.warns(None) as record: reader.read_from_parsed_yaml(yaml_content) assert not len(record)
async def test_no_warning_if_intent_in_domain(default_domain: Domain): stories = (f'version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"\n' f"stories:\n" f"- story: I am fine 💥\n" f" steps:\n" f" - intent: greet") reader = YAMLStoryReader(RegexInterpreter(), default_domain) yaml_content = io_utils.read_yaml(stories) with pytest.warns(None) as record: reader.read_from_parsed_yaml(yaml_content) assert not len(record)
async def test_warning_if_intent_not_in_domain(default_domain: Domain): stories = """ stories: - story: I am gonna make you explode 💥 steps: # Intent defined in user key. - intent: definitely not in domain """ reader = YAMLStoryReader(RegexInterpreter(), default_domain) yaml_content = io_utils.read_yaml(stories) with pytest.warns(UserWarning): reader.read_from_parsed_yaml(yaml_content)
async def test_active_loop_is_parsed(default_domain: Domain): stories = (f'version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"\n' f"stories:\n" f"- story: name\n" f" steps:\n" f" - intent: greet\n" f" - active_loop: null") reader = YAMLStoryReader(RegexInterpreter(), default_domain) yaml_content = io_utils.read_yaml(stories) with pytest.warns(None) as record: reader.read_from_parsed_yaml(yaml_content) assert not len(record)
async def test_no_warning_if_intent_in_domain(default_domain: Domain): stories = """ stories: - story: I am fine 💥 steps: - intent: greet """ reader = YAMLStoryReader(RegexInterpreter(), default_domain) yaml_content = io_utils.read_yaml(stories) with pytest.warns(None) as record: reader.read_from_parsed_yaml(yaml_content) assert len(record) == 0
def _guess_reader( filename: Text, domain: Domain, template_variables: Optional[Dict] = None, use_e2e: bool = False, ) -> StoryReader: if YAMLStoryReader.is_yaml_story_file(filename): return YAMLStoryReader(domain, template_variables, use_e2e, filename) elif MarkdownStoryReader.is_markdown_story_file(filename): return MarkdownStoryReader(domain, template_variables, use_e2e, filename) raise ValueError( f"Failed to find a reader class for the story file `{filename}`. " f"Supported formats are " f"{', '.join(MARKDOWN_FILE_EXTENSIONS.union(YAML_FILE_EXTENSIONS))}.")
async def test_warning_if_intent_not_in_domain(default_domain: Domain): stories = """ stories: - story: I am gonna make you explode 💥 steps: # Intent defined in user key. - intent: definitely not in domain """ reader = YAMLStoryReader(default_domain) yaml_content = rasa.utils.io.read_yaml(stories) with pytest.warns(UserWarning) as record: reader.read_from_parsed_yaml(yaml_content) # one for missing intent assert len(record) == 1
def _guess_reader( filename: Text, domain: Domain, interpreter: NaturalLanguageInterpreter = RegexInterpreter(), template_variables: Optional[Dict] = None, use_e2e: bool = False, ) -> StoryReader: if YAMLStoryReader.is_yaml_story_file(filename): return YAMLStoryReader(interpreter, domain, template_variables, use_e2e, filename) elif MarkdownStoryReader.is_markdown_story_file(filename): return MarkdownStoryReader(interpreter, domain, template_variables, use_e2e, filename) raise ValueError( f"Failed to find a reader class for the story file `{filename}`. " f"Supported formats are {MARKDOWN_FILE_EXTENSION}, {YAML_FILE_EXTENSIONS}." )
def is_story_file(file_path: Text) -> bool: from rasa.core.training.story_reader.markdown_story_reader import ( MarkdownStoryReader, ) from rasa.core.training.story_reader.yaml_story_reader import YAMLStoryReader """Checks if a file is a Rasa story file. Args: file_path: Path of the file which should be checked. Returns: `True` if it's a story file, otherwise `False`. """ return YAMLStoryReader.is_yaml_story_file( file_path) or MarkdownStoryReader.is_markdown_story_file(file_path)
def _get_reader( filename: Text, domain: Domain, template_variables: Optional[Dict] = None, use_e2e: bool = False, ) -> StoryReader: if rasa.shared.data.is_likely_markdown_file(filename): return MarkdownStoryReader(domain, template_variables, use_e2e, filename) elif rasa.shared.data.is_likely_yaml_file(filename): return YAMLStoryReader(domain, template_variables, use_e2e, filename) else: # This is a use case for uploading the story over REST API. # The source file has a random name. return _guess_reader(filename, domain, template_variables, use_e2e)
def _get_reader( filename: Text, domain: Domain, interpreter: NaturalLanguageInterpreter = RegexInterpreter(), template_variables: Optional[Dict] = None, use_e2e: bool = False, ) -> StoryReader: if filename.endswith(MARKDOWN_FILE_EXTENSION): return MarkdownStoryReader(interpreter, domain, template_variables, use_e2e, filename) elif Path(filename).suffix in YAML_FILE_EXTENSIONS: return YAMLStoryReader(interpreter, domain, template_variables, use_e2e, filename) else: # This is a use case for uploading the story over REST API. # The source file has a random name. return _guess_reader(filename, domain, interpreter, template_variables, use_e2e)
def test_end_to_end_story_with_shortcut_intent(): intent = "greet" plain_text = f'/{intent}{{"name": "test"}}' story = f""" stories: - story: my story steps: - user: | {plain_text} intent: {intent} """ story_as_yaml = rasa.utils.io.read_yaml(story) steps = YAMLStoryReader().read_from_parsed_yaml(story_as_yaml) user_uttered = steps[0].events[0] assert user_uttered == UserUttered( plain_text, intent={"name": intent}, entities=[{"entity": "name", "start": 6, "end": 22, "value": "test"}], )
def test_is_test_story_file(tmp_path: Path): path = str(tmp_path / "test_stories.yml") rasa.utils.io.write_yaml({"stories": []}, path) assert YAMLStoryReader.is_yaml_test_stories_file(path)
def test_is_not_test_story_file_if_it_doesnt_contain_stories(tmp_path: Path): path = str(tmp_path / "test_stories.yml") rasa.utils.io.write_yaml({"nlu": []}, path) assert not YAMLStoryReader.is_yaml_test_stories_file(path)
def test_is_not_test_story_file_without_test_prefix(tmp_path: Path): path = str(tmp_path / "stories.yml") rasa.utils.io.write_yaml({"stories": []}, path) assert not YAMLStoryReader.is_yaml_test_stories_file(path)
async def test_is_yaml_file(file: Text, is_yaml_file: bool): assert YAMLStoryReader.is_yaml_story_file(file) == is_yaml_file
def test_is_not_test_story_file_if_empty(tmp_path: Path): path = str(tmp_path / "test_stories.yml") assert not YAMLStoryReader.is_yaml_test_stories_file(path)