-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathtest_util.py
160 lines (137 loc) · 4.54 KB
/
test_util.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
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import pytest
@pytest.fixture
def module_under_test():
from data_validation import util
return util
def test_timed_call_fn_no_args(module_under_test, caplog):
"""Test timed_call works with no parameters."""
caplog.set_level(logging.DEBUG)
caplog.clear()
def fn():
return 1
assert fn() == module_under_test.timed_call("Pure fn", fn)
assert any(_ for _ in caplog.messages if "Pure fn" in _)
def test_timed_call_fn_args(module_under_test, caplog):
"""Test timed_call works with positional parameters."""
caplog.set_level(logging.DEBUG)
caplog.clear()
def fn_args(a1: int, a2: int):
return a1
assert fn_args(1, 2) == module_under_test.timed_call("args fn", fn_args, 1, 2)
assert any(_ for _ in caplog.messages if "args fn" in _)
def test_timed_call_fn_cwargs(module_under_test, caplog):
"""Test timed_call works with named parameters."""
caplog.set_level(logging.DEBUG)
caplog.clear()
def fn_cwargs(c1: int = None):
return c1
assert fn_cwargs(3) == module_under_test.timed_call("cwargs fn", fn_cwargs, c1=3)
assert any(_ for _ in caplog.messages if "cwargs fn" in _)
@pytest.mark.parametrize(
"test_input,expected,sep",
[
# Test with default separator of space.
(
"abc",
[
"abc",
],
None,
),
(
"a b c",
[
"a",
"b",
"c",
],
None,
),
(
"a 'b c'",
[
"a",
"'b c'",
],
None,
),
# Test with separator of ":" which matches --filters separator.
(
"col1 = 1:col2 = 1",
[
"col1 = 1",
"col2 = 1",
],
":",
),
(
"col1 = ':'",
[
"col1 = ':'",
],
":",
),
(
"col1 = ':':col2 = ''':'''",
[
"col1 = ':'",
"col2 = ''':'''",
],
":",
),
(
"col_ts >= to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS') "
"and col_ts < to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS')",
[
"col_ts >= to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS') "
"and col_ts < to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS')",
],
":",
),
(
"col_ts1 between to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS') and to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS')"
":"
"col_ts2 between to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS') and to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS')",
[
"col_ts1 between to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS') and to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS')",
"col_ts2 between to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS') and to_timestamp('2024-04-01 16:00:00','YYYY-MM-DD HH24:MI:SS')",
],
":",
),
],
)
def test_split_not_in_quotes(
module_under_test, test_input: str, expected: tuple, sep: str
):
if sep:
result = module_under_test.split_not_in_quotes(test_input, sep=sep)
else:
result = module_under_test.split_not_in_quotes(test_input)
assert result == expected
@pytest.mark.parametrize(
"test_input,expected",
[
("", None),
(None, None),
({"a": 123}, {"a": 123}),
('{"a": 123}', {"a": 123}),
("{'a': 123}", {"a": 123}),
],
)
def test_dvt_config_string_to_dict(module_under_test, test_input: str, expected):
result = module_under_test.dvt_config_string_to_dict(test_input)
assert result == expected