@@ -3,110 +3,153 @@ package template
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "path/filepath"
6
7
"testing"
7
8
"text/template"
8
9
9
- "github.com/databricks/cli/cmd/root"
10
10
"github.com/databricks/cli/libs/jsonschema"
11
11
"github.com/stretchr/testify/assert"
12
12
"github.com/stretchr/testify/require"
13
13
)
14
14
15
- func testConfig (t * testing.T ) * config {
16
- c , err := newConfig (context .Background (), "./testdata/config-test-schema/test-schema.json" )
17
- require .NoError (t , err )
18
- return c
19
- }
20
-
21
15
func TestTemplateConfigAssignValuesFromFile (t * testing.T ) {
22
- c := testConfig ( t )
16
+ testDir := "./testdata/config-assign-from-file"
23
17
24
- err := c .assignValuesFromFile ("./testdata/config-assign-from-file/config.json" )
25
- assert .NoError (t , err )
18
+ ctx := context .Background ()
19
+ c , err := newConfig (ctx , filepath .Join (testDir , "schema.json" ))
20
+ require .NoError (t , err )
26
21
27
- assert .Equal (t , int64 (1 ), c .values ["int_val" ])
28
- assert .Equal (t , float64 (2 ), c .values ["float_val" ])
29
- assert .Equal (t , true , c .values ["bool_val" ])
30
- assert .Equal (t , "hello" , c .values ["string_val" ])
22
+ err = c .assignValuesFromFile (filepath .Join (testDir , "config.json" ))
23
+ if assert .NoError (t , err ) {
24
+ assert .Equal (t , int64 (1 ), c .values ["int_val" ])
25
+ assert .Equal (t , float64 (2 ), c .values ["float_val" ])
26
+ assert .Equal (t , true , c .values ["bool_val" ])
27
+ assert .Equal (t , "hello" , c .values ["string_val" ])
28
+ }
31
29
}
32
30
33
- func TestTemplateConfigAssignValuesFromFileForInvalidIntegerValue (t * testing.T ) {
34
- c := testConfig ( t )
31
+ func TestTemplateConfigAssignValuesFromFileDoesNotOverwriteExistingConfigs (t * testing.T ) {
32
+ testDir := "./testdata/config-assign-from-file"
35
33
36
- err := c . assignValuesFromFile ( "./testdata/config-assign-from-file-invalid-int/config.json" )
37
- assert . EqualError ( t , err , "failed to load config from file ./testdata/config-assign-from-file-invalid-int/config. json: failed to parse property int_val: cannot convert \" abc \" to an integer" )
38
- }
34
+ ctx := context . Background ( )
35
+ c , err := newConfig ( ctx , filepath . Join ( testDir , "schema. json" ) )
36
+ require . NoError ( t , err )
39
37
40
- func TestTemplateConfigAssignValuesFromFileDoesNotOverwriteExistingConfigs (t * testing.T ) {
41
- c := testConfig (t )
42
38
c .values = map [string ]any {
43
39
"string_val" : "this-is-not-overwritten" ,
44
40
}
45
41
46
- err := c .assignValuesFromFile ("./testdata/config-assign-from-file/config.json" )
47
- assert .NoError (t , err )
42
+ err = c .assignValuesFromFile (filepath .Join (testDir , "config.json" ))
43
+ if assert .NoError (t , err ) {
44
+ assert .Equal (t , int64 (1 ), c .values ["int_val" ])
45
+ assert .Equal (t , float64 (2 ), c .values ["float_val" ])
46
+ assert .Equal (t , true , c .values ["bool_val" ])
47
+ assert .Equal (t , "this-is-not-overwritten" , c .values ["string_val" ])
48
+ }
49
+ }
50
+
51
+ func TestTemplateConfigAssignValuesFromFileForInvalidIntegerValue (t * testing.T ) {
52
+ testDir := "./testdata/config-assign-from-file-invalid-int"
53
+
54
+ ctx := context .Background ()
55
+ c , err := newConfig (ctx , filepath .Join (testDir , "schema.json" ))
56
+ require .NoError (t , err )
48
57
49
- assert .Equal (t , int64 (1 ), c .values ["int_val" ])
50
- assert .Equal (t , float64 (2 ), c .values ["float_val" ])
51
- assert .Equal (t , true , c .values ["bool_val" ])
52
- assert .Equal (t , "this-is-not-overwritten" , c .values ["string_val" ])
58
+ err = c .assignValuesFromFile (filepath .Join (testDir , "config.json" ))
59
+ assert .EqualError (t , err , fmt .Sprintf ("failed to load config from file %s: failed to parse property int_val: cannot convert \" abc\" to an integer" , filepath .Join (testDir , "config.json" )))
53
60
}
54
61
55
62
func TestTemplateConfigAssignValuesFromFileFiltersPropertiesNotInTheSchema (t * testing.T ) {
56
- c := testConfig ( t )
63
+ testDir := "./testdata/config-assign-from-file-unknown-property"
57
64
58
- err := c .assignValuesFromFile ("./testdata/config-assign-from-file-unknown-property/config.json" )
65
+ ctx := context .Background ()
66
+ c , err := newConfig (ctx , filepath .Join (testDir , "schema.json" ))
67
+ require .NoError (t , err )
68
+
69
+ err = c .assignValuesFromFile (filepath .Join (testDir , "config.json" ))
59
70
assert .NoError (t , err )
60
71
61
72
// assert only the known property is loaded
62
73
assert .Len (t , c .values , 1 )
63
74
assert .Equal (t , "i am a known property" , c .values ["string_val" ])
64
75
}
65
76
66
- func TestTemplateConfigAssignDefaultValues (t * testing.T ) {
67
- c := testConfig ( t )
77
+ func TestTemplateConfigAssignValuesFromDefaultValues (t * testing.T ) {
78
+ testDir := "./testdata/config-assign-from-default-value"
68
79
69
80
ctx := context .Background ()
70
- ctx = root .SetWorkspaceClient (ctx , nil )
71
- helpers := loadHelpers (ctx )
72
- r , err := newRenderer (ctx , nil , helpers , "./testdata/template-in-path/template" , "./testdata/template-in-path/library" , t .TempDir ())
81
+ c , err := newConfig (ctx , filepath .Join (testDir , "schema.json" ))
82
+ require .NoError (t , err )
83
+
84
+ r , err := newRenderer (ctx , nil , nil , "./testdata/empty/template" , "./testdata/empty/library" , t .TempDir ())
73
85
require .NoError (t , err )
74
86
75
87
err = c .assignDefaultValues (r )
76
- assert .NoError (t , err )
88
+ if assert .NoError (t , err ) {
89
+ assert .Equal (t , int64 (123 ), c .values ["int_val" ])
90
+ assert .Equal (t , float64 (123 ), c .values ["float_val" ])
91
+ assert .Equal (t , true , c .values ["bool_val" ])
92
+ assert .Equal (t , "hello" , c .values ["string_val" ])
93
+ }
94
+ }
95
+
96
+ func TestTemplateConfigAssignValuesFromTemplatedDefaultValues (t * testing.T ) {
97
+ testDir := "./testdata/config-assign-from-templated-default-value"
98
+
99
+ ctx := context .Background ()
100
+ c , err := newConfig (ctx , filepath .Join (testDir , "schema.json" ))
101
+ require .NoError (t , err )
102
+
103
+ r , err := newRenderer (ctx , nil , nil , filepath .Join (testDir , "template/template" ), filepath .Join (testDir , "template/library" ), t .TempDir ())
104
+ require .NoError (t , err )
77
105
78
- assert .Len (t , c .values , 2 )
79
- assert .Equal (t , "my_file" , c .values ["string_val" ])
80
- assert .Equal (t , int64 (123 ), c .values ["int_val" ])
106
+ // Note: only the string value is templated.
107
+ // The JSON schema package doesn't allow using a string default for integer types.
108
+ err = c .assignDefaultValues (r )
109
+ if assert .NoError (t , err ) {
110
+ assert .Equal (t , int64 (123 ), c .values ["int_val" ])
111
+ assert .Equal (t , float64 (123 ), c .values ["float_val" ])
112
+ assert .Equal (t , true , c .values ["bool_val" ])
113
+ assert .Equal (t , "world" , c .values ["string_val" ])
114
+ }
81
115
}
82
116
83
117
func TestTemplateConfigValidateValuesDefined (t * testing.T ) {
84
- c := testConfig (t )
118
+ ctx := context .Background ()
119
+ c , err := newConfig (ctx , "testdata/config-test-schema/test-schema.json" )
120
+ require .NoError (t , err )
121
+
85
122
c .values = map [string ]any {
86
123
"int_val" : 1 ,
87
124
"float_val" : 1.0 ,
88
125
"bool_val" : false ,
89
126
}
90
127
91
- err : = c .validate ()
128
+ err = c .validate ()
92
129
assert .EqualError (t , err , "validation for template input parameters failed. no value provided for required property string_val" )
93
130
}
94
131
95
132
func TestTemplateConfigValidateTypeForValidConfig (t * testing.T ) {
96
- c := testConfig (t )
133
+ ctx := context .Background ()
134
+ c , err := newConfig (ctx , "testdata/config-test-schema/test-schema.json" )
135
+ require .NoError (t , err )
136
+
97
137
c .values = map [string ]any {
98
138
"int_val" : 1 ,
99
139
"float_val" : 1.1 ,
100
140
"bool_val" : true ,
101
141
"string_val" : "abcd" ,
102
142
}
103
143
104
- err : = c .validate ()
144
+ err = c .validate ()
105
145
assert .NoError (t , err )
106
146
}
107
147
108
148
func TestTemplateConfigValidateTypeForUnknownField (t * testing.T ) {
109
- c := testConfig (t )
149
+ ctx := context .Background ()
150
+ c , err := newConfig (ctx , "testdata/config-test-schema/test-schema.json" )
151
+ require .NoError (t , err )
152
+
110
153
c .values = map [string ]any {
111
154
"unknown_prop" : 1 ,
112
155
"int_val" : 1 ,
@@ -115,20 +158,23 @@ func TestTemplateConfigValidateTypeForUnknownField(t *testing.T) {
115
158
"string_val" : "abcd" ,
116
159
}
117
160
118
- err : = c .validate ()
161
+ err = c .validate ()
119
162
assert .EqualError (t , err , "validation for template input parameters failed. property unknown_prop is not defined in the schema" )
120
163
}
121
164
122
165
func TestTemplateConfigValidateTypeForInvalidType (t * testing.T ) {
123
- c := testConfig (t )
166
+ ctx := context .Background ()
167
+ c , err := newConfig (ctx , "testdata/config-test-schema/test-schema.json" )
168
+ require .NoError (t , err )
169
+
124
170
c .values = map [string ]any {
125
171
"int_val" : "this-should-be-an-int" ,
126
172
"float_val" : 1.1 ,
127
173
"bool_val" : true ,
128
174
"string_val" : "abcd" ,
129
175
}
130
176
131
- err : = c .validate ()
177
+ err = c .validate ()
132
178
assert .EqualError (t , err , "validation for template input parameters failed. incorrect type for property int_val: expected type integer, but value is \" this-should-be-an-int\" " )
133
179
}
134
180
@@ -224,19 +270,6 @@ func TestTemplateEnumValidation(t *testing.T) {
224
270
assert .NoError (t , c .validate ())
225
271
}
226
272
227
- func TestAssignDefaultValuesWithTemplatedDefaults (t * testing.T ) {
228
- c := testConfig (t )
229
- ctx := context .Background ()
230
- ctx = root .SetWorkspaceClient (ctx , nil )
231
- helpers := loadHelpers (ctx )
232
- r , err := newRenderer (ctx , nil , helpers , "./testdata/templated-defaults/template" , "./testdata/templated-defaults/library" , t .TempDir ())
233
- require .NoError (t , err )
234
-
235
- err = c .assignDefaultValues (r )
236
- assert .NoError (t , err )
237
- assert .Equal (t , "my_file" , c .values ["string_val" ])
238
- }
239
-
240
273
func TestTemplateSchemaErrorsWithEmptyDescription (t * testing.T ) {
241
274
_ , err := newConfig (context .Background (), "./testdata/config-test-schema/invalid-test-schema.json" )
242
275
assert .EqualError (t , err , "template property property-without-description is missing a description" )
0 commit comments