-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_logging.py
162 lines (133 loc) · 3.86 KB
/
validator_logging.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
import sys
import psycopg2
import configparser
def connect_to_warehouse():
# get db connection parameters from the conf file
parser = configparser.ConfigParser()
parser.read("pipeline.conf")
dbname = parser.get("aws_creds", "database")
user = parser.get("aws_creds", "username")
password = parser.get("aws_creds", "password")
host = parser.get("aws_creds", "host")
port = parser.get("aws_creds", "port")
# connect to the Redshift cluster
rs_conn = psycopg2.connect(
"dbname=" + dbname
+ " user=" + user
+ " password=" + password
+ " host=" + host
+ " port=" + port)
return rs_conn
# execute a test made of up two scripts
# and a comparison operator
# Returns true/false for test pass/fail
def execute_test(
db_conn,
script_1,
script_2,
comp_operator):
# execute the 1st script and store the result
cursor = db_conn.cursor()
sql_file = open(script_1, 'r')
cursor.execute(sql_file.read())
record = cursor.fetchone()
result_1 = record[0]
db_conn.commit()
cursor.close()
# execute the 2nd script and store the result
cursor = db_conn.cursor()
sql_file = open(script_2, 'r')
cursor.execute(sql_file.read())
record = cursor.fetchone()
result_2 = record[0]
db_conn.commit()
cursor.close()
print("result 1 = " + str(result_1))
print("result 2 = " + str(result_2))
# compare values based on the comp_operator
if comp_operator == "equals":
return result_1 == result_2
elif comp_operator == "greater_equals":
return result_1 >= result_2
elif comp_operator == "greater":
return result_1 > result_2
elif comp_operator == "less_equals":
return result_1 <= result_2
elif comp_operator == "less":
return result_1 < result_2
elif comp_operator == "not_equal":
return result_1 != result_2
# if we made it here, something went wrong
return False
def log_result(
db_conn,
script_1,
script_2,
comp_operator,
result):
m_query = """INSERT INTO
validation_run_history(
script_1,
script_2,
comp_operator,
test_result,
test_run_at)
VALUES(%s, %s, %s, %s,
current_timestamp);"""
m_cursor = db_conn.cursor()
m_cursor.execute(
m_query,
(script_1,
script_2,
comp_operator,
result)
)
db_conn.commit()
m_cursor.close()
db_conn.close()
return
if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == "-h":
print("Usage: python validator.py"
+ "script1.sql script2.sql "
+ "comparison_operator")
print("Valid comparison_operator values:")
print("equals")
print("greater_equals")
print("greater")
print("less_equals")
print("less")
print("not_equal")
exit(0)
if len(sys.argv) != 5:
print("Usage: python validator.py"
+ "script1.sql script2.sql "
+ "comparison_operator")
exit(-1)
script_1 = sys.argv[1]
script_2 = sys.argv[2]
comp_operator = sys.argv[3]
sev_level = sys.argv[4]
# connect to the data warehouse
db_conn = connect_to_warehouse()
# execute the validation test
test_result = execute_test(
db_conn,
script_1,
script_2,
comp_operator)
# log the test in the data warehouse
log_result(
db_conn,
script_1,
script_2,
comp_operator,
test_result)
print("Result of test: " + str(test_result))
if test_result == True:
exit(0)
else:
if sev_level == "halt":
exit(-1)
else:
exit(0)