From 34e7f382e8b5742077e90516c3e601992389fa6b Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Tue, 6 Oct 2026 13:16:44 +0000 Subject: [PATCH 1/2] test(api): add missing fluent test builders --- internal/sidekick/api/test.go | 76 +++++++++++++++++++ internal/sidekick/api/test_test.go | 113 +++++++++++++++++++++++++++-- 2 files changed, 184 insertions(+), 5 deletions(-) diff --git a/internal/sidekick/api/test.go b/internal/sidekick/api/test.go index 443f6e3faf..7e19af2672 100644 --- a/internal/sidekick/api/test.go +++ b/internal/sidekick/api/test.go @@ -421,6 +421,18 @@ func (m *Method) WithOutput(msg *Message) *Method { return m } +// WithInputTypeID sets the input type ID for the method. +func (m *Method) WithInputTypeID(id string) *Method { + m.InputTypeID = id + return m +} + +// WithOutputTypeID sets the output type ID for the method. +func (m *Method) WithOutputTypeID(id string) *Method { + m.OutputTypeID = id + return m +} + // WithPathTemplate sets the path template for the first binding. func (m *Method) WithPathTemplate(pt *PathTemplate) *Method { m.ensureFirstBinding().PathTemplate = pt @@ -465,6 +477,12 @@ func (m *Method) WithSourceMethod(source *Method) *Method { return m } +// WithSourceServiceID sets the source service ID for the method. +func (m *Method) WithSourceServiceID(id string) *Method { + m.SourceServiceID = id + return m +} + // WithPagination sets the page token field for the request. func (m *Method) WithPagination(pageToken *Field) *Method { m.IsList = true @@ -576,6 +594,12 @@ func (b *PathBinding) WithQueryParameters(params map[string]bool) *PathBinding { return b } +// WithTargetResource sets the target resource for the binding. +func (b *PathBinding) WithTargetResource(tr *TargetResource) *PathBinding { + b.TargetResource = tr + return b +} + // NewTestOneOf creates a OneOf with defaults for testing. func NewTestOneOf(name string) *OneOf { return &OneOf{ @@ -882,3 +906,55 @@ func ParseTemplateForTest(template string) []PathSegment { } return segments } + +// NewTestMethodSignature creates a MethodSignature with parameter names. +func NewTestMethodSignature(names ...string) *MethodSignature { + return &MethodSignature{Names: names} +} + +// WithFields sets the resolved fields for the signature. +func (s *MethodSignature) WithFields(fields ...*Field) *MethodSignature { + s.Fields = fields + return s +} + +// NewTestResourceNamePattern creates a ResourceNamePattern with segments. +func NewTestResourceNamePattern(segments ...ResourceNameSegment) *ResourceNamePattern { + return &ResourceNamePattern{Segments: segments} +} + +// NewTestRoutingInfo creates a RoutingInfo struct with variants. +func NewTestRoutingInfo(name string, variants ...*RoutingInfoVariant) *RoutingInfo { + return &RoutingInfo{Name: name, Variants: variants} +} + +// NewTestRoutingInfoVariant creates a RoutingInfoVariant with a field path. +func NewTestRoutingInfoVariant(fieldPath ...string) *RoutingInfoVariant { + return &RoutingInfoVariant{FieldPath: fieldPath} +} + +// NewTestOperationInfo creates an OperationInfo with response and metadata type IDs. +func NewTestOperationInfo(responseTypeID, metadataTypeID string) *OperationInfo { + return &OperationInfo{ + ResponseTypeID: responseTypeID, + MetadataTypeID: metadataTypeID, + } +} + +// NewTestDiscoveryLro creates a DiscoveryLro with polling path parameters. +func NewTestDiscoveryLro(pollingParams ...string) *DiscoveryLro { + return &DiscoveryLro{PollingPathParameters: pollingParams} +} + +// NewTestSampleInfo creates a SampleInfo initialized with a resource name field. +func NewTestSampleInfo(resourceNameField *Field) *SampleInfo { + return &SampleInfo{ResourceNameField: resourceNameField} +} + +// NewTestPaginationInfo creates a PaginationInfo for pagination responses. +func NewTestPaginationInfo(nextPageToken, pageableItem *Field) *PaginationInfo { + return &PaginationInfo{ + NextPageToken: nextPageToken, + PageableItem: pageableItem, + } +} diff --git a/internal/sidekick/api/test_test.go b/internal/sidekick/api/test_test.go index e6cce65375..9087f23199 100644 --- a/internal/sidekick/api/test_test.go +++ b/internal/sidekick/api/test_test.go @@ -23,7 +23,7 @@ import ( ) func TestMethod_WithBindings_NilPathInfo(t *testing.T) { - method := &api.Method{Name: "Test"} + method := api.NewTestMethod("Test").WithPathInfo(nil) b1 := api.NewTestPathBinding("GET", (&api.PathTemplate{}).WithLiteral("v1")) b2 := api.NewTestPathBinding("POST", (&api.PathTemplate{}).WithLiteral("v2")) @@ -49,7 +49,7 @@ func TestMethod_WithBindings_NonNilPathInfo(t *testing.T) { } func TestMethod_WithBodyFieldPath_NilPathInfo(t *testing.T) { - method := &api.Method{Name: "Test"} + method := api.NewTestMethod("Test").WithPathInfo(nil) method.WithBodyFieldPath("data") if method.PathInfo == nil { @@ -61,7 +61,7 @@ func TestMethod_WithBodyFieldPath_NilPathInfo(t *testing.T) { } func TestMethod_WithVerb_NilPathInfo(t *testing.T) { - method := &api.Method{Name: "Test"} + method := api.NewTestMethod("Test").WithPathInfo(nil) method.WithVerb("PUT") if method.PathInfo == nil || len(method.PathInfo.Bindings) == 0 { @@ -73,7 +73,7 @@ func TestMethod_WithVerb_NilPathInfo(t *testing.T) { } func TestMethod_WithPathTemplate_NilPathInfo(t *testing.T) { - method := &api.Method{Name: "Test"} + method := api.NewTestMethod("Test").WithPathInfo(nil) pt := (&api.PathTemplate{}).WithLiteral("v1") method.WithPathTemplate(pt) @@ -86,7 +86,7 @@ func TestMethod_WithPathTemplate_NilPathInfo(t *testing.T) { } func TestMethod_WithQueryParameters_NilPathInfo(t *testing.T) { - method := &api.Method{Name: "Test"} + method := api.NewTestMethod("Test").WithPathInfo(nil) params := map[string]bool{"page_size": true} method.WithQueryParameters(params) @@ -357,3 +357,106 @@ func TestField_FluentSetters(t *testing.T) { t.Errorf("f.ResourceNamePattern = %v, want %v", f.ResourceNamePattern, pattern) } } + +func TestMethod_TypeIDSetters(t *testing.T) { + m := api.NewTestMethod("DoWork"). + WithInputTypeID(".test.Request"). + WithOutputTypeID(".test.Response"). + WithSourceServiceID(".test.Service") + + if m.InputTypeID != ".test.Request" { + t.Errorf("m.InputTypeID = %q, want %q", m.InputTypeID, ".test.Request") + } + if m.OutputTypeID != ".test.Response" { + t.Errorf("m.OutputTypeID = %q, want %q", m.OutputTypeID, ".test.Response") + } + if m.SourceServiceID != ".test.Service" { + t.Errorf("m.SourceServiceID = %q, want %q", m.SourceServiceID, ".test.Service") + } +} + +func TestPathBinding_WithTargetResource(t *testing.T) { + tr := &api.TargetResource{FieldPaths: [][]string{{"parent"}}} + b := api.NewTestPathBinding("GET", nil).WithTargetResource(tr) + if b.TargetResource != tr { + t.Errorf("b.TargetResource = %v, want %v", b.TargetResource, tr) + } +} + +func TestMethodSignature_Builders(t *testing.T) { + f := api.NewTestField("name") + sig := api.NewTestMethodSignature("name", "parent").WithFields(f) + + if diff := cmp.Diff([]string{"name", "parent"}, sig.Names); diff != "" { + t.Errorf("mismatch in Names (-want +got):\n%s", diff) + } + if len(sig.Fields) != 1 || sig.Fields[0] != f { + t.Errorf("sig.Fields = %v, want [%v]", sig.Fields, f) + } +} + +func TestResourceNamePattern_Builder(t *testing.T) { + p := api.NewTestResourceNamePattern( + api.ResourceNameSegment{Literal: "projects"}, + api.ResourceNameSegment{Variable: "project"}, + ) + if len(p.Segments) != 2 { + t.Fatalf("len(p.Segments) = %d, want 2", len(p.Segments)) + } + if p.Segments[0].Literal != "projects" || p.Segments[1].Variable != "project" { + t.Errorf("unexpected segments: %+v", p.Segments) + } +} + +func TestRoutingInfo_Builders(t *testing.T) { + v := api.NewTestRoutingInfoVariant("name", "id") + r := api.NewTestRoutingInfo("routing", v) + + if r.Name != "routing" { + t.Errorf("r.Name = %q, want 'routing'", r.Name) + } + if len(r.Variants) != 1 || r.Variants[0] != v { + t.Errorf("r.Variants = %v, want [%v]", r.Variants, v) + } + if diff := cmp.Diff([]string{"name", "id"}, v.FieldPath); diff != "" { + t.Errorf("mismatch in FieldPath (-want +got):\n%s", diff) + } +} + +func TestOperationInfo_Builder(t *testing.T) { + op := api.NewTestOperationInfo(".test.Response", ".test.Metadata") + if op.ResponseTypeID != ".test.Response" { + t.Errorf("op.ResponseTypeID = %q, want .test.Response", op.ResponseTypeID) + } + if op.MetadataTypeID != ".test.Metadata" { + t.Errorf("op.MetadataTypeID = %q, want .test.Metadata", op.MetadataTypeID) + } +} + +func TestDiscoveryLro_Builder(t *testing.T) { + lro := api.NewTestDiscoveryLro("project", "zone") + if diff := cmp.Diff([]string{"project", "zone"}, lro.PollingPathParameters); diff != "" { + t.Errorf("mismatch in PollingPathParameters (-want +got):\n%s", diff) + } +} + +func TestSampleInfo_Builder(t *testing.T) { + f := api.NewTestField("name") + s := api.NewTestSampleInfo(f) + if s.ResourceNameField != f { + t.Errorf("s.ResourceNameField = %v, want %v", s.ResourceNameField, f) + } +} + +func TestPaginationInfo_Builder(t *testing.T) { + token := api.NewTestField("next_page_token") + items := api.NewTestField("items") + p := api.NewTestPaginationInfo(token, items) + + if p.NextPageToken != token { + t.Errorf("p.NextPageToken = %v, want %v", p.NextPageToken, token) + } + if p.PageableItem != items { + t.Errorf("p.PageableItem = %v, want %v", p.PageableItem, items) + } +} From d81742f284a7157a279f2a8422a06243e0d6ead1 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Tue, 6 Oct 2026 14:13:05 +0000 Subject: [PATCH 2/2] fix(api): append in MethodSignature.WithFields --- internal/sidekick/api/test.go | 4 ++-- internal/sidekick/api/test_test.go | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/sidekick/api/test.go b/internal/sidekick/api/test.go index 7e19af2672..6ab7db58b0 100644 --- a/internal/sidekick/api/test.go +++ b/internal/sidekick/api/test.go @@ -912,9 +912,9 @@ func NewTestMethodSignature(names ...string) *MethodSignature { return &MethodSignature{Names: names} } -// WithFields sets the resolved fields for the signature. +// WithFields appends the resolved fields to the signature. func (s *MethodSignature) WithFields(fields ...*Field) *MethodSignature { - s.Fields = fields + s.Fields = append(s.Fields, fields...) return s } diff --git a/internal/sidekick/api/test_test.go b/internal/sidekick/api/test_test.go index 9087f23199..871f261ac0 100644 --- a/internal/sidekick/api/test_test.go +++ b/internal/sidekick/api/test_test.go @@ -384,14 +384,17 @@ func TestPathBinding_WithTargetResource(t *testing.T) { } func TestMethodSignature_Builders(t *testing.T) { - f := api.NewTestField("name") - sig := api.NewTestMethodSignature("name", "parent").WithFields(f) + f1 := api.NewTestField("name") + f2 := api.NewTestField("parent") + sig := api.NewTestMethodSignature("name", "parent"). + WithFields(f1). + WithFields(f2) if diff := cmp.Diff([]string{"name", "parent"}, sig.Names); diff != "" { t.Errorf("mismatch in Names (-want +got):\n%s", diff) } - if len(sig.Fields) != 1 || sig.Fields[0] != f { - t.Errorf("sig.Fields = %v, want [%v]", sig.Fields, f) + if diff := cmp.Diff([]*api.Field{f1, f2}, sig.Fields); diff != "" { + t.Errorf("mismatch in Fields (-want +got):\n%s", diff) } }