-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_snowflake_conn_externalbrowser.py
175 lines (146 loc) · 6.09 KB
/
test_snowflake_conn_externalbrowser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pytest
import pandas as pd
from sqlalchemy import create_engine
from snowflake.sqlalchemy import URL
import configparser
import logging
import allure
def read_config(config_file='config.ini'):
"""Reads configuration from the given file."""
config = configparser.ConfigParser()
config.read(config_file)
return config
def create_snowflake_engine(user, authenticator, account, warehouse, database, schema, role):
""" Creates a SQLAlchemy engine for Snowflake. """
try:
# Create the SQLAlchemy engine and define the connection URL
engine = create_engine(URL(
user=user,
authenticator=authenticator,
account=account,
warehouse=warehouse,
database=database,
schema=schema,
role=role
))
print("Successfully created SQLAlchemy engine for Snowflake")
return engine
except Exception as e:
print(f"Error creating SQLAlchemy engine for Snowflake: {e}")
raise
@pytest.fixture(scope='module') # 'module' as we want to reuse the database connection for all tests within this single test file
def snowflake_connection():
"""Fixture to set up a Snowflake connection and clean up after tests."""
# Read configuration
config = read_config()
# Define Snowflake connection parameters
user = config['database']['user']
authenticator = 'externalbrowser' # For AzureADSSO
account = config['database']['account']
warehouse = config['database']['warehouse']
database = config['database']['database']
schema = config['table']['schema']
role = config['user']['role']
# Create the SQLAlchemy engine
engine = create_snowflake_engine(user, authenticator, account, warehouse, database, schema, role)
try:
connection = engine.connect()
# Yield the connection to the test function
yield connection
finally: # Resource Cleanup: Ensures that the connection is closed and the engine is disposed of, no matter what happens in the try block or the test function.
# Make certain to close the connection by executing connection.close() before engine.dispose();
# otherwise, the Python Garbage collector removes the resources required to communicate with Snowflake,
# preventing the Python connector from closing the session properly.
connection.close()
engine.dispose()
def count_rows_in_table(connection, table_name):
"""Counts the number of rows in a Snowflake table."""
try:
query = f"SELECT COUNT(*) FROM {table_name};"
df = pd.read_sql(query, connection)
row_count = df.iloc[0, 0]
return row_count
except Exception as e:
print(f"Error counting rows in table {table_name}: {e}")
raise
def test_row_count_in_table(snowflake_connection):
"""Test to verify the row count in a specific Snowflake table."""
config = read_config()
table_name = config['table']['table_name']
try:
row_count = count_rows_in_table(snowflake_connection, table_name)
assert row_count == 1876, f"Expected 1876 rows in table {table_name}, but found {row_count}"
print(f"Row count test passed for table {table_name}")
except Exception as e:
print(f"Test failed due to an error: {e}")
assert False, f"Test failed due to an error: {e}"
###################################pandas.read_sql##############################################3
# import pytest # testing framework
# import pandas as pd
# import snowflake.connector # The Snowflake Python Connector for interacting with Snowflake databases.
# import configparser
# import allure
# def connect_to_snowflake(user, authenticator, account, warehouse, database, schema, role):
# """ Connects to the Snowflake database. """
# try:
# # Establishes the connection
# conn = snowflake.connector.connect(
# user=user,
# authenticator=authenticator,
# account=account,
# warehouse=warehouse,
# database=database,
# schema=schema,
# # table_name = table_name,
# role=role
# )
# return conn
# except Exception as e:
# print("Error connecting to Snowflake:", str(e))
# return None
# def read_config():
# # Read configuration
# config = configparser.ConfigParser()
# config.read('config.ini')
# return config
# @pytest.fixture
# def snowflake_connection():
# # Read configuration
# config = read_config()
# # Define Snowflake connection parameters
# user = config['database']['user']
# authenticator = 'externalbrowser' # For AzureADSSO
# account = config['database']['account']
# warehouse = config['database']['warehouse']
# database = config['database']['database']
# schema = config['table']['schema']
# # table_name = config['table']['table_name']
# role = config['user']['role']
# # Connects to Snowflake
# conn = connect_to_snowflake(user, authenticator, account, warehouse, database, schema, role)
# yield conn
# conn.close()
# def snowflake_table_to_dataframe(conn, table_name):
# """ Converts a Snowflake table to a Pandas DataFrame. """
# try:
# # Reads the table data into a DataFrame
# query = f"SELECT * FROM {table_name};"
# df = pd.read_sql(query, conn)
# return df
# except Exception as e:
# print("Error reading table:", str(e))
# return None
# def test_row_count_in_table(snowflake_connection):
# # Read configuration
# config = read_config()
# table_name = config['table']['table_name']
# try:
# # Query to count the number of rows in the table
# query = f"SELECT COUNT(*) FROM {table_name};"
# df = pd.read_sql(query, snowflake_connection)
# row_count = df.iloc[0, 0]
# # Check if the row count is 1876
# assert row_count == 1876, f"Expected 1861 rows in table {table_name}, but found {row_count}"
# except Exception as e:
# print("Error counting rows in table:", str(e))
# assert False, f"Test failed due to an error: {str(e)}"