redis/command_test.go

88 lines
2.0 KiB
Go
Raw Normal View History

2015-01-15 18:51:22 +03:00
package redis_test
import (
"time"
2019-08-08 14:29:44 +03:00
"github.com/go-redis/redis/v7"
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() {
client = redis.NewClient(redisOptions())
2017-06-17 12:53:16 +03:00
Expect(client.FlushDB().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() {
set := client.Set("foo", "bar", 0)
Expect(set.String()).To(Equal("set foo bar: OK"))
2015-01-15 18:51:22 +03:00
get := client.Get("foo")
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() {
set := client.Set("key", "hello", 0)
2015-01-15 18:51:22 +03:00
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
get := client.Get("key")
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() {
set := client.Set("key", "10", 0)
2015-01-15 18:51:22 +03:00
Expect(set.Err()).NotTo(HaveOccurred())
n, err := client.Get("key").Int64()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(10)))
un, err := client.Get("key").Uint64()
Expect(err).NotTo(HaveOccurred())
Expect(un).To(Equal(uint64(10)))
f, err := client.Get("key").Float64()
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)
err := client.Set("float_key", f, 0).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get("float_key").Float32()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(f))
})
It("supports time.Time", func() {
tm := time.Date(2019, 01, 01, 0, 0, 0, 0, time.UTC)
err := client.Set("time_key", tm, 0).Err()
Expect(err).NotTo(HaveOccurred())
s, err := client.Get("time_key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal("2019-01-01T00:00:00Z"))
tm2, err := client.Get("time_key").Time()
Expect(err).NotTo(HaveOccurred())
Expect(tm2).To(BeTemporally("==", tm))
})
2015-01-15 18:51:22 +03:00
})