diff --git a/common/customizable_schema_test.go b/common/customizable_schema_test.go index 0509e23b2e..eda3b0e2da 100644 --- a/common/customizable_schema_test.go +++ b/common/customizable_schema_test.go @@ -3,6 +3,8 @@ package common import ( "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/stretchr/testify/assert" ) @@ -37,56 +39,70 @@ func TestCustomizableSchemaSetDefault(t *testing.T) { } func TestCustomizableSchemaSetSuppressDiff(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetSuppressDiff() + assert.Truef(t, testCustomizableSchemaScm["non_optional"].DiffSuppressFunc != nil, "DiffSuppressfunc should be set in field: non_optional") } func TestCustomizableSchemaSetCustomSuppressDiff(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetCustomSuppressDiff(diffSuppressor(CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").Schema)) + assert.Truef(t, testCustomizableSchemaScm["non_optional"].DiffSuppressFunc != nil, "DiffSuppressfunc should be set in field: non_optional") } func TestCustomizableSchemaSetSensitive(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetSensitive() + assert.Truef(t, testCustomizableSchemaScm["non_optional"].Sensitive, "sensitive should be overriden to true in field: non_optional") } func TestCustomizableSchemaSetForceNew(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetForceNew() + assert.Truef(t, testCustomizableSchemaScm["non_optional"].ForceNew, "forcenew should be overriden to true in field: non_optional") } func TestCustomizableSchemaSetMaxItems(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "int_slice").SetMaxItems(5) + assert.Truef(t, testCustomizableSchemaScm["int_slice"].MaxItems == 5, "maxItems should be overriden to 5 in field: int_slice") } func TestCustomizableSchemaSetMinItems(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "int_slice").SetMinItems(3) + assert.Truef(t, testCustomizableSchemaScm["int_slice"].MinItems == 3, "maxItems should be overriden to 5 in field: int_slice") } func TestCustomizableSchemaSetConflictsWith(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetConflictsWith([]string{"abc"}) + assert.Truef(t, len(testCustomizableSchemaScm["non_optional"].ConflictsWith) == 1, "conflictsWith should be set in field: non_optional") } func TestCustomizableSchemaSetDeprecated(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDeprecated("test reason") + assert.Truef(t, testCustomizableSchemaScm["non_optional"].Deprecated == "test reason", "deprecated should be overriden in field: non_optional") } func TestCustomizableSchemaSetValidateFunc(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetValidateFunc(validation.StringInSlice([]string{"PHOTON", "STANDARD"}, false)) + assert.Truef(t, testCustomizableSchemaScm["non_optional"].ValidateFunc != nil, "validateFunc should be set in field: non_optional") } func TestCustomizableSchemaSetValidateDiagFunc(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetValidateDiagFunc(validation.ToDiagFunc(validation.IntAtLeast(0))) + assert.Truef(t, testCustomizableSchemaScm["non_optional"].ValidateDiagFunc != nil, "validateDiagFunc should be set in field: non_optional") } func TestCustomizableSchemaAddNewField(t *testing.T) { - CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDefault("abc") - assert.Truef(t, testCustomizableSchemaScm["non_optional"].Default == "abc", "default should be overriden to abc in field: non_optional") + + CustomizeSchemaPath(testCustomizableSchemaScm).AddNewField("test", &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }) + + assert.Truef(t, MustSchemaPath(testCustomizableSchemaScm, "test").Type == schema.TypeString, "field test should be added to the top level") + + CustomizeSchemaPath(testCustomizableSchemaScm, "ptr_item").AddNewField("test", &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }) + + assert.Truef(t, MustSchemaPath(testCustomizableSchemaScm, "ptr_item", "test").Type == schema.TypeString, "field ptr_item.test should be added") }