def save_tests(tests_string: Text, filename: Optional[Text] = None) -> List[Text]: """Saves `test_string` to a file `filename`. Args: tests_string: Test that needs to be saved. filename: Path to a test file. """ if not filename: filename = utils.get_project_directory() / DEFAULT_FILENAME existing_tests = get_tests_from_file(filename) new_tests = [ test for test in _split_tests(tests_string) if test not in existing_tests ] if new_tests: all_tests = [f"## {test}\n" for test in existing_tests + new_tests] io_utils.create_directory_for_file(filename) io_utils.write_text_file("\n".join(all_tests), filename) return [f"## {test}" for test in new_tests]
def test_story_file_with_minimal_story_is_story_file(tmpdir: Path): p = tmpdir / "story.md" s = """ ## my story """ write_text_file(s, p) assert data.is_story_file(str(p))
def json_pickle(file_name: Text, obj: Any) -> None: """Pickle an object to a file using json.""" import jsonpickle.ext.numpy as jsonpickle_numpy import jsonpickle jsonpickle_numpy.register_handlers() io_utils.write_text_file(jsonpickle.dumps(obj), file_name)
def dump_to_file(self, filename: Text, flat: bool = False, e2e: bool = False) -> None: from rasa.utils import io io.write_text_file(self.as_story_string(flat, e2e), filename, append=True)
def test_default_conversation_tests_are_conversation_tests(tmpdir: Path): parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) e2e_path = parent / "conversation_tests.md" e2e_story = """## my story test""" write_text_file(e2e_story, e2e_path) assert data.is_end_to_end_conversation_test_file(str(e2e_path))
def test_default_conversation_tests_are_conversation_tests_yml(tmpdir: Path): parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) e2e_path = parent / "test_stories.yml" e2e_story = """stories:""" write_text_file(e2e_story, e2e_path) assert data.is_test_stories_file(str(e2e_path))
def test_default_conversation_tests_are_conversation_tests_md(tmpdir: Path): # can be removed once conversation tests MD support is removed parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) e2e_path = parent / "conversation_tests.md" e2e_story = """## my story test""" write_text_file(e2e_story, e2e_path) assert data.is_test_stories_file(str(e2e_path))
def persist(self, path: Text) -> None: """Persist the tracker featurizer to the given path. Args: path: The path to persist the tracker featurizer to. """ featurizer_file = os.path.join(path, "featurizer.json") io_utils.create_directory_for_file(featurizer_file) # noinspection PyTypeChecker io_utils.write_text_file(str(jsonpickle.encode(self)), featurizer_file)
def test_custom_slot_type(tmpdir: Path): domain_path = str(tmpdir / "domain.yml") io_utils.write_text_file( """ slots: custom: type: tests.core.conftest.CustomSlot responses: utter_greet: - text: hey there! """, domain_path, ) Domain.load(domain_path)
def test_nlu_data_files_are_not_conversation_tests(tmpdir: Path): parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) nlu_path = parent / "nlu.md" nlu_data = """ ## intent: greet - hello - hi - hallo """ write_text_file(nlu_data, nlu_path) assert not data.is_end_to_end_conversation_test_file(str(nlu_path))
def test_is_nlu_file_with_json(): test = { "rasa_nlu_data": { "lookup_tables": [ {"name": "plates", "elements": ["beans", "rice", "tacos", "cheese"]} ] } } directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") io.write_text_file(json_to_string(test), file) assert data.is_nlu_file(file)
def persist_graph(graph: "networkx.Graph", output_file: Text) -> None: """Plots the graph and persists it into a html file.""" import networkx as nx import rasa.utils.io as io_utils expg = nx.nx_pydot.to_pydot(graph) template = io_utils.read_file(visualization_html_path()) # Insert graph into template template = template.replace("// { is-client }", "isClient = true", 1) graph_as_text = expg.to_string() # escape backslashes graph_as_text = graph_as_text.replace("\\", "\\\\") template = template.replace("// { graph-content }", f"graph = `{graph_as_text}`", 1) io_utils.write_text_file(template, output_file)
def test_domain_fails_on_unknown_custom_slot_type(tmpdir, domain_unkown_slot_type): domain_path = str(tmpdir / "domain.yml") io_utils.write_text_file(domain_unkown_slot_type, domain_path) with pytest.raises(ValueError): Domain.load(domain_path)
def test_is_not_nlu_file_with_json(): directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") io.write_text_file('{"test": "a"}', file) assert not data.is_nlu_file(file)
def write_to_file(filename: Text, text: Text) -> None: """Write a text to a file.""" io_utils.write_text_file(str(text), filename)