-
Notifications
You must be signed in to change notification settings - Fork 14
chore: Refactor client add tests #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,13 @@ import ( | |
| "ldcli/internal/flags" | ||
| ) | ||
|
|
||
| func NewCreateCmd() (*cobra.Command, error) { | ||
| func NewCreateCmd(client flags.Client) (*cobra.Command, error) { | ||
| cmd := &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create a new flag", | ||
| Long: "Create a new flag", | ||
| PreRunE: validate, | ||
| RunE: runCreate, | ||
| RunE: runCreate(client), | ||
| } | ||
|
|
||
| cmd.Flags().StringP("data", "d", "", "Input data in JSON") | ||
|
|
@@ -50,33 +50,34 @@ type inputData struct { | |
| Key string `json:"key"` | ||
| } | ||
|
|
||
| func runCreate(cmd *cobra.Command, args []string) error { | ||
| client := flags.NewClient( | ||
| viper.GetString("accessToken"), | ||
| viper.GetString("baseUri"), | ||
| ) | ||
| func runCreate(client flags.Client) func(*cobra.Command, []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bind the client in a closure similar to projects. |
||
| // rebind flags used in other subcommands | ||
| _ = viper.BindPFlag("data", cmd.Flags().Lookup("data")) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rebind the common subcommand flags. We can refactor this later. |
||
| _ = viper.BindPFlag("projKey", cmd.Flags().Lookup("projKey")) | ||
|
|
||
| var data inputData | ||
| err := json.Unmarshal([]byte(cmd.Flags().Lookup("data").Value.String()), &data) | ||
| // err := json.Unmarshal([]byte(viper.GetString("data")), &data) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| projKey := viper.GetString("projKey") | ||
| var data inputData | ||
| err := json.Unmarshal([]byte(viper.GetString("data")), &data) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| response, err := client.Create( | ||
| context.Background(), | ||
| data.Name, | ||
| data.Key, | ||
| projKey, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| response, err := client.Create( | ||
| context.Background(), | ||
| viper.GetString("accessToken"), | ||
| viper.GetString("baseUri"), | ||
| data.Name, | ||
| data.Key, | ||
| viper.GetString("projKey"), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
| fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
|
|
||
| return nil | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // validate ensures the flags are valid before using them. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package flags_test | ||
|
|
||
| import ( | ||
| "ldcli/cmd" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "ldcli/internal/errors" | ||
| "ldcli/internal/flags" | ||
| ) | ||
|
|
||
| func TestCreate(t *testing.T) { | ||
| t.Run("with valid flags calls projects API", func(t *testing.T) { | ||
| client := flags.MockClient{} | ||
| client. | ||
| On("Create", "testAccessToken", "http://test.com", "test-name", "test-key", "test-proj-key"). | ||
| Return([]byte(cmd.ValidResponse), nil) | ||
| args := []string{ | ||
| "flags", "create", | ||
| "-t", "testAccessToken", | ||
| "-u", "http://test.com", | ||
| "-d", `{"key": "test-key", "name": "test-name"}`, | ||
| "--projKey", "test-proj-key", | ||
| } | ||
|
|
||
| output, err := cmd.CallCmd(t, &client, nil, args) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.JSONEq(t, `{"valid": true}`, string(output)) | ||
| }) | ||
|
|
||
| t.Run("with an error response is an error", func(t *testing.T) { | ||
| client := flags.MockClient{} | ||
| client. | ||
| On("Create", "testAccessToken", "http://test.com", "test-name", "test-key", "test-proj-key"). | ||
| Return([]byte(`{}`), errors.NewError("An error")) | ||
| args := []string{ | ||
| "flags", "create", | ||
| "-t", "testAccessToken", | ||
| "-u", "http://test.com", | ||
| "-d", `{"key": "test-key", "name": "test-name"}`, | ||
| "--projKey", "test-proj-key", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &client, nil, args) | ||
|
|
||
| require.EqualError(t, err, "An error") | ||
| }) | ||
|
|
||
| t.Run("with missing required flags is an error", func(t *testing.T) { | ||
| args := []string{ | ||
| "flags", "create", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &flags.MockClient{}, nil, args) | ||
|
|
||
| assert.EqualError(t, err, `required flag(s) "accessToken", "data", "projKey" not set`) | ||
| }) | ||
|
|
||
| t.Run("with invalid baseUri is an error", func(t *testing.T) { | ||
| args := []string{ | ||
| "flags", "create", | ||
| "-t", "testAccessToken", | ||
| "-u", "invalid", | ||
| "-d", `{"key": "test-key", "name": "test-name"}`, | ||
| "--projKey", "test-proj-key", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &flags.MockClient{}, nil, args) | ||
|
|
||
| assert.EqualError(t, err, "baseUri is invalid") | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,29 @@ | ||
| package flags | ||
|
|
||
| import "github.com/spf13/cobra" | ||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| func NewFlagsCmd() (*cobra.Command, error) { | ||
| "ldcli/internal/flags" | ||
| ) | ||
|
|
||
| func NewFlagsCmd(client flags.Client) (*cobra.Command, error) { | ||
| cmd := &cobra.Command{ | ||
| Use: "flags", | ||
| Short: "Make requests (list, create, etc.) on flags", | ||
| Long: "Make requests (list, create, etc.) on flags", | ||
| } | ||
|
|
||
| updateCmd, err := NewUpdateCmd() | ||
| createCmd, err := NewCreateCmd(client) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swapped these to alphabetize and to check that the last one created binds the shared flags while the rest are set to the zero value. |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| createCmd, err := NewCreateCmd() | ||
| updateCmd, err := NewUpdateCmd(client) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cmd.AddCommand(updateCmd) | ||
| cmd.AddCommand(createCmd) | ||
| cmd.AddCommand(updateCmd) | ||
|
|
||
| return cmd, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed these, but I think I'm going to replace them with the actual []string in the tests. See the flag cmd tests for examples -- I think they're easier to read.