Skip to content

Commit

Permalink
SNOW-1011761: Adding unit tests for logic when creating an image repo…
Browse files Browse the repository at this point in the history
…sitory, service, or compute pool that already exists
  • Loading branch information
sfc-gh-davwang committed Feb 7, 2024
1 parent e7d9e1c commit 4dd84a9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/snowflake/cli/plugins/spcs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def handle_object_already_exists(
name=unquote_identifier(object_name),
)
else:
raise
raise error
24 changes: 24 additions & 0 deletions tests/spcs/test_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from snowflake.cli.plugins.spcs.common import validate_and_set_instances
from tests.testing_utils.fixtures import *
from click import ClickException
from snowflake.connector.errors import ProgrammingError
from snowflake.cli.plugins.spcs.common import handle_object_already_exists
from snowflake.cli.api.exceptions import ObjectAlreadyExistsError, ObjectType
from unittest.mock import Mock


@pytest.mark.parametrize(
Expand Down Expand Up @@ -36,3 +40,23 @@ def test_validate_and_set_instances_invalid(min_instances, max_instances, expect
with pytest.raises(ClickException) as exc:
validate_and_set_instances(min_instances, max_instances, "name")
assert expected_msg in exc.value.message


SPCS_OBJECT_EXISTS_ERROR = ProgrammingError(
msg="Object 'TEST_OBJECT' already exists.", errno=2002
)


def test_handle_object_exists_error():
mock_type = Mock(spec=ObjectType)
test_name = "TEST_OBJECT"
with pytest.raises(ObjectAlreadyExistsError):
handle_object_already_exists(SPCS_OBJECT_EXISTS_ERROR, mock_type, test_name)


def test_handle_object_exists_error_other_error():
# For any errors other than 'Object 'XYZ' already exists.', simply pass the error through
other_error = ProgrammingError(msg="Object does not already exist.", errno=0)
with pytest.raises(ProgrammingError) as e:
handle_object_already_exists(other_error, Mock(spec=ObjectType), "TEST_OBJECT")
assert other_error == e.value
24 changes: 24 additions & 0 deletions tests/spcs/test_compute_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from snowflake.cli.plugins.spcs.compute_pool.manager import ComputePoolManager
from snowflake.connector.cursor import SnowflakeCursor
from snowflake.cli.api.project.util import to_string_literal
from tests.spcs.test_common import SPCS_OBJECT_EXISTS_ERROR
from snowflake.cli.api.constants import ObjectType


@patch(
Expand Down Expand Up @@ -106,6 +108,28 @@ def test_create_pool_cli(mock_create, runner):
)


@patch(
"snowflake.cli.plugins.spcs.compute_pool.manager.ComputePoolManager._execute_query"
)
@patch("snowflake.cli.plugins.spcs.compute_pool.manager.handle_object_already_exists")
def test_create_repository_already_exists(mock_handle, mock_execute):
pool_name = "test_object"
mock_execute.side_effect = SPCS_OBJECT_EXISTS_ERROR
ComputePoolManager().create(
pool_name=pool_name,
min_nodes=1,
max_nodes=1,
instance_family="test_family",
auto_resume=False,
initially_suspended=True,
auto_suspend_secs=7200,
comment=to_string_literal("this is a test"),
)
mock_handle.assert_called_once_with(
SPCS_OBJECT_EXISTS_ERROR, ObjectType.COMPUTE_POOL, pool_name
)


@patch(
"snowflake.cli.plugins.spcs.compute_pool.manager.ComputePoolManager._execute_query"
)
Expand Down
14 changes: 6 additions & 8 deletions tests/spcs/test_image_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from snowflake.connector.cursor import SnowflakeCursor
from snowflake.connector.errors import ProgrammingError
from snowflake.cli.api.constants import ObjectType
from tests.spcs.test_common import SPCS_OBJECT_EXISTS_ERROR


@mock.patch(
Expand Down Expand Up @@ -44,17 +45,14 @@ def test_create_cli(mock_create, mock_cursor, runner):
"snowflake.cli.plugins.spcs.image_repository.manager.ImageRepositoryManager._execute_schema_query"
)
@mock.patch(
"snowflake.cli.plugins.spcs.image_repository.common.handle_object_already_exists"
"snowflake.cli.plugins.spcs.image_repository.manager.handle_object_already_exists"
)
def test_create_repository_exists(mock_execute, mock_handle):
repo_name = "test_repo"
object_exists_error = ProgrammingError(
msg="Object 'TEST_REPO' already exists.", errno=2002
)
mock_execute.side_effect = object_exists_error
def test_create_repository_already_exists(mock_handle, mock_execute):
repo_name = "test_object"
mock_execute.side_effect = SPCS_OBJECT_EXISTS_ERROR
ImageRepositoryManager().create(repo_name)
mock_handle.assert_called_once_with(
object_exists_error, ObjectType.IMAGE_REPOSITORY, repo_name
SPCS_OBJECT_EXISTS_ERROR, ObjectType.IMAGE_REPOSITORY, repo_name
)


Expand Down
37 changes: 33 additions & 4 deletions tests/spcs/test_services.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from pathlib import Path
from textwrap import dedent
from unittest.mock import Mock, patch

from click import ClickException
import pytest
import strictyaml
from tests.spcs.test_common import SPCS_OBJECT_EXISTS_ERROR
from snowflake.cli.api.constants import ObjectType
from snowflake.cli.plugins.spcs.services.manager import ServiceManager
from tests.testing_utils.fixtures import *
from snowflake.cli.api.project.util import to_string_literal
Expand Down Expand Up @@ -216,6 +214,37 @@ def test_create_service_with_invalid_spec(mock_read_yaml):
)


@patch("snowflake.cli.plugins.spcs.services.manager.ServiceManager._read_yaml")
@patch(
"snowflake.cli.plugins.spcs.services.manager.ServiceManager._execute_schema_query"
)
@patch("snowflake.cli.plugins.spcs.services.manager.handle_object_already_exists")
def test_create_repository_already_exists(mock_handle, mock_execute, mock_read_yaml):
service_name = "test_object"
compute_pool = "test_pool"
spec_path = "/path/to/spec.yaml"
min_instances = 42
max_instances = 42
external_access_integrations = query_warehouse = tags = comment = None
auto_resume = False
mock_execute.side_effect = SPCS_OBJECT_EXISTS_ERROR
ServiceManager().create(
service_name=service_name,
compute_pool=compute_pool,
spec_path=Path(spec_path),
min_instances=min_instances,
max_instances=max_instances,
auto_resume=auto_resume,
external_access_integrations=external_access_integrations,
query_warehouse=query_warehouse,
tags=tags,
comment=comment,
)
mock_handle.assert_called_once_with(
SPCS_OBJECT_EXISTS_ERROR, ObjectType.SERVICE, service_name
)


@patch(
"snowflake.cli.plugins.spcs.services.manager.ServiceManager._execute_schema_query"
)
Expand Down

0 comments on commit 4dd84a9

Please sign in to comment.