Repository navigation
Expand file tree
/
Copy pathapply_python_output_test.go
More file actions
272 lines (240 loc) · 7.07 KB
/
Copy pathapply_python_output_test.go
File metadata and controls
272 lines (240 loc) · 7.07 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
package python
import (
"testing"
"github.com/databricks/cli/bundle/config/mutator/resourcemutator"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn/merge"
"github.com/databricks/cli/libs/dyn"
"github.com/stretchr/testify/assert"
)
type applyPythonOutputTestCase struct {
name string
input dyn.Value
output dyn.Value
added []resourcemutator.ResourceKey
updated []resourcemutator.ResourceKey
deleted []resourcemutator.ResourceKey
}
func TestApplyPythonOutput(t *testing.T) {
job1 := mapOf("name", dyn.V("job 1"))
job2 := mapOf("name", dyn.V("job 2"))
testCases := []applyPythonOutputTestCase{
{
name: "add job (0)",
input: emptyMap(),
output: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
added: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "add job (1)",
input: mapOf("resources", emptyMap()),
output: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
added: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "add job (2)",
input: mapOf("resources", mapOf("jobs", emptyMap())),
output: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
added: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "add job (3)",
input: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
output: mapOf("resources", mapOf("jobs", mapOf2("job_1", job1, "job_2", job2))),
added: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_2",
},
},
},
{
name: "delete job (0)",
input: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
output: emptyMap(),
deleted: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "delete job (1)",
input: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
output: mapOf("resources", emptyMap()),
deleted: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "delete job (2)",
input: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
output: mapOf("resources", mapOf("jobs", emptyMap())),
deleted: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "update job",
input: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
output: mapOf("resources", mapOf("jobs", mapOf("job_1", job2))),
updated: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "update job through 'name' delete",
input: mapOf("resources", mapOf("jobs", mapOf("job_1", job1))),
output: mapOf("resources", mapOf("jobs", mapOf("job_1", emptyMap()))),
updated: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
{
name: "update job through 'description' insert",
input: mapOf("resources", mapOf("jobs",
mapOf("job_1", mapOf("name", dyn.V("name"))),
)),
output: mapOf("resources", mapOf("jobs",
mapOf("job_1", mapOf2("name", dyn.V("name"), "description", dyn.V("description"))),
)),
updated: []resourcemutator.ResourceKey{
{
Type: "jobs",
Name: "job_1",
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
merged, state, err := applyPythonOutput(tc.input, tc.output)
assert.NoError(t, err)
assert.Equal(t, tc.output, merged)
assert.ElementsMatch(t, tc.added, state.AddedResources.ToArray())
assert.ElementsMatch(t, tc.updated, state.UpdatedResources.ToArray())
assert.ElementsMatch(t, tc.deleted, state.DeletedResources.ToArray())
})
}
}
func TestMergeOutput_disallowDelete(t *testing.T) {
input := mapOf("not_resource", dyn.V("value"))
output := emptyMap()
_, _, err := applyPythonOutput(input, output)
assert.EqualError(t, err, `unexpected change at "not_resource" (delete)`)
}
func TestMergeOutput_disallowInsert(t *testing.T) {
output := mapOf("not_resource", dyn.V("value"))
input := emptyMap()
_, _, err := applyPythonOutput(input, output)
assert.EqualError(t, err, `unexpected change at "not_resource" (insert)`)
}
func TestMergeOutput_disallowUpdate(t *testing.T) {
output := mapOf("not_resource", dyn.V("value"))
input := mapOf("not_resource", dyn.V("new value"))
_, _, err := applyPythonOutput(input, output)
assert.EqualError(t, err, `unexpected change at "not_resource" (update)`)
}
type overrideVisitorOmitemptyTestCase struct {
name string
path dyn.Path
left dyn.Value
expectedErr error
}
func TestCreateOverrideVisitor_omitempty(t *testing.T) {
// Python output can omit empty sequences/mappings in output, because we don't track them as optional,
// there is no semantic difference between empty and missing, so we keep them as they were before
// Python code deleted them.
location := diag.Location{
File: "databricks.yml",
Line: 10,
Column: 20,
}
testCases := []overrideVisitorOmitemptyTestCase{
{
name: "undo delete of empty variables",
path: dyn.MustPathFromString("variables"),
left: dyn.NewValue([]dyn.Value{}, []diag.Location{location}),
expectedErr: merge.ErrOverrideUndoDelete,
},
{
name: "undo delete of empty job clusters",
path: dyn.MustPathFromString("resources.jobs.job0.job_clusters"),
left: dyn.NewValue([]dyn.Value{}, []diag.Location{location}),
expectedErr: merge.ErrOverrideUndoDelete,
},
{
name: "allow delete of non-empty job clusters",
path: dyn.MustPathFromString("resources.jobs.job0.job_clusters"),
left: dyn.NewValue([]dyn.Value{dyn.NewValue("abc", []diag.Location{location})}, []diag.Location{location}),
expectedErr: nil,
},
{
name: "undo delete of empty tags",
path: dyn.MustPathFromString("resources.jobs.job0.tags"),
left: dyn.NewValue(map[string]dyn.Value{}, []diag.Location{location}),
expectedErr: merge.ErrOverrideUndoDelete,
},
{
name: "allow delete of non-empty tags",
path: dyn.MustPathFromString("resources.jobs.job0.tags"),
left: dyn.NewValue(map[string]dyn.Value{"dev": dyn.NewValue("true", []diag.Location{location})}, []diag.Location{location}),
expectedErr: nil,
},
{
name: "undo delete of nil",
path: dyn.MustPathFromString("resources.jobs.job0.tags"),
left: dyn.NilValue.WithLocations([]diag.Location{location}),
expectedErr: merge.ErrOverrideUndoDelete,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, visitor := createOverrideVisitor(dyn.NilValue, dyn.NilValue)
err := visitor.VisitDelete(tc.path, tc.left)
assert.Equal(t, tc.expectedErr, err)
})
}
}
func mapOf(key string, value dyn.Value) dyn.Value {
return dyn.V(map[string]dyn.Value{
key: value,
})
}
func mapOf2(key1 string, value1 dyn.Value, key2 string, value2 dyn.Value) dyn.Value {
return dyn.V(map[string]dyn.Value{
key1: value1,
key2: value2,
})
}
func emptyMap() dyn.Value {
return dyn.V(map[string]dyn.Value{})
}