Skip to content

Commit

Permalink
Output tests (#192)
Browse files Browse the repository at this point in the history
* tests

* add tests

* fix tests
  • Loading branch information
0x4c6565 authored Feb 21, 2025
1 parent 9c02314 commit 9cb03ce
Show file tree
Hide file tree
Showing 22 changed files with 564 additions and 301 deletions.
46 changes: 0 additions & 46 deletions cmd/ecloud/ecloud_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,52 +291,6 @@ func Test_ecloudInstanceCreate(t *testing.T) {

assert.Equal(t, "Error retrieving new instance: test error", err.Error())
})

t.Run("AgentBackupsWithoutGateway_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

service := mocks.NewMockECloudService(mockCtrl)
cmd := ecloudInstanceCreateCmd(nil)
cmd.ParseFlags([]string{
"--name=testinstance",
"--image=img-abcdef12",
"--enable-agent-backups",
})

err := ecloudInstanceCreate(service, cmd, []string{})
assert.NotNil(t, err)
assert.Equal(t, "A backup gateway is required to use agent-level backups, please specify a backup gateway ID", err.Error())
})

t.Run("AgentBackupsWithGateway_NoError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

service := mocks.NewMockECloudService(mockCtrl)
cmd := ecloudInstanceCreateCmd(nil)
cmd.ParseFlags([]string{
"--name=testinstance",
"--image=img-abcdef12",
"--enable-agent-backups",
"--backup-gateway-id=bgw-abcdef12",
})

req := ecloud.CreateInstanceRequest{
Name: "testinstance",
ImageID: "img-abcdef12",
VCPUSockets: 1,
VCPUCoresPerSocket: 1,
BackupAgentEnabled: true,
BackupGatewayID: "bgw-abcdef12",
}

service.EXPECT().CreateInstance(req).Return("i-abcdef12", nil)
service.EXPECT().GetInstance("i-abcdef12").Return(ecloud.Instance{}, nil)

err := ecloudInstanceCreate(service, cmd, []string{})
assert.Nil(t, err)
})
}

func Test_ecloudInstanceUpdateCmd_Args(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/pss/pss_case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ans-group/cli/internal/pkg/clierrors"
"github.com/ans-group/cli/test/mocks"
"github.com/ans-group/cli/test/test_output"
"github.com/ans-group/sdk-go/pkg/connection"
"github.com/ans-group/sdk-go/pkg/service/pss"
gomock "github.com/golang/mock/gomock"
"github.com/spf13/cobra"
Expand All @@ -20,7 +21,7 @@ func Test_pssCaseList(t *testing.T) {

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetCases(gomock.Any()).Return([]pss.Case{}, nil).Times(1)
service.EXPECT().GetCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.Case]{Data: []pss.Case{}}, *connection.NewAPIRequestParameters(), nil), nil).Times(1)

pssCaseList(service, &cobra.Command{}, []string{})
})
Expand All @@ -44,7 +45,7 @@ func Test_pssCaseList(t *testing.T) {

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetCases(gomock.Any()).Return([]pss.Case{}, errors.New("test error")).Times(1)
service.EXPECT().GetCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.Case]{Data: []pss.Case{}}, *connection.NewAPIRequestParameters(), nil), errors.New("test error")).Times(1)

err := pssCaseList(service, &cobra.Command{}, []string{})
assert.Equal(t, "test error", err.Error())
Expand Down
6 changes: 3 additions & 3 deletions cmd/pss/pss_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ans-group/cli/internal/pkg/clierrors"
"github.com/ans-group/cli/test/mocks"
"github.com/ans-group/cli/test/test_output"
"github.com/ans-group/sdk-go/pkg/connection"
"github.com/ans-group/sdk-go/pkg/service/pss"
gomock "github.com/golang/mock/gomock"
"github.com/spf13/cobra"
Expand All @@ -19,8 +20,7 @@ func Test_pssChangeList(t *testing.T) {
defer mockCtrl.Finish()

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetChangeCases(gomock.Any()).Return([]pss.ChangeCase{}, nil).Times(1)
service.EXPECT().GetChangeCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.ChangeCase]{Data: []pss.ChangeCase{}}, *connection.NewAPIRequestParameters(), nil), nil).Times(1)

pssChangeList(service, &cobra.Command{}, []string{})
})
Expand All @@ -44,7 +44,7 @@ func Test_pssChangeList(t *testing.T) {

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetChangeCases(gomock.Any()).Return([]pss.ChangeCase{}, errors.New("test error")).Times(1)
service.EXPECT().GetChangeCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.ChangeCase]{Data: []pss.ChangeCase{}}, *connection.NewAPIRequestParameters(), nil), errors.New("test error")).Times(1)

err := pssChangeList(service, &cobra.Command{}, []string{})
assert.Equal(t, "test error", err.Error())
Expand Down
6 changes: 3 additions & 3 deletions cmd/pss/pss_incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ans-group/cli/internal/pkg/clierrors"
"github.com/ans-group/cli/test/mocks"
"github.com/ans-group/cli/test/test_output"
"github.com/ans-group/sdk-go/pkg/connection"
"github.com/ans-group/sdk-go/pkg/service/pss"
gomock "github.com/golang/mock/gomock"
"github.com/spf13/cobra"
Expand All @@ -19,8 +20,7 @@ func Test_pssIncidentList(t *testing.T) {
defer mockCtrl.Finish()

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetIncidentCases(gomock.Any()).Return([]pss.IncidentCase{}, nil).Times(1)
service.EXPECT().GetIncidentCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.IncidentCase]{Data: []pss.IncidentCase{}}, *connection.NewAPIRequestParameters(), nil), nil).Times(1)

pssIncidentList(service, &cobra.Command{}, []string{})
})
Expand All @@ -44,7 +44,7 @@ func Test_pssIncidentList(t *testing.T) {

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetIncidentCases(gomock.Any()).Return([]pss.IncidentCase{}, errors.New("test error")).Times(1)
service.EXPECT().GetIncidentCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.IncidentCase]{Data: []pss.IncidentCase{}}, *connection.NewAPIRequestParameters(), nil), errors.New("test error")).Times(1)

err := pssIncidentList(service, &cobra.Command{}, []string{})
assert.Equal(t, "test error", err.Error())
Expand Down
6 changes: 3 additions & 3 deletions cmd/pss/pss_problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ans-group/cli/internal/pkg/clierrors"
"github.com/ans-group/cli/test/mocks"
"github.com/ans-group/cli/test/test_output"
"github.com/ans-group/sdk-go/pkg/connection"
"github.com/ans-group/sdk-go/pkg/service/pss"
gomock "github.com/golang/mock/gomock"
"github.com/spf13/cobra"
Expand All @@ -20,7 +21,7 @@ func Test_pssProblemList(t *testing.T) {

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetProblemCases(gomock.Any()).Return([]pss.ProblemCase{}, nil).Times(1)
service.EXPECT().GetProblemCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.ProblemCase]{Data: []pss.ProblemCase{}}, *connection.NewAPIRequestParameters(), nil), nil).Times(1)

pssProblemList(service, &cobra.Command{}, []string{})
})
Expand All @@ -43,8 +44,7 @@ func Test_pssProblemList(t *testing.T) {
defer mockCtrl.Finish()

service := mocks.NewMockPSSService(mockCtrl)

service.EXPECT().GetProblemCases(gomock.Any()).Return([]pss.ProblemCase{}, errors.New("test error")).Times(1)
service.EXPECT().GetProblemCasesPaginated(gomock.Any()).Return(connection.NewPaginated(&connection.APIResponseBodyData[[]pss.ProblemCase]{Data: []pss.ProblemCase{}}, *connection.NewAPIRequestParameters(), nil), errors.New("test error")).Times(1)

err := pssProblemList(service, &cobra.Command{}, []string{})
assert.Equal(t, "test error", err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ import (
func TestOrderedFields_Set(t *testing.T) {
t.Run("SetValue", func(t *testing.T) {
f := output.NewOrderedFields()
f.Set("testkey", output.NewFieldValue("testvalue", true))
f.Set("testkey", "testvalue")

v := f.Get("testkey")

assert.Equal(t, "testvalue", v.Value)
assert.Equal(t, true, v.Default)
assert.Equal(t, "testvalue", v)
})

t.Run("SetExistingValueOverwrite", func(t *testing.T) {
f := output.NewOrderedFields()
f.Set("testkey", output.NewFieldValue("testvalue1", false))
f.Set("testkey", output.NewFieldValue("testvalue2", true))
f.Set("testkey", "testvalue1")
f.Set("testkey", "testvalue2")

v := f.Get("testkey")

assert.Equal(t, "testvalue2", v.Value)
assert.Equal(t, true, v.Default)
assert.Equal(t, "testvalue2", v)
})

t.Run("KeysPopulated", func(t *testing.T) {
f := output.NewOrderedFields()
f.Set("testkey", output.NewFieldValue("testvalue", true))
f.Set("testkey", "testvalue")

keys := f.Keys()

Expand All @@ -40,7 +38,7 @@ func TestOrderedFields_Set(t *testing.T) {

t.Run("ExistsTrue", func(t *testing.T) {
f := output.NewOrderedFields()
f.Set("testkey", output.NewFieldValue("testvalue", true))
f.Set("testkey", "testvalue")

exists := f.Exists("testkey")

Expand All @@ -49,7 +47,7 @@ func TestOrderedFields_Set(t *testing.T) {

t.Run("ExistsFalse", func(t *testing.T) {
f := output.NewOrderedFields()
f.Set("testkey", output.NewFieldValue("testvalue", true))
f.Set("testkey", "testvalue")

exists := f.Exists("testkey2")

Expand All @@ -58,10 +56,10 @@ func TestOrderedFields_Set(t *testing.T) {

t.Run("NonExistentReturnsEmptyValue", func(t *testing.T) {
f := output.NewOrderedFields()
f.Set("testkey", output.NewFieldValue("testvalue", true))
f.Set("testkey", "testvalue")

v := f.Get("testkey2")

assert.Equal(t, "", v.Value)
assert.Equal(t, "", v)
})
}
4 changes: 4 additions & 0 deletions internal/pkg/output/output_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (o *OutputHandler) Output(cmd *cobra.Command, d interface{}) error {
flag, _ = cmd.Flags().GetString("output")
}

if len(flag) == 0 {
flag = "table"
}

format, arg := ParseOutputFlag(flag)
if format == "template" && cmd.Flags().Changed("outputtemplate") {
arg, _ = cmd.Flags().GetString("outputtemplate")
Expand Down
142 changes: 142 additions & 0 deletions internal/pkg/output/output_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package output

import (
"testing"

"github.com/ans-group/cli/test"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

type testModel struct {
TestProperty1 string `json:"testproperty1"`
TestProperty2 string `json:"testproperty2"`
TestProperty3 string `json:"testproperty3"`
}

type testModelCollection []testModel

var collectionSingleRow = testModelCollection([]testModel{{"Row1TestValue1", "Row1TestValue2", "Row1TestValue3"}})
var collectionMultipleRows = testModelCollection([]testModel{{"Row1TestValue1", "Row1TestValue2", "Row1TestValue3"}, {"Row2TestValue1", "Row2TestValue2", "Row2TestValue3"}})

func TestOutputHandler_JSON(t *testing.T) {
t.Run("Success", func(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.JSON("test")
assert.NoError(t, err)
})

assert.Equal(t, "\"test\"", output)
})

t.Run("MarshalError", func(t *testing.T) {
o := NewOutputHandler()

err := o.JSON(func() {})

assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to marshal json")
})
}

func TestOutputHandler_Value(t *testing.T) {
t.Run("SingleRowDefaultFields_ExpectedStdout", func(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.Value(&cobra.Command{}, collectionSingleRow)
assert.NoError(t, err)
})

assert.Equal(t, "Row1TestValue1 Row1TestValue2 Row1TestValue3\n", output)
})

t.Run("MultipleRowsDefaultFields_ExpectedStdout", func(t *testing.T) {
o := NewOutputHandler()
output := test.CatchStdOut(t, func() {
err := o.Value(&cobra.Command{}, collectionMultipleRows)
assert.NoError(t, err)
})

assert.Equal(t, "Row1TestValue1 Row1TestValue2 Row1TestValue3\nRow2TestValue1 Row2TestValue2 Row2TestValue3\n", output)
})
}

func TestOutputHandler_JSONPath(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.JSONPath("{[].TestProperty1}", collectionSingleRow)
assert.NoError(t, err)
})

assert.Equal(t, "Row1TestValue1", output)
}

func TestOutputHandler_CSV(t *testing.T) {
t.Run("SingleRowDefaultFields_ExpectedStdout", func(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
o.CSV(&cobra.Command{}, collectionSingleRow)
})

assert.Equal(t, "testproperty1,testproperty2,testproperty3\nRow1TestValue1,Row1TestValue2,Row1TestValue3\n", output)
})

t.Run("MultipleRowsDefaultFields_ExpectedStdout", func(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
o.CSV(&cobra.Command{}, collectionMultipleRows)
})

assert.Equal(t, "testproperty1,testproperty2,testproperty3\nRow1TestValue1,Row1TestValue2,Row1TestValue3\nRow2TestValue1,Row2TestValue2,Row2TestValue3\n", output)
})
}

func TestOutputHandler_YAML(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.YAML("test")
assert.NoError(t, err)
})

assert.Equal(t, "test\n", output)
}

func TestOutputHandler_Table(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.Table(&cobra.Command{}, collectionSingleRow)
assert.NoError(t, err)
})

assert.Equal(t, "+----------------+----------------+----------------+\n| TESTPROPERTY1 | TESTPROPERTY2 | TESTPROPERTY3 |\n+----------------+----------------+----------------+\n| Row1TestValue1 | Row1TestValue2 | Row1TestValue3 |\n+----------------+----------------+----------------+\n", output)
}

func TestOutputHandler_List(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.List(&cobra.Command{}, collectionSingleRow)
assert.NoError(t, err)
})

assert.Equal(t, "testproperty1 : Row1TestValue1\ntestproperty2 : Row1TestValue2\ntestproperty3 : Row1TestValue3\n", output)
}

func TestOutputHandler_Template(t *testing.T) {
o := NewOutputHandler()

output := test.CatchStdOut(t, func() {
err := o.Template("{{.TestProperty1}}", collectionSingleRow)
assert.NoError(t, err)
})

assert.Equal(t, "Row1TestValue1\n", output)
}
Loading

0 comments on commit 9cb03ce

Please sign in to comment.