Skip to content

Commit

Permalink
SDKv2 Diff tests for Map
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Jan 15, 2025
1 parent 05799fe commit d9846f0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
77 changes: 35 additions & 42 deletions pkg/tests/diff_test/detailed_diff_map_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -13,57 +14,49 @@ import (
func TestSDKv2DetailedDiffMap(t *testing.T) {
t.Parallel()

res := schema.Resource{
Schema: map[string]*schema.Schema{
"map_prop": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}

ctyVal := func(v map[string]string) map[string]cty.Value {
ctyMap := make(map[string]cty.Value)

ctyMaker := func(v map[string]string) cty.Value {
if len(v) == 0 {
return map[string]cty.Value{
"map_prop": cty.MapValEmpty(cty.String),
}
return cty.MapValEmpty(cty.String)
}

ctyMap := make(map[string]cty.Value)
for k, v := range v {
ctyMap[k] = cty.StringVal(v)
}
return map[string]cty.Value{
"map_prop": cty.MapVal(ctyMap),
}
return cty.MapVal(ctyMap)
}

scenarios := []struct {
name string
initialValue map[string]string
changeValue map[string]string
}{
{"unchanged empty", map[string]string{}, map[string]string{}},
{"unchanged non-empty", map[string]string{"key": "val"}, map[string]string{"key": "val"}},
{"added", map[string]string{}, map[string]string{"key": "val"}},
{"removed", map[string]string{"key": "val"}, map[string]string{}},
{"value changed", map[string]string{"key": "val"}, map[string]string{"key": "val2"}},
{"key changed", map[string]string{"key": "val"}, map[string]string{"key2": "val"}},
}
var nilVal map[string]string
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
schema.TypeMap, &schema.Schema{Type: schema.TypeString}, ctyMaker,
map[string]string{"key": "val1"}, map[string]string{"key": "val2"},
map[string]string{"key": "computedVal"}, map[string]string{"key": "defaultVal"}, nilVal)

for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
scenarios = append(scenarios, diffScenario[map[string]string]{
name: "key changed",
initialValue: ref(map[string]string{"key": "val1"}),
changeValue: ref(map[string]string{"key2": "val1"}),
})

for _, schemaValueMakerPair := range schemaValueMakerPairs {
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
t.Parallel()
diff := crosstests.Diff(t, &res, ctyVal(scenario.initialValue), ctyVal(scenario.changeValue))
autogold.ExpectFile(t, testOutput{
initialValue: scenario.initialValue,
changeValue: scenario.changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
if strings.Contains(schemaValueMakerPair.name, "required") &&
(scenario.initialValue == nil || scenario.changeValue == nil) {
t.Skip("Required fields cannot be unset")
}
t.Parallel()
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
autogold.ExpectFile(t, testOutput{
initialValue: scenario.initialValue,
changeValue: scenario.changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
})
}
})
}
}
8 changes: 4 additions & 4 deletions pkg/tests/diff_test/detailed_diff_primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestSDKv2DetailedDiffString(t *testing.T) {

var nilVal string
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
schema.TypeString, nil, cty.StringVal, "val1", "val2", "computed", "default", nilVal)

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

var nilVal bool
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
schema.TypeBool, nil, cty.BoolVal, true, false, true, false, nilVal)

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

var nilVal int64
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
schema.TypeInt, nil, cty.NumberIntVal, 1, 2, 3, 4, nilVal)

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

var nilVal float64
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
schema.TypeFloat, nil, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}
10 changes: 9 additions & 1 deletion pkg/tests/diff_test/value_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func runSDKv2TestMatrix[T any](
}

func generateBaseTests[T any](
typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
typ schema.ValueType, elem any, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) {
valueOne := ref(val1)
valueTwo := ref(val2)
Expand All @@ -78,6 +78,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Optional: true,
},
},
Expand All @@ -87,6 +88,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Optional: true,
ForceNew: true,
},
Expand All @@ -97,6 +99,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Required: true,
},
},
Expand All @@ -106,6 +109,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
ForceNew: true,
Required: true,
},
Expand All @@ -126,6 +130,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Optional: true,
Computed: true,
},
Expand All @@ -141,6 +146,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Optional: true,
Computed: true,
ForceNew: true,
Expand All @@ -157,6 +163,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Optional: true,
Default: defaultVal,
},
Expand All @@ -167,6 +174,7 @@ func generateBaseTests[T any](
Schema: map[string]*schema.Schema{
"prop": {
Type: typ,
Elem: elem,
Optional: true,
Default: defaultVal,
ForceNew: true,
Expand Down

0 comments on commit d9846f0

Please sign in to comment.