Beispiel #1
0
def test_cli_output_default_cwd() -> None:
    """Test the default value for the output path for several schemas"""
    test_case1 = "basic"
    test_path1 = get_test_case_path(test_case1)
    test_case2 = "with_default"
    test_path2 = get_test_case_path(test_case2)

    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [f"{test_path1},{test_path2}"])
        assert_cli_runner_result(result)

        assert Path(f"{test_case1}.html").exists()
        assert Path(f"{test_case2}.html").exists()

        assert_css_and_js_copied(Path.cwd())
 def generate_case(self,
                   case_name: str,
                   config: GenerationConfiguration = None) -> str:
     """Get the generated markdown schema string for a given schema test case"""
     return generate_from_schema(get_test_case_path(case_name),
                                 None,
                                 config=config)
Beispiel #3
0
def test_generate_from_schema_deprecation_warning(
        tmp_path: Path, caplog: LogCaptureFixture, minify: bool,
        expect_warning: bool) -> None:
    caplog.set_level(logging.INFO)

    generate_from_schema(get_test_case_path("basic"), minify=minify)

    _assert_deprecation_message(caplog, expect_warning)
def test_config_parameters() -> None:
    """Test providing configuration parameters using the --config CLI parameter"""
    test_path = get_test_case_path("basic")
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [test_path, "--config", "copy_css=false", "--config", "copy_js=false"])
        assert_cli_runner_result(result)

        assert_css_and_js_not_copied(Path.cwd())
def test_config_parameters_flags_no() -> None:
    """Test providing configuration parameters using the --config CLI parameter and the special 'no_' syntax for flags"""
    test_path = get_test_case_path("basic")
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [test_path, "--config", "no_copy_css", "--config", "no_copy_js"])
        assert_cli_runner_result(result)

        assert_css_and_js_not_copied(Path.cwd())
Beispiel #6
0
def test_generate_using_cli_multiple(tmp_path: Path) -> None:
    """Test providing several schemas to render using the CLI"""
    test_case1 = "basic"
    test_path1 = get_test_case_path(test_case1)
    test_case2 = "with_default"
    test_path2 = get_test_case_path(test_case2)

    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main,
                               [f"{test_path1},{test_path2}",
                                str(tmp_path)])
        assert_cli_runner_result(result)

        assert (tmp_path / f"{test_case1}.html").exists()
        assert (tmp_path / f"{test_case2}.html").exists()

        assert_css_and_js_copied(tmp_path)
Beispiel #7
0
def test_generate_from_schema_using_path(tmp_path: Path) -> None:
    """Test providing a schema path as a str with the file not opened"""
    rendered = generate_from_schema(get_test_case_path("basic"))

    soup = BeautifulSoup(rendered, "html.parser")

    assert_basic_case(soup)

    assert_css_and_js_not_copied(tmp_path)
def test_generate_from_schema_using_file_object(tmp_path: Path) -> None:
    """Test providing a schema path as an opened file object"""
    with open(get_test_case_path("basic")) as test_case_fp:
        rendered = generate_from_schema(test_case_fp)

    soup = BeautifulSoup(rendered, "html.parser")

    assert_basic_case(soup)

    assert_css_and_js_not_copied(tmp_path)
Beispiel #9
0
def test_cli_output_default_html() -> None:
    """Test the default value for the output path for a single HTML schema"""
    test_path = get_test_case_path("basic")

    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [str(test_path)])
        assert_cli_runner_result(result)

        assert Path("schema_doc.html").exists()
Beispiel #10
0
def test_generate_from_file_name_with_invalid_output_dir(
        tmp_path: Path) -> None:
    """Test generating from file names for input and output where the output directory is absent"""
    test_case_path = get_test_case_path("basic")
    result_path = tmp_path / "nonsense" / "result_with_another_name.html"

    with pytest.raises(FileNotFoundError) as exception_info:
        generate_from_filename(test_case_path, str(result_path.resolve()),
                               False, False, False, False)
        assert f"{os.path.dirname(result_path)} not found" in str(
            exception_info.value)
def test_generate_using_cli_default() -> None:
    """Test the standard case of generating using the CLI"""
    test_path = get_test_case_path("basic")
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [test_path])
        assert_cli_runner_result(result)

        assert Path("schema_doc.html").exists()

        assert_css_and_js_copied(Path.cwd())
def test_generate_using_cli_default_result_file() -> None:
    """Test providing a different result file path to the CLI"""
    test_path = get_test_case_path("basic")
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [test_path, "doc.html"])
        assert_cli_runner_result(result)

        assert Path("doc.html").exists()

        assert_css_and_js_copied(Path.cwd())
Beispiel #13
0
def test_cli_output_default_md() -> None:
    """Test the default value for the output path for a single MD schema"""
    test_path = get_test_case_path("basic")

    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(
            main, [str(test_path), "--config", "template_name=md"])
        assert_cli_runner_result(result)

        assert Path("schema_doc.md").exists()
Beispiel #14
0
def test_nonexistent_output_path() -> None:
    """Test providing a non-existent output path fails with informative error message"""
    test_path = get_test_case_path("basic")
    output_dir = get_nonexistent_output_path("schema")
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [test_path, output_dir])
        assert_cli_runner_exited(
            result,
            f"Output path file is in a directory that does not exist: {os.path.dirname(output_dir)}",
        )
Beispiel #15
0
def test_generate_multiple_path_inputs(tmp_path: Path) -> None:
    """Test generating using the all-purpose "generate" method with multiple Path inputs"""
    test_case1 = "basic"
    test_case2 = "with_default"
    test_case_path1 = get_test_case_path(test_case1)
    test_case_path2 = get_test_case_path(test_case2)

    result_path = tmp_path / "test_generate"
    result_path.mkdir()

    schemas = get_schemas_to_render_from_cli_arguments(test_case_path1,
                                                       result_path, "md")
    schemas += get_schemas_to_render_from_cli_arguments(
        test_case_path2, result_path, "md")

    template_renderer = MagicMock(TemplateRenderer)
    template_renderer.render.return_value = ""
    template_renderer.config = GenerationConfiguration()
    generated = generate_schemas_doc(schemas, template_renderer)

    assert generated is not None
def test_config_file_parameter_json() -> None:
    test_path = get_test_case_path("basic")
    runner = CliRunner()
    with runner.isolated_filesystem():
        config_file_name = "jsfh_config.json"
        with open(config_file_name, "w", encoding="utf-8") as config_file:
            config_file.write("""{"copy_css": false, "copy_js": false}""")

        result = runner.invoke(main, [test_path, "--config-file", config_file_name])
        assert_cli_runner_result(result)

        assert_css_and_js_not_copied(Path.cwd())
Beispiel #17
0
def test_generate_from_file_object_deprecation_warning(
        tmp_path: Path, caplog: LogCaptureFixture, minify: bool,
        expect_warning: bool) -> None:
    result_file_path = tmp_path / "result_file.html"

    caplog.set_level(logging.INFO)
    with open(get_test_case_path("basic")) as test_case_fp:
        with result_file_path.open("w", encoding="utf-8") as result_write_fp:
            generate_from_file_object(test_case_fp,
                                      result_write_fp,
                                      minify=minify)

    _assert_deprecation_message(caplog, expect_warning)
Beispiel #18
0
def test_generate_from_file_name_deprecation_warning(
        tmp_path: Path, caplog: LogCaptureFixture, minify: bool,
        expect_warning: bool) -> None:
    test_case_path = get_test_case_path("basic")
    result_path = tmp_path / "result_file.html"

    caplog.set_level(logging.INFO)

    generate_from_filename(test_case_path,
                           str(result_path.resolve()),
                           minify=minify)

    _assert_deprecation_message(caplog, expect_warning)
def test_generate_from_schema_using_file_object_already_loaded(
        tmp_path: Path) -> None:
    """Test providing a schema path as an opened file object but also the loaded schema in a dict.
    Ensure the schema is not loaded again
    """
    test_case_path = os.path.realpath(get_test_case_path("basic"))

    with open(test_case_path, encoding="utf-8") as test_case_fp:
        loaded = {test_case_path: yaml.safe_load(test_case_fp.read())}

    with patch("yaml.safe_load") as patched_yaml_load:
        with open(get_test_case_path("basic")) as test_case_fp:
            rendered = generate_from_schema(test_case_fp,
                                            loaded_schemas=loaded)

        patched_yaml_load.assert_not_called()

    soup = BeautifulSoup(rendered, "html.parser")

    assert_basic_case(soup)

    assert_css_and_js_not_copied(tmp_path)
Beispiel #20
0
def test_generate_from_schema_config_object(tmp_path: Path,
                                            caplog: LogCaptureFixture) -> None:
    config = GenerationConfiguration(minify=False,
                                     default_from_description=True,
                                     copy_css=False,
                                     copy_js=False)

    caplog.set_level(logging.INFO)

    generate_from_schema(get_test_case_path("basic"), config=config)

    _assert_deprecation_message(caplog, False)
    assert_css_and_js_not_copied(tmp_path)
Beispiel #21
0
def test_generate_from_file_name(tmp_path: Path) -> None:
    """Test generating from file names for input and output"""
    test_case_path = get_test_case_path("basic")
    result_path = tmp_path / "result_with_another_name.html"

    generate_from_filename(test_case_path, str(result_path.resolve()), False,
                           False, False, False)

    with result_path.open(encoding="utf-8") as result_fp:
        soup = BeautifulSoup(result_fp.read(), "html.parser")

    assert (tmp_path / "result_with_another_name.html").exists()

    assert (tmp_path / "schema_doc.min.js").exists()
def test_config_parameters_flags_yes() -> None:
    """Test providing configuration parameters using the --config CLI parameter and the special syntax for flags"""
    test_path = get_test_case_path("basic")
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(main, [test_path, "--config", "expand_buttons"])
        assert_cli_runner_result(result)

        assert_css_and_js_copied(Path.cwd())
        assert Path("schema_doc.html").exists()

        with open("schema_doc.html", "r", encoding="utf-8") as schema_doc:
            soup = BeautifulSoup(schema_doc.read(), "html.parser")
            expand_button = soup.find("button", text="Expand all")
            assert expand_button
Beispiel #23
0
def test_generate_from_file_object(tmp_path: Path) -> None:
    """Test generating from open file objects for input and output"""
    result_file_path = tmp_path / "result_with_another_name.html"

    with open(get_test_case_path("basic")) as test_case_fp:
        with result_file_path.open("w", encoding="utf-8") as result_write_fp:
            generate_from_file_object(test_case_fp, result_write_fp, False,
                                      False, False, False)

    with result_file_path.open(encoding="utf-8") as result_fp:
        soup = BeautifulSoup(result_fp.read(), "html.parser")

    assert_basic_case(soup)

    assert (tmp_path / "result_with_another_name.html").exists()

    assert (tmp_path / "schema_doc.min.js").exists()
Beispiel #24
0
def test_generate_no_file_output(tmp_path: Path,
                                 monkeypatch: MonkeyPatch) -> None:
    """Test generating and getting output as a str instead of writing to file"""
    monkeypatch.chdir(tmp_path)

    test_case_name = "basic"
    test_case_file_name = f"{test_case_name}.json"
    test_case_path = get_test_case_path("basic")

    schemas = get_schemas_to_render(test_case_path, None, "md")
    template_renderer = MagicMock(TemplateRenderer)
    template_renderer.render.return_value = ""
    template_renderer.config = GenerationConfiguration()
    generated = generate_schemas_doc(schemas, template_renderer)

    assert list(generated.keys()) == [test_case_file_name]

    # Ensure no file is written to current working directory
    assert len(list(tmp_path.glob("**"))) == 1  # glob("**") returns itself
def test_references() -> None:
    intermediate = build_intermediate_representation(
        get_test_case_path("references"), GenerationConfiguration())

    assert intermediate
def test_basic() -> None:
    intermediate = build_intermediate_representation(
        get_test_case_path("basic"), GenerationConfiguration())

    assert intermediate