2015-01-15 18:51:22 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2020-02-03 13:18:37 +03:00
|
|
|
"errors"
|
2019-07-25 12:21:12 +03:00
|
|
|
"time"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
redis "github.com/go-redis/redis/v8"
|
2017-02-18 17:42:34 +03:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2016-04-12 19:41:56 +03:00
|
|
|
var _ = Describe("Cmd", func() {
|
2015-01-15 18:51:22 +03:00
|
|
|
var client *redis.Client
|
|
|
|
|
2016-03-14 13:36:18 +03:00
|
|
|
BeforeEach(func() {
|
2016-03-16 17:57:24 +03:00
|
|
|
client = redis.NewClient(redisOptions())
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2016-04-12 19:41:56 +03:00
|
|
|
It("implements Stringer", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
set := client.Set(ctx, "foo", "bar", 0)
|
2016-05-06 21:12:31 +03:00
|
|
|
Expect(set.String()).To(Equal("set foo bar: OK"))
|
2015-01-15 18:51:22 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
get := client.Get(ctx, "foo")
|
2016-05-06 21:12:31 +03:00
|
|
|
Expect(get.String()).To(Equal("get foo: bar"))
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
|
2016-04-12 19:41:56 +03:00
|
|
|
It("has val/err", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
set := client.Set(ctx, "key", "hello", 0)
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
get := client.Get(ctx, "key")
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(get.Val()).To(Equal("hello"))
|
|
|
|
|
|
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
})
|
|
|
|
|
2016-04-12 19:41:56 +03:00
|
|
|
It("has helpers", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
set := client.Set(ctx, "key", "10", 0)
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
n, err := client.Get(ctx, "key").Int64()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(10)))
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
un, err := client.Get(ctx, "key").Uint64()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(un).To(Equal(uint64(10)))
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
f, err := client.Get(ctx, "key").Float64()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(f).To(Equal(float64(10)))
|
|
|
|
})
|
|
|
|
|
2019-04-10 15:27:06 +03:00
|
|
|
It("supports float32", func() {
|
|
|
|
f := float32(66.97)
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := client.Set(ctx, "float_key", f, 0).Err()
|
2019-04-10 15:27:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
val, err := client.Get(ctx, "float_key").Float32()
|
2019-04-10 15:27:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(f))
|
|
|
|
})
|
2019-07-25 12:21:12 +03:00
|
|
|
|
|
|
|
It("supports time.Time", func() {
|
2020-03-12 18:33:50 +03:00
|
|
|
tm := time.Date(2019, 01, 01, 9, 45, 10, 222125, time.UTC)
|
2019-07-25 12:21:12 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := client.Set(ctx, "time_key", tm, 0).Err()
|
2019-07-25 12:21:12 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
s, err := client.Get(ctx, "time_key").Result()
|
2019-07-25 12:21:12 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-03-12 18:33:50 +03:00
|
|
|
Expect(s).To(Equal("2019-01-01T09:45:10.000222125Z"))
|
2019-07-25 12:21:12 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
tm2, err := client.Get(ctx, "time_key").Time()
|
2019-07-25 12:21:12 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(tm2).To(BeTemporally("==", tm))
|
|
|
|
})
|
2020-02-03 13:18:37 +03:00
|
|
|
|
|
|
|
It("allow to set custom error", func() {
|
|
|
|
e := errors.New("custom error")
|
|
|
|
cmd := redis.Cmd{}
|
|
|
|
cmd.SetErr(e)
|
|
|
|
_, err := cmd.Result()
|
|
|
|
Expect(err).To(Equal(e))
|
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|