From 02d131f17940681e68d184f4d0966475f3fd764b Mon Sep 17 00:00:00 2001 From: Venelin Date: Tue, 14 Jan 2025 13:41:22 +0200 Subject: [PATCH] move and rename test generation function --- .../diff_test/detailed_diff_primitive_test.go | 144 +----------------- pkg/tests/diff_test/value_makers.go | 137 +++++++++++++++++ 2 files changed, 141 insertions(+), 140 deletions(-) diff --git a/pkg/tests/diff_test/detailed_diff_primitive_test.go b/pkg/tests/diff_test/detailed_diff_primitive_test.go index 3e9ede799..adcc50412 100644 --- a/pkg/tests/diff_test/detailed_diff_primitive_test.go +++ b/pkg/tests/diff_test/detailed_diff_primitive_test.go @@ -1,11 +1,9 @@ package tests import ( - "context" "strings" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hexops/autogold/v2" "github.com/zclconf/go-cty/cty" @@ -13,145 +11,11 @@ import ( crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests" ) -func generatePrimitiveSchemaValueMakerPairs[T any]( - typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T, -) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) { - valueOne := ref(val1) - valueTwo := ref(val2) - noValue := ref(nilVal) - - ctyVal := func(v *T) map[string]cty.Value { - if v == nil { - return map[string]cty.Value{} - } - return map[string]cty.Value{ - "prop": ctyMaker(*v), - } - } - - optionalSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Optional: true, - }, - }, - } - - optionalForceNewSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Optional: true, - ForceNew: true, - }, - }, - } - - requiredSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Required: true, - }, - }, - } - - requiredForceNewSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - ForceNew: true, - Required: true, - }, - }, - } - - setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - if _, ok := d.GetOk("prop"); !ok { - err := d.Set("prop", computedVal) - if err != nil { - return diag.FromErr(err) - } - } - return nil - } - - optionalComputedSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Optional: true, - Computed: true, - }, - }, - CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { - rd.SetId("id") - return setComputedFunc(ctx, rd, i) - }, - UpdateContext: setComputedFunc, - } - - optionalComputedForceNewSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Optional: true, - Computed: true, - ForceNew: true, - }, - }, - CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { - rd.SetId("id") - return setComputedFunc(ctx, rd, i) - }, - UpdateContext: setComputedFunc, - } - - optionalDefaultSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Optional: true, - Default: defaultVal, - }, - }, - } - - optionalDefaultForceNewSchema := schema.Resource{ - Schema: map[string]*schema.Schema{ - "prop": { - Type: typ, - Optional: true, - Default: defaultVal, - ForceNew: true, - }, - }, - } - - return []diffSchemaValueMakerPair[T]{ - {"optional", optionalSchema, ctyVal}, - {"optionalForceNew", optionalForceNewSchema, ctyVal}, - {"required", requiredSchema, ctyVal}, - {"requiredForceNew", requiredForceNewSchema, ctyVal}, - {"optionalComputed", optionalComputedSchema, ctyVal}, - {"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal}, - {"optionalDefault", optionalDefaultSchema, ctyVal}, - {"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal}, - }, []diffScenario[T]{ - {"unchanged empty", noValue, noValue}, - {"unchanged non-empty", valueOne, valueOne}, - {"added", noValue, valueOne}, - {"removed", valueOne, noValue}, - {"changed", valueOne, valueTwo}, - } -} - func TestSDKv2DetailedDiffString(t *testing.T) { t.Parallel() var nilVal string - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( + schemaValueMakerPairs, scenarios := generateBaseTests( schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal) for _, schemaValueMakerPair := range schemaValueMakerPairs { @@ -182,7 +46,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) { t.Parallel() var nilVal bool - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( + schemaValueMakerPairs, scenarios := generateBaseTests( schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal) for _, schemaValueMakerPair := range schemaValueMakerPairs { @@ -213,7 +77,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) { t.Parallel() var nilVal int64 - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( + schemaValueMakerPairs, scenarios := generateBaseTests( schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal) for _, schemaValueMakerPair := range schemaValueMakerPairs { @@ -244,7 +108,7 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) { t.Parallel() var nilVal float64 - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( + schemaValueMakerPairs, scenarios := generateBaseTests( schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal) for _, schemaValueMakerPair := range schemaValueMakerPairs { diff --git a/pkg/tests/diff_test/value_makers.go b/pkg/tests/diff_test/value_makers.go index 26faccb51..4c80ed90f 100644 --- a/pkg/tests/diff_test/value_makers.go +++ b/pkg/tests/diff_test/value_makers.go @@ -1,6 +1,9 @@ package tests import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/zclconf/go-cty/cty" ) @@ -21,6 +24,140 @@ type diffScenario[T any] struct { changeValue *T } +func generateBaseTests[T any]( + typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T, +) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) { + valueOne := ref(val1) + valueTwo := ref(val2) + noValue := ref(nilVal) + + ctyVal := func(v *T) map[string]cty.Value { + if v == nil { + return map[string]cty.Value{} + } + return map[string]cty.Value{ + "prop": ctyMaker(*v), + } + } + + optionalSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Optional: true, + }, + }, + } + + optionalForceNewSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Optional: true, + ForceNew: true, + }, + }, + } + + requiredSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Required: true, + }, + }, + } + + requiredForceNewSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + ForceNew: true, + Required: true, + }, + }, + } + + setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + if _, ok := d.GetOk("prop"); !ok { + err := d.Set("prop", computedVal) + if err != nil { + return diag.FromErr(err) + } + } + return nil + } + + optionalComputedSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Optional: true, + Computed: true, + }, + }, + CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + rd.SetId("id") + return setComputedFunc(ctx, rd, i) + }, + UpdateContext: setComputedFunc, + } + + optionalComputedForceNewSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + rd.SetId("id") + return setComputedFunc(ctx, rd, i) + }, + UpdateContext: setComputedFunc, + } + + optionalDefaultSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Optional: true, + Default: defaultVal, + }, + }, + } + + optionalDefaultForceNewSchema := schema.Resource{ + Schema: map[string]*schema.Schema{ + "prop": { + Type: typ, + Optional: true, + Default: defaultVal, + ForceNew: true, + }, + }, + } + + return []diffSchemaValueMakerPair[T]{ + {"optional", optionalSchema, ctyVal}, + {"optionalForceNew", optionalForceNewSchema, ctyVal}, + {"required", requiredSchema, ctyVal}, + {"requiredForceNew", requiredForceNewSchema, ctyVal}, + {"optionalComputed", optionalComputedSchema, ctyVal}, + {"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal}, + {"optionalDefault", optionalDefaultSchema, ctyVal}, + {"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal}, + }, []diffScenario[T]{ + {"unchanged empty", noValue, noValue}, + {"unchanged non-empty", valueOne, valueOne}, + {"added", noValue, valueOne}, + {"removed", valueOne, noValue}, + {"changed", valueOne, valueTwo}, + } +} + func listValueMaker(arr *[]string) cty.Value { if arr == nil { return cty.NullVal(cty.DynamicPseudoType)