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_utils.py
213 lines (171 loc) · 7.34 KB
/
test_utils.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
import unittest
import re
from data_diff.utils import (
remove_passwords_in_dict,
match_regexps,
match_like,
number_to_human,
diff_int_dynamic_color_template,
dbt_diff_string_template,
columns_removed_template,
columns_added_template,
columns_type_changed_template,
)
from data_diff.__main__ import _remove_passwords_in_dict
class TestUtils(unittest.TestCase):
def test_remove_passwords_in_dict(self):
# Test replacing password value
d = {"password": "mypassword"}
remove_passwords_in_dict(d)
assert d["password"] == "***"
# Test replacing password in database URL
d = {"database_url": "mysql://user:mypassword@localhost/db"}
remove_passwords_in_dict(d, "$$$$")
assert d["database_url"] == "mysql://user:$$$$@localhost/db"
# Test replacing motherduck token in database URL
d = {"database_url": "md:datafold_demo?motherduck_token=jaiwefjoaisdk"}
remove_passwords_in_dict(d, "$$$$")
assert d["database_url"] == "md:datafold_demo?motherduck_token=$$$$"
# Test replacing password in nested dictionary
d = {"info": {"password": "mypassword"}}
remove_passwords_in_dict(d, "%%")
assert d["info"]["password"] == "%%"
# Test replacing a motherduck token in nested dictionary
d = {
"database1": {"driver": "duckdb", "filepath": "md:datafold_demo?motherduck_token=awieojfaowiejacijobhiwaef"}
}
remove_passwords_in_dict(d, "%%")
assert d["database1"]["filepath"] == "md:datafold_demo?motherduck_token=%%"
# Test __main__ utility version of this function
def test__main__remove_passwords_in_dict(self):
# Test replacing password value
d = {"password": "mypassword"}
_remove_passwords_in_dict(d)
assert d["password"] == "**********"
# Test replacing password in database URL
d = {"database_url": "mysql://user:mypassword@localhost/db"}
_remove_passwords_in_dict(d)
assert d["database_url"] == "mysql://user:***@localhost/db"
# Test replacing motherduck token in database URL
d = {"database_url": "md:datafold_demo?motherduck_token=jaiwefjoaisdk"}
_remove_passwords_in_dict(d)
assert d["database_url"] == "md:datafold_demo?motherduck_token=***"
# Test replacing password in nested dictionary
d = {"info": {"password": "mypassword"}}
_remove_passwords_in_dict(d)
assert d["info"]["password"] == "**********"
# Test replacing a motherduck token in nested dictionary
d = {
"database1": {"driver": "duckdb", "filepath": "md:datafold_demo?motherduck_token=awieojfaowiejacijobhiwaef"}
}
_remove_passwords_in_dict(d)
assert d["database1"]["filepath"] == "md:datafold_demo?motherduck_token=**********"
def test_match_regexps(self):
def only_results(x):
return [v for k, v in x]
# Test with no matches
regexps = {"a*": 1, "b*": 2}
s = "c"
assert only_results(match_regexps(regexps, s)) == []
# Test with one match
regexps = {"a*": 1, "b*": 2}
s = "b"
assert only_results(match_regexps(regexps, s)) == [2]
# Test with multiple matches
regexps = {"abc": 1, "ab*c": 2, "c*": 3}
s = "abc"
assert only_results(match_regexps(regexps, s)) == [1, 2]
# Test with regexp that doesn't match the end of the string
regexps = {"a*b": 1}
s = "acb"
assert only_results(match_regexps(regexps, s)) == []
def test_match_like(self):
strs = ["abc", "abcd", "ab", "bcd", "def"]
# Test exact match
pattern = "abc"
result = list(match_like(pattern, strs))
assert result == ["abc"]
# Test % match
pattern = "a%"
result = list(match_like(pattern, strs))
self.assertEqual(result, ["abc", "abcd", "ab"])
# Test ? match
pattern = "a?c"
result = list(match_like(pattern, strs))
self.assertEqual(result, ["abc"])
def test_number_to_human(self):
# Test basic conversion
assert number_to_human(1000) == "1k"
assert number_to_human(1000000) == "1m"
assert number_to_human(1000000000) == "1b"
# Test decimal values
assert number_to_human(1234) == "1k"
assert number_to_human(12345) == "12k"
assert number_to_human(123456) == "123k"
assert number_to_human(1234567) == "1m"
assert number_to_human(12345678) == "12m"
assert number_to_human(123456789) == "123m"
assert number_to_human(1234567890) == "1b"
# Test negative values
assert number_to_human(-1000) == "-1k"
assert number_to_human(-1000000) == "-1m"
assert number_to_human(-1000000000) == "-1b"
class TestDiffIntDynamicColorTemplate(unittest.TestCase):
def test_string_input(self):
self.assertEqual(diff_int_dynamic_color_template("test_string"), "test_string")
def test_positive_diff_value(self):
self.assertEqual(diff_int_dynamic_color_template(10), "[green]+10[/]")
def test_negative_diff_value(self):
self.assertEqual(diff_int_dynamic_color_template(-10), "[red]-10[/]")
def test_zero_diff(self):
self.assertEqual(diff_int_dynamic_color_template(0), "0")
class TestDbtDiffStringTemplateNoMock(unittest.TestCase):
def test_dbt_diff_string_template(self):
self.maxDiff = None
expected_output = """
rows PROD <> DEV
--------- ------ ------------ ------------------
Total 10 20 [[green]+10[/]]
Added [green]+5[/]
Removed [red]-2[/]
Different 3
Unchanged 5
columns # diff values
--------- ---------------
info values
deps # data assets
------ ---------------
dep assets"""
output = dbt_diff_string_template(
total_rows_table1=10,
total_rows_table2=20,
total_rows_diff=10,
rows_added=5,
rows_removed=2,
rows_updated=3,
rows_unchanged=5,
extra_info_dict={"info": "values"},
extra_info_str="extra info",
is_cloud=False,
deps_impacts={"dep": "assets"},
)
self.assertEqual(output, expected_output)
class TestColumnsTemplateMethods(unittest.TestCase):
def extract_columns_set(self, output):
# Extract quoted words by regex
output_list = re.findall(r"'(\w*)'", output)
# Convert list to set
output_set = set(output_list)
return output_set
def test_columns_removed_template(self):
output = columns_removed_template({"column1", "column2"})
self.assertIn("[red]Columns removed [-2]:[/]", output)
self.assertEqual(self.extract_columns_set(output), {"column1", "column2"})
def test_columns_added_template(self):
output = columns_added_template({"column1", "column2"})
self.assertIn("[green]Columns added [+2]:", output)
self.assertEqual(self.extract_columns_set(output), {"column1", "column2"})
def test_columns_type_changed_template(self):
output = columns_type_changed_template({"column1", "column2"})
self.assertIn("Type changed [2]: [green]", output)
self.assertEqual(self.extract_columns_set(output), {"column1", "column2"})