Skip to content

Commit

Permalink
SNOW-1011771: Updating tests for REPLACE AND IF NOT EXISTS case on im…
Browse files Browse the repository at this point in the history
…age-repository create to throw error
  • Loading branch information
sfc-gh-davwang committed Feb 28, 2024
1 parent b091164 commit c52e33a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/snowflake/cli/api/commands/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 10 additions & 5 deletions src/snowflake/cli/plugins/spcs/image_repository/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 26 additions & 6 deletions tests/spcs/test_image_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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."]],
Expand All @@ -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(
Expand Down

0 comments on commit c52e33a

Please sign in to comment.