-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathtest_transform_variable.py
More file actions
228 lines (216 loc) · 6.22 KB
/
test_transform_variable.py
File metadata and controls
228 lines (216 loc) · 6.22 KB
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
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, Union
import pytest
from databricks.bundles.core import (
Variable,
VariableOr,
VariableOrDict,
VariableOrList,
VariableOrOptional,
)
from databricks.bundles.core._transform import (
_find_union_arg,
_transform,
_unwrap_variable_path,
)
class FakeEnum(Enum):
VALUE1 = "value1"
VALUE2 = "value2"
@dataclass
class Fake:
dict_field: VariableOrOptional[dict[str, VariableOr[str]]] = field(
default_factory=dict
)
str_field: VariableOrOptional[str] = None
enum_field: VariableOrOptional[FakeEnum] = None
@pytest.mark.parametrize(
"input,tpe,expected",
[
(
{"key1": "${var.my_var}"},
dict[str, VariableOr[str]],
{"key1": Variable(path="var.my_var", type=str)},
),
(
{"dict_field": {"key1": "${var.my_var}"}},
Fake,
Fake(dict_field={"key1": Variable(path="var.my_var", type=str)}),
),
(
{"dict_field": "${var.my_var}"},
Fake,
Fake(
dict_field=Variable(
path="var.my_var",
type=dict[str, VariableOr[str]],
)
),
),
(
{"str_field": "${var.my_var}"},
Fake,
Fake(str_field=Variable(path="var.my_var", type=str)),
),
(
"${var.my_var}",
Variable[str],
Variable(path="var.my_var", type=str),
),
(
"${var.my_var}",
Variable[dict[str, str]],
Variable(path="var.my_var", type=dict[str, str]),
),
(
"${var.my_var}",
Variable[Fake],
Variable(path="var.my_var", type=Fake),
),
(
"${var.my_var}",
Union[Variable[Fake], Fake],
Variable(path="var.my_var", type=Fake),
),
# _transform keeps variable types when possible
(
{"enum_field": Variable(path="var.my_var", type=FakeEnum)},
Fake,
Fake(enum_field=Variable(path="var.my_var", type=FakeEnum)),
),
# _transform can get a Variable[str], that should be turned into Variable[Enum]
(
{"enum_field": Variable(path="var.my_var", type=str)},
Fake,
Fake(enum_field=Variable(path="var.my_var", type=FakeEnum)),
),
(
{"enum_field": "value1"},
Fake,
Fake(enum_field=FakeEnum.VALUE1),
),
],
)
def test_transform_variable(input, tpe, expected):
assert _transform(tpe, input) == expected
@pytest.mark.parametrize(
"input,tpe,expected",
# The following lines use `type: ignore` because Pyright raises an error: `__getitem__` method is not defined on the type `UnionType`.
# This is a known issue, and more details can be found at: https://github.com/microsoft/pyright/issues/8319
[
# if value looks like variable, it should be variable
(
"${var.my_var}",
VariableOr[bool], # type:ignore
Variable[bool],
),
(
"${var.my_var}",
VariableOrOptional[bool], # type:ignore
Variable[bool],
),
# if not, we should return value, even if type doesn't match, because
# it's our best guess
(
42,
VariableOr[str], # type:ignore
str,
),
(
42,
VariableOrOptional[str], # type:ignore
str,
),
# variable types only matter for typechecker, we ignore them
# when we do instanceof check
(
Variable(path="my_var", type=str),
VariableOr[FakeEnum], # type:ignore
Variable[FakeEnum],
),
(
Variable(path="my_var", type=str),
VariableOrOptional[FakeEnum], # type:ignore
Variable[FakeEnum],
),
# if value is None, we should always choose NoneType
(
None,
VariableOrOptional[str], # type:ignore
type(None),
),
(
None,
Optional[str],
type(None),
),
# if value is None, but value is non-nullable we need to return None value
(
None,
VariableOr[str], # type:ignore
None,
),
(
[],
VariableOrList[int],
list[VariableOr[int]],
),
(
{},
VariableOrDict[int],
dict[str, VariableOr[int]],
),
# when we see "None", it can become list or dict even if type is not optional
# this is needed for "create" method that always has optional collections
# while dataclasses have them required
(
None,
VariableOrList[int],
list[VariableOr[int]],
),
(
None,
VariableOrDict[int],
dict[str, VariableOr[int]],
),
],
)
def test_find_union_arg(input, tpe, expected):
assert _find_union_arg(input, tpe) == expected
@pytest.mark.parametrize(
"input,expected",
[
pytest.param(
"${var.my_var}",
"var.my_var",
id="simple variable",
),
pytest.param(
"${var.my_var} ${var.my_var}",
None,
id="multiple variables aren't allowed",
),
pytest.param(
"${var.my_var[0]}",
"var.my_var[0]",
id="variable with subscript",
),
pytest.param(
"${var.my_var[0].foo}",
"var.my_var[0].foo",
id="variable with subscript + attribute",
),
pytest.param(
"${var.my_var[0].foo.bar}",
"var.my_var[0].foo.bar",
id="variable with multiple subscripts",
),
pytest.param(
"${var.my_var[0].foo[0]}",
"var.my_var[0].foo[0]",
id="variable with subscript inside attribute",
),
],
)
def test_unwrap_variable_path(input, expected):
assert _unwrap_variable_path(input) == expected