memcache_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/service/main/identify/model"
  6. "go-common/library/cache/memcache"
  7. "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestDaoSetAccessCache(t *testing.T) {
  10. var (
  11. c = context.Background()
  12. key = "123"
  13. res = &model.IdentifyInfo{Mid: 1, Csrf: "csrf", Expires: 1577811661}
  14. )
  15. convey.Convey("SetAccessCache", t, func(ctx convey.C) {
  16. d.SetAccessCache(c, key, res)
  17. ctx.Convey("No return values", func(ctx convey.C) {
  18. })
  19. })
  20. }
  21. func TestDaoAccessCache(t *testing.T) {
  22. var (
  23. c = context.Background()
  24. hitKey = "123"
  25. missKey = "miss"
  26. )
  27. convey.Convey("AccessCache", t, func(ctx convey.C) {
  28. res, err := d.AccessCache(c, hitKey)
  29. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  30. ctx.So(err, convey.ShouldBeNil)
  31. ctx.So(res, convey.ShouldNotBeNil)
  32. ctx.So(res.Mid, convey.ShouldEqual, 1)
  33. })
  34. })
  35. convey.Convey("AccessCache miss", t, func(ctx convey.C) {
  36. res, err := d.AccessCache(c, missKey)
  37. ctx.Convey("Then err and res should be nil.", func(ctx convey.C) {
  38. ctx.So(err, convey.ShouldBeNil)
  39. ctx.So(res, convey.ShouldBeNil)
  40. })
  41. })
  42. }
  43. func TestDaoDelCache(t *testing.T) {
  44. var (
  45. c = context.Background()
  46. hitKey = "123"
  47. missKey = "miss"
  48. )
  49. convey.Convey("DelCache", t, func(ctx convey.C) {
  50. err := d.DelCache(c, hitKey)
  51. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  52. ctx.So(err, convey.ShouldBeNil)
  53. })
  54. })
  55. convey.Convey("DelCache miss", t, func(ctx convey.C) {
  56. err := d.DelCache(c, missKey)
  57. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  58. ctx.So(err, convey.ShouldBeNil)
  59. })
  60. })
  61. }
  62. func TestDaocacheKey(t *testing.T) {
  63. var (
  64. key = "1"
  65. )
  66. convey.Convey("cacheKey", t, func(ctx convey.C) {
  67. p1 := cacheKey(key)
  68. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  69. ctx.So(p1, convey.ShouldNotBeNil)
  70. })
  71. })
  72. }
  73. func TestDaologinCacheKey(t *testing.T) {
  74. var (
  75. mid = int64(1)
  76. ip = "127.0.0.1"
  77. )
  78. convey.Convey("loginCacheKey", t, func(ctx convey.C) {
  79. p1 := loginCacheKey(mid, ip)
  80. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  81. ctx.So(p1, convey.ShouldEqual, "l1127.0.0.1")
  82. })
  83. })
  84. }
  85. func TestDao_SetLoginCache(t *testing.T) {
  86. var (
  87. c = context.Background()
  88. mid = int64(1)
  89. ip = "127.0.0.1"
  90. expires = int32(1577811661)
  91. )
  92. convey.Convey("SetLoginCache", t, func(ctx convey.C) {
  93. err := d.SetLoginCache(c, mid, ip, expires)
  94. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  95. ctx.So(err, convey.ShouldBeIn, nil, memcache.ErrNotStored)
  96. })
  97. })
  98. }
  99. func TestDao_ExistMIDAndIP(t *testing.T) {
  100. var (
  101. c = context.Background()
  102. mid = int64(1)
  103. hitIP = "127.0.0.1"
  104. missIP = "127.0.0.2"
  105. )
  106. convey.Convey("IsExistMID", t, func(ctx convey.C) {
  107. ok, err := d.ExistMIDAndIP(c, mid, hitIP)
  108. ctx.Convey("Then err should be nil.ok should be true.", func(ctx convey.C) {
  109. ctx.So(err, convey.ShouldBeNil)
  110. ctx.So(ok, convey.ShouldEqual, true)
  111. })
  112. })
  113. convey.Convey("IsExistMID miss", t, func(ctx convey.C) {
  114. ok, err := d.ExistMIDAndIP(c, mid, missIP)
  115. ctx.Convey("Then err should be nil and ok should be false.", func(ctx convey.C) {
  116. ctx.So(err, convey.ShouldBeNil)
  117. ctx.So(ok, convey.ShouldEqual, false)
  118. })
  119. })
  120. }