Skip to content

Commit

Permalink
tests: improve unit tests for describe commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 15, 2024
1 parent d9c701c commit 9781642
Show file tree
Hide file tree
Showing 12 changed files with 661 additions and 361 deletions.
43 changes: 33 additions & 10 deletions pkg/cmd/describe/cache_setting/cache_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
api "github.com/aziontech/azion-cli/pkg/api/cache_setting"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/utils"
Expand All @@ -25,9 +26,28 @@ var (
cacheSettingsID int64
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
type DescribeCmd struct {
Io *iostreams.IOStreams
AskInput func(string) (string, error)
Get func(context.Context, int64, int64) (api.GetResponse, error)
}

func NewDescribeCmd(f *cmdutil.Factory) *DescribeCmd {
return &DescribeCmd{
Io: f.IOStreams,
AskInput: func(prompt string) (string, error) {
return utils.AskInput(prompt)
},
Get: func(ctx context.Context, appID, cacheID int64) (api.GetResponse, error) {
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
return client.Get(ctx, appID, cacheID)
},
}
}

func NewCobraCmd(describe *DescribeCmd, f *cmdutil.Factory) *cobra.Command {
opts := &contracts.DescribeOptions{}
cmd := &cobra.Command{
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.DescribeShortDescription,
Long: msg.DescribeLongDescription,
Expand All @@ -41,7 +61,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

if !cmd.Flags().Changed("application-id") {
answer, err := utils.AskInput(msg.DescibeAskInputApplicationID)
answer, err := describe.AskInput(msg.DescibeAskInputApplicationID)
if err != nil {
return err
}
Expand All @@ -56,7 +76,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
}

if !cmd.Flags().Changed("cache-setting-id") {
answer, err := utils.AskInput(msg.DescribeAskInputCacheID)
answer, err := describe.AskInput(msg.DescribeAskInputCacheID)
if err != nil {
return err
}
Expand All @@ -70,9 +90,8 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
cacheSettingsID = num
}

client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
ctx := context.Background()
resp, err := client.Get(ctx, applicationID, cacheSettingsID)
resp, err := describe.Get(ctx, applicationID, cacheSettingsID)
if err != nil {
return fmt.Errorf(msg.ErrorGetCache.Error(), err)
}
Expand Down Expand Up @@ -107,8 +126,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.DescribeFlagApplicationID)
cmd.Flags().Int64Var(&cacheSettingsID, "cache-setting-id", 0, msg.DescribeFlagCacheSettingsID)
cmd.Flags().BoolP("help", "h", false, msg.DescribeHelpFlag)
return cmd
cobraCmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.DescribeFlagApplicationID)
cobraCmd.Flags().Int64Var(&cacheSettingsID, "cache-setting-id", 0, msg.DescribeFlagCacheSettingsID)
cobraCmd.Flags().BoolP("help", "h", false, msg.DescribeHelpFlag)
return cobraCmd
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDescribeCmd(f), f)
}
98 changes: 69 additions & 29 deletions pkg/cmd/describe/cache_setting/cache_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,85 @@ import (
"net/http"
"testing"

"github.com/aziontech/azion-cli/pkg/logger"
"go.uber.org/zap/zapcore"

"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func TestDescribe(t *testing.T) {
logger.New(zapcore.DebugLevel)
t.Run("describe an Cache Settings", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("GET", "edge_applications/1673635839/cache_settings/107313"),
httpmock.JSONFromFile("./fixtures/cache_settings.json"),
)

f, _, _ := testutils.NewFactory(mock)

cmd := NewCmd(f)
cmd.SetArgs([]string{"--application-id", "1673635839", "--cache-setting-id", "107313"})

err := cmd.Execute()
require.NoError(t, err)
})
t.Run("not found", func(t *testing.T) {
mock := &httpmock.Registry{}
tests := []struct {
name string
request httpmock.Matcher
response httpmock.Responder
args []string
expectErr bool
mockInput func(string) (string, error)
}{
{
name: "describe a cache setting",
request: httpmock.REST("GET", "edge_applications/1673635839/cache_settings/107313"),
response: httpmock.JSONFromFile("./fixtures/cache_settings.json"),
args: []string{"--application-id", "1673635839", "--cache-setting-id", "107313"},
expectErr: false,
},
{
name: "describe a cache setting - no app id",
request: httpmock.REST("GET", "edge_applications/1673635839/cache_settings/107313"),
response: httpmock.JSONFromFile("./fixtures/cache_settings.json"),
args: []string{"--cache-setting-id", "107313"},
expectErr: false,
mockInput: func(s string) (string, error) {
return "1673635839", nil
},
},
{
name: "describe a cache setting - no cache id",
request: httpmock.REST("GET", "edge_applications/1673635839/cache_settings/107313"),
response: httpmock.JSONFromFile("./fixtures/cache_settings.json"),
args: []string{"--application-id", "1673635839"},
expectErr: false,
mockInput: func(s string) (string, error) {
return "107313", nil
},
},
{
name: "not found",
request: httpmock.REST("GET", "edge_applications/1673635839/cache_settings/107313"),
response: httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
args: []string{"--application-id", "1673635839", "--cache-setting-id", "107313"},
expectErr: true,
},
{
name: "no id sent",
request: httpmock.REST("GET", "edge_applications/1673635839/cache_settings/0"),
response: httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
args: []string{"--application-id", "1673635839", "--cache-setting-id", "0"},
expectErr: true,
},
}

mock.Register(
httpmock.REST("GET", "edge_applications/1673635839/cache_settings/107313"),
httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &httpmock.Registry{}
mock.Register(tt.request, tt.response)

f, _, _ := testutils.NewFactory(mock)
f, _, _ := testutils.NewFactory(mock)
descCmd := NewDescribeCmd(f)
descCmd.AskInput = tt.mockInput
cmd := NewCobraCmd(descCmd, f)
cmd.SetArgs(tt.args)

cmd := NewCmd(f)
err := cmd.Execute()

err := cmd.Execute()
require.Error(t, err)
})
if tt.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
102 changes: 50 additions & 52 deletions pkg/cmd/describe/domains/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,62 @@ import (
"net/http"
"testing"

"github.com/aziontech/azion-cli/pkg/logger"
"go.uber.org/zap/zapcore"

"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func TestDescribe(t *testing.T) {
logger.New(zapcore.DebugLevel)
t.Run("describe an domains", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("GET", "domains/1675272891"),
httpmock.JSONFromFile("./fixtures/domain.json"),
)

f, _, _ := testutils.NewFactory(mock)

cmd := NewCmd(f)

cmd.SetArgs([]string{"--domain-id", "1675272891"})

err := cmd.Execute()
require.NoError(t, err)
})
t.Run("not found", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("GET", "domains/878"),
httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
)

f, _, _ := testutils.NewFactory(mock)

cmd := NewCmd(f)
cmd.SetArgs([]string{"--domain-id", "878"})

err := cmd.Execute()
require.Error(t, err)
})

t.Run("no id sent", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("GET", "edge_applications/1234"),
httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
)

f, _, _ := testutils.NewFactory(mock)

cmd := NewCmd(f)

err := cmd.Execute()

require.Error(t, err)
})
tests := []struct {
name string
request httpmock.Matcher
response httpmock.Responder
args []string
expectErr bool
}{
{
name: "describe a domain",
request: httpmock.REST("GET", "domains/1675272891"),
response: httpmock.JSONFromFile("./fixtures/domain.json"),
args: []string{"--domain-id", "1675272891"},
expectErr: false,
},
{
name: "not found",
request: httpmock.REST("GET", "domains/878"),
response: httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
args: []string{"--domain-id", "878"},
expectErr: true,
},
{
name: "no id sent",
request: httpmock.REST("GET", "edge_applications/1234"),
response: httpmock.StatusStringResponse(http.StatusNotFound, "Not Found"),
args: []string{},
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &httpmock.Registry{}
mock.Register(tt.request, tt.response)

f, _, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)
cmd.SetArgs(tt.args)

err := cmd.Execute()

if tt.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
Loading

0 comments on commit 9781642

Please sign in to comment.