redis_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/app/interface/openplatform/article/model"
  7. xtime "go-common/library/time"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func Test_pingRedis(t *testing.T) {
  11. Convey("ping redis", t, WithDao(func(d *Dao) {
  12. So(d.pingRedis(context.TODO()), ShouldBeNil)
  13. }))
  14. }
  15. func Test_UppersCache(t *testing.T) {
  16. var (
  17. mid = int64(1)
  18. mid2 = int64(2)
  19. now = time.Now().Unix()
  20. err error
  21. a1 = model.Meta{ID: 1, PublishTime: xtime.Time(now), Author: &model.Author{Mid: mid}}
  22. a2 = model.Meta{ID: 2, PublishTime: xtime.Time(now - 1), Author: &model.Author{Mid: mid}}
  23. a3 = model.Meta{ID: 3, PublishTime: xtime.Time(now - 2), Author: &model.Author{Mid: mid2}}
  24. idsm = map[int64][][2]int64{
  25. mid: [][2]int64{[2]int64{a1.ID, int64(a1.PublishTime)}, [2]int64{a2.ID, int64(a2.PublishTime)}},
  26. mid2: [][2]int64{[2]int64{a3.ID, int64(a3.PublishTime)}},
  27. }
  28. )
  29. Convey("add cache", t, WithDao(func(d *Dao) {
  30. err = d.AddUpperCaches(context.TODO(), idsm)
  31. So(err, ShouldBeNil)
  32. Convey("get cache", func() {
  33. res, err := d.UppersCaches(context.TODO(), []int64{mid, mid2}, 0, 2)
  34. So(res, ShouldResemble, map[int64][]int64{mid: []int64{1, 2}, mid2: []int64{3}})
  35. So(err, ShouldBeNil)
  36. })
  37. Convey("purge cache", func() {
  38. err := d.DelUpperCache(context.TODO(), a1.Author.Mid, a1.ID)
  39. So(err, ShouldBeNil)
  40. })
  41. Convey("count cache", func() {
  42. res, err := d.UpperArtsCountCache(context.TODO(), a1.Author.Mid)
  43. So(err, ShouldBeNil)
  44. So(res, ShouldEqual, 2)
  45. })
  46. }))
  47. }
  48. func Test_RankCache(t *testing.T) {
  49. var (
  50. cid = int64(2)
  51. list = []*model.Rank{
  52. &model.Rank{
  53. Aid: 3,
  54. Score: 3,
  55. },
  56. &model.Rank{
  57. Aid: 2,
  58. Score: 2,
  59. },
  60. &model.Rank{
  61. Aid: 1,
  62. Score: 1,
  63. },
  64. }
  65. rank = model.RankResp{Note: "note", List: list}
  66. err error
  67. )
  68. Convey("add cache", t, WithDao(func(d *Dao) {
  69. err = d.AddRankCache(context.TODO(), cid, rank)
  70. So(err, ShouldBeNil)
  71. Convey("get cache", func() {
  72. res, err := d.RankCache(context.TODO(), cid)
  73. So(err, ShouldBeNil)
  74. So(res, ShouldResemble, rank)
  75. res, err = d.RankCache(context.TODO(), 1000)
  76. So(err, ShouldBeNil)
  77. So(res.List, ShouldBeEmpty)
  78. })
  79. Convey("expire cache", func() {
  80. res, err := d.ExpireRankCache(context.TODO(), cid)
  81. So(err, ShouldBeNil)
  82. So(res, ShouldEqual, true)
  83. })
  84. }))
  85. }
  86. func Test_HotspotCache(t *testing.T) {
  87. var (
  88. id = int64(1)
  89. c = context.TODO()
  90. arts = [][2]int64{[2]int64{0, -1}, [2]int64{1, 1}, [2]int64{2, 2}, [2]int64{3, 3}, [2]int64{4, 4}, [2]int64{5, 5}}
  91. )
  92. Convey("work", t, WithCleanCache(func() {
  93. ok, err := d.ExpireHotspotArtsCache(c, model.HotspotTypePtime, id)
  94. So(err, ShouldBeNil)
  95. So(ok, ShouldBeFalse)
  96. err = d.AddCacheHotspotArts(context.TODO(), model.HotspotTypePtime, id, arts, true)
  97. So(err, ShouldBeNil)
  98. ok, err = d.ExpireHotspotArtsCache(c, model.HotspotTypePtime, id)
  99. So(err, ShouldBeNil)
  100. So(ok, ShouldBeTrue)
  101. var num int64
  102. num, err = d.HotspotArtsCacheCount(c, model.HotspotTypePtime, id)
  103. So(err, ShouldBeNil)
  104. So(num, ShouldEqual, len(arts))
  105. Convey("get cache", func() {
  106. res, err := d.HotspotArtsCache(context.TODO(), model.HotspotTypePtime, id, 0, -1)
  107. So(err, ShouldBeNil)
  108. So(res, ShouldResemble, []int64{5, 4, 3, 2, 1, 0})
  109. })
  110. }))
  111. }