redis_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "go-common/library/cache/redis"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestPingRedis(t *testing.T) {
  10. Convey("TestPingRedis: ", t, func() {
  11. err := d.PingRedis(context.TODO())
  12. So(err, ShouldBeNil)
  13. })
  14. }
  15. func TestGetRedisVersionID(t *testing.T) {
  16. var (
  17. ver int64
  18. err error
  19. )
  20. ctx := context.TODO()
  21. conn := d.redis.Get(ctx)
  22. defer conn.Close()
  23. Convey("TestGetRedisVersionID: ", t, func() {
  24. if ver, _ = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1))); ver > 0 {
  25. _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
  26. }
  27. conn.Do("SET", fmt.Sprintf(_keyVersionID, 1), 123)
  28. ver, err = d.RedisVersionID(ctx, 1)
  29. So(ver, ShouldEqual, 123)
  30. So(err, ShouldBeNil)
  31. _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
  32. })
  33. }
  34. func TestSetnxRedisVersionID(t *testing.T) {
  35. var (
  36. ver int64
  37. err error
  38. )
  39. ctx := context.TODO()
  40. conn := d.redis.Get(ctx)
  41. defer conn.Close()
  42. Convey("TestSetnxRedisVersionID: ", t, func() {
  43. if ver, _ = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1))); ver > 0 {
  44. _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
  45. }
  46. err = d.SetnxRedisVersionID(ctx, 1, 146)
  47. So(err, ShouldBeNil)
  48. ver, _ = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
  49. So(ver, ShouldEqual, 146)
  50. _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
  51. })
  52. }
  53. func TestUpdateRedisVersionID(t *testing.T) {
  54. var (
  55. ver int64
  56. err error
  57. )
  58. ctx := context.TODO()
  59. conn := d.redis.Get(ctx)
  60. defer conn.Close()
  61. Convey("TestUpdateRedisVersionID: ", t, func() {
  62. if ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1))); err == nil {
  63. _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
  64. So(err, ShouldBeNil)
  65. } else {
  66. So(err, ShouldEqual, redis.ErrNil)
  67. }
  68. ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
  69. fmt.Println(ver)
  70. So(err, ShouldEqual, redis.ErrNil)
  71. err = d.UpdateRedisVersionID(ctx, 1, 123)
  72. So(err, ShouldBeNil)
  73. ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
  74. So(err, ShouldBeNil)
  75. So(ver, ShouldEqual, 123)
  76. err = d.UpdateRedisVersionID(ctx, 1, 132)
  77. So(err, ShouldBeNil)
  78. ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, 1)))
  79. So(ver, ShouldEqual, 132)
  80. So(err, ShouldBeNil)
  81. _, err = conn.Do("DEL", fmt.Sprintf(_keyVersionID, 1))
  82. So(err, ShouldBeNil)
  83. })
  84. }
  85. func TestEnd(t *testing.T) {
  86. d.Close()
  87. }