Skip to content

Commit

Permalink
error cases and test
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeffryes committed Jul 1, 2024
1 parent 9dba312 commit 023d86c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,10 @@ func (g *packageGenerator) genTypeSpec(typeName, propName string, prop *discover
Items: typeSpec,
},
}, nil
case "nil":
return nil, errors.New(fmt.Sprintf("nil is not a valid array element type: %v", prop))
case "object":
return nil, errors.New(fmt.Sprintf("object is not a valid array element type: %v", prop))
default:
return &schema.TypeSpec{
Type: "object",
Expand Down
39 changes: 39 additions & 0 deletions provider/pkg/gen/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pulumi/pulumi-google-native/provider/pkg/resources"

"github.com/stretchr/testify/require"
"google.golang.org/api/discovery/v1"
)

var root = filepath.Join("..", "..", "..")
Expand Down Expand Up @@ -213,3 +214,41 @@ func loadSchema() (*schema.PackageSpec, error) {
}
return &pkg, nil
}

type TypeSpecTestCase struct {
name string
schema *discovery.JsonSchema
expected *schema.TypeSpec
}

func Test_genTypeSpec(t *testing.T) {
cases := []TypeSpecTestCase{
{
name: "map[string, string]",
schema: &discovery.JsonSchema{
Type: "object",
AdditionalProperties: &discovery.JsonSchema{
Type: "string",
},
},
expected: &schema.TypeSpec{
Type: "object",
AdditionalProperties: &schema.TypeSpec{Type: "string"},
},
},
}
g := packageGenerator{
pkg: nil,
metadata: nil,
rest: nil,
mod: "foo",
visitedTypes: codegen.NewStringSet(),
docName: "foo",
}

for _, tt := range cases {
out, err := g.genTypeSpec("foo", "bar", tt.schema, true)
assert.NoError(t, err)
assert.Equal(t, tt.expected, out)
}
}

0 comments on commit 023d86c

Please sign in to comment.