Skip to content

Commit

Permalink
SNOW-1011766: Updating integration tests for repository url to cover …
Browse files Browse the repository at this point in the history
…full flow of create repo, show repos, get repo url
  • Loading branch information
sfc-gh-davwang committed Jan 31, 2024
1 parent b772fdf commit e4ba8bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/snowflake/cli/api/project/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,8 @@ def validate_version(version: str):
raise ValueError(
f"Project definition version {version} is not supported by this version of Snowflake CLI. Supported versions: {SUPPORTED_VERSIONS}"
)


def escape_like_pattern(pattern: str) -> str:
pattern = pattern.replace("%", r"\\}%").replace("_", r"\\_")
return pattern
17 changes: 5 additions & 12 deletions src/snowflake/cli/plugins/spcs/image_repository/manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from urllib.parse import urlparse

from click import ClickException
from snowflake.cli.api.project.util import escape_like_pattern
from snowflake.cli.api.sql_execution import SqlExecutionMixin
from snowflake.connector.cursor import SnowflakeCursor

Expand All @@ -16,18 +17,10 @@ def get_role(self):
return self._conn.role

def get_repository_url_list(self, repo_name: str) -> SnowflakeCursor:
role = self.get_role()
database = self.get_database()
schema = self.get_schema()

registry_query = f"""
use role {role};
use database {database};
use schema {schema};
show image repositories like '{repo_name}';
"""

return self._execute_query(registry_query)
repository_query = (
f"show image repositories like '{escape_like_pattern(repo_name)}'"
)
return self._execute_query(repository_query)

def get_repository_url(self, repo_name):
database = self.get_database()
Expand Down
21 changes: 14 additions & 7 deletions tests_integration/spcs/test_image_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from snowflake.cli.api.project.util import escape_like_pattern

from tests_integration.test_utils import contains_row_with, row_from_snowflake_session
from tests_integration.testing_utils.naming_utils import ObjectNameProvider

INTEGRATION_DATABASE = "SNOWCLI_DB"
INTEGRATION_SCHEMA = "PUBLIC"
Expand Down Expand Up @@ -61,15 +63,20 @@ def _list_tags(runner):


@pytest.mark.integration
def test_get_repo_url(runner, snowflake_session):
# requires at least one repository to exist in the schema of snowflake_session
expect = snowflake_session.execute_string(f"show image repositories")
expect_row = row_from_snowflake_session(expect)[0]
expect_name = expect_row["name"]
expect_url = expect_row["repository_url"]
def test_get_repo_url(runner, snowflake_session, test_database):
repo_name = ObjectNameProvider("Test_Repo").create_and_get_next_object_name()
snowflake_session.execute_string(f"create image repository {repo_name}")

created_repo = snowflake_session.execute_string(
f"show image repositories like '{escape_like_pattern(repo_name)}'"
)
created_row = row_from_snowflake_session(created_repo)[0]
created_name = created_row["name"]
assert created_name.lower() == repo_name.lower()

expect_url = created_row["repository_url"]
result = runner.invoke_with_connection(
["spcs", "image-repository", "url", expect_name]
["spcs", "image-repository", "url", created_name]
)
assert isinstance(result.output, str), result.output
assert result.output.strip() == expect_url

0 comments on commit e4ba8bd

Please sign in to comment.