-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from tdari/feat/add_piece_command
feat(cli): add create piece functionality
- Loading branch information
Showing
6 changed files
with
299 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
from domino.cli import cli | ||
|
||
|
||
@pytest.fixture | ||
def runner(): | ||
return CliRunner() | ||
|
||
|
||
def test_create_piece_success_in_pieces_dir_without_repository_path( | ||
runner, tmpdir, monkeypatch | ||
): | ||
pieces_path = str(Path(tmpdir.mkdir("pieces"))) | ||
piece_name = "TestPiece" | ||
monkeypatch.chdir(pieces_path) | ||
result = runner.invoke(cli.cli_create_piece, ["--name", f"{piece_name}"]) | ||
assert result.exit_code == 0 | ||
|
||
|
||
def test_create_piece_success_in_repository_dir_without_repository_path( | ||
runner, tmpdir, monkeypatch | ||
): | ||
repository_path = Path(tmpdir.mkdir("repo")) | ||
piece_name = "TestPiece" | ||
monkeypatch.chdir(repository_path) | ||
result = runner.invoke(cli.cli_create_piece, ["--name", f"{piece_name}"]) | ||
assert result.exit_code == 0 | ||
|
||
|
||
def test_create_piece_success_with_repository_path(runner, tmpdir): | ||
repository_path = Path(tmpdir.mkdir("repo")) | ||
tmpdir.mkdir("repo/pieces") | ||
piece_name = "TestPiece" | ||
result = runner.invoke( | ||
cli.cli_create_piece, | ||
["--name", f"{piece_name}", "--repository-path", f"{repository_path}"], | ||
) | ||
assert result.exit_code == 0 | ||
|
||
|
||
def test_create_piece_success_in_pieces_dir_without_args(runner, tmpdir): | ||
tmpdir.mkdir("repo") | ||
tmpdir.mkdir("repo/pieces") | ||
result = runner.invoke(cli.cli_create_piece) | ||
assert result.exit_code == 0 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"piece_name", | ||
[ | ||
"1TestPiece", | ||
"Test", | ||
"TestPiec", | ||
"Test Piece", | ||
"Testpiece", | ||
"TESTPIECE", | ||
"", | ||
" ", | ||
".", | ||
], | ||
) | ||
def test_create_piece_fail_invalid_piece_name(runner, tmpdir, piece_name): | ||
repository_path = (Path(tmpdir.mkdir("repo")),) | ||
result = runner.invoke( | ||
cli.cli_create_piece, | ||
["--name", f"{piece_name}", "--repository-path", f"{repository_path}"], | ||
) | ||
if len(piece_name) < 1: | ||
assert "Piece name must have at least one character." in result.output | ||
else: | ||
assert ( | ||
f"Validation Error: {piece_name} is not a valid piece name." | ||
in result.output | ||
) | ||
|
||
|
||
def test_create_piece_already_exists(runner, tmpdir): | ||
repository_path = Path(tmpdir.mkdir("repo")) | ||
tmpdir.mkdir("repo/pieces") | ||
piece_name = "TestPiece" | ||
runner.invoke( | ||
cli.cli_create_piece, | ||
["--name", f"{piece_name}", "--repository-path", f"{repository_path}"], | ||
) | ||
result = runner.invoke( | ||
cli.cli_create_piece, | ||
["--name", f"{piece_name}", "--repository-path", f"{repository_path}"], | ||
) | ||
assert f"{piece_name} is already exists" in result.output | ||
|
||
|
||
def test_create_piece_invalid_pieces_path(runner, tmpdir): | ||
repository_path = Path(tmpdir.mkdir("repo")) | ||
piece_name = "TestPiece" | ||
result = runner.invoke( | ||
cli.cli_create_piece, | ||
["--name", f"{piece_name}", "--repository-path", f"{repository_path}"], | ||
) | ||
assert f"{repository_path / 'pieces'} is not a valid repository path." | ||
|
||
|
||
def test_create_piece_pieces_directory_not_exists(runner, tmpdir, monkeypatch): | ||
repository_path = Path(tmpdir.mkdir("repo")) | ||
monkeypatch.chdir(repository_path) | ||
result = runner.invoke(cli.cli_create_piece, ["--name", "TestPiece"]) | ||
assert "No pieces directory found." in result.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import textwrap | ||
from string import Template | ||
|
||
def piece_function(name: str): | ||
template = textwrap.dedent(\ | ||
'''\ | ||
from domino.base_piece import BasePiece | ||
from .models import InputModel, OutputModel | ||
class $name(BasePiece): | ||
def piece_function(self, input_data: InputModel): | ||
output_value = "" | ||
return OutputModel(output_field=output_value) | ||
''') | ||
template = Template(template) | ||
return template.substitute({"name": name}) | ||
|
||
def piece_models(name: str): | ||
template = textwrap.dedent(\ | ||
'''\ | ||
from pydantic import BaseModel, Field | ||
class InputModel(BaseModel): | ||
""" | ||
$name Input Model | ||
""" | ||
field: str = Field( | ||
description="Input field.", | ||
) | ||
class OutputModel(BaseModel): | ||
""" | ||
$name Output Model | ||
""" | ||
field: str = Field( | ||
description="Output field.", | ||
) | ||
''') | ||
template = Template(template) | ||
return template.substitute({"name": name}) | ||
|
||
def piece_test(name: str): | ||
template = textwrap.dedent(\ | ||
'''\ | ||
from domino.testing import piece_dry_run | ||
def test_$name(): | ||
input = {"field": ""} | ||
output = piece_dry_run( | ||
"$name", | ||
input, | ||
) | ||
assert output["field"] is not None | ||
''') | ||
template = Template(template) | ||
return template.substitute({"name": name}) | ||
|
||
def piece_metadata(piece_name: str): | ||
metadata = { | ||
"name": f"{piece_name}", | ||
"dependency": { | ||
"dockerfile": None, | ||
"requirements_file": None | ||
}, | ||
"tags": [], | ||
"style": { | ||
"node_label": "Piece", | ||
"node_type": "default", | ||
"node_style": { | ||
"backgroundColor": "#ebebeb" | ||
}, | ||
"useIcon": True, | ||
"icon_class_name": "fa-solid fa-circle", | ||
"iconStyle": { | ||
"cursor": "pointer" | ||
} | ||
}, | ||
} | ||
|
||
return metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters