Skip to content

Commit

Permalink
WIP provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Oct 14, 2024
1 parent add6376 commit b85ceb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 3 additions & 1 deletion pf/tests/provider_configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package tfbridgetests

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -78,7 +79,8 @@ func TestConfigureProperties(t *testing.T) {
t.Parallel()

rapid.Check(t, func(r *rapid.T) {
schema := propProviderSchema.Schema().Draw(r, "schema")
ctx := context.Background()
schema := propProviderSchema.Schema(ctx).Draw(r, "schema")
value := propProviderValue.WithValue(schema).Draw(r, "value")
crosstests.Configure(t, schema, value.Tf.AsValueMap(), value.Pu)
})
Expand Down
23 changes: 17 additions & 6 deletions pf/tests/util/property/pf/schema/provider/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"pgregory.net/rapid"
)

const tfIdentifierRegexp = "[a-z_][a-z_0-9]*"
const defaultDepth = 3
const (
tfIdentifierRegexp = "[a-z_][a-z_0-9]*"
defaultDepth = 3
maxObjectSize = 4
)

func Schema() *rapid.Generator[schema.Schema] {
func Schema(ctx context.Context) *rapid.Generator[schema.Schema] {
return rapid.Custom(func(t *rapid.T) schema.Schema {
// We need to generate a set of attributes and blocks, all with non-overlapping names.
names := attrAndBlockNames().Draw(t, "names")
Expand All @@ -36,7 +41,7 @@ func Schema() *rapid.Generator[schema.Schema] {
DeprecationMessage: rapid.String().Draw(t, "deprecation message"),
}

if diags := s.Validate(); diags.HasError() {
if diags := s.ValidateImplementation(ctx); diags.HasError() {
t.Fatalf("Invalid schema generated: %#v", diags)
}

Expand All @@ -46,7 +51,12 @@ func Schema() *rapid.Generator[schema.Schema] {

func attrAndBlockNames() *rapid.Generator[struct{ attr, block []string }] {
return rapid.Custom(func(t *rapid.T) struct{ attr, block []string } {
attrAndBlockNames := rapid.SliceOfDistinct(rapid.StringMatching(tfIdentifierRegexp), rapid.ID).Draw(t, "names")
attrAndBlockNames := rapid.SliceOfNDistinct(
rapid.StringMatching(tfIdentifierRegexp),
-1,
maxObjectSize,
rapid.ID,
).Draw(t, "names")
split := rapid.IntRange(0, len(attrAndBlockNames)).Draw(t, "attr block split")
attrNames, blockNames := attrAndBlockNames[:split], attrAndBlockNames[split:]
return struct{ attr, block []string }{attrNames, blockNames}
Expand Down Expand Up @@ -348,9 +358,10 @@ func nestedAttributeObject(depth int) *rapid.Generator[schema.NestedAttributeObj
return rapid.Custom(func(t *rapid.T) schema.NestedAttributeObject {

return schema.NestedAttributeObject{
Attributes: rapid.MapOf(
Attributes: rapid.MapOfN(
rapid.StringMatching(tfIdentifierRegexp),
attribute(depth-1),
-1, maxObjectSize,
).Draw(t, "Attributes"),
CustomType: nil, // We are omitting custom types for now.
Validators: nil, // We are omitting validators here
Expand Down

0 comments on commit b85ceb5

Please sign in to comment.