Skip to content

Commit 356cae9

Browse files
committed
Enhance Snowflake connection fixture with error handling and improved assertions
1 parent 8de17e7 commit 356cae9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tests/testing_snowflake_connection.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import snowflake.connector
33
import os
44
from dotenv import load_dotenv
5-
load_dotenv()
5+
6+
load_dotenv() # Load environment variables from a .env file (if used locally)
7+
68
@pytest.fixture
79
def snowflake_connection():
810
"""Fixture to establish a connection to Snowflake."""
@@ -17,24 +19,28 @@ def snowflake_connection():
1719
schema=os.getenv("SNOWFLAKE_SCHEMA")
1820
)
1921
yield conn
22+
except Exception as e:
23+
print(f"Error connecting to Snowflake: {e}") # Print error for debugging
24+
yield None # Yield None if connection fails
2025
finally:
21-
conn.close()
26+
if conn is not None:
27+
conn.close() # Close the connection only if it was created
2228

2329
def test_snowflake_connection(snowflake_connection):
2430
"""Test if Snowflake connection is successful."""
25-
assert snowflake_connection is not None
26-
assert snowflake_connection.is_closed() == False
31+
assert snowflake_connection is not None, "Connection should not be None"
32+
assert snowflake_connection.is_closed() == False, "Connection should be open"
2733

2834
def test_snowflake_query_execution(snowflake_connection):
2935
"""Test if a simple query executes successfully."""
3036
cursor = snowflake_connection.cursor()
3137
try:
3238
cursor.execute("SELECT CURRENT_VERSION()")
3339
result = cursor.fetchone()
34-
assert result is not None
35-
assert isinstance(result[0], str)
40+
assert result is not None, "Query result should not be None"
41+
assert isinstance(result[0], str), "Result should be a string"
3642
finally:
3743
cursor.close()
3844

3945
if __name__ == "__main__":
40-
pytest.main()
46+
pytest.main()

0 commit comments

Comments
 (0)