-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After receiving feedback on the code structure of the vdk-snowflake plugin, a refactoring was necessary to optimize it. This change optimizes the configuration of the plugin and changes the result message printed after executing a query to use tabular format, instead of json string. Testing Done: Added unit test and tested locally by running a data job and queries Signed-off-by: Andon Andonov <[email protected]>
- Loading branch information
Showing
4 changed files
with
131 additions
and
61 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
projects/vdk-core/plugins/vdk-snowflake/src/vdk/internal/snowflake_configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2021 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from vdk.internal.core.config import Configuration | ||
from vdk.internal.core.config import ConfigurationBuilder | ||
|
||
SNOWFLAKE_ACCOUNT = "SNOWFLAKE_ACCOUNT" | ||
SNOWFLAKE_USER = "SNOWFLAKE_USER" | ||
SNOWFLAKE_PASSWORD = "SNOWFLAKE_PASSWORD" | ||
SNOWFLAKE_WAREHOUSE = "SNOWFLAKE_WAREHOUSE" | ||
SNOWFLAKE_DATABASE = "SNOWFLAKE_DATABASE" | ||
SNOWFLAKE_SCHEMA = "SNOWFLAKE_SCHEMA" | ||
|
||
|
||
class SnowflakeConfiguration: | ||
def __init__(self, config: Configuration) -> None: | ||
self.__config = config | ||
|
||
def get_snowflake_account(self): | ||
return self.__config.get_required_value(SNOWFLAKE_ACCOUNT) | ||
|
||
def get_snowflake_user(self): | ||
return self.__config.get_required_value(SNOWFLAKE_USER) | ||
|
||
def get_snowflake_password(self): | ||
return self.__config.get_required_value(SNOWFLAKE_PASSWORD) | ||
|
||
def get_snowflake_warehouse(self): | ||
return self.__config.get_value(SNOWFLAKE_WAREHOUSE) | ||
|
||
def get_snowflake_database(self): | ||
return self.__config.get_value(SNOWFLAKE_DATABASE) | ||
|
||
def get_snowflake_schema(self): | ||
return self.__config.get_value(SNOWFLAKE_SCHEMA) | ||
|
||
|
||
def add_definitions(config_builder: ConfigurationBuilder): | ||
config_builder.add( | ||
key=SNOWFLAKE_ACCOUNT, | ||
default_value=None, | ||
description="The Snowflake account identifier as described in https://docs.snowflake.com/en/user-guide/admin-account-identifier.html It is required to connect to a Snowflake instance.", | ||
) | ||
config_builder.add(key=SNOWFLAKE_USER, default_value=None, description="User name") | ||
config_builder.add( | ||
key=SNOWFLAKE_PASSWORD, default_value=None, description="User password" | ||
) | ||
config_builder.add( | ||
key=SNOWFLAKE_WAREHOUSE, | ||
default_value=None, | ||
description="The warehouse to be used.", | ||
) | ||
config_builder.add( | ||
key=SNOWFLAKE_DATABASE, | ||
default_value=None, | ||
description="The snowflake database to be used.", | ||
) | ||
config_builder.add( | ||
key=SNOWFLAKE_SCHEMA, default_value=None, description="The database schema" | ||
) |
86 changes: 26 additions & 60 deletions
86
projects/vdk-core/plugins/vdk-snowflake/src/vdk/internal/snowflake_plugin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
projects/vdk-core/plugins/vdk-snowflake/tests/test_snowflake_plugin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2021 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import os | ||
from unittest import mock | ||
|
||
import pytest | ||
from click.testing import Result | ||
from vdk.internal import snowflake_plugin | ||
from vdk.internal.snowflake_connection import SnowflakeConnection | ||
from vdk.internal.test_utils.util_funcs import cli_assert_equal | ||
from vdk.internal.test_utils.util_funcs import CliEntryBasedTestRunner | ||
|
||
|
||
@pytest.fixture | ||
def mocked_connection(monkeypatch): | ||
def mock_execute_query(*args, **kwargs): | ||
return [["Query successfully executed."]] | ||
|
||
monkeypatch.delattr( | ||
"vdk.internal.snowflake_connection.SnowflakeConnection._connect" | ||
) | ||
monkeypatch.setattr(SnowflakeConnection, "execute_query", mock_execute_query) | ||
|
||
|
||
def test_snowflake_plugin(mocked_connection): | ||
""" | ||
Test if the configuration of the Snowflake plugin | ||
and its general setup work as expected. | ||
""" | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
"VDK_DB_DEFAULT_TYPE": "SNOWFLAKE", | ||
"VDK_SNOWFLAKE_ACCOUNT": "testaccount", | ||
"VDK_SNOWFLAKE_USER": "testuser", | ||
"VDK_SNOWFLAKE_PASSWORD": "testpassword", | ||
}, | ||
): | ||
runner = CliEntryBasedTestRunner(snowflake_plugin) | ||
|
||
query_result: Result = runner.invoke( | ||
["snowflake-query", "--query", f"SELECT 1"] | ||
) | ||
|
||
cli_assert_equal(0, query_result) | ||
assert "Query successfully executed." in query_result.output |