chore: add more tests (#2794)

master
Kevin Wan 2 years ago committed by GitHub
parent 02265d0bfe
commit 963b52fb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1897,17 +1897,17 @@ func (s *Redis) Zadd(key string, score int64, value string) (bool, error) {
return s.ZaddCtx(context.Background(), key, score, value)
}
// ZaddFloat is the implementation of redis zadd command.
func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) {
return s.ZaddFloatCtx(context.Background(), key, score, value)
}
// ZaddCtx is the implementation of redis zadd command.
func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
val bool, err error) {
return s.ZaddFloatCtx(ctx, key, float64(score), value)
}
// ZaddFloat is the implementation of redis zadd command.
func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) {
return s.ZaddFloatCtx(context.Background(), key, score, value)
}
// ZaddFloatCtx is the implementation of redis zadd command.
func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (
val bool, err error) {

@ -213,6 +213,7 @@ func TestRedis_Hmset(t *testing.T) {
}
func TestRedis_Hscan(t *testing.T) {
t.Run("scan", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
key := "hash:test"
fieldsAndValues := make(map[string]string)
@ -238,9 +239,17 @@ func TestRedis_Hscan(t *testing.T) {
assert.Equal(t, sum, 3100)
_, err = New(client.Addr, badType()).Del(key)
assert.NotNil(t, err)
assert.Error(t, err)
_, err = client.Del(key)
assert.Nil(t, err)
assert.NoError(t, err)
})
})
t.Run("scan with error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Del("hash:test")
assert.Error(t, err)
})
})
}
@ -285,6 +294,7 @@ func TestRedis_Keys(t *testing.T) {
}
func TestRedis_HyperLogLog(t *testing.T) {
t.Run("hyperloglog", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
client.Ping()
r := New(client.Addr)
@ -312,9 +322,18 @@ func TestRedis_HyperLogLog(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, int64(2), val)
})
})
t.Run("hyperloglog with error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Pfadd("key1", "val1")
assert.Error(t, err)
})
})
}
func TestRedis_List(t *testing.T) {
t.Run("list", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
_, err := New(client.Addr, badType()).Lpush("key", "value1", "value2")
assert.NotNil(t, err)
@ -379,9 +398,27 @@ func TestRedis_List(t *testing.T) {
assert.Nil(t, err)
assert.EqualValues(t, []string{"value2", "value3"}, vals)
})
})
t.Run("list error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Llen("key")
assert.Error(t, err)
_, err = client.Lpush("key", "value1", "value2")
assert.Error(t, err)
_, err = client.Lrem("key", 2, "value1")
assert.Error(t, err)
_, err = client.Rpush("key", "value3", "value4")
assert.Error(t, err)
})
})
}
func TestRedis_Mget(t *testing.T) {
t.Run("mget", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
err := client.Set("key1", "value1")
assert.Nil(t, err)
@ -393,19 +430,38 @@ func TestRedis_Mget(t *testing.T) {
assert.Nil(t, err)
assert.EqualValues(t, []string{"value1", "", "value2", ""}, vals)
})
})
t.Run("mget error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Mget("key1", "key0")
assert.Error(t, err)
})
})
}
func TestRedis_SetBit(t *testing.T) {
t.Run("setbit", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
_, err := New(client.Addr, badType()).SetBit("key", 1, 1)
assert.NotNil(t, err)
assert.Error(t, err)
val, err := client.SetBit("key", 1, 1)
assert.Nil(t, err)
if assert.NoError(t, err) {
assert.Equal(t, 0, val)
}
})
})
t.Run("setbit error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.SetBit("key", 1, 1)
assert.Error(t, err)
})
})
}
func TestRedis_GetBit(t *testing.T) {
t.Run("getbit", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
val, err := client.SetBit("key", 2, 1)
assert.Nil(t, err)
@ -416,6 +472,14 @@ func TestRedis_GetBit(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, v)
})
})
t.Run("getbit error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.GetBit("key", 2)
assert.Error(t, err)
})
})
}
func TestRedis_BitCount(t *testing.T) {
@ -631,6 +695,7 @@ func TestRedis_Sscan(t *testing.T) {
}
func TestRedis_Set(t *testing.T) {
t.Run("set", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
_, err := New(client.Addr, badType()).Sadd("key", 1, 2, 3, 4)
assert.NotNil(t, err)
@ -712,6 +777,26 @@ func TestRedis_Set(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 3, num)
})
})
t.Run("set with error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Sadd("key", 1, 2, 3, 4)
assert.Error(t, err)
_, err = client.Srem("key", 3, 4)
assert.Error(t, err)
_, err = client.Sunionstore("key3", "key1", "key2")
assert.Error(t, err)
_, err = client.Sdiffstore("key4", "key1", "key2")
assert.Error(t, err)
_, err = client.Sinterstore("key4", "key1", "key2")
assert.Error(t, err)
})
})
}
func TestRedis_GetSet(t *testing.T) {
@ -794,6 +879,7 @@ func TestRedis_SetExNx(t *testing.T) {
}
func TestRedis_SetGetDelHashField(t *testing.T) {
t.Run("hash", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
err := client.Hset("key", "field", "value")
assert.Nil(t, err)
@ -816,9 +902,30 @@ func TestRedis_SetGetDelHashField(t *testing.T) {
assert.Nil(t, err)
assert.False(t, ok)
})
})
t.Run("hash error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Hdel("key", "field")
assert.Error(t, err)
_, err = client.Hincrby("key", "field", 1)
assert.Error(t, err)
_, err = client.HincrbyFloat("key", "field", 1)
assert.Error(t, err)
_, err = client.Hlen("key")
assert.Error(t, err)
_, err = client.Hmget("key", "field")
assert.Error(t, err)
})
})
}
func TestRedis_SortedSet(t *testing.T) {
t.Run("sorted set", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
ok, err := client.ZaddFloat("key", 1, "value1")
assert.Nil(t, err)
@ -1016,6 +1123,74 @@ func TestRedis_SortedSet(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 2, ival)
})
})
t.Run("sorted set error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.ZaddFloat("key", 1, "value")
assert.Error(t, err)
_, err = client.Zadds("key")
assert.Error(t, err)
_, err = client.Zcard("key")
assert.Error(t, err)
_, err = client.Zcount("key", 1, 2)
assert.Error(t, err)
_, err = client.Zincrby("key", 1, "value")
assert.Error(t, err)
_, err = client.Zscore("key", "value")
assert.Error(t, err)
_, err = client.Zrem("key", "value")
assert.Error(t, err)
_, err = client.Zremrangebyscore("key", 1, 2)
assert.Error(t, err)
_, err = client.Zremrangebyrank("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrangeWithScores("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrangeWithScoresByFloat("key", 1, 2)
assert.Error(t, err)
_, err = client.ZRevRangeWithScores("key", 1, 2)
assert.Error(t, err)
_, err = client.ZRevRangeWithScoresByFloat("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrangebyscoreWithScores("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrangebyscoreWithScoresByFloat("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrangebyscoreWithScoresAndLimit("key", 1, 2, 1, 1)
assert.Error(t, err)
_, err = client.ZrangebyscoreWithScoresByFloatAndLimit("key", 1, 2, 1, 1)
assert.Error(t, err)
_, err = client.ZrevrangebyscoreWithScores("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrevrangebyscoreWithScoresByFloat("key", 1, 2)
assert.Error(t, err)
_, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 1, 2, 1, 1)
assert.Error(t, err)
_, err = client.ZrevrangebyscoreWithScoresByFloatAndLimit("key", 1, 2, 1, 1)
assert.Error(t, err)
})
})
}
func TestRedis_SortedSetByFloat64(t *testing.T) {
@ -1200,6 +1375,24 @@ func TestRedisEvalSha(t *testing.T) {
})
}
func TestRedis_Ttl(t *testing.T) {
t.Run("TTL", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
if assert.NoError(t, client.Set("key", "value")) {
_, err := client.Ttl("key")
assert.NoError(t, err)
}
})
})
t.Run("TTL error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.Ttl("key")
assert.Error(t, err)
})
})
}
func TestRedisToPairs(t *testing.T) {
pairs := toPairs([]red.Z{
{
@ -1293,13 +1486,15 @@ func TestRedisBlpop(t *testing.T) {
}
func TestRedisBlpopEx(t *testing.T) {
t.Run("blpopex", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
client.Ping()
var node mockedNode
_, _, err := client.BlpopEx(nil, "foo")
assert.NotNil(t, err)
assert.Error(t, err)
_, _, err = client.BlpopEx(node, "foo")
assert.NotNil(t, err)
assert.Error(t, err)
})
})
}
@ -1315,6 +1510,7 @@ func TestRedisBlpopWithTimeout(t *testing.T) {
}
func TestRedisGeo(t *testing.T) {
t.Run("geo", func(t *testing.T) {
runOnRedis(t, func(client *Redis) {
client.Ping()
geoLocation := []*GeoLocation{{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
@ -1361,6 +1557,34 @@ func TestRedisGeo(t *testing.T) {
assert.Equal(t, v6[0].Name, "Agrigento")
assert.Equal(t, v6[1].Name, "Palermo")
})
})
t.Run("geo error", func(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) {
_, err := client.GeoAdd("sicily", &GeoLocation{
Longitude: 13.3,
Latitude: 38.1,
Name: "Palermo",
})
assert.Error(t, err)
_, err = client.GeoDist("sicily", "Palermo", "Catania", "m")
assert.Error(t, err)
_, err = client.GeoRadius("sicily", 15, 37, &red.GeoRadiusQuery{
WithDist: true,
})
assert.Error(t, err)
_, err = client.GeoRadiusByMember("sicily", "Agrigento", &red.GeoRadiusQuery{
Unit: "km",
})
assert.Error(t, err)
_, err = client.GeoPos("sicily", "Palermo", "Catania")
assert.Error(t, err)
})
})
}
func TestSetSlowThreshold(t *testing.T) {
@ -1393,6 +1617,29 @@ func runOnRedis(t *testing.T, fn func(client *Redis)) {
_ = client.Close()
}
}()
fn(New(s.Addr()))
}
func runOnRedisWithError(t *testing.T, fn func(client *Redis)) {
logx.Disable()
s, err := miniredis.Run()
assert.NoError(t, err)
defer func() {
client, err := clientManager.GetResource(s.Addr(), func() (io.Closer, error) {
return nil, errors.New("should already exist")
})
if err != nil {
t.Error(err)
}
if client != nil {
_ = client.Close()
}
}()
s.SetError("mock error")
fn(New(s.Addr()))
}

Loading…
Cancel
Save