-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shawn Wang <[email protected]>
- Loading branch information
Showing
3 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package metadata | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testStruct struct { | ||
StringField *string | ||
BoolField *bool | ||
IntField *int64 | ||
StructField *testNestedStruct | ||
} | ||
|
||
type testNestedStruct struct { | ||
StringField *string | ||
BoolField *bool | ||
IntField *int64 | ||
} | ||
|
||
var testSchema = map[string]*schema.Schema{ | ||
"string_field": { | ||
Type: schema.TypeString, | ||
}, | ||
"bool_field": { | ||
Type: schema.TypeBool, | ||
}, | ||
"int_field": { | ||
Type: schema.TypeInt, | ||
}, | ||
"struct_field": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"string_field": { | ||
Type: schema.TypeString, | ||
}, | ||
"bool_field": { | ||
Type: schema.TypeBool, | ||
}, | ||
"int_field": { | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func basicStringSchema() *ExtendedSchema { | ||
return &ExtendedSchema{ | ||
Schema: schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Metadata: Metadata{ | ||
SchemaType: "string", | ||
SdkFieldName: "StringField", | ||
}, | ||
} | ||
} | ||
|
||
func basicBoolSchema() *ExtendedSchema { | ||
return &ExtendedSchema{ | ||
Schema: schema.Schema{ | ||
Type: schema.TypeBool, | ||
}, | ||
Metadata: Metadata{ | ||
SchemaType: "bool", | ||
SdkFieldName: "BoolField", | ||
}, | ||
} | ||
} | ||
|
||
func basicIntSchema() *ExtendedSchema { | ||
return &ExtendedSchema{ | ||
Schema: schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
Metadata: Metadata{ | ||
SchemaType: "int", | ||
SdkFieldName: "IntField", | ||
}, | ||
} | ||
} | ||
|
||
var testExtendedSchema = map[string]*ExtendedSchema{ | ||
"string_field": basicStringSchema(), | ||
"bool_field": basicBoolSchema(), | ||
"int_field": basicIntSchema(), | ||
"struct_field": { | ||
Schema: schema.Schema{ | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Elem: &ExtendedResource{ | ||
Schema: map[string]*ExtendedSchema{ | ||
"string_field": basicStringSchema(), | ||
"bool_field": basicBoolSchema(), | ||
"int_field": basicIntSchema(), | ||
}, | ||
}, | ||
}, | ||
Metadata: Metadata{ | ||
SchemaType: "struct", | ||
SdkFieldName: "StructField", | ||
ReflectType: reflect.TypeOf(testNestedStruct{}), | ||
}, | ||
}, | ||
} | ||
|
||
func TestStructToSchema(t *testing.T) { | ||
testStr := "test_string" | ||
testBool := true | ||
testInt := int64(123) | ||
obj := testStruct{ | ||
StringField: &testStr, | ||
BoolField: &testBool, | ||
IntField: &testInt, | ||
StructField: &testNestedStruct{ | ||
StringField: &testStr, | ||
BoolField: &testBool, | ||
IntField: &testInt, | ||
}, | ||
} | ||
d := schema.TestResourceDataRaw( | ||
t, testSchema, map[string]interface{}{}) | ||
|
||
elem := reflect.ValueOf(&obj).Elem() | ||
StructToSchema(elem, d, testExtendedSchema, "", nil) | ||
assert.Equal(t, "test_string", d.Get("string_field").(string)) | ||
assert.Equal(t, true, d.Get("bool_field").(bool)) | ||
assert.Equal(t, 123, d.Get("int_field").(int)) | ||
nestedObj := d.Get("struct_field").([]interface{})[0].(map[string]interface{}) | ||
assert.Equal(t, "test_string", nestedObj["string_field"].(string)) | ||
assert.Equal(t, true, nestedObj["bool_field"].(bool)) | ||
assert.Equal(t, 123, nestedObj["int_field"].(int)) | ||
} | ||
|
||
func TestSchemaToStruct(t *testing.T) { | ||
d := schema.TestResourceDataRaw( | ||
t, testSchema, map[string]interface{}{ | ||
"string_field": "test_string", | ||
"bool_field": true, | ||
"int_field": 100, | ||
"struct_field": []interface{}{ | ||
map[string]interface{}{ | ||
"string_field": "nested_string", | ||
"bool_field": true, | ||
"int_field": 1, | ||
}, | ||
}, | ||
}) | ||
|
||
obj := testStruct{} | ||
elem := reflect.ValueOf(&obj).Elem() | ||
SchemaToStruct(elem, d, testExtendedSchema, "", nil) | ||
assert.Equal(t, "test_string", *obj.StringField) | ||
assert.Equal(t, true, *obj.BoolField) | ||
assert.Equal(t, int64(100), *obj.IntField) | ||
assert.Equal(t, "nested_string", *obj.StructField.StringField) | ||
assert.Equal(t, true, *obj.StructField.BoolField) | ||
assert.Equal(t, int64(1), *obj.StructField.IntField) | ||
} |