123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package dao
- import (
- "context"
- "fmt"
- "testing"
- "go-common/library/cache/redis"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestPingRedis(t *testing.T) {
- Convey("TestPingRedis: ", t, func() {
- err := d.PingRedis(context.TODO())
- So(err, ShouldBeNil)
- })
- }
- func TestGetRedisVersionID(t *testing.T) {
- var (
- ver int64
- err error
- )
- ctx := context.TODO()
- conn := d.redis.Get(ctx)
- defer conn.Close()
- Convey("TestGetRedisVersionID: ", t, func() {
- if ver, _ = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1))); ver > 0 {
- _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
- }
- conn.Do("SET", fmt.Sprintf(_keyVersionID, 1), 123)
- ver, err = d.RedisVersionID(ctx, 1)
- So(ver, ShouldEqual, 123)
- So(err, ShouldBeNil)
- _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
- })
- }
- func TestSetnxRedisVersionID(t *testing.T) {
- var (
- ver int64
- err error
- )
- ctx := context.TODO()
- conn := d.redis.Get(ctx)
- defer conn.Close()
- Convey("TestSetnxRedisVersionID: ", t, func() {
- if ver, _ = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1))); ver > 0 {
- _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
- }
- err = d.SetnxRedisVersionID(ctx, 1, 146)
- So(err, ShouldBeNil)
- ver, _ = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
- So(ver, ShouldEqual, 146)
- _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
- })
- }
- func TestUpdateRedisVersionID(t *testing.T) {
- var (
- ver int64
- err error
- )
- ctx := context.TODO()
- conn := d.redis.Get(ctx)
- defer conn.Close()
- Convey("TestUpdateRedisVersionID: ", t, func() {
- if ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1))); err == nil {
- _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
- So(err, ShouldBeNil)
- } else {
- So(err, ShouldEqual, redis.ErrNil)
- }
- ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
- fmt.Println(ver)
- So(err, ShouldEqual, redis.ErrNil)
- err = d.UpdateRedisVersionID(ctx, 1, 123)
- So(err, ShouldBeNil)
- ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
- So(err, ShouldBeNil)
- So(ver, ShouldEqual, 123)
- err = d.UpdateRedisVersionID(ctx, 1, 132)
- So(err, ShouldBeNil)
- ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
- So(ver, ShouldEqual, 132)
- So(err, ShouldBeNil)
- _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
- So(err, ShouldBeNil)
- })
- }
- func TestEnd(t *testing.T) {
- d.Close()
- }
|