Skip to content

Commit fff0280

Browse files
SDKv2 Diff tests for Map
1 parent ec47d3c commit fff0280

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
@@ -16,7 +16,7 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
1616

1717
var nilVal string
1818
schemaValueMakerPairs, scenarios := generateBaseTests(
19-
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
19+
schema.TypeString, nil, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
2020

2121
for _, schemaValueMakerPair := range schemaValueMakerPairs {
2222
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
@@ -47,7 +47,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) {
4747

4848
var nilVal bool
4949
schemaValueMakerPairs, scenarios := generateBaseTests(
50-
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
50+
schema.TypeBool, nil, cty.BoolVal, true, false, true, false, nilVal)
5151

5252
for _, schemaValueMakerPair := range schemaValueMakerPairs {
5353
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
@@ -78,7 +78,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) {
7878

7979
var nilVal int64
8080
schemaValueMakerPairs, scenarios := generateBaseTests(
81-
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
81+
schema.TypeInt, nil, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
8282

8383
for _, schemaValueMakerPair := range schemaValueMakerPairs {
8484
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
@@ -109,7 +109,7 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) {
109109

110110
var nilVal float64
111111
schemaValueMakerPairs, scenarios := generateBaseTests(
112-
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
112+
schema.TypeFloat, nil, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
113113

114114
for _, schemaValueMakerPair := range schemaValueMakerPairs {
115115
t.Run(schemaValueMakerPair.name, func(t *testing.T) {

pkg/tests/diff_test/value_makers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type diffScenario[T any] struct {
2525
}
2626

2727
func generateBaseTests[T any](
28-
typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
28+
typ schema.ValueType, elem any, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
2929
) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) {
3030
valueOne := ref(val1)
3131
valueTwo := ref(val2)
@@ -44,6 +44,7 @@ func generateBaseTests[T any](
4444
Schema: map[string]*schema.Schema{
4545
"prop": {
4646
Type: typ,
47+
Elem: elem,
4748
Optional: true,
4849
},
4950
},
@@ -53,6 +54,7 @@ func generateBaseTests[T any](
5354
Schema: map[string]*schema.Schema{
5455
"prop": {
5556
Type: typ,
57+
Elem: elem,
5658
Optional: true,
5759
ForceNew: true,
5860
},
@@ -63,6 +65,7 @@ func generateBaseTests[T any](
6365
Schema: map[string]*schema.Schema{
6466
"prop": {
6567
Type: typ,
68+
Elem: elem,
6669
Required: true,
6770
},
6871
},
@@ -72,6 +75,7 @@ func generateBaseTests[T any](
7275
Schema: map[string]*schema.Schema{
7376
"prop": {
7477
Type: typ,
78+
Elem: elem,
7579
ForceNew: true,
7680
Required: true,
7781
},
@@ -92,6 +96,7 @@ func generateBaseTests[T any](
9296
Schema: map[string]*schema.Schema{
9397
"prop": {
9498
Type: typ,
99+
Elem: elem,
95100
Optional: true,
96101
Computed: true,
97102
},
@@ -107,6 +112,7 @@ func generateBaseTests[T any](
107112
Schema: map[string]*schema.Schema{
108113
"prop": {
109114
Type: typ,
115+
Elem: elem,
110116
Optional: true,
111117
Computed: true,
112118
ForceNew: true,
@@ -123,6 +129,7 @@ func generateBaseTests[T any](
123129
Schema: map[string]*schema.Schema{
124130
"prop": {
125131
Type: typ,
132+
Elem: elem,
126133
Optional: true,
127134
Default: defaultVal,
128135
},
@@ -133,6 +140,7 @@ func generateBaseTests[T any](
133140
Schema: map[string]*schema.Schema{
134141
"prop": {
135142
Type: typ,
143+
Elem: elem,
136144
Optional: true,
137145
Default: defaultVal,
138146
ForceNew: true,

0 commit comments

Comments
 (0)