2
2
import snowflake .connector
3
3
import os
4
4
from dotenv import load_dotenv
5
- load_dotenv ()
5
+
6
+ load_dotenv () # Load environment variables from a .env file (if used locally)
7
+
6
8
@pytest .fixture
7
9
def snowflake_connection ():
8
10
"""Fixture to establish a connection to Snowflake."""
@@ -17,24 +19,28 @@ def snowflake_connection():
17
19
schema = os .getenv ("SNOWFLAKE_SCHEMA" )
18
20
)
19
21
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
20
25
finally :
21
- conn .close ()
26
+ if conn is not None :
27
+ conn .close () # Close the connection only if it was created
22
28
23
29
def test_snowflake_connection (snowflake_connection ):
24
30
"""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"
27
33
28
34
def test_snowflake_query_execution (snowflake_connection ):
29
35
"""Test if a simple query executes successfully."""
30
36
cursor = snowflake_connection .cursor ()
31
37
try :
32
38
cursor .execute ("SELECT CURRENT_VERSION()" )
33
39
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"
36
42
finally :
37
43
cursor .close ()
38
44
39
45
if __name__ == "__main__" :
40
- pytest .main ()
46
+ pytest .main ()
0 commit comments