Skip to content

Commit

Permalink
tests: add unit tests for origin/cache/function delete commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 3, 2024
1 parent ccbca19 commit db814a2
Show file tree
Hide file tree
Showing 8 changed files with 603 additions and 167 deletions.
51 changes: 39 additions & 12 deletions pkg/cmd/delete/cache_setting/cache_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
msg "github.com/aziontech/azion-cli/messages/cache_setting"
api "github.com/aziontech/azion-cli/pkg/api/cache_setting"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"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 @@ -21,20 +22,42 @@ var (
cacheSettingsID int64
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
type DeleteCmd struct {
Io *iostreams.IOStreams
ReadInput func(string) (string, error)
DeleteCache func(context.Context, int64, int64) error
AskInput func(msg string) (string, error)
}

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

func NewCobraCmd(delete *DeleteCmd, f *cmdutil.Factory) *cobra.Command {
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.DeleteShortDescription,
Long: msg.DeleteLongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azion delete cache-setting --application-id 1673635839 --cache-setting-id 107313
`),
$ azion delete cache-setting --application-id 1673635839 --cache-setting-id 107313
`),
RunE: func(cmd *cobra.Command, args []string) error {
var err error

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

if !cmd.Flags().Changed("cache-setting-id") {
answer, err := utils.AskInput(msg.DeleteAskInputCacheID)
answer, err := delete.AskInput(msg.DeleteAskInputCacheID)
if err != nil {
return err
}
Expand All @@ -67,7 +90,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {

ctx := context.Background()

err := client.Delete(ctx, applicationID, cacheSettingsID)
err = client.Delete(ctx, applicationID, cacheSettingsID)
if err != nil {
return fmt.Errorf(msg.ErrorFailToDelete.Error(), err)
}
Expand All @@ -78,12 +101,16 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
Flags: f.Flags,
}
return output.Print(&deleteOut)

},
}

cmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.DeleteFlagApplicationID)
cmd.Flags().Int64Var(&cacheSettingsID, "cache-setting-id", 0, msg.DeleteFlagCacheSettingsID)
cmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag)
return cmd
cobraCmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag)
cobraCmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.DeleteFlagApplicationID)
cobraCmd.Flags().Int64Var(&cacheSettingsID, "cache-setting-id", 0, msg.DeleteFlagCacheSettingsID)

return cobraCmd
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDeleteCmd(f), f)
}
132 changes: 99 additions & 33 deletions pkg/cmd/delete/cache_setting/cache_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

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

msg "github.com/aziontech/azion-cli/messages/cache_setting"
Expand All @@ -14,38 +15,103 @@ import (
"github.com/stretchr/testify/require"
)

func TestCreate(t *testing.T) {
func mockAppId(msg string) (string, error) {
return "1673635839", nil
}

func mockCacheId(msg string) (string, error) {
return "107313", nil
}

func mockInvalid(msg string) (string, error) {
return "invalid", nil
}

func TestDeleteWithAskInput(t *testing.T) {
logger.New(zapcore.DebugLevel)
t.Run("delete by id", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("DELETE", "edge_applications/1673635839/cache_settings/107313"),
httpmock.StatusStringResponse(204, ""),
)

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

_, err := cmd.ExecuteC()
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf(msg.DeleteOutputSuccess, 107313), stdout.String())
})

t.Run("delete that is not found", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("DELETE", "edge_applications/1234/cache_settings/107313"),
httpmock.StatusStringResponse(404, "Not Found"),
)

f, _, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)
cmd.SetArgs([]string{"--application-id", "1234", "--cache-setting-id", "107313"})

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

tests := []struct {
name string
applicationID string
cacheSettingID string
method string
endpoint string
statusCode int
responseBody string
expectedOutput string
expectError bool
mockInputs func(msg string) (string, error)
mockError error
}{
{
name: "ask for application id success",
cacheSettingID: "107313",
method: "DELETE",
endpoint: "edge_applications/1673635839/cache_settings/107313",
statusCode: 204,
responseBody: "",
expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, 107313),
expectError: false,
mockInputs: mockAppId,
mockError: nil,
},
{
name: "ask for cache setting id success",
applicationID: "1673635839",
method: "DELETE",
endpoint: "edge_applications/1673635839/cache_settings/107313",
statusCode: 204,
responseBody: "",
expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, 107313),
expectError: false,
mockInputs: mockCacheId,
mockError: nil,
},
{
name: "error in input",
cacheSettingID: "107313",
method: "DELETE",
endpoint: "edge_applications/invalid/cache_settings/107313",
statusCode: 400,
responseBody: "Bad Request",
expectedOutput: "",
expectError: true,
mockInputs: mockInvalid,
mockError: fmt.Errorf("invalid argument \"\" for \"--cache-setting-id\" flag: strconv.ParseInt: parsing \"\": invalid syntax"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &httpmock.Registry{}
mock.Register(
httpmock.REST(tt.method, tt.endpoint),
httpmock.StatusStringResponse(tt.statusCode, tt.responseBody),
)

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

deleteCmd := NewDeleteCmd(f)
deleteCmd.AskInput = tt.mockInputs
cobraCmd := NewCobraCmd(deleteCmd, f)

if tt.applicationID != "" && tt.cacheSettingID != "" {
cobraCmd.SetArgs([]string{"--application-id", tt.applicationID, "--cache-setting-id", tt.cacheSettingID})
} else if tt.applicationID != "" {
cobraCmd.SetArgs([]string{"--application-id", tt.applicationID})
} else {
cobraCmd.SetArgs([]string{"--cache-setting-id", tt.cacheSettingID})
}

_, err := cobraCmd.ExecuteC()
if tt.expectError {
require.Error(t, err)
logger.Debug("Expected error occurred", zap.Error(err))
} else {
require.NoError(t, err)
assert.Equal(t, tt.expectedOutput, stdout.String())
logger.Debug("Expected output", zap.String("output", stdout.String()))
}
})
}
}
48 changes: 38 additions & 10 deletions pkg/cmd/delete/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,52 @@ import (
msg "github.com/aziontech/azion-cli/messages/delete/domain"
api "github.com/aziontech/azion-cli/pkg/api/domain"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"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"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
var domainID int64
cmd := &cobra.Command{
var domainID int64

type DeleteCmd struct {
Io *iostreams.IOStreams
ReadInput func(string) (string, error)
DeleteDomain func(context.Context, int64) error
AskInput func(string) (string, error)
}

func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd {
return &DeleteCmd{
Io: f.IOStreams,
ReadInput: func(prompt string) (string, error) {
return utils.AskInput(prompt)
},
DeleteDomain: func(ctx context.Context, domainID int64) error {
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
return client.Delete(ctx, domainID)
},
AskInput: utils.AskInput,
}
}

func NewCobraCmd(delete *DeleteCmd, f *cmdutil.Factory) *cobra.Command {
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.ShortDescription,
Long: msg.LongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azion delete domain --domain-id 1234
`),
`),
RunE: func(cmd *cobra.Command, args []string) error {
var err error

if !cmd.Flags().Changed("domain-id") {

answer, err := utils.AskInput(msg.AskDeleteInput)
answer, err := delete.AskInput(msg.AskDeleteInput)
if err != nil {
return err
}
Expand All @@ -49,7 +72,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {

ctx := context.Background()

err := client.Delete(ctx, domainID)
err = client.Delete(ctx, domainID)
if err != nil {
return fmt.Errorf(msg.ErrorFailToDeleteDomain.Error(), err)
}
Expand All @@ -63,7 +86,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().Int64Var(&domainID, "domain-id", 0, msg.FlagId)
cmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
return cmd
cobraCmd.Flags().Int64Var(&domainID, "domain-id", 0, msg.FlagId)
cobraCmd.Flags().BoolP("help", "h", false, msg.HelpFlag)

return cobraCmd
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDeleteCmd(f), f)
}
Loading

0 comments on commit db814a2

Please sign in to comment.