Skip to content

Commit f04108e

Browse files
SDKv2 Diff tests for Map
1 parent 8552a4a commit f04108e

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed
Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -13,57 +14,45 @@ import (
1314
func TestSDKv2DetailedDiffMap(t *testing.T) {
1415
t.Parallel()
1516

16-
res := schema.Resource{
17-
Schema: map[string]*schema.Schema{
18-
"map_prop": {
19-
Type: schema.TypeMap,
20-
Optional: true,
21-
Elem: &schema.Schema{Type: schema.TypeString},
22-
},
23-
},
24-
}
25-
26-
ctyVal := func(v map[string]string) map[string]cty.Value {
17+
ctyMaker := func(v map[string]string) cty.Value {
2718
ctyMap := make(map[string]cty.Value)
28-
29-
if len(v) == 0 {
30-
return map[string]cty.Value{
31-
"map_prop": cty.MapValEmpty(cty.String),
32-
}
33-
}
34-
3519
for k, v := range v {
3620
ctyMap[k] = cty.StringVal(v)
3721
}
38-
return map[string]cty.Value{
39-
"map_prop": cty.MapVal(ctyMap),
40-
}
22+
return cty.MapVal(ctyMap)
4123
}
4224

43-
scenarios := []struct {
44-
name string
45-
initialValue map[string]string
46-
changeValue map[string]string
47-
}{
48-
{"unchanged empty", map[string]string{}, map[string]string{}},
49-
{"unchanged non-empty", map[string]string{"key": "val"}, map[string]string{"key": "val"}},
50-
{"added", map[string]string{}, map[string]string{"key": "val"}},
51-
{"removed", map[string]string{"key": "val"}, map[string]string{}},
52-
{"value changed", map[string]string{"key": "val"}, map[string]string{"key": "val2"}},
53-
{"key changed", map[string]string{"key": "val"}, map[string]string{"key2": "val"}},
54-
}
25+
var nilVal map[string]string
26+
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
27+
schema.TypeMap, ctyMaker, map[string]string{"key": "val1"}, map[string]string{"key": "val2"},
28+
map[string]string{"key": "computedVal"}, map[string]string{"key": "defaultVal"}, nilVal)
5529

56-
for _, scenario := range scenarios {
57-
t.Run(scenario.name, func(t *testing.T) {
30+
scenarios = append(scenarios, diffScenario[map[string]string]{
31+
name: "key changed",
32+
initialValue: ref(map[string]string{"key": "val1"}),
33+
changeValue: ref(map[string]string{"key2": "val1"}),
34+
})
35+
36+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
37+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
5838
t.Parallel()
59-
diff := crosstests.Diff(t, &res, ctyVal(scenario.initialValue), ctyVal(scenario.changeValue))
60-
autogold.ExpectFile(t, testOutput{
61-
initialValue: scenario.initialValue,
62-
changeValue: scenario.changeValue,
63-
tfOut: diff.TFOut,
64-
pulumiOut: diff.PulumiOut,
65-
detailedDiff: diff.PulumiDiff.DetailedDiff,
66-
})
39+
for _, scenario := range scenarios {
40+
t.Run(scenario.name, func(t *testing.T) {
41+
if strings.Contains(schemaValueMakerPair.name, "required") &&
42+
(scenario.initialValue == nil || scenario.changeValue == nil) {
43+
t.Skip("Required fields cannot be unset")
44+
}
45+
t.Parallel()
46+
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
47+
autogold.ExpectFile(t, testOutput{
48+
initialValue: scenario.initialValue,
49+
changeValue: scenario.changeValue,
50+
tfOut: diff.TFOut,
51+
pulumiOut: diff.PulumiOut,
52+
detailedDiff: diff.PulumiDiff.DetailedDiff,
53+
})
54+
})
55+
}
6756
})
6857
}
6958
}

0 commit comments

Comments
 (0)