Skip to content

Commit 6ae2b43

Browse files
SDKv2 Diff tests for Defaults in sets and lists
1 parent f73e706 commit 6ae2b43

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

pkg/tests/diff_test/detailed_diff_list_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ func TestSDKv2DetailedDiffList(t *testing.T) {
199199
},
200200
}
201201

202+
listBlockSchemaNestedDefault := schema.Resource{
203+
Schema: map[string]*schema.Schema{
204+
"prop": {
205+
Type: schema.TypeList,
206+
Optional: true,
207+
Elem: &schema.Resource{
208+
Schema: map[string]*schema.Schema{
209+
"nested_prop": {
210+
Type: schema.TypeString,
211+
Optional: true,
212+
},
213+
"default": {
214+
Type: schema.TypeString,
215+
Optional: true,
216+
Default: "default",
217+
},
218+
},
219+
},
220+
},
221+
},
222+
}
223+
202224
listPairs := []diffSchemaValueMakerPair[[]string]{
203225
{"list attribute", listAttrSchema, listValueMaker},
204226
{"list attribute force new", listAttrSchemaForceNew, listValueMaker},
@@ -207,6 +229,11 @@ func TestSDKv2DetailedDiffList(t *testing.T) {
207229
{"list block nested force new", listBlockSchemaNestedForceNew, nestedListValueMaker},
208230
{"list block sensitive", listBlockSchemaSensitive, nestedListValueMaker},
209231
{"list block nested sensitive", listBlockSchemaNestedSensitive, nestedListValueMaker},
232+
{"list block nested default", listBlockSchemaNestedDefault, nestedListValueMaker},
233+
{
234+
"list block nested default with default specified in program",
235+
listBlockSchemaNestedDefault, nestedListValueMakerWithDefaultSpecified,
236+
},
210237
}
211238

212239
maxItemsOnePairs := []diffSchemaValueMakerPair[[]string]{

pkg/tests/diff_test/detailed_diff_set_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,37 @@ func TestSDKv2DetailedDiffSetBlockSensitive(t *testing.T) {
546546

547547
runSDKv2TestMatrix(t, diffSchemaValueMakerPairs, setScenarios())
548548
}
549+
550+
func TestSDKv2DetailedDiffSetDefault(t *testing.T) {
551+
t.Parallel()
552+
// Note Default is not valid for set types.
553+
554+
blockSchemaNestedDefault := schema.Resource{
555+
Schema: map[string]*schema.Schema{
556+
"prop": {
557+
Type: schema.TypeSet,
558+
Optional: true,
559+
Elem: &schema.Resource{
560+
Schema: map[string]*schema.Schema{
561+
"nested_prop": {
562+
Type: schema.TypeString,
563+
Optional: true,
564+
},
565+
"default": {
566+
Type: schema.TypeString,
567+
Optional: true,
568+
Default: "default",
569+
},
570+
},
571+
},
572+
},
573+
},
574+
}
575+
576+
diffSchemaValueMakerPairs := []diffSchemaValueMakerPair[[]string]{
577+
{"block nested default", blockSchemaNestedDefault, nestedListValueMaker},
578+
{"block nested default with default specified in program", blockSchemaNestedDefault, nestedListValueMakerWithDefaultSpecified},
579+
}
580+
581+
runSDKv2TestMatrix(t, diffSchemaValueMakerPairs, setScenarios())
582+
}

pkg/tests/diff_test/value_makers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,29 @@ func nestedListValueMakerWithComputedSpecified(arr *[]string) map[string]cty.Val
294294
}
295295
}
296296

297+
func nestedListValueMakerWithDefaultSpecified(arr *[]string) map[string]cty.Value {
298+
if arr == nil {
299+
return map[string]cty.Value{}
300+
}
301+
302+
if len(*arr) == 0 {
303+
return map[string]cty.Value{
304+
"prop": cty.ListValEmpty(cty.DynamicPseudoType),
305+
}
306+
}
307+
308+
slice := make([]cty.Value, len(*arr))
309+
for i, v := range *arr {
310+
slice[i] = cty.ObjectVal(map[string]cty.Value{
311+
"nested_prop": cty.StringVal(v),
312+
"default": cty.StringVal("non-default-" + v),
313+
})
314+
}
315+
return map[string]cty.Value{
316+
"prop": cty.ListVal(slice),
317+
}
318+
}
319+
297320
var _ valueMaker[[]string] = nestedListValueMakerWithComputedSpecified
298321

299322
type testOutput struct {

0 commit comments

Comments
 (0)