Skip to content

Commit 014f6f6

Browse files
committed
Add tests for Snowflake connection and query execution
1 parent 7562e8c commit 014f6f6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

tests/testing_snowflake_connection.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,40 @@
33
import os
44
from dotenv import load_dotenv
55

6-
load_dotenv() # Load environment variables from a .env file (if used locally)
7-
6+
load_dotenv()
87
@pytest.fixture
98
def snowflake_connection():
109
"""Fixture to establish a connection to Snowflake."""
1110
conn = None
1211
try:
13-
# Print the connection parameters for debugging
14-
print("Connecting to Snowflake with the following parameters:")
15-
print(f"User: {os.getenv('SNOWFLAKE_USER')}")
16-
print(f"Account: {os.getenv('SNOWFLAKE_ACCOUNT')}")
17-
print(f"Warehouse: {os.getenv('SNOWFLAKE_WAREHOUSE')}")
18-
print(f"Database: {os.getenv('SNOWFLAKE_DATABASE')}")
19-
print(f"Schema: {os.getenv('SNOWFLAKE_SCHEMA')}")
20-
2112
conn = snowflake.connector.connect(
2213
user=os.getenv("SNOWFLAKE_USER"),
23-
password=os.getenv("SNOWFLAKE_PASSWORD"),
2414
account=os.getenv("SNOWFLAKE_ACCOUNT"),
15+
password=os.getenv("SNOWFLAKE_PASSWORD"),
2516
warehouse=os.getenv("SNOWFLAKE_WAREHOUSE"),
2617
database=os.getenv("SNOWFLAKE_DATABASE"),
2718
schema=os.getenv("SNOWFLAKE_SCHEMA")
2819
)
2920
yield conn
30-
except Exception as e:
31-
print(f"Error connecting to Snowflake: {e}") # Print error for debugging
32-
yield None # Yield None if connection fails
3321
finally:
34-
if conn is not None:
35-
conn.close() # Close the connection only if it was created
22+
if conn:
23+
conn.close()
24+
25+
def test_snowflake_connection(snowflake_connection):
26+
"""Test if Snowflake connection is successful."""
27+
assert snowflake_connection is not None
28+
assert snowflake_connection.is_closed() == False
29+
30+
def test_snowflake_query_execution(snowflake_connection):
31+
"""Test if a simple query executes successfully."""
32+
cursor = snowflake_connection.cursor()
33+
try:
34+
cursor.execute("SELECT CURRENT_VERSION()")
35+
result = cursor.fetchone()
36+
assert result is not None
37+
assert isinstance(result[0], str)
38+
finally:
39+
cursor.close()
40+
41+
if __name__ == "__main__":
42+
pytest.main()

0 commit comments

Comments
 (0)