Skip to content

Commit 7585988

Browse files
authored
tfsdk: Add unit testing covering attr.TypeWithValidate in (Attribute).validate() (#92)
Reference: #17 This was covered already by the `(Config).GetAttribute()` call in the function, however this testing required merging the new `internal/testing/types` package.
1 parent 96e3a5e commit 7585988

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tfsdk/attribute_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
testtypes "github.com/hashicorp/terraform-plugin-framework/internal/testing/types"
910
"github.com/hashicorp/terraform-plugin-framework/types"
1011
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1112
"github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -867,6 +868,60 @@ func TestAttributeValidate(t *testing.T) {
867868
},
868869
},
869870
},
871+
"type-with-validate-error": {
872+
req: ValidateAttributeRequest{
873+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
874+
Config: Config{
875+
Raw: tftypes.NewValue(tftypes.Object{
876+
AttributeTypes: map[string]tftypes.Type{
877+
"test": tftypes.String,
878+
},
879+
}, map[string]tftypes.Value{
880+
"test": tftypes.NewValue(tftypes.String, "testvalue"),
881+
}),
882+
Schema: Schema{
883+
Attributes: map[string]Attribute{
884+
"test": {
885+
Type: testtypes.StringTypeWithValidateError{},
886+
Required: true,
887+
},
888+
},
889+
},
890+
},
891+
},
892+
resp: ValidateAttributeResponse{
893+
Diagnostics: []*tfprotov6.Diagnostic{
894+
testtypes.TestErrorDiagnostic,
895+
},
896+
},
897+
},
898+
"type-with-validate-warning": {
899+
req: ValidateAttributeRequest{
900+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
901+
Config: Config{
902+
Raw: tftypes.NewValue(tftypes.Object{
903+
AttributeTypes: map[string]tftypes.Type{
904+
"test": tftypes.String,
905+
},
906+
}, map[string]tftypes.Value{
907+
"test": tftypes.NewValue(tftypes.String, "testvalue"),
908+
}),
909+
Schema: Schema{
910+
Attributes: map[string]Attribute{
911+
"test": {
912+
Type: testtypes.StringTypeWithValidateWarning{},
913+
Required: true,
914+
},
915+
},
916+
},
917+
},
918+
},
919+
resp: ValidateAttributeResponse{
920+
Diagnostics: []*tfprotov6.Diagnostic{
921+
testtypes.TestWarningDiagnostic,
922+
},
923+
},
924+
},
870925
"nested-attr-list-no-validation": {
871926
req: ValidateAttributeRequest{
872927
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),

0 commit comments

Comments
 (0)