-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathtest_bundle.py
More file actions
117 lines (78 loc) · 3.12 KB
/
test_bundle.py
File metadata and controls
117 lines (78 loc) · 3.12 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
from dataclasses import dataclass
import pytest
from databricks.bundles.core import (
Bundle,
Variable,
VariableOr,
VariableOrList,
)
def test_variable_not_a_variable():
bundle = Bundle(target="development", variables={})
assert bundle.resolve_variable(42) == 42
def test_variable_unknown():
bundle = Bundle(target="development", variables={"bar": 1})
var = Variable(path="var.foo", type=str)
with pytest.raises(ValueError) as exc_info:
bundle.resolve_variable(var)
assert (
str(exc_info.value)
== "Can't find 'foo' variable. Did you define it in databricks.yml?"
)
def test_variable_get_bool_true():
bundle = Bundle(target="development", variables={"foo": "true"})
var = Variable(path="var.foo", type=bool)
assert bundle.resolve_variable(var) is True
def test_variable_get_bool_false():
bundle = Bundle(target="development", variables={"foo": "false"})
var = Variable(path="var.foo", type=bool)
assert bundle.resolve_variable(var) is False
def test_variable_complex():
@dataclass
class Fake:
name: str
bundle = Bundle(target="development", variables={"foo": {"name": "bar"}})
var = Variable(path="var.foo", type=Fake)
assert bundle.resolve_variable(var) == Fake(name="bar")
def test_variable_failed_to_parse():
@dataclass
class Fake:
name: str
bundle = Bundle(target="development", variables={"foo": {}})
var = Variable(path="var.foo", type=Fake)
with pytest.raises(ValueError) as exc_info:
bundle.resolve_variable(var)
assert str(exc_info.value) == "Failed to read 'foo' variable value"
assert exc_info.value.__cause__
def test_variable_uses_variable():
bundle = Bundle(target="development", variables={"foo": "${var.bar}"})
var = Variable(path="var.foo", type=Variable[str])
assert bundle.resolve_variable(var) == Variable(path="var.bar", type=str)
def test_variable_uses_variable_not_supported():
bundle = Bundle(target="development", variables={"foo": "${var.bar}"})
var = Variable(path="var.foo", type=str)
with pytest.raises(ValueError) as exc_info:
bundle.resolve_variable(var)
assert (
str(exc_info.value)
== "Failed to resolve 'foo' because refers to another variable 'var.bar'. "
"Change variable type to Variable[VariableOr[str]]"
)
def test_variable_uses_variable_not_supported_complex():
@dataclass
class Fake:
name: str
bundle = Bundle(target="development", variables={"foo": "${var.bar}"})
var = Variable(path="var.foo", type=Fake)
with pytest.raises(ValueError) as exc_info:
bundle.resolve_variable(var)
assert (
str(exc_info.value)
== "Failed to resolve 'foo' because refers to another variable 'var.bar'. "
"Change variable type to Variable[VariableOr[Fake]]"
)
def test_resolve_variable_or_list():
bundle = Bundle(
target="development", variables={"foo": ["${var.bar}"], "bar": "baz"}
)
var: VariableOrList[str] = Variable(path="var.foo", type=list[VariableOr[str]])
assert bundle.resolve_variable_list(var) == ["baz"]