From 43a0dcbb366b30392d2ae4e4d9928671ab3c3043 Mon Sep 17 00:00:00 2001 From: Hendrik Makait Date: Tue, 30 Jan 2024 20:24:07 +0100 Subject: [PATCH] Create namespace from CLI --- pyiceberg/cli/console.py | 21 +++++++++++++++++++-- tests/cli/test_console.py | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pyiceberg/cli/console.py b/pyiceberg/cli/console.py index 092910b5f6..095c1ef6dc 100644 --- a/pyiceberg/cli/console.py +++ b/pyiceberg/cli/console.py @@ -206,6 +206,23 @@ def version(ctx: Context) -> None: ctx.obj["output"].version(__version__) +@run.group() +def create() -> None: + """Operation to create a namespace.""" + + +@create.command() +@click.argument("identifier") +@click.pass_context +@catch_exception() +def namespace(ctx: Context, identifier: str) -> None: + """Create a namespace.""" + catalog, output = _catalog_and_output(ctx) + + catalog.create_namespace(identifier) + output.text(f"Created namespace: {identifier}") + + @run.group() def drop() -> None: """Operations to drop a namespace or table.""" @@ -223,11 +240,11 @@ def table(ctx: Context, identifier: str) -> None: # noqa: F811 output.text(f"Dropped table: {identifier}") -@drop.command() +@drop.command() # type: ignore @click.argument("identifier") @click.pass_context @catch_exception() -def namespace(ctx: Context, identifier: str) -> None: +def namespace(ctx: Context, identifier: str) -> None: # noqa: F811 """Drop a namespace.""" catalog, output = _catalog_and_output(ctx) diff --git a/tests/cli/test_console.py b/tests/cli/test_console.py index 5eec231cca..d77b290ec6 100644 --- a/tests/cli/test_console.py +++ b/tests/cli/test_console.py @@ -308,6 +308,22 @@ def test_drop_namespace_does_not_exists(catalog: InMemoryCatalog) -> None: assert result.output == "Namespace does not exist: ('doesnotexist',)\n" +def test_create_namespace(catalog: InMemoryCatalog) -> None: + runner = CliRunner() + result = runner.invoke(run, ["create", "namespace", TEST_TABLE_NAMESPACE]) + assert result.exit_code == 0 + assert result.output == """Created namespace: default\n""" + + +def test_create_namespace_already_exists(catalog: InMemoryCatalog) -> None: + catalog.create_namespace(TEST_TABLE_NAMESPACE) + + runner = CliRunner() + result = runner.invoke(run, ["create", "namespace", TEST_TABLE_NAMESPACE]) + assert result.exit_code == 1 + assert result.output == "Namespace already exists: ('default',)\n" + + def test_rename_table(catalog: InMemoryCatalog) -> None: catalog.create_table( identifier=TEST_TABLE_IDENTIFIER,