Skip to content

Commit

Permalink
Adding a few tests (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Oct 27, 2023
1 parent 2167c66 commit 400dfd7
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 7 deletions.
56 changes: 50 additions & 6 deletions internal/schema/custom_nested_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,12 @@ func TestCustomNestedObjectValue_renderToObjectValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
name string
attributeTypes map[string]string
attrTypes map[string]string
expected []byte
expectedError error
name string
attributeTypes map[string]string
attrTypes map[string]string
collectionTypes map[string]map[string]string
expected []byte
expectedError error
}{
"default": {
name: "Example",
Expand All @@ -851,6 +852,49 @@ map[string]attr.Value{
"bool_attribute": v.BoolAttribute,
})
return objVal, diags
}`),
},
"collection-type": {
name: "Example",
attributeTypes: map[string]string{
"list_attribute": "List",
},
attrTypes: map[string]string{
"list_attribute": "basetypes.ListType{\nElemType: types.BoolType,\n}",
},
collectionTypes: map[string]map[string]string{
"list_attribute": {
"ElementType": "types.BoolType",
"TypeValueFunc": "types.ListValue",
},
},
expected: []byte(`
func (v ExampleValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics
listAttributeVal, d := types.ListValue(types.BoolType, v.ListAttribute.Elements())
diags.Append(d...)
if d.HasError() {
return types.ObjectUnknown(map[string]attr.Type{
"list_attribute": basetypes.ListType{
ElemType: types.BoolType,
},
}), diags
}
objVal, diags := types.ObjectValue(
map[string]attr.Type{
"list_attribute": basetypes.ListType{
ElemType: types.BoolType,
},
},
map[string]attr.Value{
"list_attribute": listAttributeVal,
})
return objVal, diags
}`),
},
Expand All @@ -862,7 +906,7 @@ return objVal, diags
t.Run(name, func(t *testing.T) {
t.Parallel()

customObjectValue := NewCustomNestedObjectValue(testCase.name, testCase.attributeTypes, testCase.attrTypes, nil, nil)
customObjectValue := NewCustomNestedObjectValue(testCase.name, testCase.attributeTypes, testCase.attrTypes, nil, testCase.collectionTypes)

got, err := customObjectValue.renderToObjectValue()

Expand Down
1 change: 1 addition & 0 deletions internal/schema/templates/nested_object_from.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if diags.HasError() {
return New{{$.Name}}ValueUnknown(), diags
}
{{- else if $value.CollectionType.ElementType}}

{{$key.ToCamelCase}}Val, d := {{$value.CollectionType.TypeValueFrom}}(ctx, {{$value.CollectionType.ElementType}}, apiObject.{{$key.ToPascalCase}})

diags.Append(d...)
Expand Down
2 changes: 1 addition & 1 deletion internal/schema/templates/nested_object_to.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ return &{{.AssocExtType.TypeReference}}{
{{$key.ToPascalCase}}: {{$value.AssocExtType.ToCamelCase}},
{{- else if $value.Default}}
{{$key.ToPascalCase}}: v.{{$key.ToPascalCase}}.{{$value.Default}}(),
{{- else if $value.CollectionType}}
{{- else if $value.CollectionType.GoType}}
{{$key.ToPascalCase}}: {{$key.ToCamelCase}}Field,
{{- end}}
{{- end}}
Expand Down
86 changes: 86 additions & 0 deletions internal/schema/to_from_nested_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,44 @@ if diags.HasError() {
return NewExampleValueUnknown(), diags
}
return ExampleValue{
BoolAttribute: boolAttributeVal,
state: attr.ValueStateKnown,
}, diags
}
`),
},
"collection-type": {
name: "Example",
assocExtType: &AssocExtType{
AssociatedExternalType: &schema.AssociatedExternalType{
Type: "*apisdk.Type",
},
},
fromFuncs: map[string]ToFromConversion{
"bool_attribute": {
CollectionType: CollectionFields{
ElementType: "types.BoolType",
TypeValueFrom: "types.ListValueFrom",
},
},
},
expected: []byte(`
func (v ExampleValue) FromApisdkType(ctx context.Context, apiObject *apisdk.Type) (ExampleValue, diag.Diagnostics) {
var diags diag.Diagnostics
if apiObject == nil {
return NewExampleValueNull(), diags
}
boolAttributeVal, d := types.ListValueFrom(ctx, types.BoolType, apiObject.BoolAttribute)
diags.Append(d...)
if diags.HasError() {
return NewExampleValueUnknown(), diags
}
return ExampleValue{
BoolAttribute: boolAttributeVal,
state: attr.ValueStateKnown,
Expand Down Expand Up @@ -202,6 +240,54 @@ return nil, diags
return &apisdk.Type{
BoolAttribute: apiBoolAttribute,
}, diags
}`),
},
"collection-type": {
name: "Example",
assocExtType: &AssocExtType{
&schema.AssociatedExternalType{
Import: &code.Import{
Path: "example.com/apisdk",
},
Type: "*apisdk.Type",
},
},
toFuncs: map[string]ToFromConversion{
"bool_attribute": {
CollectionType: CollectionFields{
GoType: "[]*bool",
},
},
},
expected: []byte(`func (v ExampleValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) {
var diags diag.Diagnostics
if v.IsNull() {
return nil, diags
}
if v.IsUnknown() {
diags.Append(diag.NewErrorDiagnostic(
"ExampleValue Value Is Unknown",
` + "`" + `"ExampleValue" is unknown.` + "`" + `,
))
return nil, diags
}
var boolAttributeField []*bool
d := v.BoolAttribute.ElementsAs(ctx, &boolAttributeField, false)
diags.Append(d...)
if diags.HasError() {
return nil, diags
}
return &apisdk.Type{
BoolAttribute: boolAttributeField,
}, diags
}`),
},
}
Expand Down

0 comments on commit 400dfd7

Please sign in to comment.