Skip to content

Commit

Permalink
fix: apply gotests
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Jul 21, 2020
1 parent 1769d63 commit a4ad9c6
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 2 deletions.
115 changes: 114 additions & 1 deletion internal/db/kvs/redis/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

redis "github.com/go-redis/redis/v7"
"github.com/vdaas/vald/internal/net"

"go.uber.org/goleak"
)

Expand Down Expand Up @@ -3078,3 +3077,117 @@ func TestWithInitialPingDuration(t *testing.T) {
})
}
}

func TestWithPingFlag(t *testing.T) {
// Change interface type to the type of object you are testing
type T = interface{}
type args struct {
flag bool
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
beforeFunc func(args)
afterFunc func(args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}
*/

tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
flag: false,
},
want: want {
obj: new(T),
},
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
flag: false,
},
want: want {
obj: new(T),
},
}
}(),
*/
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithPingFlag(test.args.flag)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option do not return an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithPingFlag(test.args.flag)
obj := new(T)
got(obj)
if err := test.checkFunc(test.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
})
}
}
201 changes: 200 additions & 1 deletion internal/db/kvs/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package redis

import (
"context"
"crypto/tls"
"reflect"
"testing"
"time"

redis "github.com/go-redis/redis/v7"
"github.com/vdaas/vald/internal/errors"

"github.com/vdaas/vald/internal/net"
"go.uber.org/goleak"
)

Expand Down Expand Up @@ -103,3 +106,199 @@ func TestNew(t *testing.T) {
})
}
}

func Test_redisClient_ping(t *testing.T) {
type args struct {
ctx context.Context
}
type fields struct {
addrs []string
clusterSlots func() ([]redis.ClusterSlot, error)
db int
dialTimeout time.Duration
dialer func(ctx context.Context, network, addr string) (net.Conn, error)
idleCheckFrequency time.Duration
idleTimeout time.Duration
initialPingDuration time.Duration
initialPingTimeLimit time.Duration
keyPref string
maxConnAge time.Duration
maxRedirects int
maxRetries int
maxRetryBackoff time.Duration
minIdleConns int
minRetryBackoff time.Duration
onConnect func(*redis.Conn) error
onNewNode func(*redis.Client)
password string
poolSize int
poolTimeout time.Duration
readOnly bool
readTimeout time.Duration
routeByLatency bool
routeRandomly bool
tlsConfig *tls.Config
writeTimeout time.Duration
client Redis
pingEnabled bool
}
type want struct {
err error
}
type test struct {
name string
args args
fields fields
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
ctx: nil,
},
fields: fields {
addrs: nil,
clusterSlots: nil,
db: 0,
dialTimeout: nil,
dialer: nil,
idleCheckFrequency: nil,
idleTimeout: nil,
initialPingDuration: nil,
initialPingTimeLimit: nil,
keyPref: "",
maxConnAge: nil,
maxRedirects: 0,
maxRetries: 0,
maxRetryBackoff: nil,
minIdleConns: 0,
minRetryBackoff: nil,
onConnect: nil,
onNewNode: nil,
password: "",
poolSize: 0,
poolTimeout: nil,
readOnly: false,
readTimeout: nil,
routeByLatency: false,
routeRandomly: false,
tlsConfig: nil,
writeTimeout: nil,
client: nil,
pingEnabled: false,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
ctx: nil,
},
fields: fields {
addrs: nil,
clusterSlots: nil,
db: 0,
dialTimeout: nil,
dialer: nil,
idleCheckFrequency: nil,
idleTimeout: nil,
initialPingDuration: nil,
initialPingTimeLimit: nil,
keyPref: "",
maxConnAge: nil,
maxRedirects: 0,
maxRetries: 0,
maxRetryBackoff: nil,
minIdleConns: 0,
minRetryBackoff: nil,
onConnect: nil,
onNewNode: nil,
password: "",
poolSize: 0,
poolTimeout: nil,
readOnly: false,
readTimeout: nil,
routeByLatency: false,
routeRandomly: false,
tlsConfig: nil,
writeTimeout: nil,
client: nil,
pingEnabled: false,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
rc := &redisClient{
addrs: test.fields.addrs,
clusterSlots: test.fields.clusterSlots,
db: test.fields.db,
dialTimeout: test.fields.dialTimeout,
dialer: test.fields.dialer,
idleCheckFrequency: test.fields.idleCheckFrequency,
idleTimeout: test.fields.idleTimeout,
initialPingDuration: test.fields.initialPingDuration,
initialPingTimeLimit: test.fields.initialPingTimeLimit,
keyPref: test.fields.keyPref,
maxConnAge: test.fields.maxConnAge,
maxRedirects: test.fields.maxRedirects,
maxRetries: test.fields.maxRetries,
maxRetryBackoff: test.fields.maxRetryBackoff,
minIdleConns: test.fields.minIdleConns,
minRetryBackoff: test.fields.minRetryBackoff,
onConnect: test.fields.onConnect,
onNewNode: test.fields.onNewNode,
password: test.fields.password,
poolSize: test.fields.poolSize,
poolTimeout: test.fields.poolTimeout,
readOnly: test.fields.readOnly,
readTimeout: test.fields.readTimeout,
routeByLatency: test.fields.routeByLatency,
routeRandomly: test.fields.routeRandomly,
tlsConfig: test.fields.tlsConfig,
writeTimeout: test.fields.writeTimeout,
client: test.fields.client,
pingEnabled: test.fields.pingEnabled,
}

err := rc.ping(test.args.ctx)
if err := test.checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}

0 comments on commit a4ad9c6

Please sign in to comment.