3
3
import os
4
4
from dotenv import load_dotenv
5
5
6
- load_dotenv () # Load environment variables from a .env file (if used locally)
7
-
6
+ load_dotenv ()
8
7
@pytest .fixture
9
8
def snowflake_connection ():
10
9
"""Fixture to establish a connection to Snowflake."""
11
10
conn = None
12
11
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
-
21
12
conn = snowflake .connector .connect (
22
13
user = os .getenv ("SNOWFLAKE_USER" ),
23
- password = os .getenv ("SNOWFLAKE_PASSWORD" ),
24
14
account = os .getenv ("SNOWFLAKE_ACCOUNT" ),
15
+ password = os .getenv ("SNOWFLAKE_PASSWORD" ),
25
16
warehouse = os .getenv ("SNOWFLAKE_WAREHOUSE" ),
26
17
database = os .getenv ("SNOWFLAKE_DATABASE" ),
27
18
schema = os .getenv ("SNOWFLAKE_SCHEMA" )
28
19
)
29
20
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
33
21
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