From 320b85df9a4422c4f5e2205508231f2506f1e7fd Mon Sep 17 00:00:00 2001 From: Julie Qiu Date: Mon, 13 May 2024 18:16:19 -0400 Subject: [PATCH] chore(internal/testing/sample): add package (#1508) 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. --- internal/gengapic/BUILD.bazel | 1 + internal/gengapic/example_test.go | 32 +++---- internal/testing/sample/BUILD.bazel | 8 ++ internal/testing/sample/sample.go | 134 ++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 18 deletions(-) create mode 100644 internal/testing/sample/BUILD.bazel create mode 100644 internal/testing/sample/sample.go diff --git a/internal/gengapic/BUILD.bazel b/internal/gengapic/BUILD.bazel index 1e4b57582..3f27a780b 100644 --- a/internal/gengapic/BUILD.bazel +++ b/internal/gengapic/BUILD.bazel @@ -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", diff --git a/internal/gengapic/example_test.go b/internal/gengapic/example_test.go index e12ce29a5..2be61d906 100644 --- a/internal/gengapic/example_test.go +++ b/internal/gengapic/example_test.go @@ -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" @@ -284,27 +285,23 @@ 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{} @@ -312,17 +309,17 @@ func TestGenSnippetFile(t *testing.T) { 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)), }, }, } @@ -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) diff --git a/internal/testing/sample/BUILD.bazel b/internal/testing/sample/BUILD.bazel new file mode 100644 index 000000000..527ac977d --- /dev/null +++ b/internal/testing/sample/BUILD.bazel @@ -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__"], +) diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go new file mode 100644 index 000000000..84290d387 --- /dev/null +++ b/internal/testing/sample/sample.go @@ -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) +}