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]
Пример #2
0
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))
Пример #3
0
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)
Пример #4
0
    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)
Пример #5
0
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))
Пример #6
0
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))
Пример #7
0
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))
Пример #8
0
    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)
Пример #9
0
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)
Пример #10
0
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))
Пример #11
0
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)
Пример #12
0
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)
Пример #13
0
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)
Пример #14
0
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)
Пример #15
0
def write_to_file(filename: Text, text: Text) -> None:
    """Write a text to a file."""

    io_utils.write_text_file(str(text), filename)