From c52e33a01176b742bf3b3d2f1689a11928cf95a3 Mon Sep 17 00:00:00 2001 From: David Wang Date: Wed, 28 Feb 2024 11:15:27 -0800 Subject: [PATCH] SNOW-1011771: Updating tests for REPLACE AND IF NOT EXISTS case on image-repository create to throw error --- src/snowflake/cli/api/commands/flags.py | 4 ++- .../plugins/spcs/image_repository/manager.py | 15 ++++++--- tests/spcs/test_image_repository.py | 32 +++++++++++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/snowflake/cli/api/commands/flags.py b/src/snowflake/cli/api/commands/flags.py index f109d2f9e9..f77a654083 100644 --- a/src/snowflake/cli/api/commands/flags.py +++ b/src/snowflake/cli/api/commands/flags.py @@ -246,7 +246,9 @@ def _password_callback(value: str): def _create_mode_callback(ctx: typer.Context, param: typer.CallbackParam, value: bool): for key in CREATE_MODE_OPTION_NAMES: - if ctx.params.get(key, False): + if value and ctx.params.get( + key, False + ): # if the current parameter is set to True and a previous parameter is also Truthy curr_opt = param.opts[0] key_opt = [x for x in ctx.command.params if x.name == key][0].opts[0] raise click.ClickException( diff --git a/src/snowflake/cli/plugins/spcs/image_repository/manager.py b/src/snowflake/cli/plugins/spcs/image_repository/manager.py index 0dc686a9bf..53116ffddc 100644 --- a/src/snowflake/cli/plugins/spcs/image_repository/manager.py +++ b/src/snowflake/cli/plugins/spcs/image_repository/manager.py @@ -57,12 +57,17 @@ def create( if_not_exists: bool, replace: bool, ): + if if_not_exists and replace: + raise ValueError( + "'replace' and 'if_not_exists' options are mutually exclusive for ImageRepositoryManager.create" + ) + elif replace: + create_statement = "create or replace image repository" + elif if_not_exists: + create_statement = "create image repository if not exists" + else: + create_statement = "create image repository" - create_statement = ( - f"{'create or replace' if replace else 'create'} image repository" - ) - if if_not_exists: - create_statement = f"{create_statement} if not exists" try: return self._execute_schema_query(f"{create_statement} {name}") except ProgrammingError as e: diff --git a/tests/spcs/test_image_repository.py b/tests/spcs/test_image_repository.py index 6afcb78777..df06ef9798 100644 --- a/tests/spcs/test_image_repository.py +++ b/tests/spcs/test_image_repository.py @@ -50,7 +50,7 @@ (False, False, "create image repository test_repo"), (False, True, "create image repository if not exists test_repo"), (True, False, "create or replace image repository test_repo"), - (True, True, "create or replace image repository if not exists test_repo"), + # (True, True) is an invalid case as OR REPLACE and IF NOT EXISTS are mutually exclusive. ], ) @mock.patch( @@ -67,14 +67,22 @@ def test_create(mock_execute, replace, if_not_exists, expected_query): assert result == cursor +def test_create_replace_and_if_not_exist(): + with pytest.raises(ValueError) as e: + ImageRepositoryManager().create( + name="test_repo", replace=True, if_not_exists=True + ) + assert "mutually exclusive" in str(e.value) + + @pytest.mark.parametrize( "replace, if_not_exists", - [(False, False), (True, False), (False, True), (True, True)], + [(False, False), (True, False), (False, True)], ) @mock.patch( "snowflake.cli.plugins.spcs.image_repository.manager.ImageRepositoryManager.create" ) -def test_create_cli(mock_create, mock_cursor, runner, replace, if_not_exists): +def test_create_cli(mock_create, mock_cursor, runner, replace, if_not_exists, snapshot): repo_name = "test_repo" cursor = mock_cursor( rows=[[f"Image Repository {repo_name.upper()} successfully created."]], @@ -91,9 +99,21 @@ def test_create_cli(mock_create, mock_cursor, runner, replace, if_not_exists): name=repo_name, replace=replace, if_not_exists=if_not_exists ) assert result.exit_code == 0, result.output - assert ( - f"Image Repository {repo_name.upper()} successfully created." in result.output - ) + assert result.output == snapshot + + +def test_create_cli_replace_and_if_not_exists(runner, snapshot): + command = [ + "spcs", + "image-repository", + "create", + "test_repo", + "--replace", + "--if-not-exists", + ] + result = runner.invoke(command) + assert result.exit_code == 1 + assert result.output == snapshot @mock.patch(