Skip to content

chore: Refactor client add tests - #66

Merged
dbolson merged 1 commit into
refactor-client--add-testsfrom
refactor-client--add-tests-2
Mar 26, 2024
Merged

chore: Refactor client add tests#66
dbolson merged 1 commit into
refactor-client--add-testsfrom
refactor-client--add-tests-2

Conversation

@dbolson

@dbolson dbolson commented Mar 25, 2024

Copy link
Copy Markdown
Contributor

Refactor the flag cmd tests to inject a mock client.

Comment thread cmd/cmdtest.go
return args
}

func ArgsValidProjectsCreate() []string {

Copy link
Copy Markdown
Contributor Author

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.

Comment thread cmd/flags/create.go
func runCreate(client flags.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// rebind flags used in other subcommands
_ = viper.BindPFlag("data", cmd.Flags().Lookup("data"))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebind the common subcommand flags. We can refactor this later.

Comment thread cmd/flags/create.go
viper.GetString("baseUri"),
)
func runCreate(client flags.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bind the client in a closure similar to projects.

Comment thread cmd/flags/flags.go
}

updateCmd, err := NewUpdateCmd()
createCmd, err := NewCreateCmd(client)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Comment thread cmd/root.go
)

func NewRootCommand(client projects.Client) (*cobra.Command, error) {
func NewRootCommand(flagsClient flags.Client, projectsClient projects.Client) (*cobra.Command, error) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't want to try to make one giant API client interface, although maybe we could end up composing one based off the smaller interfaces.

Comment thread internal/flags/flags.go
// client creates an LD API client. It's not set as a field on the struct because the CLI flags
// are evaluated when running the command, not when executing the program. That means we don't have
// the flag values until the command's RunE method is called.
func (c FlagsClient) client(accessToken string, baseURI string) *ldapi.APIClient {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can refactor this and the projects one to a single place.

Comment thread internal/projects/mock.go

type MockClient struct {
mock.Mock
AccessToken string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were unused.

@dbolson
dbolson merged commit 0b52c05 into refactor-client--add-tests Mar 26, 2024
@dbolson
dbolson deleted the refactor-client--add-tests-2 branch March 26, 2024 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants