11package list
22
33import (
4+ "context"
45 "strconv"
56 "testing"
67 "time"
78
9+ "github.com/google/go-cmp/cmp"
10+ "github.com/google/go-cmp/cmp/cmpopts"
811 "github.com/google/uuid"
912 "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1013 "github.com/stackitcloud/stackit-cli/internal/pkg/print"
@@ -26,6 +29,10 @@ var testLabels = &map[string]string{
2629 "key2" : "value2" ,
2730}
2831
32+ type testCtxKey struct {}
33+
34+ var testCtx = context .WithValue (context .Background (), testCtxKey {}, "foo" )
35+ var testClient = & iaas.APIClient {}
2936var testLimitFlag = int64 (10 )
3037
3138func fixtureFlagValues (mods ... func (flagValues map [string ]string )) map [string ]string {
@@ -42,6 +49,16 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
4249 return flagValues
4350}
4451
52+ func fixtureRequest (mods ... func (request * iaas.ApiListRoutingTablesOfAreaRequest )) iaas.ApiListRoutingTablesOfAreaRequest {
53+ request := testClient .ListRoutingTablesOfArea (testCtx , testOrgId , testNetworkAreaId , testRegion )
54+ request = request .LabelSelector (testLabelSelectorFlag )
55+
56+ for _ , mod := range mods {
57+ mod (& request )
58+ }
59+ return request
60+ }
61+
4562func fixtureInputModel (mods ... func (model * inputModel )) * inputModel {
4663 model := & inputModel {
4764 GlobalFlagModel : & globalflags.GlobalFlagModel {
@@ -226,3 +243,38 @@ func TestOutputResult(t *testing.T) {
226243 })
227244 }
228245}
246+
247+ func TestBuildRequest (t * testing.T ) {
248+ tests := []struct {
249+ description string
250+ model * inputModel
251+ expectedRequest iaas.ApiListRoutingTablesOfAreaRequest
252+ }{
253+ {
254+ description : "valid input with label selector" ,
255+ model : fixtureInputModel (),
256+ expectedRequest : fixtureRequest (),
257+ },
258+ {
259+ description : "missing label selector" ,
260+ model : fixtureInputModel (func (model * inputModel ) {
261+ model .LabelSelector = nil
262+ }),
263+ expectedRequest : fixtureRequest (func (request * iaas.ApiListRoutingTablesOfAreaRequest ) {
264+ * request = testClient .ListRoutingTablesOfArea (testCtx , testOrgId , testNetworkAreaId , testRegion )
265+ }),
266+ },
267+ }
268+
269+ for _ , tt := range tests {
270+ t .Run (tt .description , func (t * testing.T ) {
271+ request := buildRequest (testCtx , tt .model , testClient )
272+
273+ if diff := cmp .Diff (request , tt .expectedRequest ,
274+ cmp .AllowUnexported (tt .expectedRequest ),
275+ cmpopts .EquateComparable (testCtx )); diff != "" {
276+ t .Errorf ("buildRequest() mismatch (-got +want):\n %s" , diff )
277+ }
278+ })
279+ }
280+ }
0 commit comments