This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathtest_joindiff.py
318 lines (258 loc) · 11 KB
/
test_joindiff.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from typing import List
from datetime import datetime
import attrs
from data_diff.queries.ast_classes import TablePath
from data_diff.queries.api import table, commit
from data_diff.table_segment import TableSegment
from data_diff import databases as db
from data_diff.joindiff_tables import JoinDiffer
from tests.test_diff_tables import DiffTestCase
from tests.common import (
random_table_suffix,
test_each_database_in_list,
)
TEST_DATABASES = {
db.PostgreSQL,
db.MySQL,
db.Snowflake,
db.BigQuery,
db.DuckDB,
db.Oracle,
db.Redshift,
db.Presto,
db.Trino,
db.Vertica,
}
test_each_database = test_each_database_in_list(TEST_DATABASES)
@test_each_database_in_list({db.Snowflake, db.BigQuery, db.DuckDB})
class TestCompositeKey(DiffTestCase):
src_schema = {"id": int, "userid": int, "movieid": int, "rating": float, "timestamp": datetime}
dst_schema = {"id": int, "userid": int, "movieid": int, "rating": float, "timestamp": datetime}
def setUp(self):
super().setUp()
self.differ = JoinDiffer()
def test_composite_key(self):
time = "2022-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
cols = "id userid movieid rating timestamp".split()
self.connection.query(
[
self.src_table.insert_rows([[1, 1, 1, 9, time_obj], [2, 2, 2, 9, time_obj]], columns=cols),
self.dst_table.insert_rows([[1, 1, 1, 9, time_obj], [2, 3, 2, 9, time_obj]], columns=cols),
commit,
]
)
# Sanity
table1 = TableSegment(
self.connection, self.table_src_path, ("id",), "timestamp", ("userid",), case_sensitive=False
)
table2 = TableSegment(
self.connection, self.table_dst_path, ("id",), "timestamp", ("userid",), case_sensitive=False
)
diff = list(self.differ.diff_tables(table1, table2))
assert len(diff) == 2
assert self.differ.stats["exclusive_count"] == 0
# Test pks diffed, by checking exclusive_count
table1 = TableSegment(self.connection, self.table_src_path, ("id", "userid"), "timestamp", case_sensitive=False)
table2 = TableSegment(self.connection, self.table_dst_path, ("id", "userid"), "timestamp", case_sensitive=False)
diff = list(self.differ.diff_tables(table1, table2))
assert len(diff) == 2
assert self.differ.stats["exclusive_count"] == 2
@test_each_database
class TestJoindiff(DiffTestCase):
src_schema = {"id": int, "userid": int, "movieid": int, "rating": float, "timestamp": datetime}
dst_schema = {"id": int, "userid": int, "movieid": int, "rating": float, "timestamp": datetime}
def setUp(self):
super().setUp()
self.table = TableSegment(self.connection, self.table_src_path, ("id",), "timestamp", case_sensitive=False)
self.table2 = TableSegment(self.connection, self.table_dst_path, ("id",), "timestamp", case_sensitive=False)
self.differ = JoinDiffer()
def test_diff_small_tables(self):
time = "2022-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
cols = "id userid movieid rating timestamp".split()
self.connection.query(
[
self.src_table.insert_rows([[1, 1, 1, 9, time_obj], [2, 2, 2, 9, time_obj]], columns=cols),
self.dst_table.insert_rows([[1, 1, 1, 9, time_obj]], columns=cols),
commit,
]
)
diff_res = self.differ.diff_tables(self.table, self.table2)
info = diff_res.info_tree.info
diff = list(diff_res)
expected_row = ("2", time + ".000000")
expected = [("-", expected_row)]
self.assertEqual(expected, diff)
self.assertEqual(2, info.rowcounts[1])
self.assertEqual(1, info.rowcounts[2])
# self.assertEqual(2, self.differ.stats["table1_max_id"])
# self.assertEqual(1, self.differ.stats["table2_min_id"])
# Test materialize
materialize_path = self.connection.dialect.parse_table_name(f"test_mat_{random_table_suffix()}")
mdiffer = attrs.evolve(self.differ, materialize_to_table=materialize_path)
diff = list(mdiffer.diff_tables(self.table, self.table2))
self.assertEqual(expected, diff)
t = TablePath(materialize_path)
rows = self.connection.query(t.select(), List[tuple])
# is_xa, is_xb, is_diff1, is_diff2, row1, row2
# assert rows == [(1, 0, 1, 1) + expected_row + (None, None)], rows
assert rows == [(1, 0, 1, 1) + (expected_row[0], None, expected_row[1], None)], rows
self.connection.query(t.drop())
# Test materialize all rows
mdiffer = attrs.evolve(mdiffer, materialize_all_rows=True)
diff = list(mdiffer.diff_tables(self.table, self.table2))
self.assertEqual(expected, diff)
rows = self.connection.query(t.select(), List[tuple])
assert len(rows) == 2, len(rows)
self.connection.query(t.drop())
def test_diff_table_above_bisection_threshold(self):
time = "2022-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
cols = "id userid movieid rating timestamp".split()
self.connection.query(
[
self.src_table.insert_rows(
[
[1, 1, 1, 9, time_obj],
[2, 2, 2, 9, time_obj],
[3, 3, 3, 9, time_obj],
[4, 4, 4, 9, time_obj],
[5, 5, 5, 9, time_obj],
],
columns=cols,
),
self.dst_table.insert_rows(
[
[1, 1, 1, 9, time_obj],
[2, 2, 2, 9, time_obj],
[3, 3, 3, 9, time_obj],
[4, 4, 4, 9, time_obj],
],
columns=cols,
),
commit,
]
)
diff_res = self.differ.diff_tables(self.table, self.table2)
info = diff_res.info_tree.info
diff = list(diff_res)
expected = [("-", ("5", time + ".000000"))]
self.assertEqual(expected, diff)
self.assertEqual(5, info.rowcounts[1])
self.assertEqual(4, info.rowcounts[2])
def test_return_empty_array_when_same(self):
time = "2022-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
cols = "id userid movieid rating timestamp".split()
self.connection.query(
[
self.src_table.insert_row(1, 1, 1, 9, time_obj, columns=cols),
self.dst_table.insert_row(1, 1, 1, 9, time_obj, columns=cols),
]
)
diff = list(self.differ.diff_tables(self.table, self.table2))
self.assertEqual([], diff)
def test_diff_sorted_by_key(self):
time = "2022-01-01 00:00:00"
time2 = "2021-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
time_obj2 = datetime.fromisoformat(time2)
cols = "id userid movieid rating timestamp".split()
self.connection.query(
[
self.src_table.insert_rows(
[
[1, 1, 1, 9, time_obj],
[2, 2, 2, 9, time_obj2],
[3, 3, 3, 9, time_obj],
[4, 4, 4, 9, time_obj2],
[5, 5, 5, 9, time_obj],
],
columns=cols,
),
self.dst_table.insert_rows(
[
[1, 1, 1, 9, time_obj],
[2, 2, 2, 9, time_obj],
[3, 3, 3, 9, time_obj],
[4, 4, 4, 9, time_obj],
[5, 5, 5, 9, time_obj],
],
columns=cols,
),
commit,
]
)
diff = list(self.differ.diff_tables(self.table, self.table2))
expected = {
("-", ("2", time2 + ".000000")),
("+", ("2", time + ".000000")),
("-", ("4", time2 + ".000000")),
("+", ("4", time + ".000000")),
}
self.assertEqual(expected, set(diff))
keys = [k for _, (k, _) in diff]
assert keys[0] == keys[1] and keys[2] == keys[3] # same keys
def test_dup_pks(self):
time = "2022-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
cols = "id rating timestamp".split()
self.connection.query(
[
self.src_table.insert_rows([[1, 9, time_obj], [1, 10, time_obj]], columns=cols),
self.dst_table.insert_row(1, 9, time_obj, columns=cols),
]
)
x = self.differ.diff_tables(self.table, self.table2)
self.assertRaises(ValueError, list, x)
def test_null_pks(self):
time = "2022-01-01 00:00:00"
time_obj = datetime.fromisoformat(time)
cols = "id rating timestamp".split()
self.connection.query(
[
self.src_table.insert_row(None, 9, time_obj, columns=cols),
self.dst_table.insert_row(1, 9, time_obj, columns=cols),
]
)
x = self.differ.diff_tables(self.table, self.table2)
self.assertRaises(ValueError, list, x)
@test_each_database_in_list(
d for d in TEST_DATABASES if d.DIALECT_CLASS.SUPPORTS_PRIMARY_KEY and d.SUPPORTS_UNIQUE_CONSTAINT
)
class TestUniqueConstraint(DiffTestCase):
def setUp(self):
super().setUp()
self.src_table = table(
self.table_src_path,
schema={"id": int, "userid": int, "movieid": int, "rating": float},
)
self.dst_table = table(
self.table_dst_path,
schema={"id": int, "userid": int, "movieid": int, "rating": float},
)
self.connection.query(
[self.src_table.create(primary_keys=["id"]), self.dst_table.create(primary_keys=["id", "userid"]), commit]
)
self.differ = JoinDiffer()
def test_unique_constraint(self):
self.connection.query(
[
self.src_table.insert_rows([[1, 1, 1, 9], [2, 2, 2, 9]]),
self.dst_table.insert_rows([[1, 1, 1, 9], [2, 2, 2, 9]]),
commit,
]
)
# Test no active validation
table = TableSegment(self.connection, self.table_src_path, ("id",), case_sensitive=False)
table2 = TableSegment(self.connection, self.table_dst_path, ("id",), case_sensitive=False)
res = list(self.differ.diff_tables(table, table2))
assert not res
assert "validated_unique_keys" not in self.differ.stats
# Test active validation
table = TableSegment(self.connection, self.table_src_path, ("userid",), case_sensitive=False)
table2 = TableSegment(self.connection, self.table_dst_path, ("userid",), case_sensitive=False)
res = list(self.differ.diff_tables(table, table2))
assert not res
self.assertEqual(self.differ.stats["validated_unique_keys"], [["userid"]])