Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse database and schema from Snowflake URL #394

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion recap/clients/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,26 @@ def __init__(self, connection: Connection) -> None:
def create(**kwargs) -> Generator[SnowflakeClient, None, None]:
import snowflake.connector

with snowflake.connector.connect(**kwargs) as client:
snowflake_args = {}
snowflake_args["account"] = kwargs.pop("host")

match kwargs.get("paths"):
case str(database), None:
snowflake_args["database"] = database
case str(database), str(schema):
snowflake_args["database"] = database
snowflake_args["schema"] = schema

with snowflake.connector.connect(**(snowflake_args | kwargs)) as client:
yield SnowflakeClient(client) # type: ignore

def ls_catalogs(self) -> list[str]:
cursor = self.connection.cursor()
cursor.execute(
"""
SELECT database_name
FROM information_schema.databases
ORDER BY database_name ASC
"""
)
return [row[0] for row in cursor.fetchall()]
18 changes: 18 additions & 0 deletions tests/unit/clients/test_snowflake.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import MagicMock, patch

import fakesnow
import pytest
import snowflake.connector
Expand Down Expand Up @@ -334,3 +336,19 @@ def test_ls(self):
assert client.ls() == ["TESTDB"]
assert client.ls("TESTDB") == ["PUBLIC"]
assert client.ls("TESTDB", "PUBLIC") == ["TEST_TYPES"]

def test_snowflake_client_create(self):
with patch("snowflake.connector.connect") as mock_connect:
mock_connection = MagicMock()
mock_connect.return_value.__enter__.return_value = mock_connection

with SnowflakeClient.create(
host="some_account",
paths=("some_database", "some_schema"),
) as _:
mock_connect.assert_called_once_with(
account="some_account",
database="some_database",
schema="some_schema",
paths=("some_database", "some_schema"),
)