Skip to content
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
76 changes: 76 additions & 0 deletions internal/sidekick/api/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 appends the resolved fields to the signature.
func (s *MethodSignature) WithFields(fields ...*Field) *MethodSignature {
s.Fields = append(s.Fields, fields...)
return s
}
Comment thread
coryan marked this conversation as resolved.

// 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,
}
}
116 changes: 111 additions & 5 deletions internal/sidekick/api/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -357,3 +357,109 @@ 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) {
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 diff := cmp.Diff([]*api.Field{f1, f2}, sig.Fields); diff != "" {
t.Errorf("mismatch in Fields (-want +got):\n%s", diff)
}
}

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)
}
}
Loading