Skip to content

Commit

Permalink
add quotes for 'use' sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pczajka committed Apr 8, 2024
1 parent 979dc05 commit c68e154
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/snowflake/cli/api/sql_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _execute_queries(self, queries: str, **kwargs):
return list(self._execute_string(dedent(queries), **kwargs))

def use(self, object_type: ObjectType, name: str):
return self._execute_query(f"use {object_type.value.sf_name} {name}")
return self._execute_query(f'use {object_type.value.sf_name} "{name}"')

@contextmanager
def use_role(self, new_role: str):
Expand Down
3 changes: 0 additions & 3 deletions tests_integration/config/connection_configs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
[connections.integration]
schema = "public"
role = "INTEGRATION_TESTS"

[cli.features]
enable_snowgit = true
34 changes: 29 additions & 5 deletions tests_integration/snowflake_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,48 @@
import pytest
from snowflake import connector
from snowflake.cli.api.exceptions import EnvironmentVariableNotFoundError
from contextlib import contextmanager

_ENV_PARAMETER_PREFIX = "SNOWFLAKE_CONNECTIONS_INTEGRATION"


@pytest.fixture(scope="function")
def test_database(snowflake_session):
database_name = f"db_{uuid.uuid4().hex}"
def add_uuid_to_name(name: str) -> str:
return f"{name}_{uuid.uuid4().hex}"


@contextmanager
def setup_test_database(snowflake_session, database_name: str):
database_name = f'"{database_name}"'
snowflake_session.execute_string(
f"create database {database_name}; use database {database_name}; use schema public;"
)
os.environ[f"{_ENV_PARAMETER_PREFIX}_DATABASE"] = database_name
os.environ[f"{_ENV_PARAMETER_PREFIX}_DATABASE"] = f"{database_name}"

yield database_name
yield

snowflake_session.execute_string(f"drop database {database_name}")
del os.environ[f"{_ENV_PARAMETER_PREFIX}_DATABASE"]


@contextmanager
def setup_test_schema(snowflake_session, schema_name: str):
schema_name = f'"{schema_name}"'
snowflake_session.execute_string(
f"create schema {schema_name}; use schema {schema_name};"
)
os.environ[f"{_ENV_PARAMETER_PREFIX}_SCHEMA"] = f"{schema_name}"
yield
snowflake_session.execute_string(f"drop schema {schema_name}")
del os.environ[f"{_ENV_PARAMETER_PREFIX}_SCHEMA"]


@pytest.fixture(scope="function")
def test_database(snowflake_session):
database_name = add_uuid_to_name("db")
with setup_test_database(snowflake_session, database_name):
yield database_name


@pytest.fixture(scope="function")
def test_role(snowflake_session):
role_name = f"role_{uuid.uuid4().hex}"
Expand Down
36 changes: 36 additions & 0 deletions tests_integration/test_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
from tests_integration.snowflake_connector import (
setup_test_database,
setup_test_schema,
add_uuid_to_name,
)


@pytest.mark.integration
def test_connection_test_simple(runner):
result = runner.invoke_with_connection_json(["connection", "test"])
assert result.exit_code == 0, result.output
assert result.json["Status"] == "OK"


@pytest.mark.integration
def test_connection_dashed_database(runner, snowflake_session):
database = add_uuid_to_name("dashed-database")
with setup_test_database(snowflake_session, database):
result = runner.invoke_with_connection_json(["connection", "test"])
assert result.exit_code == 0, result.output
assert result.json["Database"] == database


@pytest.mark.integration
@pytest.mark.skip(
reason="BUG: connections test command seem to override every setting with schema PUBLIC"
)
def test_connection_dashed_schema(
runner, test_database, snowflake_session, snowflake_home
):
schema = "dashed-schema-name"
with setup_test_schema(snowflake_session, schema):
result = runner.invoke_with_connection(["connection", "test", "--debug"])
assert result.exit_code == 0, result.output
assert f'use schema "{schema}"' in result.output

0 comments on commit c68e154

Please sign in to comment.