...

Source file src/github.com/redis/go-redis/v9/command_test.go

Documentation: github.com/redis/go-redis/v9

     1  package redis_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/redis/go-redis/v9"
     8  
     9  	. "github.com/bsm/ginkgo/v2"
    10  	. "github.com/bsm/gomega"
    11  )
    12  
    13  var _ = Describe("Cmd", func() {
    14  	var client *redis.Client
    15  
    16  	BeforeEach(func() {
    17  		client = redis.NewClient(redisOptions())
    18  		Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
    19  	})
    20  
    21  	AfterEach(func() {
    22  		Expect(client.Close()).NotTo(HaveOccurred())
    23  	})
    24  
    25  	It("implements Stringer", func() {
    26  		set := client.Set(ctx, "foo", "bar", 0)
    27  		Expect(set.String()).To(Equal("set foo bar: OK"))
    28  
    29  		get := client.Get(ctx, "foo")
    30  		Expect(get.String()).To(Equal("get foo: bar"))
    31  	})
    32  
    33  	It("has val/err", func() {
    34  		set := client.Set(ctx, "key", "hello", 0)
    35  		Expect(set.Err()).NotTo(HaveOccurred())
    36  		Expect(set.Val()).To(Equal("OK"))
    37  
    38  		get := client.Get(ctx, "key")
    39  		Expect(get.Err()).NotTo(HaveOccurred())
    40  		Expect(get.Val()).To(Equal("hello"))
    41  
    42  		Expect(set.Err()).NotTo(HaveOccurred())
    43  		Expect(set.Val()).To(Equal("OK"))
    44  	})
    45  
    46  	It("has helpers", func() {
    47  		set := client.Set(ctx, "key", "10", 0)
    48  		Expect(set.Err()).NotTo(HaveOccurred())
    49  
    50  		n, err := client.Get(ctx, "key").Int64()
    51  		Expect(err).NotTo(HaveOccurred())
    52  		Expect(n).To(Equal(int64(10)))
    53  
    54  		un, err := client.Get(ctx, "key").Uint64()
    55  		Expect(err).NotTo(HaveOccurred())
    56  		Expect(un).To(Equal(uint64(10)))
    57  
    58  		f, err := client.Get(ctx, "key").Float64()
    59  		Expect(err).NotTo(HaveOccurred())
    60  		Expect(f).To(Equal(float64(10)))
    61  	})
    62  
    63  	It("supports float32", func() {
    64  		f := float32(66.97)
    65  
    66  		err := client.Set(ctx, "float_key", f, 0).Err()
    67  		Expect(err).NotTo(HaveOccurred())
    68  
    69  		val, err := client.Get(ctx, "float_key").Float32()
    70  		Expect(err).NotTo(HaveOccurred())
    71  		Expect(val).To(Equal(f))
    72  	})
    73  
    74  	It("supports time.Time", func() {
    75  		tm := time.Date(2019, 1, 1, 9, 45, 10, 222125, time.UTC)
    76  
    77  		err := client.Set(ctx, "time_key", tm, 0).Err()
    78  		Expect(err).NotTo(HaveOccurred())
    79  
    80  		s, err := client.Get(ctx, "time_key").Result()
    81  		Expect(err).NotTo(HaveOccurred())
    82  		Expect(s).To(Equal("2019-01-01T09:45:10.000222125Z"))
    83  
    84  		tm2, err := client.Get(ctx, "time_key").Time()
    85  		Expect(err).NotTo(HaveOccurred())
    86  		Expect(tm2).To(BeTemporally("==", tm))
    87  	})
    88  
    89  	It("allows to set custom error", func() {
    90  		e := errors.New("custom error")
    91  		cmd := redis.Cmd{}
    92  		cmd.SetErr(e)
    93  		_, err := cmd.Result()
    94  		Expect(err).To(Equal(e))
    95  	})
    96  })
    97  

View as plain text