Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): show dataset metadata for tag #2919

Merged
merged 4 commits into from May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions renku/core/dataset/dataset.py
Expand Up @@ -733,17 +733,30 @@ def update_datasets(
return imported_datasets_view_models, dataset_files_view_models


def show_dataset(name):
def show_dataset(name: str, tag: Optional[str] = None):
"""Show detailed dataset information.

Args:
name: Name of dataset to show details for.
name(str): Name of dataset to show details for.
tag(str, optional): Tags for which to get the metadata (Default value = None).

Returns:
dict: JSON dictionary of dataset details.
"""
dataset = DatasetsProvenance().get_by_name(name, strict=True)
datasets_provenance = DatasetsProvenance()
dataset = datasets_provenance.get_by_name(name, strict=True)
m-alisafaee marked this conversation as resolved.
Show resolved Hide resolved

if tag is None:
return DatasetDetailsJson().dump(dataset)

tags = datasets_provenance.get_all_tags(dataset=cast(Dataset, dataset))

selected_tag = next((t for t in tags if t.name == tag), None)

if selected_tag is None:
raise errors.DatasetTagNotFound(tag)

dataset = datasets_provenance.get_by_id(selected_tag.dataset_id.value)
return DatasetDetailsJson().dump(dataset)


Expand Down
8 changes: 8 additions & 0 deletions renku/core/errors.py
Expand Up @@ -270,6 +270,14 @@ def __init__(self, *, name=None, message=None):
super().__init__(msg)


class DatasetTagNotFound(RenkuException):
"""Raise when a tag can't be found."""

def __init__(self, tag) -> None:
msg = f"Couldn't find dataset tag '{tag}'."
super().__init__(msg)


class ExternalFileNotFound(RenkuException):
"""Raise when an external file is not found."""

Expand Down
7 changes: 5 additions & 2 deletions renku/ui/cli/dataset.py
Expand Up @@ -133,6 +133,8 @@
Description:
Just some dataset

You can also show details for a specific tag using the ``--tag`` option.

Deleting a dataset:

.. code-block:: console
Expand Down Expand Up @@ -681,13 +683,14 @@ def edit(name, title, description, creators, metadata, keyword):


@dataset.command("show")
@click.option("-t", "--tag", default=None, type=click.STRING, help="Tag for which to show dataset metadata.")
@click.argument("name", shell_complete=_complete_datasets)
def show(name):
def show(tag, name):
"""Show metadata of a dataset."""
from renku.command.dataset import show_dataset_command
from renku.ui.cli.utils.terminal import print_markdown

result = show_dataset_command().build().execute(name=name)
result = show_dataset_command().build().execute(name=name, tag=tag)
ds = result.output

click.echo(click.style("Name: ", bold=True, fg=color.MAGENTA) + click.style(ds["name"], bold=True))
Expand Down
63 changes: 63 additions & 0 deletions tests/cli/test_datasets.py
Expand Up @@ -128,6 +128,69 @@ def test_dataset_show(runner, client, subdirectory):
assert "##" not in result.output


def test_dataset_show_tag(runner, client, subdirectory):
"""Test creating and showing a dataset with metadata."""
result = runner.invoke(cli, ["dataset", "show", "my-dataset"])
assert 1 == result.exit_code, format_result_exception(result)
assert 'Dataset "my-dataset" is not found.' in result.output

metadata = {
"@id": "https://example.com/annotation1",
"@type": "https://schema.org/specialType",
"https://schema.org/specialProperty": "some_unique_value",
}
metadata_path = client.path / "metadata.json"
metadata_path.write_text(json.dumps(metadata))

result = runner.invoke(
cli,
[
"dataset",
"create",
"my-dataset",
"--title",
"Long Title",
"--description",
"description1",
],
)
assert 0 == result.exit_code, format_result_exception(result)
assert "OK" in result.output

result = runner.invoke(cli, ["dataset", "tag", "my-dataset", "tag1"])
assert 0 == result.exit_code, format_result_exception(result)

result = runner.invoke(cli, ["dataset", "edit", "-d", "description2", "my-dataset"])
assert 0 == result.exit_code, format_result_exception(result)
assert "Successfully updated: description" in result.output

result = runner.invoke(cli, ["dataset", "tag", "my-dataset", "tag2"])
assert 0 == result.exit_code, format_result_exception(result)

result = runner.invoke(cli, ["dataset", "edit", "-d", "description3", "my-dataset"])
assert 0 == result.exit_code, format_result_exception(result)
assert "Successfully updated: description" in result.output

result = runner.invoke(cli, ["dataset", "tag", "my-dataset", "tag3"])
assert 0 == result.exit_code, format_result_exception(result)

result = runner.invoke(cli, ["dataset", "show", "my-dataset"])
assert 0 == result.exit_code, format_result_exception(result)
assert "description3" in result.output

result = runner.invoke(cli, ["dataset", "show", "--tag", "tag3", "my-dataset"])
assert 0 == result.exit_code, format_result_exception(result)
assert "description3" in result.output
Panaetius marked this conversation as resolved.
Show resolved Hide resolved

result = runner.invoke(cli, ["dataset", "show", "--tag", "tag2", "my-dataset"])
assert 0 == result.exit_code, format_result_exception(result)
assert "description2" in result.output

result = runner.invoke(cli, ["dataset", "show", "--tag", "tag1", "my-dataset"])
assert 0 == result.exit_code, format_result_exception(result)
assert "description1" in result.output


def test_datasets_create_different_names(runner, client):
"""Test creating datasets with same title but different name."""
result = runner.invoke(cli, ["dataset", "create", "dataset-1", "--title", "title"])
Expand Down