diff --git a/core/stores/redis/conf_test.go b/core/stores/redis/conf_test.go new file mode 100644 index 00000000..7bdbd1f4 --- /dev/null +++ b/core/stores/redis/conf_test.go @@ -0,0 +1,110 @@ +package redis + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/stringx" +) + +func TestRedisConf(t *testing.T) { + tests := []struct { + name string + RedisConf + ok bool + }{ + { + name: "missing host", + RedisConf: RedisConf{ + Host: "", + Type: NodeType, + Pass: "", + }, + ok: false, + }, + { + name: "missing type", + RedisConf: RedisConf{ + Host: "localhost:6379", + Type: "", + Pass: "", + }, + ok: false, + }, + { + name: "ok", + RedisConf: RedisConf{ + Host: "localhost:6379", + Type: NodeType, + Pass: "", + }, + ok: true, + }, + } + + for _, test := range tests { + t.Run(stringx.RandId(), func(t *testing.T) { + if test.ok { + assert.Nil(t, test.RedisConf.Validate()) + assert.NotNil(t, test.RedisConf.NewRedis()) + } else { + assert.NotNil(t, test.RedisConf.Validate()) + } + }) + } +} + +func TestRedisKeyConf(t *testing.T) { + tests := []struct { + name string + RedisKeyConf + ok bool + }{ + { + name: "missing host", + RedisKeyConf: RedisKeyConf{ + RedisConf: RedisConf{ + Host: "", + Type: NodeType, + Pass: "", + }, + Key: "foo", + }, + ok: false, + }, + { + name: "missing key", + RedisKeyConf: RedisKeyConf{ + RedisConf: RedisConf{ + Host: "localhost:6379", + Type: NodeType, + Pass: "", + }, + Key: "", + }, + ok: false, + }, + { + name: "ok", + RedisKeyConf: RedisKeyConf{ + RedisConf: RedisConf{ + Host: "localhost:6379", + Type: NodeType, + Pass: "", + }, + Key: "foo", + }, + ok: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if test.ok { + assert.Nil(t, test.RedisKeyConf.Validate()) + } else { + assert.NotNil(t, test.RedisKeyConf.Validate()) + } + }) + } +} diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index f284c717..c928f8c4 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -440,12 +440,27 @@ func TestRedis_SortedSet(t *testing.T) { val, err = client.Zscore("key", "value1") assert.Nil(t, err) assert.Equal(t, int64(5), val) - ok, err = client.Zadd("key", 6, "value2") + val, err = client.Zadds("key", Pair{ + Key: "value2", + Score: 6, + }, Pair{ + Key: "value3", + Score: 7, + }) assert.Nil(t, err) - assert.True(t, ok) - ok, err = client.Zadd("key", 7, "value3") + assert.Equal(t, int64(2), val) + pairs, err := client.ZRevRangeWithScores("key", 1, 3) assert.Nil(t, err) - assert.True(t, ok) + assert.EqualValues(t, []Pair{ + { + Key: "value2", + Score: 6, + }, + { + Key: "value1", + Score: 5, + }, + }, pairs) rank, err := client.Zrank("key", "value2") assert.Nil(t, err) assert.Equal(t, int64(1), rank) @@ -487,7 +502,7 @@ func TestRedis_SortedSet(t *testing.T) { vals, err = client.Zrevrange("key", 0, -1) assert.Nil(t, err) assert.EqualValues(t, []string{"value4", "value1"}, vals) - pairs, err := client.ZrangeWithScores("key", 0, -1) + pairs, err = client.ZrangeWithScores("key", 0, -1) assert.Nil(t, err) assert.EqualValues(t, []Pair{ { @@ -565,6 +580,13 @@ func TestRedis_Pipelined(t *testing.T) { }) } +func TestRedisString(t *testing.T) { + runOnRedis(t, func(client *Redis) { + client.Ping() + assert.Equal(t, client.Addr, client.String()) + }) +} + func runOnRedis(t *testing.T, fn func(client *Redis)) { s, err := miniredis.Run() assert.Nil(t, err) @@ -576,7 +598,9 @@ func runOnRedis(t *testing.T, fn func(client *Redis)) { t.Error(err) } - client.Close() + if client != nil { + client.Close() + } }() fn(NewRedis(s.Addr(), NodeType))