-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathtest_transform.py
More file actions
347 lines (237 loc) · 7.52 KB
/
test_transform.py
File metadata and controls
347 lines (237 loc) · 7.52 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import math
from dataclasses import dataclass
from enum import Enum
from typing import Optional
import pytest
from databricks.bundles.core import (
Variable,
VariableOr,
VariableOrList,
VariableOrOptional,
)
from databricks.bundles.core._transform import _transform
from databricks.bundles.core._transform_to_json import _transform_to_json_object
from databricks.bundles.jobs import (
ClusterSpec,
ClusterSpecDict,
CronSchedule,
ForEachTask,
NotebookTask,
PauseStatus,
Task,
)
class Color(Enum):
RED = "red"
BLUE = "blue"
@dataclass
class MyDataclass:
color: Optional[Color]
def test_transform_int():
@dataclass
class Fake:
field: Optional[int] = None
out = _transform(Fake, {"field": 42})
assert out == Fake(field=42)
def test_transform_bool():
@dataclass
class Fake:
field: Optional[bool] = None
out = _transform(Fake, {"field": "false"})
assert out == Fake(field=False)
def test_transform_str():
@dataclass
class Fake:
field: Optional[str] = None
out = _transform(Fake, {"field": "test"})
assert out == Fake(field="test")
def test_transform_str_list():
@dataclass
class Fake:
field: Optional[list[str]] = None
out = _transform(Fake, {"field": ["a", "b"]})
assert out == Fake(field=["a", "b"])
def test_transform_str_to_list():
with pytest.raises(ValueError) as exc_info:
_transform(list[str], "abc")
assert str(exc_info.value) == "Unexpected type: list[str] for 'abc'"
def test_transform_str_to_dict():
with pytest.raises(ValueError) as exc_info:
_transform(dict[str, str], "abc")
assert str(exc_info.value) == "Unexpected type: dict[str, str] for 'abc'"
def test_transform_none_to_optional_list():
@dataclass
class Fake:
field: Optional[list[str]] = None
out = _transform(Fake, {"field": None})
assert out == Fake(field=None)
def test_transform_none_to_list():
@dataclass
class Fake:
field: list[str]
out = _transform(Fake, {"field": None})
assert out == Fake(field=[])
def test_transform_none_to_optional_dict():
@dataclass
class Fake:
field: Optional[dict[str, str]] = None
out = _transform(Fake, {"field": None})
assert out == Fake(field=None)
def test_transform_none_to_dict():
@dataclass
class Fake:
field: dict[str, str]
out = _transform(Fake, {"field": None})
assert out == Fake(field={})
def test_transform_none_to_dict_of_int():
@dataclass
class Fake:
field: dict[str, int]
out = _transform(Fake, {"field": None})
assert out == Fake(field={})
def test_transform_enum_from_str():
@dataclass
class Fake:
field: Optional[Color] = None
out = _transform(Fake, {"field": "red"})
assert out == Fake(field=Color.RED)
def test_transform_enum_from_enum():
@dataclass
class Fake:
field: Optional[Color] = None
out = _transform(Fake, {"field": Color.RED})
assert out == Fake(field=Color.RED)
def test_transform_enum_list():
@dataclass
class Fake:
field: list[Color]
out = _transform(Fake, {"field": ["red", "blue"]})
assert out == Fake(field=[Color.RED, Color.BLUE])
def test_transform_nested_class_as_class():
@dataclass
class Nested:
field: VariableOr[int]
@dataclass
class Fake:
nested: Nested
out = _transform(Fake, {"nested": Nested(field=42)})
assert out == Fake(nested=Nested(field=42))
# these have to be defined in top-level, or types don't resolve
@dataclass
class ForwardRefA:
b: Optional["ForwardRefB"]
@dataclass
class ForwardRefB:
value: int
def test_transform_forward_ref():
out = _transform(ForwardRefA, {"b": {"value": 42}})
assert out == ForwardRefA(b=ForwardRefB(value=42))
def test_complex_cluster_spec_roundtrip():
# this is what is pre-populated in clusters created from UI
cluster_spec_dict: ClusterSpecDict = {
"autoscale": {"min_workers": 1, "max_workers": 2},
"cluster_name": "test cluster",
"spark_version": "13.3.x-scala2.12",
"aws_attributes": {
"first_on_demand": 1,
"availability": "SPOT_WITH_FALLBACK",
"zone_id": "auto",
"spot_bid_price_percent": 100,
"ebs_volume_count": 0,
},
"node_type_id": "i3.xlarge",
"driver_node_type_id": "i3.xlarge",
"spark_env_vars": {"PYSPARK_PYTHON": "/databricks/python3/bin/python3"},
"autotermination_minutes": 120,
"enable_elastic_disk": False,
"enable_local_disk_encryption": False,
"data_security_mode": "USER_ISOLATION",
"runtime_engine": "PHOTON",
}
cluster_spec = _transform(ClusterSpec, cluster_spec_dict)
cluster_spec_dict_2 = _transform_to_json_object(cluster_spec)
assert cluster_spec_dict == cluster_spec_dict_2
def test_cron_schedule():
cron_schedule = _transform(
CronSchedule,
{
"quartz_cron_expression": "0 0 0 * * ?",
"timezone_id": "UTC",
"pause_status": Variable(path="var.pause_status", type=str),
},
)
assert cron_schedule == CronSchedule(
quartz_cron_expression="0 0 0 * * ?",
timezone_id="UTC",
pause_status=Variable(path="var.pause_status", type=PauseStatus),
)
def test_for_each_task():
"""
Test the special case of recursive data class.
"""
task = _transform(
Task,
{
"task_key": "loop",
"for_each_task": {
"inputs": "[1, 2, 3]",
"task": {
"task_key": "loop_iteration",
"notebook_task": {"notebook_path": "notebooks/foo.ipynb"},
},
},
},
)
assert task == Task(
task_key="loop",
for_each_task=ForEachTask(
inputs="[1, 2, 3]",
task=Task(
task_key="loop_iteration",
notebook_task=NotebookTask(notebook_path="notebooks/foo.ipynb"),
),
),
)
def test_transform_dict_keys():
@dataclass
class Fake:
tags: dict[str, VariableOr[str]]
job = Fake(
tags={
"key1": Variable(path="var.my_var", type=str), # type:ignore
}
)
assert job.tags == {"key1": Variable(path="var.my_var", type=str)}
def test_unknown_fields():
@dataclass
class Fake:
field: Optional[str] = None
with pytest.raises(ValueError) as exc_info:
_transform(Fake, {"field": "test", "unknown": "unknown"})
assert str(exc_info.value) == "Unexpected field 'unknown' for class Fake"
def test_transform_cls_field():
# we have a hack for handing "cls" field coming from locals()
# test that we don't break normal case when it doesn't happen
@dataclass
class Fake:
cls: str
out = _transform(Fake, {"cls": "test"})
assert out == Fake(cls="test")
def test_transform_none_to_variable_or_list():
@dataclass
class Fake:
field: VariableOrList[str]
out = _transform(Fake, {"field": None})
assert out == Fake(field=[])
def test_forward_ref():
@dataclass
class A:
field: VariableOrOptional["MyDataclass"]
out = _transform(A, {"field": {"color": "red"}})
assert out == A(field=MyDataclass(color=Color.RED))
def test_transform_float():
value = float(math.pi)
@dataclass
class Fake:
field: Optional[float] = None
out = _transform(Fake, {"field": value})
assert out == Fake(field=value)