Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for customizable_schema.go #3192

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions common/customizable_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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"
)

var testCustomizableSchemaScm = StructToSchema(testStruct{}, nil)

func TestCustomizableSchemaSetOptional(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetOptional()
assert.Truef(t, testCustomizableSchemaScm["non_optional"].Optional, "optional should be overriden to true in field: non-optional")
}

func TestCustomizableSchemaSetRequired(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "float").SetRequired()
assert.Truef(t, testCustomizableSchemaScm["float"].Required, "required should be overriden to true in field: float")
}

func TestCustomizableSchemaSetReadOnly(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "bool").SetReadOnly()
assert.Truef(t, testCustomizableSchemaScm["bool"].Computed, "computed should be overriden to true in field: bool")
assert.Falsef(t, testCustomizableSchemaScm["bool"].Optional, "optional should be overriden to false in field: bool")
assert.Falsef(t, testCustomizableSchemaScm["bool"].Required, "required should be overriden to false in field: bool")
assert.Truef(t, testCustomizableSchemaScm["bool"].MaxItems == 0, "maxItems should be overriden to 0 in field: bool")
}

func TestCustomizableSchemaSetComputed(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "string").SetComputed()
assert.Truef(t, testCustomizableSchemaScm["string"].Computed, "computed should be overriden to true in field: string")
}

func TestCustomizableSchemaSetDefault(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")
}

func TestCustomizableSchemaSetSuppressDiff(t *testing.T) {
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").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").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").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, "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, "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").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").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").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").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).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")
}
Loading