diff --git a/internal/snippets/BUILD.bazel b/internal/snippets/BUILD.bazel index 853a4ca24..74a5c7c06 100644 --- a/internal/snippets/BUILD.bazel +++ b/internal/snippets/BUILD.bazel @@ -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", ], diff --git a/internal/snippets/snippets_test.go b/internal/snippets/snippets_test.go index 4d2242703..3f3986623 100644 --- a/internal/snippets/snippets_test.go +++ b/internal/snippets/snippets_test.go @@ -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") @@ -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", }, }, @@ -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) } } diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go index 84290d387..f14514361 100644 --- a/internal/testing/sample/sample.go +++ b/internal/testing/sample/sample.go @@ -92,6 +92,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 ( @@ -126,6 +129,10 @@ 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.