@@ -19,6 +19,7 @@ import (
1919
2020 "github.com/google/go-cmp/cmp"
2121 "github.com/googleapis/librarian/internal/sidekick/internal/api"
22+ "github.com/googleapis/librarian/internal/sidekick/internal/config"
2223)
2324
2425func TestPageSimple (t * testing.T ) {
@@ -82,7 +83,7 @@ func TestPageSimple(t *testing.T) {
8283 Methods : []* api.Method {method },
8384 }
8485 model := api .NewTestAPI ([]* api.Message {request , response , resource }, []* api.Enum {}, []* api.Service {service })
85- updateMethodPagination (model )
86+ updateMethodPagination (nil , model )
8687 if method .Pagination != request .Fields [1 ] {
8788 t .Errorf ("mismatch, want=%v, got=%v" , request .Fields [1 ], method .Pagination )
8889 }
@@ -95,6 +96,91 @@ func TestPageSimple(t *testing.T) {
9596 }
9697}
9798
99+ func TestPageWithOverride (t * testing.T ) {
100+ resource := & api.Message {
101+ Name : "Resource" ,
102+ ID : ".package.Resource" ,
103+ }
104+ request := & api.Message {
105+ Name : "Request" ,
106+ ID : ".package.Request" ,
107+ Fields : []* api.Field {
108+ {
109+ Name : "parent" ,
110+ JSONName : "parent" ,
111+ ID : ".package.Request.parent" ,
112+ Typez : api .STRING_TYPE ,
113+ },
114+ {
115+ Name : "page_token" ,
116+ JSONName : "pageToken" ,
117+ ID : ".package.Request.pageToken" ,
118+ Typez : api .STRING_TYPE ,
119+ },
120+ {
121+ Name : "page_size" ,
122+ JSONName : "pageSize" ,
123+ ID : ".package.Request.pageSize" ,
124+ Typez : api .INT32_TYPE ,
125+ },
126+ },
127+ }
128+ response := & api.Message {
129+ Name : "Response" ,
130+ ID : ".package.Response" ,
131+ Fields : []* api.Field {
132+ {
133+ Name : "next_page_token" ,
134+ JSONName : "nextPageToken" ,
135+ ID : ".package.Request.nextPageToken" ,
136+ Typez : api .STRING_TYPE ,
137+ },
138+ {
139+ Name : "warnings" ,
140+ JSONName : "warnings" ,
141+ ID : ".package.Request.warnings" ,
142+ Typez : api .MESSAGE_TYPE ,
143+ TypezID : ".package.Warning" ,
144+ Repeated : true ,
145+ },
146+ {
147+ Name : "items" ,
148+ JSONName : "items" ,
149+ ID : ".package.Request.items" ,
150+ Typez : api .MESSAGE_TYPE ,
151+ TypezID : ".package.Resource" ,
152+ Repeated : true ,
153+ },
154+ },
155+ }
156+ method := & api.Method {
157+ Name : "List" ,
158+ ID : ".package.Service.List" ,
159+ InputTypeID : ".package.Request" ,
160+ OutputTypeID : ".package.Response" ,
161+ }
162+ service := & api.Service {
163+ Name : "Service" ,
164+ ID : ".package.Service" ,
165+ Methods : []* api.Method {method },
166+ }
167+ model := api .NewTestAPI ([]* api.Message {request , response , resource }, []* api.Enum {}, []* api.Service {service })
168+ overrides := []config.PaginationOverride {
169+ {ID : ".package.Service.List" , ItemField : "items" },
170+ }
171+ updateMethodPagination (overrides , model )
172+ if method .Pagination != request .Fields [1 ] {
173+ t .Errorf ("mismatch, want=%v, got=%v" , request .Fields [1 ], method .Pagination )
174+ }
175+ want := & api.PaginationInfo {
176+ NextPageToken : response .Fields [0 ],
177+ PageableItem : response .Fields [2 ],
178+ }
179+ if diff := cmp .Diff (want , response .Pagination ); diff != "" {
180+ t .Errorf ("mismatch, (-want, +got):\n %s" , diff )
181+ }
182+ }
183+
98184func TestPageMissingInputType (t * testing.T ) {
99185 resource := & api.Message {
100186 Name : "Resource" ,
@@ -132,7 +218,7 @@ func TestPageMissingInputType(t *testing.T) {
132218 Methods : []* api.Method {method },
133219 }
134220 model := api .NewTestAPI ([]* api.Message {response , resource }, []* api.Enum {}, []* api.Service {service })
135- updateMethodPagination (model )
221+ updateMethodPagination (nil , model )
136222 if method .Pagination != nil {
137223 t .Errorf ("mismatch, want=nil, got=%v" , method .Pagination )
138224 }
@@ -179,7 +265,7 @@ func TestPageMissingOutputType(t *testing.T) {
179265 Methods : []* api.Method {method },
180266 }
181267 model := api .NewTestAPI ([]* api.Message {request , resource }, []* api.Enum {}, []* api.Service {service })
182- updateMethodPagination (model )
268+ updateMethodPagination (nil , model )
183269 if method .Pagination != nil {
184270 t .Errorf ("mismatch, want=nil, got=%v" , method .Pagination )
185271 }
@@ -227,7 +313,7 @@ func TestPageBadRequest(t *testing.T) {
227313 Methods : []* api.Method {method },
228314 }
229315 model := api .NewTestAPI ([]* api.Message {request , response , resource }, []* api.Enum {}, []* api.Service {service })
230- updateMethodPagination (model )
316+ updateMethodPagination (nil , model )
231317 if method .Pagination != nil {
232318 t .Errorf ("mismatch, want=nil, got=%v" , method .Pagination )
233319 }
@@ -279,7 +365,7 @@ func TestPageBadResponse(t *testing.T) {
279365 Methods : []* api.Method {method },
280366 }
281367 model := api .NewTestAPI ([]* api.Message {request , response , resource }, []* api.Enum {}, []* api.Service {service })
282- updateMethodPagination (model )
368+ updateMethodPagination (nil , model )
283369 if method .Pagination != nil {
284370 t .Errorf ("mismatch, want=nil, got=%v" , method .Pagination )
285371 }
@@ -444,21 +530,26 @@ func TestPaginationResponseErrors(t *testing.T) {
444530 }
445531
446532 for _ , input := range []* api.Message {badToken , badItems , nil } {
447- if got := paginationResponseInfo (input ); got != nil {
533+ if got := paginationResponseInfo (nil , ".package.Service.List" , input ); got != nil {
448534 t .Errorf ("expected paginationResponseInfo(...) == nil, got=%v, input=%v" , got , input )
449535 }
450536 }
451537}
452538
453539func TestPaginationResponseItem (t * testing.T ) {
540+ overrides := []config.PaginationOverride {
541+ {ID : ".package.Service.List" , ItemField : "--invalid--" },
542+ }
454543 for _ , test := range []struct {
455- Name string
456- Repeated bool
457- Typez api.Typez
544+ Name string
545+ Repeated bool
546+ Typez api.Typez
547+ Overrides []config.PaginationOverride
458548 }{
459- {"badRepeated" , false , api .MESSAGE_TYPE },
460- {"badType" , true , api .STRING_TYPE },
461- {"bothBad" , false , api .ENUM_TYPE },
549+ {"badRepeated" , false , api .MESSAGE_TYPE , nil },
550+ {"badType" , true , api .STRING_TYPE , nil },
551+ {"bothBad" , false , api .ENUM_TYPE , nil },
552+ {"badOverride" , true , api .MESSAGE_TYPE , overrides },
462553 } {
463554 response := & api.Message {
464555 Name : "Response" ,
@@ -472,7 +563,7 @@ func TestPaginationResponseItem(t *testing.T) {
472563 },
473564 },
474565 }
475- got := paginationResponseItem (response )
566+ got := paginationResponseItem (test . Overrides , ".package.Service.List" , response )
476567 if got != nil {
477568 t .Errorf ("the field should not be a pagination item, got=%v" , got )
478569 }
0 commit comments