test: replace hand-written mocks with gomock (#63)

This commit is contained in:
Daniel Moran
2021-05-06 10:19:41 -04:00
committed by GitHub
parent c44fec7d6d
commit d995f7d182
24 changed files with 1313 additions and 983 deletions

View File

@ -1,10 +1,12 @@
package internal_test
import (
"bytes"
"context"
"errors"
"testing"
"github.com/golang/mock/gomock"
"github.com/influxdata/influx-cli/v2/internal"
"github.com/influxdata/influx-cli/v2/internal/api"
"github.com/influxdata/influx-cli/v2/internal/mock"
@ -14,28 +16,28 @@ import (
func Test_PingSuccess(t *testing.T) {
t.Parallel()
client := &mock.HealthApi{
GetHealthExecuteFn: func(req api.ApiGetHealthRequest) (api.HealthCheck, error) {
return api.HealthCheck{Status: api.HEALTHCHECKSTATUS_PASS}, nil
},
}
ctrl := gomock.NewController(t)
client := mock.NewMockHealthApi(ctrl)
client.EXPECT().GetHealth(gomock.Any()).Return(api.ApiGetHealthRequest{ApiService: client})
client.EXPECT().GetHealthExecute(gomock.Any()).Return(api.HealthCheck{Status: api.HEALTHCHECKSTATUS_PASS}, nil)
stdio := mock.NewMockStdio(nil, true)
stdio := mock.NewMockStdIO(ctrl)
bytesWritten := bytes.Buffer{}
stdio.EXPECT().Write(gomock.Any()).DoAndReturn(bytesWritten.Write).AnyTimes()
cli := &internal.CLI{StdIO: stdio}
require.NoError(t, cli.Ping(context.Background(), client))
require.Equal(t, "OK\n", stdio.Stdout())
require.Equal(t, "OK\n", bytesWritten.String())
}
func Test_PingFailedRequest(t *testing.T) {
t.Parallel()
e := "the internet is down"
client := &mock.HealthApi{
GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, error) {
return api.HealthCheck{}, errors.New(e)
},
}
ctrl := gomock.NewController(t)
client := mock.NewMockHealthApi(ctrl)
client.EXPECT().GetHealth(gomock.Any()).Return(api.ApiGetHealthRequest{ApiService: client})
client.EXPECT().GetHealthExecute(gomock.Any()).Return(api.HealthCheck{}, errors.New(e))
cli := &internal.CLI{}
err := cli.Ping(context.Background(), client)
@ -47,11 +49,11 @@ func Test_PingFailedStatus(t *testing.T) {
t.Parallel()
e := "I broke"
client := &mock.HealthApi{
GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, error) {
return api.HealthCheck{}, &api.HealthCheck{Status: api.HEALTHCHECKSTATUS_FAIL, Message: &e}
},
}
ctrl := gomock.NewController(t)
client := mock.NewMockHealthApi(ctrl)
client.EXPECT().GetHealth(gomock.Any()).Return(api.ApiGetHealthRequest{ApiService: client})
client.EXPECT().GetHealthExecute(gomock.Any()).
Return(api.HealthCheck{}, &api.HealthCheck{Status: api.HEALTHCHECKSTATUS_FAIL, Message: &e})
cli := &internal.CLI{}
err := cli.Ping(context.Background(), client)
@ -63,11 +65,11 @@ func Test_PingFailedStatusNoMessage(t *testing.T) {
t.Parallel()
name := "foo"
client := &mock.HealthApi{
GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, error) {
return api.HealthCheck{}, &api.HealthCheck{Status: api.HEALTHCHECKSTATUS_FAIL, Name: name}
},
}
ctrl := gomock.NewController(t)
client := mock.NewMockHealthApi(ctrl)
client.EXPECT().GetHealth(gomock.Any()).Return(api.ApiGetHealthRequest{ApiService: client})
client.EXPECT().GetHealthExecute(gomock.Any()).
Return(api.HealthCheck{}, &api.HealthCheck{Status: api.HEALTHCHECKSTATUS_FAIL, Name: name})
cli := &internal.CLI{}
err := cli.Ping(context.Background(), client)