Skip to content

Commit

Permalink
chore(internal/snippets,gengapic): use sample values in tests (#1514)
Browse files Browse the repository at this point in the history
Refactor internal/snippets/snippets_test.go and
internal/gengapic/example_test.go to use the internal/testing/sample
package.

For #1512
  • Loading branch information
julieqiu committed May 14, 2024
1 parent 779cd45 commit 8c289e4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
54 changes: 16 additions & 38 deletions internal/gengapic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,54 +283,33 @@ func TestExample(t *testing.T) {
func TestGenSnippetFile(t *testing.T) {
var g generator
g.imports = map[pbinfo.ImportSpec]bool{}
g.serviceConfig = &serviceconfig.Service{
Apis: []*apipb.Api{
{Name: sample.ProtoServiceName},
},
}
g.snippetMetadata = snippets.NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName)
g.serviceConfig = sample.ServiceConfig()

inputType := &descriptorpb.DescriptorProto{
Name: proto.String(sample.CreateRequest),
}
outputType := &descriptorpb.DescriptorProto{
Name: proto.String(sample.Resource),
}
serv := sample.Service()
g.snippetMetadata = snippets.NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName)
g.snippetMetadata.AddService(serv.GetName(), sample.ServiceURL)

file := &descriptorpb.FileDescriptorProto{
Options: &descriptorpb.FileOptions{
GoPackage: proto.String(sample.GoProtoPackagePath),
},
Package: proto.String(sample.ProtoPackagePath),
}
inputType := sample.InputType(sample.CreateRequest)
outputType := sample.OutputType(sample.Resource)

files := []*descriptorpb.FileDescriptorProto{}
g.descInfo = pbinfo.Of(files)

file := sample.File()
for _, typ := range []*descriptorpb.DescriptorProto{
inputType, outputType,
} {
g.descInfo.Type[sample.DescriptorInfoTypeName(typ.GetName())] = typ
g.descInfo.ParentFile[typ] = file
}

serv := &descriptorpb.ServiceDescriptorProto{
Name: proto.String(sample.ServiceName),
Method: []*descriptorpb.MethodDescriptorProto{
{
Name: proto.String(sample.CreateMethod),
InputType: proto.String(sample.DescriptorInfoTypeName(sample.CreateRequest)),
OutputType: proto.String(sample.DescriptorInfoTypeName(sample.Resource)),
},
},
}

for _, tst := range []struct {
tstName string
for _, test := range []struct {
name string
options options
imports map[pbinfo.ImportSpec]bool
}{
{
tstName: "snippet",
name: "snippet",
options: options{
pkgName: "migration",
transports: []transport{grpc, rest},
Expand All @@ -341,20 +320,19 @@ func TestGenSnippetFile(t *testing.T) {
},
},
} {
t.Run(tst.tstName, func(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
g.reset()
g.opts = &tst.options
g.snippetMetadata.AddService(serv.GetName(), sample.ServiceURL)
g.opts = &test.options
err := g.genSnippetFile(serv, serv.Method[0])
if err != nil {
t.Fatal(err)
}
g.commit(filepath.Join("cloud.google.com/go", "internal", "generated", "snippets", "bigquery", "main.go"), "main")
if diff := cmp.Diff(g.imports, tst.imports); diff != "" {
t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff)
if diff := cmp.Diff(test.imports, g.imports); diff != "" {
t.Errorf("TestExample(%s) imports mismatch: (-want +got):\n%s", test.name, diff)
}
got := *g.resp.File[0].Content + *g.resp.File[1].Content
txtdiff.Diff(t, got, filepath.Join("testdata", tst.tstName+".want"))
txtdiff.Diff(t, got, filepath.Join("testdata", test.name+".want"))
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/testing/sample/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ go_library(
srcs = ["sample.go"],
importpath = "github.com/googleapis/gapic-generator-go/internal/testing/sample",
visibility = ["//:__subpackages__"],
deps = [
"@org_golang_google_genproto_googleapis_api//serviceconfig",
"@org_golang_google_protobuf//proto",
"@org_golang_google_protobuf//types/descriptorpb",
"@org_golang_google_protobuf//types/known/apipb",
],
)
57 changes: 57 additions & 0 deletions internal/testing/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package sample

import (
"fmt"

"google.golang.org/genproto/googleapis/api/serviceconfig"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/known/apipb"
)

const (
Expand Down Expand Up @@ -139,3 +144,55 @@ const (
func DescriptorInfoTypeName(typ string) string {
return fmt.Sprintf(".%s.%s", ProtoPackagePath, typ)
}

// ServiceConfig returns service config information.
func ServiceConfig() *serviceconfig.Service {
return &serviceconfig.Service{
Apis: []*apipb.Api{
{Name: ProtoServiceName},
},
}
}

// Service returns a service descriptor using the sample values.
func Service() *descriptorpb.ServiceDescriptorProto {
return &descriptorpb.ServiceDescriptorProto{
Name: proto.String(ServiceName),
Method: []*descriptorpb.MethodDescriptorProto{
{
Name: proto.String(CreateMethod),
InputType: proto.String(DescriptorInfoTypeName(CreateRequest)),
OutputType: proto.String(DescriptorInfoTypeName(Resource)),
},
{
Name: proto.String(GetMethod),
InputType: proto.String(DescriptorInfoTypeName(GetRequest)),
OutputType: proto.String(DescriptorInfoTypeName(Resource)),
},
},
}
}

// InputType returns an input type for a method.
func InputType(input string) *descriptorpb.DescriptorProto {
return &descriptorpb.DescriptorProto{
Name: proto.String(input),
}
}

// OutputType returns an output type for a method.
func OutputType(output string) *descriptorpb.DescriptorProto {
return &descriptorpb.DescriptorProto{
Name: proto.String(output),
}
}

// File returns a proto file.
func File() *descriptorpb.FileDescriptorProto {
return &descriptorpb.FileDescriptorProto{
Options: &descriptorpb.FileOptions{
GoPackage: proto.String(GoProtoPackagePath),
},
Package: proto.String(ProtoPackagePath),
}
}

0 comments on commit 8c289e4

Please sign in to comment.