Skip to content

Commit 9885865

Browse files
committed
review changes
1 parent 3c9eed9 commit 9885865

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

internal/cmd/routingtable/list/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
131131

132132
return request
133133
}
134+
134135
func outputResult(p *print.Printer, outputFormat string, routingTables []iaas.RoutingTable, orgId string) error {
135136
if routingTables == nil {
136137
return fmt.Errorf("list routing-table items are nil")

internal/cmd/routingtable/list/list_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package list
22

33
import (
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{}
2936
var testLimitFlag = int64(10)
3037

3138
func 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+
4562
func 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

Comments
 (0)