Skip to content

Commit

Permalink
feat: added testing unit for command list from device groups
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm committed Apr 17, 2023
1 parent f4ea045 commit a81aea1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
16 changes: 16 additions & 0 deletions pkg/cmd/device_groups/list/.fixtures/resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"count": 1,
"total_pages": 1,
"schema_version": 3,
"links": {
"previous": null,
"next": null
},
"results": [
{
"id": 2257,
"name": "Mob1le",
"user_agent": "Mobile|Android|iPhone"
}
]
}
10 changes: 10 additions & 0 deletions pkg/cmd/device_groups/list/.fixtures/resp_without_items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"count": 0,
"total_pages": 1,
"schema_version": 3,
"links": {
"previous": null,
"next": null
},
"results": []
}
6 changes: 3 additions & 3 deletions pkg/cmd/device_groups/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func PrintTable(cmd *cobra.Command, f *cmdutil.Factory, opts *contracts.ListOpti
}

tbl := table.New("ID", "NAME")
tbl.WithWriter(f.IOStreams.Out)
table.DefaultWriter = f.IOStreams.Out
if cmd.Flags().Changed("details") {
tbl = table.New("ID", "NAME", "USER AGENT")
Expand All @@ -85,9 +86,9 @@ func PrintTable(cmd *cobra.Command, f *cmdutil.Factory, opts *contracts.ListOpti

for _, v := range applications.Results {
if cmd.Flags().Changed("details") {
tbl.AddRow(v.Id, v.Name, v.UserAgent)
tbl.AddRow(*v.Id, v.Name, v.UserAgent)
} else {
tbl.AddRow(v.Id, v.Name)
tbl.AddRow(*v.Id, v.Name)
}
}

Expand All @@ -103,6 +104,5 @@ func PrintTable(cmd *cobra.Command, f *cmdutil.Factory, opts *contracts.ListOpti

*numberPage += 1
opts.Page = *numberPage
f.IOStreams.Out = table.DefaultWriter
return applications.TotalPages, nil
}
47 changes: 47 additions & 0 deletions pkg/cmd/device_groups/list/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package list

import (
"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestList(t *testing.T) {
t.Run("command list with successes", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("GET", "edge_applications/1673635846/device_groups"),
httpmock.JSONFromFile(".fixtures/resp.json"),
)

f, stdout, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)

cmd.SetArgs([]string{"-a", "1673635846"})

_, err := cmd.ExecuteC()
require.NoError(t, err)
assert.Equal(t, "ID NAME \n2257 Mob1le \n", stdout.String())
})

t.Run("command list response without items", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("GET", "edge_applications/1673635847/device_groups"),
httpmock.JSONFromFile(".fixtures/resp_without_items.json"),
)

f, stdout, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)

cmd.SetArgs([]string{"-a", "1673635847"})

_, err := cmd.ExecuteC()
require.NoError(t, err)
assert.Equal(t, "ID NAME \n", stdout.String())
})
}

0 comments on commit a81aea1

Please sign in to comment.