Ejemplo n.º 1
0
def json(
    file: str,
    output: str = typer.Option(None, help="save output into a file."),
) -> None:
    """Generate json from ADFH file.

    Args:
        file: Path to ADFH file.
        output: If --output is used, it will save it in file.
    """
    console = Console()
    data = read_adfh_file(file=file)

    if type(data) == str:
        console.print(data, style="bold red")

    else:
        ihsan_type = IhsanType(**data)  # type: ignore

        if output:
            typer.confirm(
                f"The output will be saved in {output}, are you sure?",
                abort=True)
            with open(output, "w") as output_file:
                output_file.write(ihsan_type.json())
        else:
            console.print(ihsan_type.json(), style="bold green")
Ejemplo n.º 2
0
def sdl(
    file: str,
    output: str = typer.Option(None, help="save output into a file."),
) -> None:
    """Generate SDL aka GraphQL schema from ADFH file.

    Args:
        file: Path to ADFH file.
        output: If --output is used, it will save it in file.
    """
    console = Console()
    data = read_adfh_file(file=file)

    if type(data) == str:
        console.print(data, style="bold red")

    else:
        ihsan_type = IhsanType(**data)  # type: ignore
        sdl_output = to_sdl(schema=ihsan_type)
        if output:
            typer.confirm(
                f"The output will be saved in {output}, are you sure?",
                abort=True)
            with open(output, "w") as output_file:
                output_file.write(sdl_output)
        else:
            console.print(sdl_output, style="bold green")
        console.print(
            "Use -> https://app.graphqleditor.com/ to test the schema :)",
            style="bold blue",
        )
Ejemplo n.º 3
0
def test_to_sdl_successfully(tmp_path: pathlib.PosixPath,
                             toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    sdl_output = to_sdl(schema=ihsan_type)

    assert "enum StatusType" in sdl_output
    assert "REJECTED" in sdl_output
    assert "APPROVED" in sdl_output
    assert "DENY" in sdl_output
    assert "type Item" in sdl_output
    assert "type Company" in sdl_output
    assert "title: String!" in sdl_output
    assert "id: String" in sdl_output
    assert "status: StatusType" in sdl_output
    assert "is_done: Boolean" in sdl_output
    assert "view: Int" in sdl_output
    assert "type Query" in sdl_output
    assert "todoList: [Item]" in sdl_output
    assert "CompanyList: [Company]" in sdl_output
    assert "todoDetail(id: String, ): Item" in sdl_output
    assert "type Mutation" in sdl_output
    assert "todoAdd(title: String!, ): Item" in sdl_output
    assert "companyRemove(id: String, ): Company" in sdl_output
    assert "schema" in sdl_output
    assert "query: Query" in sdl_output
    assert "mutation: Mutation" in sdl_output
Ejemplo n.º 4
0
def test_find_field_fail(tmp_path: pathlib.PosixPath, toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    with pytest.raises(ValidationError):
        assert find_field(fields=ihsan_type.adfh.fields_list,
                          field_id="my not awesome id")
Ejemplo n.º 5
0
def test_find_action_fail(tmp_path: pathlib.PosixPath, toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    action = find_action(actions=ihsan_type.adfh.actions, keyword="list view")

    assert action == []
Ejemplo n.º 6
0
def test_find_model_successfully(tmp_path: pathlib.PosixPath,
                                 toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    model = find_model(models=ihsan_type.adfh.models,
                       model_id="my awesome Item")
    assert model.id == "my awesome Item"
    assert model.name == "Item"
Ejemplo n.º 7
0
def test_get_all_field_with_certain_type_fail(tmp_path: pathlib.PosixPath,
                                              toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    fields = get_all_field_with_certain_type(
        fields=ihsan_type.adfh.fields_list, keyword="not choice")

    assert fields == []
Ejemplo n.º 8
0
def test_find_field_successfully(tmp_path: pathlib.PosixPath,
                                 toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    field = find_field(fields=ihsan_type.adfh.fields_list,
                       field_id="my awesome id")
    assert field.id == "my awesome id"
    assert field.name == "id"
    assert field.type == "String"
    assert field.mandatory == ""
    assert field.options is None
    assert field.text is None
Ejemplo n.º 9
0
def test_get_all_field_with_certain_type_successfully(
        tmp_path: pathlib.PosixPath, toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    fields = get_all_field_with_certain_type(
        fields=ihsan_type.adfh.fields_list, keyword="choice")

    assert type(fields) == list
    assert fields[0].id == "my awesome status"
    assert fields[0].name == "status"
    assert fields[0].type == "choice"
    assert fields[0].options == ["rejected", "approved", "deny"]
    assert fields[0].mandatory == "no"
Ejemplo n.º 10
0
def test_to_sdl_without_choice_successfully(
        tmp_path: pathlib.PosixPath, toml_adfh_without_choice: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh_without_choice)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    sdl_output = to_sdl(schema=ihsan_type)

    assert "type Item" in sdl_output
    assert "type Company" in sdl_output
    assert "title: String!" in sdl_output
    assert "id: String" in sdl_output
    assert "is_done: Boolean" in sdl_output
    assert "view: Int" in sdl_output
    assert "schema" in sdl_output
    assert "query: Query" in sdl_output
    assert "mutation: Mutation" in sdl_output
Ejemplo n.º 11
0
def test_find_action_successfully(tmp_path: pathlib.PosixPath,
                                  toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    ihsan_type = IhsanType(**data)
    list_action = find_action(actions=ihsan_type.adfh.actions,
                              keyword="show me list")
    remove_action = find_action(actions=ihsan_type.adfh.actions,
                                keyword="let me remove")
    add_action = find_action(actions=ihsan_type.adfh.actions,
                             keyword="let me add")
    detail_action = find_action(actions=ihsan_type.adfh.actions,
                                keyword="show me a certain item")

    assert type(list_action) == list
    assert type(remove_action) == list
    assert type(add_action) == list
    assert type(detail_action) == list
    assert len(list_action) == 2
    assert len(remove_action) == 1
    assert len(add_action) == 1
    assert len(detail_action) == 1
Ejemplo n.º 12
0
def test_to_sdl_without_actions_successfully(tmp_path: pathlib.PosixPath,
                                             toml_adfh: str) -> None:
    """It exits with a status code of zero."""
    create_adfh_file(directory_path=tmp_path, adfh=toml_adfh)
    data = read_adfh_file(file=(pathlib.Path(tmp_path) /
                                "adfh.toml").as_posix())
    data["adfh"].pop("actions")
    ihsan_type = IhsanType(**data)
    sdl_output = to_sdl(schema=ihsan_type)

    assert "enum StatusType" in sdl_output
    assert "REJECTED" in sdl_output
    assert "APPROVED" in sdl_output
    assert "DENY" in sdl_output
    assert "type Item" in sdl_output
    assert "type Company" in sdl_output
    assert "title: String!" in sdl_output
    assert "id: String" in sdl_output
    assert "status: StatusType" in sdl_output
    assert "is_done: Boolean" in sdl_output
    assert "view: Int" in sdl_output
    assert "schema" in sdl_output
    assert "query: Query" in sdl_output
    assert "mutation: Mutation" in sdl_output