Skip to content

Commit d9846f0

Browse files
SDKv2 Diff tests for Map
1 parent 05799fe commit d9846f0

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed
Lines changed: 35 additions & 42 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,49 @@ 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 {
27-
ctyMap := make(map[string]cty.Value)
28-
17+
ctyMaker := func(v map[string]string) cty.Value {
2918
if len(v) == 0 {
30-
return map[string]cty.Value{
31-
"map_prop": cty.MapValEmpty(cty.String),
32-
}
19+
return cty.MapValEmpty(cty.String)
3320
}
34-
21+
ctyMap := make(map[string]cty.Value)
3522
for k, v := range v {
3623
ctyMap[k] = cty.StringVal(v)
3724
}
38-
return map[string]cty.Value{
39-
"map_prop": cty.MapVal(ctyMap),
40-
}
25+
return cty.MapVal(ctyMap)
4126
}
4227

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-
}
28+
var nilVal map[string]string
29+
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
30+
schema.TypeMap, &schema.Schema{Type: schema.TypeString}, ctyMaker,
31+
map[string]string{"key": "val1"}, map[string]string{"key": "val2"},
32+
map[string]string{"key": "computedVal"}, map[string]string{"key": "defaultVal"}, nilVal)
5533

56-
for _, scenario := range scenarios {
57-
t.Run(scenario.name, func(t *testing.T) {
34+
scenarios = append(scenarios, diffScenario[map[string]string]{
35+
name: "key changed",
36+
initialValue: ref(map[string]string{"key": "val1"}),
37+
changeValue: ref(map[string]string{"key2": "val1"}),
38+
})
39+
40+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
41+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
5842
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-
})
43+
for _, scenario := range scenarios {
44+
t.Run(scenario.name, func(t *testing.T) {
45+
if strings.Contains(schemaValueMakerPair.name, "required") &&
46+
(scenario.initialValue == nil || scenario.changeValue == nil) {
47+
t.Skip("Required fields cannot be unset")
48+
}
49+
t.Parallel()
50+
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
51+
autogold.ExpectFile(t, testOutput{
52+
initialValue: scenario.initialValue,
53+
changeValue: scenario.changeValue,
54+
tfOut: diff.TFOut,
55+
pulumiOut: diff.PulumiOut,
56+
detailedDiff: diff.PulumiDiff.DetailedDiff,
57+
})
58+
})
59+
}
6760
})
6861
}
6962
}

pkg/tests/diff_test/detailed_diff_primitive_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
1212

1313
var nilVal string
1414
schemaValueMakerPairs, scenarios := generateBaseTests(
15-
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
15+
schema.TypeString, nil, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
1616

1717
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
1818
}
@@ -22,7 +22,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) {
2222

2323
var nilVal bool
2424
schemaValueMakerPairs, scenarios := generateBaseTests(
25-
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
25+
schema.TypeBool, nil, cty.BoolVal, true, false, true, false, nilVal)
2626

2727
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
2828
}
@@ -32,7 +32,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) {
3232

3333
var nilVal int64
3434
schemaValueMakerPairs, scenarios := generateBaseTests(
35-
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
35+
schema.TypeInt, nil, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
3636

3737
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
3838
}
@@ -42,7 +42,7 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) {
4242

4343
var nilVal float64
4444
schemaValueMakerPairs, scenarios := generateBaseTests(
45-
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
45+
schema.TypeFloat, nil, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
4646

4747
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
4848
}

pkg/tests/diff_test/value_makers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func runSDKv2TestMatrix[T any](
5959
}
6060

6161
func generateBaseTests[T any](
62-
typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
62+
typ schema.ValueType, elem any, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
6363
) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) {
6464
valueOne := ref(val1)
6565
valueTwo := ref(val2)
@@ -78,6 +78,7 @@ func generateBaseTests[T any](
7878
Schema: map[string]*schema.Schema{
7979
"prop": {
8080
Type: typ,
81+
Elem: elem,
8182
Optional: true,
8283
},
8384
},
@@ -87,6 +88,7 @@ func generateBaseTests[T any](
8788
Schema: map[string]*schema.Schema{
8889
"prop": {
8990
Type: typ,
91+
Elem: elem,
9092
Optional: true,
9193
ForceNew: true,
9294
},
@@ -97,6 +99,7 @@ func generateBaseTests[T any](
9799
Schema: map[string]*schema.Schema{
98100
"prop": {
99101
Type: typ,
102+
Elem: elem,
100103
Required: true,
101104
},
102105
},
@@ -106,6 +109,7 @@ func generateBaseTests[T any](
106109
Schema: map[string]*schema.Schema{
107110
"prop": {
108111
Type: typ,
112+
Elem: elem,
109113
ForceNew: true,
110114
Required: true,
111115
},
@@ -126,6 +130,7 @@ func generateBaseTests[T any](
126130
Schema: map[string]*schema.Schema{
127131
"prop": {
128132
Type: typ,
133+
Elem: elem,
129134
Optional: true,
130135
Computed: true,
131136
},
@@ -141,6 +146,7 @@ func generateBaseTests[T any](
141146
Schema: map[string]*schema.Schema{
142147
"prop": {
143148
Type: typ,
149+
Elem: elem,
144150
Optional: true,
145151
Computed: true,
146152
ForceNew: true,
@@ -157,6 +163,7 @@ func generateBaseTests[T any](
157163
Schema: map[string]*schema.Schema{
158164
"prop": {
159165
Type: typ,
166+
Elem: elem,
160167
Optional: true,
161168
Default: defaultVal,
162169
},
@@ -167,6 +174,7 @@ func generateBaseTests[T any](
167174
Schema: map[string]*schema.Schema{
168175
"prop": {
169176
Type: typ,
177+
Elem: elem,
170178
Optional: true,
171179
Default: defaultVal,
172180
ForceNew: true,

0 commit comments

Comments
 (0)