forked from datafold/data-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_postgresql.py
155 lines (118 loc) · 5.36 KB
/
test_postgresql.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
import unittest
from urllib.parse import quote
from data_diff.queries.api import table, commit
from data_diff import TableSegment, HashDiffer
from data_diff import databases as db
from tests.common import get_conn, random_table_suffix, connect
from data_diff import connect_to_table
class TestUUID(unittest.TestCase):
def setUp(self) -> None:
self.connection = get_conn(db.PostgreSQL)
table_suffix = random_table_suffix()
self.table_src_name = f"src{table_suffix}"
self.table_dst_name = f"dst{table_suffix}"
self.table_src = table(self.table_src_name)
self.table_dst = table(self.table_dst_name)
def test_uuid(self):
self.connection.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";', None)
queries = [
self.table_src.drop(True),
self.table_dst.drop(True),
f"CREATE TABLE {self.table_src_name} (id uuid DEFAULT uuid_generate_v4 (), comment VARCHAR, PRIMARY KEY (id))",
commit,
self.table_src.insert_rows([[i] for i in range(100)], columns=["comment"]),
commit,
self.table_dst.create(self.table_src),
commit,
self.table_src.insert_row("This one is different", columns=["comment"]),
commit,
]
for query in queries:
self.connection.query(query)
a = TableSegment(self.connection, self.table_src.path, ("id",), "comment")
b = TableSegment(self.connection, self.table_dst.path, ("id",), "comment")
differ = HashDiffer()
diff = list(differ.diff_tables(a, b))
uuid = diff[0][1][0]
self.assertEqual(diff, [("-", (uuid, "This one is different"))])
# Compare with MySql
mysql_conn = get_conn(db.MySQL)
rows = self.connection.query(self.table_src.select(), list)
queries = [
f"CREATE TABLE {self.table_dst_name} (id VARCHAR(128), comment VARCHAR(128))",
commit,
self.table_dst.insert_rows(rows, columns=["id", "comment"]),
commit,
]
for q in queries:
mysql_conn.query(q)
c = TableSegment(mysql_conn, (self.table_dst_name,), ("id",), "comment")
diff = list(differ.diff_tables(a, c))
assert not diff, diff
diff = list(differ.diff_tables(c, a))
assert not diff, diff
self.connection.query(self.table_src.drop(True))
self.connection.query(self.table_dst.drop(True))
mysql_conn.query(self.table_dst.drop(True))
class Test100Fields(unittest.TestCase):
def setUp(self) -> None:
self.connection = get_conn(db.PostgreSQL)
table_suffix = random_table_suffix()
self.table_src_name = f"src{table_suffix}"
self.table_dst_name = f"dst{table_suffix}"
self.table_src = table(self.table_src_name)
self.table_dst = table(self.table_dst_name)
def test_100_fields(self):
self.connection.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";', None)
columns = [f"col{i}" for i in range(100)]
fields = " ,".join(f'"{field}" TEXT' for field in columns)
queries = [
self.table_src.drop(True),
self.table_dst.drop(True),
f"CREATE TABLE {self.table_src_name} (id uuid DEFAULT uuid_generate_v4 (), {fields})",
commit,
self.table_src.insert_rows([[f"{x * y}" for x in range(100)] for y in range(10)], columns=columns),
commit,
self.table_dst.create(self.table_src),
commit,
self.table_src.insert_rows([[1 for x in range(100)]], columns=columns),
commit,
]
for query in queries:
self.connection.query(query)
a = TableSegment(self.connection, self.table_src.path, ("id",), extra_columns=tuple(columns))
b = TableSegment(self.connection, self.table_dst.path, ("id",), extra_columns=tuple(columns))
differ = HashDiffer()
diff = list(differ.diff_tables(a, b))
id_ = diff[0][1][0]
result = (id_,) + tuple("1" for x in range(100))
self.assertEqual(diff, [("-", result)])
class TestSpecialCharacterPassword(unittest.TestCase):
def setUp(self) -> None:
self.connection = get_conn(db.PostgreSQL)
table_suffix = random_table_suffix()
self.table_name = f"table{table_suffix}"
self.table = table(self.table_name)
def test_special_char_password(self):
password = "passw!!!@rd"
# Setup user with special character '@' in password
self.connection.query("DROP USER IF EXISTS test;", None)
self.connection.query(f"CREATE USER test WITH PASSWORD '{password}';", None)
password_quoted = quote(password)
db_config = {
"driver": "postgresql",
"host": "localhost",
"port": 5432,
"dbname": "postgres",
"user": "test",
"password": password_quoted,
}
# verify pythonic connection method
connect_to_table(
db_config,
self.table_name,
)
# verify connection method with URL string unquoted after it's verified
db_url = f"postgresql://{db_config['user']}:{db_config['password']}@{db_config['host']}:{db_config['port']}/{db_config['dbname']}"
connection_verified = connect(db_url)
assert connection_verified._args.get("password") == password