-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.py
113 lines (94 loc) · 3.01 KB
/
validator.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
import sys
import psycopg2
import configparser
def connect_to_warehouse():
# get db connection parameters from the conf file
parser = configparser.ConfigParser(interpolation=None)
parser.read('pipeline.conf')
dbname = parser.get('aws_creds', 'database')
user = parser.get('aws_creds', 'user')
password = parser.get('aws_creds', 'password')
host = parser.get('aws_creds', 'host')
port = parser.get('aws_creds', 'port')
rs_con = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
return rs_con
# execute a test made up of two scripts
# and a comparison operator
# Returns the true/false for test pass/fail
def execute_test(
db_con,
script_1,
script_2,
comp_operator):
# execute the 1st script and store the result
cursor = db_con.cursor()
sql_file = open(script_1, 'r')
cursor.execute(sql_file.read())
record = cursor.fetchone()
result_1 = record[0]
db_con.commit()
cursor.close()
# execute the 2nd script and store the result
cursor = db_con.cursor()
sql_file = open(script_2, 'r')
cursor.execute(sql_file.read())
record = cursor.fetchone()
result_2 = record[0]
db_con.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
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-h':
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) != 4:
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]
# connect to the data warehouse
db_con = connect_to_warehouse()
# execute the validation test
test_result = execute_test(
db_con, script_1, script_2, comp_operator
)
print('Result of test: ' + str(test_result))
if test_result == True:
exit(0)
else:
exit(-1)