Skip to content

Commit

Permalink
add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jhilgart committed Apr 9, 2024
1 parent acd6b42 commit 820de6b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def _fetch_valid_tables_and_views(conn: SnowflakeConnection) -> pd.DataFrame:
def _get_df(query: str) -> pd.DataFrame:
cursor = conn.cursor().execute(query)
assert cursor is not None, "cursor should not be none here."

df = pd.DataFrame(
cursor.fetchall(), columns=[c.name for c in cursor.description]
)
Expand Down
57 changes: 56 additions & 1 deletion semantic_model_generator/tests/snowflake_connector_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest import mock
from unittest.mock import call, patch
from unittest.mock import MagicMock, call, patch

import pandas as pd
import pytest
Expand Down Expand Up @@ -190,3 +190,58 @@ def test_get_valid_schema_table_columns_df(
# Assert that the connection executed the expected queries.
query = "select t.TABLE_SCHEMA, t.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, c.COMMENT as COLUMN_COMMENT\nfrom information_schema.tables as t\njoin information_schema.columns as c on t.table_schema = c.table_schema and t.table_name = c.table_name where t.table_schema ilike 'TEST_SCHEMA_1' AND LOWER(t.table_name) in ('table_1') \norder by 1, 2, c.ordinal_position"
mock_conn.cursor().execute.assert_any_call(query)


@pytest.fixture
def snowflake_data():
return [
# This mimics the return value of cursor.fetchall() for tables and views
([("table1", "schema1", "A table comment")], [("column1", "dtype")]),
([("view1", "schema1", "A view comment")], [("column1", "dtype")]),
]


@pytest.fixture
def expected_df():
# Expected DataFrame structure based on mocked fetchall data
return pd.DataFrame(
{
snowflake_connector._TABLE_NAME_COL: ["table1", "view1"],
snowflake_connector._TABLE_SCHEMA_COL: ["schema1", "schema1"],
snowflake_connector._TABLE_COMMENT_COL: [
"A table comment",
"A view comment",
],
}
)


def test_fetch_valid_tables_and_views(snowflake_data, expected_df):
# Mock SnowflakeConnection and cursor
mock_conn = mock.MagicMock()
mock_cursor = mock_conn.cursor.return_value
mock_cursor.execute.return_value = mock_cursor
# Set side effects for fetchall and description based on snowflake_data fixture
mock_cursor.fetchall.side_effect = [snowflake_data[0][0], snowflake_data[1][0]]

mock_name_one = MagicMock()
mock_name_one.name = "name"
mock_name_two = MagicMock()
mock_name_two.name = "schema_name"
mock_name_three = MagicMock()
mock_name_three.name = "comment"

mocked_descriptions = [mock_name_one, mock_name_two, mock_name_three]
mock_cursor.description = mocked_descriptions

# Call the function to test
result_df = snowflake_connector._fetch_valid_tables_and_views(mock_conn)

# Assert the result is as expected
pd.testing.assert_frame_equal(
result_df.reset_index(drop=True), expected_df.reset_index(drop=True)
)

# Verify execute was called with correct queries
mock_cursor.execute.assert_any_call("show tables in database")
mock_cursor.execute.assert_any_call("show views in database")

0 comments on commit 820de6b

Please sign in to comment.