Skip to content

Commit

Permalink
First basic unit test
Browse files Browse the repository at this point in the history
TODO is testing the CLI flag too, and maybe testing that the cleanup works
with custom templates.
  • Loading branch information
jasonmhite committed Jul 17, 2023
1 parent 6101f72 commit 1029481
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
52 changes: 52 additions & 0 deletions tests/framework/cli/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,58 @@ def dummy_config(fake_root_dir, fake_metadata):

return config_path

def write_file_contents(fname: Path, contents: str):
"""Little helper to make setting up a test template dir easier.
Automatically creates the parent dir of a file first if it doesn't exist to cut
down on extraneous LOC. SO BE WARNED you need to clean up after yourself.
"""
fname.parent.mkdir(parents=True, exist_ok=True)

with fname.open('w') as f:
f.write(contents)

@fixture()
def fake_local_template_dir(fake_repo_path):
"""Set up a local template directory. This won't be functional we're just testing the actual layout works.
Note that this is not scoped to module because we don't want to have this folder present in most of the tests,
so we will tear it down every time.
"""
template_path = (fake_repo_path / Path("templates"))
pipeline_template_path = template_path / Path("pipeline")
cookiecutter_template_path = pipeline_template_path / "{{ cookiecutter.pipeline_name }}"

cookiecutter_template_path.mkdir(parents=True)

# Create the absolute bare minimum files
write_file_contents(
pipeline_template_path / "cookiecutter.json",
"""
{"pipeline_name": "default", "kedro_version": "{{ cookiecutter.kedro_version }}"}
""".strip()
)

write_file_contents(
cookiecutter_template_path / "pipeline_{{ cookiecutter.pipeline_name }}.py",
r"print('hello world')",
)

write_file_contents(cookiecutter_template_path / "__init__.py", "")

write_file_contents(
cookiecutter_template_path / r"config/parameters/{{ cookiecutter.pipeline_name }}.yml",
"foo: bar",
)

write_file_contents(
cookiecutter_template_path / r"tests/test_{{ cookiecutter.pipeline_name }}.py",
"",
)

yield template_path.resolve()

shutil.rmtree(template_path)

@fixture(scope="module")
def fake_metadata(fake_root_dir):
Expand Down
25 changes: 24 additions & 1 deletion tests/framework/cli/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,32 @@ def test_create_pipeline( # pylint: disable=too-many-locals
actual_files = {f.name for f in test_dir.iterdir()}
assert actual_files == expected_files

@pytest.mark.parametrize("env", [None, "local"])
def test_create_pipeline_template( # pylint: disable=too-many-locals
self, fake_repo_path, fake_project_cli, fake_metadata, env, fake_package_path, fake_local_template_dir
):
pipelines_dir = fake_package_path / "pipelines"
assert pipelines_dir.is_dir()

assert not (pipelines_dir / PIPELINE_NAME).exists()

cmd = ["pipeline", "create", PIPELINE_NAME]
cmd += ["-e", env] if env else []
result = CliRunner().invoke(fake_project_cli, cmd, obj=fake_metadata)

assert f"Using pipeline template at: '{fake_repo_path / 'templates'}" in result.output
assert f"Creating the pipeline '{PIPELINE_NAME}': OK" in result.output
assert f"Location: '{pipelines_dir / PIPELINE_NAME}'" in result.output
assert f"Pipeline '{PIPELINE_NAME}' was successfully created." in result.output

# Dummy pipeline rendered correctly
assert((pipelines_dir / PIPELINE_NAME / f"pipeline_{PIPELINE_NAME}.py").exists())

assert result.exit_code == 0

@pytest.mark.parametrize("env", [None, "local"])
def test_create_pipeline_skip_config(
self, fake_repo_path, fake_project_cli, fake_metadata, env
self, fake_repo_path, fake_project_cli, fake_metadata, env
):
"""Test creation of a pipeline with no config"""

Expand Down

0 comments on commit 1029481

Please sign in to comment.