chore: add more tests (#2792)

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests
master
Kevin Wan 2 years ago committed by GitHub
parent 2e57e91826
commit 02265d0bfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -40,6 +40,16 @@ func TestRedisConf(t *testing.T) {
}, },
ok: true, ok: true,
}, },
{
name: "ok",
RedisConf: RedisConf{
Host: "localhost:6379",
Type: ClusterType,
Pass: "pwd",
Tls: true,
},
ok: true,
},
} }
for _, test := range tests { for _, test := range tests {

@ -91,13 +91,16 @@ func TestHookProcessPipelineCase1(t *testing.T) {
log.SetOutput(&buf) log.SetOutput(&buf)
defer log.SetOutput(writer) defer log.SetOutput(writer)
_, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{})
assert.NoError(t, err)
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{ ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name()) assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{ assert.NoError(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{}))
assert.NoError(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
})) }))
assert.False(t, strings.Contains(buf.String(), "slow")) assert.False(t, strings.Contains(buf.String(), "slow"))

@ -1025,7 +1025,7 @@ func (s *Redis) IncrbyCtx(ctx context.Context, key string, increment int64) (val
return return
} }
// Incrby is the implementation of redis incrby command. // IncrbyFloat is the implementation of redis incrby command.
func (s *Redis) IncrbyFloat(key string, increment float64) (float64, error) { func (s *Redis) IncrbyFloat(key string, increment float64) (float64, error) {
return s.IncrbyFloatCtx(context.Background(), key, increment) return s.IncrbyFloatCtx(context.Background(), key, increment)
} }

@ -89,7 +89,9 @@ func TestRedis_Eval(t *testing.T) {
func TestRedis_GeoHash(t *testing.T) { func TestRedis_GeoHash(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
_, err := client.GeoHash("parent", "child1", "child2") _, err := client.GeoHash("parent", "child1", "child2")
assert.NotNil(t, err) assert.Error(t, err)
_, err = New(client.Addr, badType()).GeoHash("parent", "child1", "child2")
assert.Error(t, err)
}) })
} }
@ -286,9 +288,13 @@ func TestRedis_HyperLogLog(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
client.Ping() client.Ping()
r := New(client.Addr) r := New(client.Addr)
_, err := New(client.Addr, badType()).Pfadd("key1", "val1")
assert.Error(t, err)
ok, err := r.Pfadd("key1", "val1") ok, err := r.Pfadd("key1", "val1")
assert.Nil(t, err) assert.Nil(t, err)
assert.True(t, ok) assert.True(t, ok)
_, err = New(client.Addr, badType()).Pfcount("key1")
assert.Error(t, err)
val, err := r.Pfcount("key1") val, err := r.Pfcount("key1")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(1), val) assert.Equal(t, int64(1), val)
@ -298,6 +304,8 @@ func TestRedis_HyperLogLog(t *testing.T) {
val, err = r.Pfcount("key2") val, err = r.Pfcount("key2")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(1), val) assert.Equal(t, int64(1), val)
err = New(client.Addr, badType()).Pfmerge("key3", "key1", "key2")
assert.Error(t, err)
err = r.Pfmerge("key1", "key2") err = r.Pfmerge("key1", "key2")
assert.Nil(t, err) assert.Nil(t, err)
val, err = r.Pfcount("key1") val, err = r.Pfcount("key1")
@ -363,6 +371,8 @@ func TestRedis_List(t *testing.T) {
vals, err = client.Lrange("key", 0, 10) vals, err = client.Lrange("key", 0, 10)
assert.Nil(t, err) assert.Nil(t, err)
assert.EqualValues(t, []string{"value2", "value3", "value4"}, vals) assert.EqualValues(t, []string{"value2", "value3", "value4"}, vals)
err = New(client.Addr, badType()).Ltrim("key", 0, 1)
assert.Error(t, err)
err = client.Ltrim("key", 0, 1) err = client.Ltrim("key", 0, 1)
assert.Nil(t, err) assert.Nil(t, err)
vals, err = client.Lrange("key", 0, 10) vals, err = client.Lrange("key", 0, 10)
@ -1016,6 +1026,8 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
val, err := client.ZscoreByFloat("key", "value1") val, err := client.ZscoreByFloat("key", "value1")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 10.345, val) assert.Equal(t, 10.345, val)
_, err = New(client.Addr, badType()).ZscoreByFloat("key", "value1")
assert.Error(t, err)
_, _ = client.ZaddFloat("key", 10.346, "value2") _, _ = client.ZaddFloat("key", 10.346, "value2")
_, err = New(client.Addr, badType()).ZRevRangeWithScoresByFloat("key", 0, -1) _, err = New(client.Addr, badType()).ZRevRangeWithScoresByFloat("key", 0, -1)
assert.NotNil(t, err) assert.NotNil(t, err)
@ -1032,8 +1044,11 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
}, },
}, pairs) }, pairs)
_, err = New(client.Addr, badType()).ZrangeWithScoresByFloat("key", 0, -1)
assert.Error(t, err)
pairs, err = client.ZrangeWithScoresByFloat("key", 0, -1) pairs, err = client.ZrangeWithScoresByFloat("key", 0, -1)
assert.Nil(t, err) if assert.NoError(t, err) {
assert.EqualValues(t, []FloatPair{ assert.EqualValues(t, []FloatPair{
{ {
Key: "value1", Key: "value1",
@ -1044,6 +1059,8 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
Score: 10.346, Score: 10.346,
}, },
}, pairs) }, pairs)
}
_, err = New(client.Addr, badType()).ZrangebyscoreWithScoresByFloat("key", 0, 20) _, err = New(client.Addr, badType()).ZrangebyscoreWithScoresByFloat("key", 0, 20)
assert.NotNil(t, err) assert.NotNil(t, err)
pairs, err = client.ZrangebyscoreWithScoresByFloat("key", 0, 20) pairs, err = client.ZrangebyscoreWithScoresByFloat("key", 0, 20)
@ -1058,6 +1075,8 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
Score: 10.346, Score: 10.346,
}, },
}, pairs) }, pairs)
_, err = client.ZrangebyscoreWithScoresByFloatAndLimit("key", 10.1, 12.2, 1, 0)
assert.NoError(t, err)
_, err = New(client.Addr, badType()).ZrangebyscoreWithScoresByFloatAndLimit( _, err = New(client.Addr, badType()).ZrangebyscoreWithScoresByFloatAndLimit(
"key", 10.1, 12.2, 1, 1) "key", 10.1, 12.2, 1, 1)
assert.NotNil(t, err) assert.NotNil(t, err)
@ -1083,6 +1102,8 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
Score: 10.345, Score: 10.345,
}, },
}, pairs) }, pairs)
_, err = client.ZrevrangebyscoreWithScoresByFloatAndLimit("key", 10, 12, 1, 0)
assert.NoError(t, err)
_, err = New(client.Addr, badType()).ZrevrangebyscoreWithScoresByFloatAndLimit( _, err = New(client.Addr, badType()).ZrevrangebyscoreWithScoresByFloatAndLimit(
"key", 10, 12, 1, 1) "key", 10, 12, 1, 1)
assert.NotNil(t, err) assert.NotNil(t, err)
@ -1094,17 +1115,21 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
Score: 10.345, Score: 10.345,
}, },
}, pairs) }, pairs)
}) })
} }
func TestRedis_IncrbyFloat(t *testing.T) { func TestRedis_IncrbyFloat(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
incrVal, err := client.IncrbyFloat("key", 0.002) incrVal, err := client.IncrbyFloat("key", 0.002)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0.002, incrVal) assert.Equal(t, 0.002, incrVal)
_, err = New(client.Addr, badType()).IncrbyFloat("key", -0.001)
assert.Error(t, err)
incrVal2, err := client.IncrbyFloat("key", -0.001) incrVal2, err := client.IncrbyFloat("key", -0.001)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0.001, incrVal2) assert.Equal(t, 0.001, incrVal2)
_, err = New(client.Addr, badType()).HincrbyFloat("hkey", "i", 0.002)
assert.Error(t, err)
hincrVal, err := client.HincrbyFloat("hkey", "i", 0.002) hincrVal, err := client.HincrbyFloat("hkey", "i", 0.002)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0.002, hincrVal) assert.Equal(t, 0.002, hincrVal)
@ -1113,6 +1138,7 @@ func TestRedis_IncrbyFloat(t *testing.T) {
assert.Equal(t, 0.001, hincrVal2) assert.Equal(t, 0.001, hincrVal2)
}) })
} }
func TestRedis_Pipelined(t *testing.T) { func TestRedis_Pipelined(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
assert.NotNil(t, New(client.Addr, badType()).Pipelined(func(pipeliner Pipeliner) error { assert.NotNil(t, New(client.Addr, badType()).Pipelined(func(pipeliner Pipeliner) error {
@ -1166,6 +1192,8 @@ func TestRedisEvalSha(t *testing.T) {
client.Ping() client.Ping()
scriptHash, err := client.ScriptLoad(`return redis.call("EXISTS", KEYS[1])`) scriptHash, err := client.ScriptLoad(`return redis.call("EXISTS", KEYS[1])`)
assert.Nil(t, err) assert.Nil(t, err)
_, err = New(client.Addr, badType()).EvalSha(scriptHash, []string{"key1"})
assert.Error(t, err)
result, err := client.EvalSha(scriptHash, []string{"key1"}) result, err := client.EvalSha(scriptHash, []string{"key1"})
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(0), result) assert.Equal(t, int64(0), result)
@ -1195,6 +1223,29 @@ func TestRedisToPairs(t *testing.T) {
}, pairs) }, pairs)
} }
func TestRedisToFloatPairs(t *testing.T) {
pairs := toFloatPairs([]red.Z{
{
Member: 1,
Score: 1,
},
{
Member: 2,
Score: 2,
},
})
assert.EqualValues(t, []FloatPair{
{
Key: "1",
Score: 1,
},
{
Key: "2",
Score: 2,
},
}, pairs)
}
func TestRedis_Zscan(t *testing.T) { func TestRedis_Zscan(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
key := "key" key := "key"
@ -1266,29 +1317,46 @@ func TestRedisBlpopWithTimeout(t *testing.T) {
func TestRedisGeo(t *testing.T) { func TestRedisGeo(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
client.Ping() client.Ping()
geoLocation := []*GeoLocation{{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"}, {Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"}} geoLocation := []*GeoLocation{{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
{Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"}}
v, err := client.GeoAdd("sicily", geoLocation...) v, err := client.GeoAdd("sicily", geoLocation...)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(2), v) assert.Equal(t, int64(2), v)
_, err = New(client.Addr, badType()).GeoDist("sicily", "Palermo", "Catania", "m")
assert.Error(t, err)
v2, err := client.GeoDist("sicily", "Palermo", "Catania", "m") v2, err := client.GeoDist("sicily", "Palermo", "Catania", "m")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 166274, int(v2)) assert.Equal(t, 166274, int(v2))
// GeoHash not support // GeoHash not support
_, err = New(client.Addr, badType()).GeoPos("sicily", "Palermo", "Catania")
assert.Error(t, err)
v3, err := client.GeoPos("sicily", "Palermo", "Catania") v3, err := client.GeoPos("sicily", "Palermo", "Catania")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(v3[0].Longitude), int64(13)) assert.Equal(t, int64(v3[0].Longitude), int64(13))
assert.Equal(t, int64(v3[0].Latitude), int64(38)) assert.Equal(t, int64(v3[0].Latitude), int64(38))
assert.Equal(t, int64(v3[1].Longitude), int64(15)) assert.Equal(t, int64(v3[1].Longitude), int64(15))
assert.Equal(t, int64(v3[1].Latitude), int64(37)) assert.Equal(t, int64(v3[1].Latitude), int64(37))
v4, err := client.GeoRadius("sicily", 15, 37, &red.GeoRadiusQuery{WithDist: true, Unit: "km", Radius: 200}) _, err = New(client.Addr, badType()).GeoRadius("sicily", 15, 37,
&red.GeoRadiusQuery{WithDist: true, Unit: "km", Radius: 200})
assert.Error(t, err)
v4, err := client.GeoRadius("sicily", 15, 37, &red.GeoRadiusQuery{
WithDist: true,
Unit: "km", Radius: 200,
})
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(v4[0].Dist), int64(190)) assert.Equal(t, int64(v4[0].Dist), int64(190))
assert.Equal(t, int64(v4[1].Dist), int64(56)) assert.Equal(t, int64(v4[1].Dist), int64(56))
geoLocation2 := []*GeoLocation{{Longitude: 13.583333, Latitude: 37.316667, Name: "Agrigento"}} geoLocation2 := []*GeoLocation{{Longitude: 13.583333, Latitude: 37.316667, Name: "Agrigento"}}
_, err = New(client.Addr, badType()).GeoAdd("sicily", geoLocation2...)
assert.Error(t, err)
v5, err := client.GeoAdd("sicily", geoLocation2...) v5, err := client.GeoAdd("sicily", geoLocation2...)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, int64(1), v5) assert.Equal(t, int64(1), v5)
v6, err := client.GeoRadiusByMember("sicily", "Agrigento", &red.GeoRadiusQuery{Unit: "km", Radius: 100}) _, err = New(client.Addr, badType()).GeoRadiusByMember("sicily", "Agrigento",
&red.GeoRadiusQuery{Unit: "km", Radius: 100})
assert.Error(t, err)
v6, err := client.GeoRadiusByMember("sicily", "Agrigento",
&red.GeoRadiusQuery{Unit: "km", Radius: 100})
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, v6[0].Name, "Agrigento") assert.Equal(t, v6[0].Name, "Agrigento")
assert.Equal(t, v6[1].Name, "Palermo") assert.Equal(t, v6[1].Name, "Palermo")

Loading…
Cancel
Save