redis_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. "go-common/app/service/main/feed/model"
  9. feed "go-common/app/service/main/feed/model"
  10. xtime "go-common/library/time"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func Test_FeedValue(t *testing.T) {
  14. var (
  15. arc = api.Arc{Aid: 1}
  16. arc2 = api.Arc{Aid: 2}
  17. f feed.Feed
  18. )
  19. Convey("with fold avs", t, func() {
  20. f = feed.Feed{ID: 100, Fold: []*api.Arc{&arc, &arc2}}
  21. So(appFeedValue(&f), ShouldEqual, "0,100,1,2")
  22. })
  23. Convey("without fold avs", t, func() {
  24. f = feed.Feed{ID: 1}
  25. So(appFeedValue(&f), ShouldEqual, "0,1")
  26. })
  27. Convey("bangumi", t, func() {
  28. f = feed.Feed{ID: 100, Type: feed.BangumiType}
  29. So(appFeedValue(&f), ShouldEqual, "1,100")
  30. })
  31. }
  32. func Test_RecoverFeed(t *testing.T) {
  33. var (
  34. arc = api.Arc{Aid: 1}
  35. arc2 = api.Arc{Aid: 2}
  36. b = feed.Feed{ID: 100, Type: feed.BangumiType}
  37. f feed.Feed
  38. )
  39. Convey("bangumi", t, func() {
  40. r, err := recoverFeed("1,100")
  41. So(r, ShouldResemble, &b)
  42. So(err, ShouldBeNil)
  43. })
  44. Convey("with fold avs", t, func() {
  45. f = feed.Feed{ID: 100, Fold: []*api.Arc{&arc, &arc2}}
  46. r, err := recoverFeed("0,100,1,2")
  47. So(r, ShouldResemble, &f)
  48. So(err, ShouldBeNil)
  49. })
  50. Convey("without fold avs", t, func() {
  51. f = feed.Feed{ID: 100}
  52. r, err := recoverFeed("0,100")
  53. So(r, ShouldResemble, &f)
  54. So(err, ShouldBeNil)
  55. })
  56. }
  57. func Test_pingRedis(t *testing.T) {
  58. Convey("ping redis", t, func() {
  59. So(d.pingRedis(context.TODO()), ShouldBeNil)
  60. })
  61. }
  62. func Test_LastAccessCache(t *testing.T) {
  63. var (
  64. mid = int64(1)
  65. ts = int64(100)
  66. err error
  67. )
  68. Convey("add cache", t, func() {
  69. err = d.AddLastAccessCache(context.TODO(), model.TypeApp, mid, ts)
  70. So(err, ShouldBeNil)
  71. Convey("get cache", func() {
  72. t1, err := d.LastAccessCache(context.TODO(), model.TypeApp, mid)
  73. So(t1, ShouldEqual, t1)
  74. So(err, ShouldBeNil)
  75. })
  76. })
  77. }
  78. func Test_FeedCache(t *testing.T) {
  79. var (
  80. mid = int64(1)
  81. now = time.Now().Unix()
  82. err error
  83. a1 = api.Arc{Aid: 1, PubDate: xtime.Time(now)}
  84. a2 = api.Arc{Aid: 2, PubDate: xtime.Time(now - 1000)}
  85. a3 = api.Arc{Aid: 3}
  86. bangumi = feed.Bangumi{SeasonID: 100}
  87. f = feed.Feed{ID: 1, Archive: &a1, PubDate: a1.PubDate, Fold: []*api.Arc{&a3}}
  88. f1 = feed.Feed{ID: 2, Archive: &a2, PubDate: a2.PubDate}
  89. b = feed.Feed{ID: 100, Type: feed.BangumiType, Bangumi: &bangumi}
  90. feeds = []*feed.Feed{&f, &f1, &b}
  91. )
  92. Convey("add cache", t, func() {
  93. for name, client := range map[string]int{"app": model.TypeApp, "web": model.TypeWeb} {
  94. err = d.AddFeedCache(context.TODO(), client, mid, feeds)
  95. So(err, ShouldBeNil)
  96. Convey(name+"get cache", func() {
  97. res, bids, err := d.FeedCache(context.TODO(), client, mid, 0, 0)
  98. So(res, ShouldResemble, []*feed.Feed{{ID: f.ID, Fold: []*api.Arc{&a3}, PubDate: f.PubDate}})
  99. So(bids, ShouldBeEmpty)
  100. So(err, ShouldBeNil)
  101. })
  102. Convey(name+"get cache when end > length", func() {
  103. res, bids, err := d.FeedCache(context.TODO(), client, mid, 0, 10)
  104. So(res, ShouldResemble, []*feed.Feed{
  105. {ID: a1.Aid, Fold: []*api.Arc{&a3}, PubDate: a1.PubDate},
  106. {ID: a2.Aid, PubDate: a2.PubDate},
  107. {ID: 100, Type: feed.BangumiType},
  108. })
  109. So(bids, ShouldResemble, []int64{100})
  110. So(err, ShouldBeNil)
  111. })
  112. Convey(name+"expire cache", func() {
  113. ok, err := d.ExpireFeedCache(context.TODO(), client, mid)
  114. So(ok, ShouldEqual, true)
  115. So(err, ShouldBeNil)
  116. })
  117. Convey(name+"purge cache", func() {
  118. err := d.PurgeFeedCache(context.TODO(), client, mid)
  119. So(err, ShouldBeNil)
  120. })
  121. }
  122. })
  123. }
  124. func Test_UppersCache(t *testing.T) {
  125. var (
  126. mid = int64(1)
  127. mid2 = int64(2)
  128. now = time.Now().Unix()
  129. err error
  130. a1 = archive.AidPubTime{Aid: 1, PubDate: xtime.Time(now), Copyright: 1}
  131. a2 = archive.AidPubTime{Aid: 2, PubDate: xtime.Time(now - 1), Copyright: 0}
  132. a3 = archive.AidPubTime{Aid: 3, PubDate: xtime.Time(now - 2), Copyright: 0}
  133. )
  134. Convey("add cache", t, func() {
  135. err = d.AddUpperCaches(context.TODO(), map[int64][]*archive.AidPubTime{mid: {&a1, &a2}, mid2: {&a3}})
  136. So(err, ShouldBeNil)
  137. Convey("get cache", func() {
  138. _, err := d.UppersCaches(context.TODO(), []int64{mid, mid2}, 0, 2)
  139. So(err, ShouldBeNil)
  140. // So(res, ShouldResemble, map[int64][]*archive.AidPubTime{mid: {&a1, &a2}, mid2: {&a3}})
  141. })
  142. Convey("expire cache", func() {
  143. res, err := d.ExpireUppersCache(context.TODO(), []int64{mid})
  144. So(err, ShouldBeNil)
  145. So(res, ShouldResemble, map[int64]bool{mid: true})
  146. })
  147. Convey("get expired cache", func() {
  148. d.redisExpireUpper = 0
  149. res, err := d.ExpireUppersCache(context.TODO(), []int64{mid})
  150. So(err, ShouldBeNil)
  151. So(res, ShouldResemble, map[int64]bool{mid: false})
  152. _, err = d.UppersCaches(context.TODO(), []int64{mid}, 0, 2)
  153. So(err, ShouldBeNil)
  154. // So(nres, ShouldResemble, map[int64][]*archive.AidPubTime{mid: {&a1, &a2}})
  155. })
  156. Convey("purge cache", func() {
  157. err := d.DelUpperCache(context.TODO(), mid, a1.Aid)
  158. So(err, ShouldBeNil)
  159. })
  160. })
  161. }
  162. func Test_ArchiveFeedCache(t *testing.T) {
  163. var (
  164. mid = int64(1)
  165. now = time.Now().Unix()
  166. err error
  167. a1 = api.Arc{Aid: 1, PubDate: xtime.Time(now), Author: api.Author{Mid: mid}}
  168. a2 = api.Arc{Aid: 2, PubDate: xtime.Time(now - 1), Author: api.Author{Mid: mid}}
  169. a3 = api.Arc{Aid: 3, PubDate: xtime.Time(now - 2), Author: api.Author{Mid: mid}}
  170. f1 = feed.Feed{ID: a1.Aid, Archive: &a1, PubDate: a1.PubDate, Fold: []*api.Arc{&a3}}
  171. f2 = feed.Feed{ID: a2.Aid, Archive: &a2, PubDate: a2.PubDate}
  172. fs = []*feed.Feed{&f1, &f2}
  173. )
  174. Convey("add cache", t, func() {
  175. err = d.AddArchiveFeedCache(context.TODO(), mid, fs)
  176. So(err, ShouldBeNil)
  177. Convey("get cache", func() {
  178. as, err := d.ArchiveFeedCache(context.TODO(), mid, 0, 2)
  179. So(as, ShouldResemble, []*feed.Feed{
  180. {ID: a1.Aid, PubDate: a1.PubDate, Fold: []*api.Arc{{Aid: 3}}},
  181. {ID: a2.Aid, PubDate: a2.PubDate},
  182. })
  183. So(err, ShouldBeNil)
  184. })
  185. })
  186. }
  187. func Test_BangumiFeedCache(t *testing.T) {
  188. var (
  189. mid = int64(1)
  190. err error
  191. b1 = feed.Bangumi{SeasonID: 100}
  192. b2 = feed.Bangumi{SeasonID: 200}
  193. f1 = feed.Feed{ID: b1.SeasonID, Type: feed.BangumiType, Bangumi: &b1}
  194. f2 = feed.Feed{ID: b2.SeasonID, Type: feed.BangumiType, Bangumi: &b2}
  195. fs = []*feed.Feed{&f1, &f2}
  196. )
  197. Convey("add cache", t, func() {
  198. err = d.AddBangumiFeedCache(context.TODO(), mid, fs)
  199. So(err, ShouldBeNil)
  200. Convey("get cache", func() {
  201. res, err := d.BangumiFeedCache(context.TODO(), mid, 0, 2)
  202. So(res, ShouldResemble, []int64{b2.SeasonID, b1.SeasonID})
  203. So(err, ShouldBeNil)
  204. })
  205. })
  206. }
  207. func Test_UnreadCountCache(t *testing.T) {
  208. var (
  209. mid = int64(1)
  210. count = 100
  211. err error
  212. )
  213. Convey("add cache", t, func() {
  214. err = d.AddUnreadCountCache(context.TODO(), model.TypeApp, mid, count)
  215. So(err, ShouldBeNil)
  216. Convey("get cache", func() {
  217. c, err := d.UnreadCountCache(context.TODO(), model.TypeApp, mid)
  218. So(c, ShouldEqual, count)
  219. So(err, ShouldBeNil)
  220. })
  221. Convey("get wrong cache", func() {
  222. c, err := d.UnreadCountCache(context.TODO(), model.TypeWeb, mid)
  223. So(c, ShouldEqual, 0)
  224. So(err, ShouldBeNil)
  225. })
  226. })
  227. }