Skip to content

Commit

Permalink
chore(internal/testing/sample): add package (googleapis#1508)
Browse files Browse the repository at this point in the history
Package sample is added, which provides sample values for use in tests.

The intent is to make it easier to reason about the various test cases,
by using one example and explaining where various values are defined,
using bigquery as an example.

As a starting point, TestGenSnippetFile has been refactor to use the
constants in internal/testing/sample, instead of hardcoding values in
the tests. Other tests will be refactored in follow up PRs.
  • Loading branch information
julieqiu authored May 13, 2024
1 parent e0cc950 commit 320b85d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 18 deletions.
1 change: 1 addition & 0 deletions internal/gengapic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ go_test(
"//internal/grpc_service_config",
"//internal/pbinfo",
"//internal/snippets",
"//internal/testing/sample",
"//internal/txtdiff",
"@com_github_google_go_cmp//cmp",
"@com_google_cloud_go_longrunning//autogen/longrunningpb",
Expand Down
32 changes: 14 additions & 18 deletions internal/gengapic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"github.com/googleapis/gapic-generator-go/internal/snippets"
"github.com/googleapis/gapic-generator-go/internal/testing/sample"
"github.com/googleapis/gapic-generator-go/internal/txtdiff"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/genproto/googleapis/api/serviceconfig"
Expand Down Expand Up @@ -284,45 +285,41 @@ func TestGenSnippetFile(t *testing.T) {
g.imports = map[pbinfo.ImportSpec]bool{}
g.serviceConfig = &serviceconfig.Service{
Apis: []*apipb.Api{
{Name: "google.cloud.bigquery.migration.v2.MigrationService"},
{Name: sample.ProtoServiceName},
},
}

protoPkg := "google.cloud.bigquery.migration.v2"
libPkg := "cloud.google.com/go/bigquery/migration/apiv2"
pkgName := "bigquerymigration"
g.snippetMetadata = snippets.NewMetadata(protoPkg, libPkg, pkgName)
g.snippetMetadata = snippets.NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName)

inputType := &descriptorpb.DescriptorProto{
Name: proto.String("CreateMigrationWorkflowRequest"),
Name: proto.String(sample.CreateRequest),
}
outputType := &descriptorpb.DescriptorProto{
Name: proto.String("MigrationWorkflow"),
Name: proto.String(sample.Resource),
}

file := &descriptorpb.FileDescriptorProto{
Options: &descriptorpb.FileOptions{
GoPackage: proto.String("cloud.google.com/go/bigquery/migration/apiv2/migrationpb"),
GoPackage: proto.String(sample.GoProtoPackagePath),
},
Package: proto.String(protoPkg),
Package: proto.String(sample.ProtoPackagePath),
}

files := []*descriptorpb.FileDescriptorProto{}
g.descInfo = pbinfo.Of(files)
for _, typ := range []*descriptorpb.DescriptorProto{
inputType, outputType,
} {
g.descInfo.Type[".google.cloud.bigquery.migration.v2."+typ.GetName()] = typ
g.descInfo.Type[sample.DescriptorInfoTypeName(typ.GetName())] = typ
g.descInfo.ParentFile[typ] = file
}

serv := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("MigrationService"),
Name: proto.String(sample.ServiceName),
Method: []*descriptorpb.MethodDescriptorProto{
{
Name: proto.String("CreateMigrationWorkflow"),
InputType: proto.String(".google.cloud.bigquery.migration.v2.CreateMigrationWorkflowRequest"),
OutputType: proto.String(".google.cloud.bigquery.migration.v2.MigrationWorkflow"),
Name: proto.String(sample.CreateMethod),
InputType: proto.String(sample.DescriptorInfoTypeName(sample.CreateRequest)),
OutputType: proto.String(sample.DescriptorInfoTypeName(sample.Resource)),
},
},
}
Expand All @@ -340,15 +337,14 @@ func TestGenSnippetFile(t *testing.T) {
},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Name: "migrationpb", Path: "cloud.google.com/go/bigquery/migration/apiv2/migrationpb"}: true,
{Name: sample.GoProtoPackageName, Path: sample.GoProtoPackagePath}: true,
},
},
} {
t.Run(tst.tstName, func(t *testing.T) {
g.reset()
g.opts = &tst.options
defaultHost := "bigquerymigration.googleapis.com"
g.snippetMetadata.AddService(serv.GetName(), defaultHost)
g.snippetMetadata.AddService(serv.GetName(), sample.ServiceURL)
err := g.genSnippetFile(serv, serv.Method[0])
if err != nil {
t.Fatal(err)
Expand Down
8 changes: 8 additions & 0 deletions internal/testing/sample/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "sample",
srcs = ["sample.go"],
importpath = "github.com/googleapis/gapic-generator-go/internal/testing/sample",
visibility = ["//:__subpackages__"],
)
134 changes: 134 additions & 0 deletions internal/testing/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package sample provides functionality for generating sample values of
// the types contained in the internal package for testing purposes.
package sample

import (
"fmt"
)

const (
// ServiceURL is the hostname of the service.
//
// Example:
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L36
ServiceURL = "bigquerymigration.googleapis.com"

// ServiceName is the name of the service.
//
// Example:
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L35
ServiceName = "MigrationService"

// CreateMethod is the name of the RPC method for creating a resource.
// The same name is used for the proto RPC method and the Go method.
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2/migrationpb#MigrationServiceClient.CreateMigrationWorkflow
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L41
CreateMethod = "CreateMigrationWorkflow"

// CreateRequest is the name of the request for creating a resource.
// The same name is used for the proto message and the Go type.
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2/migrationpb#CreateMigrationWorkflowRequest
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L110
CreateRequest = "CreateMigrationWorkflowRequest"

// GetMethod is the name of the RPC method used to fetch a resource.
// The same name is used for the proto RPC method and the Go method.
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2/migrationpb#MigrationServiceClient.GetMigrationWorkflow
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L51
GetMethod = "GetMigrationWorkflow"

// GetRequest is the name of the request for fetching a resource.
// The same name is used for the proto message and the Go type.
//
// A GetRequest often contains `google.api.resource_reference`, in order to
// reference the name of the resource (see https://aip.dev/4231#referencing-other-resources).
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2/migrationpb#GetMigrationWorkflowRequest
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L126
GetRequest = "GetMigrationWorkflowRequest"

// Resource is the name of the resource returned by a Get or Create request.
//
// A resource message often contains a `google.api.resource` option with a
// type and pattern (see https://aip.dev/4231#resource-messages).
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2/migrationpb#MigrationWorkflow
// https://github.com/googleapis/googleapis/blob/master/google/cloud/bigquery/migration/v2alpha/migration_entities.proto#L38
Resource = "MigrationWorkflow"
)

const (

// ProtoServiceName is the fully qualified name of service.
//
// Example:
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L35.
ProtoServiceName = "google.cloud.bigquery.migration.v2.MigrationService"

// ProtoPackagePath is the package path of the proto file.
//
// Example:
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L17
ProtoPackagePath = "google.cloud.bigquery.migration.v2"
)

const (
// GoPackageName is the package name for the auto-generated Go package.
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2
GoPackageName = "migration"

// GoPackagePath is the package import path for the auto-generated Go
// package.
//
// Example:
// https://pkg.go.dev/cloud.google.com/go/bigquery/migration/apiv2
// https://github.com/googleapis/googleapis/blob/f7df662a24c56ecaab79cb7d808fed4d2bb4981d/google/cloud/bigquery/migration/v2/migration_service.proto#L28
GoPackagePath = "cloud.google.com/go/bigquery/migration/apiv2"

// GoProtoPackageName is the package name of the auto-generated proto
// package, which is imported by package at GoPackagePath. This name is
// derived from the value following the ";" `go_package` in the proto file.
//
// Example:
// 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
GoProtoPackageName = "migrationpb"

// GoProtoPackagePath is the package import path of the auto-generated proto
// package. This name is derived from the value before the ";"
// `go_package` in the proto file.
//
// Example:
// 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"
)

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

0 comments on commit 320b85d

Please sign in to comment.