Skip to content

Commit

Permalink
chore(internal/snippets,gengapic): use sample values in tests
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 googleapis#1512
  • Loading branch information
julieqiu committed May 13, 2024
1 parent 320b85d commit 255f36d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 63 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
1 change: 1 addition & 0 deletions internal/snippets/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_test(
embed = [":snippets"],
deps = [
"//internal/snippets/metadata",
"//internal/testing/sample",
"@com_github_google_go_cmp//cmp",
"@org_golang_google_protobuf//proto",
],
Expand Down
41 changes: 16 additions & 25 deletions internal/snippets/snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,37 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/googleapis/gapic-generator-go/internal/snippets/metadata"
"github.com/googleapis/gapic-generator-go/internal/testing/sample"
"google.golang.org/protobuf/proto"
)

var protoPkg = "google.cloud.bigquery.migration.v2"
var libPkg = "cloud.google.com/go/bigquery/migration/apiv2"
var defaultHost = "bigquerymigration.googleapis.com"
var version = "v2"
var pkgName = "migration"

func TestNewMetadata(t *testing.T) {
sm := NewMetadata(protoPkg, libPkg, pkgName)
sm := NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName)

if sm.protoPkg != protoPkg {
t.Errorf("%s: got %s want %s,", t.Name(), sm.protoPkg, protoPkg)
if sm.protoPkg != sample.ProtoPackagePath {
t.Errorf("%s: got %s want %s,", t.Name(), sm.protoPkg, sample.ProtoPackagePath)
}
if sm.libPkg != libPkg {
t.Errorf("%s: got %s want %s", t.Name(), sm.libPkg, libPkg)
if sm.libPkg != sample.GoPackagePath {
t.Errorf("%s: got %s want %s", t.Name(), sm.libPkg, sample.GoPackagePath)
}
if got := len(sm.protoServices); got != 0 {
t.Errorf("%s: got %d want empty", t.Name(), len(sm.protoServices))
}
if sm.apiVersion != version {
t.Errorf("%s: got %s want %s", t.Name(), sm.apiVersion, version)
if sm.apiVersion != sample.ProtoVersion {
t.Errorf("%s: got %s want %s", t.Name(), sm.apiVersion, sample.ProtoVersion)
}
}

func TestToMetadataJSON(t *testing.T) {
// Build fixture
sm := NewMetadata(protoPkg, libPkg, pkgName)
sm := NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName)
regionTagStart := 18
regionTagEnd := 50
for i := 0; i < 2; i++ {
serviceName := fmt.Sprintf("Foo%dService", i)
methodName := fmt.Sprintf("Bar%dMethod", i)
sm.AddService(serviceName, defaultHost)
sm.AddMethod(serviceName, methodName, protoPkg, serviceName, regionTagEnd)
sm.AddService(serviceName, sample.ServiceURL)
sm.AddMethod(serviceName, methodName, sample.ProtoPackagePath, serviceName, regionTagEnd)
sm.UpdateMethodDoc(serviceName, methodName, methodName+" doc\n New line.")
sm.UpdateMethodResult(serviceName, methodName, "mypackage."+methodName+"Result")
sm.AddParams(serviceName, methodName, "mypackage."+methodName+"Request")
Expand All @@ -63,12 +58,12 @@ func TestToMetadataJSON(t *testing.T) {
// Build expectation
want := &metadata.Index{
ClientLibrary: &metadata.ClientLibrary{
Name: libPkg,
Name: sample.GoPackagePath,
Version: VersionPlaceholder,
Language: metadata.Language_GO,
Apis: []*metadata.Api{
{
Id: protoPkg,
Id: sample.ProtoPackagePath,
Version: "v2",
},
},
Expand Down Expand Up @@ -142,15 +137,11 @@ func TestToMetadataJSON(t *testing.T) {
}

func TestRegionTag(t *testing.T) {
protoPkg := "google.cloud.bigquery.migration.v2"
libPkg := "google.golang.org/genproto/googleapis/cloud/bigquery/migration/v2"
sm := NewMetadata(protoPkg, libPkg, pkgName)
serviceName := "MigrationService"
defaultHost := "bigquerymigration.googleapis.com"
sm.AddService(serviceName, defaultHost)
methodName := "GetMigrationWorkflow"
sm := NewMetadata(sample.ProtoPackagePath, libPkg, sample.GoPackagePath)
sm.AddService(sample.ServiceName, sample.ServiceURL)
want := "bigquerymigration_v2_generated_MigrationService_GetMigrationWorkflow_sync"
if got := sm.RegionTag(serviceName, methodName); got != want {
if got := sm.RegionTag(sample.ServiceName, sample.GetMethod); got != want {
t.Errorf("%s: got %s want %s", t.Name(), got, want)
}
}
59 changes: 59 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 @@ -92,6 +97,9 @@ const (
// Example:
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L17
ProtoPackagePath = "google.cloud.bigquery.migration.v2"

// ProtoVersion is the major version as defined in the protofile.
ProtoVersion = "v2"
)

const (
Expand Down Expand Up @@ -126,9 +134,60 @@ const (
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2/migrationpb.
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L28
GoProtoPackagePath = "cloud.google.com/go/bigquery/migration/apiv2/migrationpb"

// GoVersion is the version used in the package path for versioning the Go
// module containing the package.
GoVersion = "apiv2"
)

// DescriptorInfoTypeName constructs the name format used by g.descInfo.Type.
func DescriptorInfoTypeName(typ string) string {
return fmt.Sprintf(".%s.%s", ProtoPackagePath, typ)
}

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

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)),
},
},
}
}

func InputType(input string) *descriptorpb.DescriptorProto {
return &descriptorpb.DescriptorProto{
Name: proto.String(input),
}
}

func OutputType(output string) *descriptorpb.DescriptorProto {
return &descriptorpb.DescriptorProto{
Name: proto.String(output),
}
}

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

0 comments on commit 255f36d

Please sign in to comment.