|
8 | 8 | import json |
9 | 9 | import os |
10 | 10 | import sys |
| 11 | +from textwrap import dedent |
11 | 12 | from unittest.mock import patch |
12 | 13 |
|
13 | 14 | import pytest |
14 | 15 |
|
15 | 16 | import snowflake.connector |
| 17 | +from snowflake.connector.errors import Error |
| 18 | + |
| 19 | +from ..randomize import random_string |
16 | 20 |
|
17 | 21 | try: |
18 | 22 | from snowflake.connector.auth import ( |
|
28 | 32 |
|
29 | 33 | try: # pragma: no cover |
30 | 34 | from snowflake.connector.auth import AuthByUsrPwdMfa |
| 35 | + from snowflake.connector.config_manager import CONFIG_MANAGER |
31 | 36 | from snowflake.connector.constants import ENV_VAR_PARTNER, QueryStatus |
32 | 37 | except ImportError: |
33 | 38 | ENV_VAR_PARTNER = "SF_PARTNER" |
34 | | - QueryStatus = None |
| 39 | + QueryStatus = CONFIG_MANAGER = None |
35 | 40 |
|
36 | 41 | class AuthByUsrPwdMfa(AuthByDefault): |
37 | 42 | def __init__(self, password: str, mfa_token: str) -> None: |
@@ -211,3 +216,103 @@ def test_negative_custom_auth(auth_class): |
211 | 216 | user="user", |
212 | 217 | auth_class=auth_class, |
213 | 218 | ) |
| 219 | + |
| 220 | + |
| 221 | +def test_missing_default_connection(monkeypatch, tmp_path): |
| 222 | + connections_file = tmp_path / "connections.toml" |
| 223 | + config_file = tmp_path / "config.toml" |
| 224 | + with monkeypatch.context() as m: |
| 225 | + m.delenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", raising=False) |
| 226 | + m.delenv("SNOWFLAKE_CONNECTIONS", raising=False) |
| 227 | + m.setattr(CONFIG_MANAGER, "conf_file_cache", None) |
| 228 | + m.setattr(CONFIG_MANAGER, "file_path", config_file) |
| 229 | + |
| 230 | + with pytest.raises( |
| 231 | + Error, |
| 232 | + match="Default connection with name 'default' cannot be found, known ones are \\[\\]", |
| 233 | + ): |
| 234 | + snowflake.connector.connect(connections_file_path=connections_file) |
| 235 | + |
| 236 | + |
| 237 | +def test_missing_default_connection_conf_file(monkeypatch, tmp_path): |
| 238 | + connection_name = random_string(5) |
| 239 | + connections_file = tmp_path / "connections.toml" |
| 240 | + config_file = tmp_path / "config.toml" |
| 241 | + config_file.write_text( |
| 242 | + dedent( |
| 243 | + f"""\ |
| 244 | + default_connection_name = "{connection_name}" |
| 245 | + """ |
| 246 | + ) |
| 247 | + ) |
| 248 | + with monkeypatch.context() as m: |
| 249 | + m.delenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", raising=False) |
| 250 | + m.delenv("SNOWFLAKE_CONNECTIONS", raising=False) |
| 251 | + m.setattr(CONFIG_MANAGER, "conf_file_cache", None) |
| 252 | + m.setattr(CONFIG_MANAGER, "file_path", config_file) |
| 253 | + |
| 254 | + with pytest.raises( |
| 255 | + Error, |
| 256 | + match=f"Default connection with name '{connection_name}' cannot be found, known ones are \\[\\]", |
| 257 | + ): |
| 258 | + snowflake.connector.connect(connections_file_path=connections_file) |
| 259 | + |
| 260 | + |
| 261 | +def test_missing_default_connection_conn_file(monkeypatch, tmp_path): |
| 262 | + connections_file = tmp_path / "connections.toml" |
| 263 | + config_file = tmp_path / "config.toml" |
| 264 | + connections_file.write_text( |
| 265 | + dedent( |
| 266 | + """\ |
| 267 | + [con_a] |
| 268 | + user = "test user" |
| 269 | + account = "test account" |
| 270 | + password = "test password" |
| 271 | + """ |
| 272 | + ) |
| 273 | + ) |
| 274 | + with monkeypatch.context() as m: |
| 275 | + m.delenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", raising=False) |
| 276 | + m.delenv("SNOWFLAKE_CONNECTIONS", raising=False) |
| 277 | + m.setattr(CONFIG_MANAGER, "conf_file_cache", None) |
| 278 | + m.setattr(CONFIG_MANAGER, "file_path", config_file) |
| 279 | + |
| 280 | + with pytest.raises( |
| 281 | + Error, |
| 282 | + match="Default connection with name 'default' cannot be found, known ones are \\['con_a'\\]", |
| 283 | + ): |
| 284 | + snowflake.connector.connect(connections_file_path=connections_file) |
| 285 | + |
| 286 | + |
| 287 | +def test_missing_default_connection_conf_conn_file(monkeypatch, tmp_path): |
| 288 | + connection_name = random_string(5) |
| 289 | + connections_file = tmp_path / "connections.toml" |
| 290 | + config_file = tmp_path / "config.toml" |
| 291 | + config_file.write_text( |
| 292 | + dedent( |
| 293 | + f"""\ |
| 294 | + default_connection_name = "{connection_name}" |
| 295 | + """ |
| 296 | + ) |
| 297 | + ) |
| 298 | + connections_file.write_text( |
| 299 | + dedent( |
| 300 | + """\ |
| 301 | + [con_a] |
| 302 | + user = "test user" |
| 303 | + account = "test account" |
| 304 | + password = "test password" |
| 305 | + """ |
| 306 | + ) |
| 307 | + ) |
| 308 | + with monkeypatch.context() as m: |
| 309 | + m.delenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", raising=False) |
| 310 | + m.delenv("SNOWFLAKE_CONNECTIONS", raising=False) |
| 311 | + m.setattr(CONFIG_MANAGER, "conf_file_cache", None) |
| 312 | + m.setattr(CONFIG_MANAGER, "file_path", config_file) |
| 313 | + |
| 314 | + with pytest.raises( |
| 315 | + Error, |
| 316 | + match=f"Default connection with name '{connection_name}' cannot be found, known ones are \\['con_a'\\]", |
| 317 | + ): |
| 318 | + snowflake.connector.connect(connections_file_path=connections_file) |
0 commit comments