Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(internal/snippets,gengapic): use sample values in tests #1513

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
7 changes: 7 additions & 0 deletions internal/testing/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down