|
1 | 1 | import tempfile |
2 | 2 | from typing import Optional |
3 | | -from unittest.mock import MagicMock |
| 3 | +from unittest.mock import MagicMock, patch |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from cryptography.hazmat.primitives import serialization |
7 | 7 | from cryptography.hazmat.primitives.asymmetric import rsa |
8 | 8 |
|
9 | 9 | from feast.infra.utils.snowflake.snowflake_utils import ( |
| 10 | + GetSnowflakeConnection, |
10 | 11 | execute_snowflake_statement, |
11 | 12 | parse_private_key_path, |
12 | 13 | ) |
@@ -75,6 +76,64 @@ def test_parse_private_key_path_key_path_encrypted(encrypted_private_key): |
75 | 76 | ) |
76 | 77 |
|
77 | 78 |
|
| 79 | +class _AttrDict(dict): |
| 80 | + __getattr__ = dict.__getitem__ |
| 81 | + |
| 82 | + |
| 83 | +def _make_config(**overrides): |
| 84 | + defaults = { |
| 85 | + "type": "snowflake.offline", |
| 86 | + "account": "test_account", |
| 87 | + "user": "test_user", |
| 88 | + "password": "test_password", # pragma: allowlist secret |
| 89 | + "role": "test_role", |
| 90 | + "warehouse": "test_wh", |
| 91 | + "database": "test_db", |
| 92 | + "schema_": "test_schema", |
| 93 | + "config_path": "", |
| 94 | + } |
| 95 | + defaults.update(overrides) |
| 96 | + return _AttrDict(defaults) |
| 97 | + |
| 98 | + |
| 99 | +@patch("feast.infra.utils.snowflake.snowflake_utils.snowflake.connector") |
| 100 | +class TestGetSnowflakeConnectionIdentifierQuoting: |
| 101 | + @pytest.fixture(autouse=True) |
| 102 | + def _clear_cache(self): |
| 103 | + with patch("feast.infra.utils.snowflake.snowflake_utils._cache", {}): |
| 104 | + yield |
| 105 | + |
| 106 | + @pytest.mark.parametrize( |
| 107 | + "config_key,connect_key,value", |
| 108 | + [ |
| 109 | + ("warehouse", "warehouse", "MY_WH"), |
| 110 | + ("role", "role", "ANALYST"), |
| 111 | + ("database", "database", "PROD_DB"), |
| 112 | + ("schema_", "schema", "PUBLIC"), |
| 113 | + ], |
| 114 | + ) |
| 115 | + def test_identifier_passed_without_quoting( |
| 116 | + self, mock_connector, config_key, connect_key, value |
| 117 | + ): |
| 118 | + mock_connector.connect.return_value = MagicMock() |
| 119 | + |
| 120 | + with GetSnowflakeConnection(_make_config(**{config_key: value})): |
| 121 | + pass |
| 122 | + |
| 123 | + kwargs = mock_connector.connect.call_args[1] |
| 124 | + assert kwargs[connect_key] == value |
| 125 | + |
| 126 | + def test_schema_key_renamed_from_schema_underscore(self, mock_connector): |
| 127 | + mock_connector.connect.return_value = MagicMock() |
| 128 | + |
| 129 | + with GetSnowflakeConnection(_make_config(schema_="analytics")): |
| 130 | + pass |
| 131 | + |
| 132 | + kwargs = mock_connector.connect.call_args[1] |
| 133 | + assert "schema" in kwargs |
| 134 | + assert "schema_" not in kwargs |
| 135 | + |
| 136 | + |
78 | 137 | class TestExecuteSnowflakeStatement: |
79 | 138 | def test_empty_query_is_passed_through_to_execute(self): |
80 | 139 | mock_conn = MagicMock() |
|
0 commit comments