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 : add python -m argilla workspaces create command #3676

Merged
merged 16 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ These are the section headers that we use:
- Added `filter_by` method in `RemoteFeedbackDataset` to filter based on `response_status` ([#3610](https://github.com/argilla-io/argilla/pull/3610)).
- Added `list_workspaces` function (to be used as `rg.list_workspaces`, but `Workspace.list` is preferred) to list all the workspaces from an user in Argilla ([#3641](https://github.com/argilla-io/argilla/pull/3641)).
- Added `list_datasets` function (to be used as `rg.list_datasets`) to list the `TextClassification`, `TokenClassification`, and `Text2Text` datasets in Argilla ([#3638](https://github.com/argilla-io/argilla/pull/3638)).
- Added `workspaces list` command to list Argilla workspaces ([#3594](https://github.com/argilla-io/argilla/pull/3594)).
- Added `datasets list` command to list Argilla datasets ([#3658](https://github.com/argilla-io/argilla/pull/3658)).
- Added `users create` command to create users ([#3667](https://github.com/argilla-io/argilla/pull/3667)).
- Added `users delete` command to delete users ([#3671](https://github.com/argilla-io/argilla/pull/3671)).
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
- Added `workspaces create` command to create an Argilla workspace ([#3676](https://github.com/argilla-io/argilla/pull/3676)).
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
- Added `RemoteSuggestionSchema` to manage suggestions in Argilla, including the `delete` method to delete suggestios from Argilla via `DELETE /api/v1/suggestions/{suggestion_id}` ([#3651](https://github.com/argilla-io/argilla/pull/3651)).
- Added `delete_suggestions` to `RemoteFeedbackRecord` to remove suggestions from Argilla via `DELETE /api/v1/records/{record_id}/suggestions` ([#3651](https://github.com/argilla-io/argilla/pull/3651)).

Expand Down
2 changes: 2 additions & 0 deletions src/argilla/tasks/workspaces/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
from argilla.tasks.async_typer import AsyncTyper
from argilla.tasks.callback import init_callback

from .create import create_workspace
from .list import list_workspaces

app = AsyncTyper(help="Holds CLI commands for workspace management.", no_args_is_help=True, callback=init_callback)

app.command(name="list", help="Lists workspaces of the logged user.")(list_workspaces)
app.command(name="create", help="Create a workspace for the logged user.")(create_workspace)
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
Expand Down
41 changes: 41 additions & 0 deletions src/argilla/tasks/workspaces/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import typer

from argilla.client.workspaces import Workspace


def create_workspace(
name: str = typer.Argument(
default=None,
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
help="The name of the workspace to be created",
)
) -> None:
"""Creates a workspace for the logged user in Argilla"""
if not name:
raise typer.BadParameter("Workspace name must be specified.")
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
try:
Workspace.create(name=name)
typer.echo(f"Workspace with the name=`{name}` successfully created.")
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
except ValueError as e:
typer.echo(e)
raise typer.Exit(code=1) from e
except RuntimeError as e:
typer.echo("An unexpected error occurred when trying to create the workspace")
raise typer.Exit(code=1) from e


if __name__ == "__main__":
typer.run(create_workspace)
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
73 changes: 73 additions & 0 deletions tests/unit/tasks/workspaces/test_create.py
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
eshwarhs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING
from unittest.mock import ANY

import pytest

if TYPE_CHECKING:
from click.testing import CliRunner
from pytest_mock import MockerFixture
from typer import Typer

from argilla.client.workspaces import Workspace


@pytest.mark.usefixtures("login_mock")
def test_cli_workspaces_create_without_name(cli_runner: "CliRunner", cli: "Typer", mocker: "MockerFixture"):
result = cli_runner.invoke(cli, "workspaces create")
assert result.exit_code == 2


@pytest.mark.usefixtures("not_logged_mock")
def test_cli_workspaces_create_needs_login(cli_runner: "CliRunner", cli: "Typer", mocker: "MockerFixture"):
result = cli_runner.invoke(cli, "workspaces create")

assert "You are not logged in. Please run `argilla login` to login to an Argilla server." in result.stdout
assert result.exit_code == 1


@pytest.mark.usefixtures("login_mock")
def test_cli_workspaces_create_with_name(cli_runner: "CliRunner", cli: "Typer", mocker: "MockerFixture") -> None:
workspaces_create_mock = mocker.patch("argilla.client.workspaces.Workspace.create")
result = cli_runner.invoke(cli, "workspaces create workspace25")

assert result.exit_code == 0
workspaces_create_mock.assert_called_once_with(name="workspace25")


@pytest.mark.usefixtures("login_mock")
def test_workspace_create_already_exists(cli_runner: "CliRunner", cli: "Typer", mocker: "MockerFixture"):
mocker.patch(
"argilla.client.workspaces.Workspace.create",
side_effect=ValueError("Workspace with name=`workspace1` already exists, so please use a different name."),
)

result = cli_runner.invoke(cli, "workspaces create workspace1")

assert result.stdout == "Workspace with name=`workspace1` already exists, so please use a different name.\n"
assert result.exit_code == 1


@pytest.mark.usefixtures("login_mock")
def test_workspace_create_runtime_exception(cli_runner: "CliRunner", cli: "Typer", mocker: "MockerFixture"):
mocker.patch(
"argilla.client.workspaces.Workspace.create",
side_effect=RuntimeError("An unexpected error occurred when trying to create the workspace"),
)

result = cli_runner.invoke(cli, "workspaces create workspace1")
assert result.stdout == "An unexpected error occurred when trying to create the workspace\n"
assert result.exit_code == 1
Loading