redis_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/job/main/reply-feed/model"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestDaokeyUV(t *testing.T) {
  9. convey.Convey("keyUV", t, func(ctx convey.C) {
  10. var (
  11. action = ""
  12. hour = 0
  13. slot = 0
  14. kind = ""
  15. )
  16. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  17. p1 := keyUV(action, hour, slot, kind)
  18. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  19. ctx.So(p1, convey.ShouldNotBeNil)
  20. })
  21. })
  22. })
  23. }
  24. func TestDaoUV(t *testing.T) {
  25. convey.Convey("addUV+countUV", t, func(ctx convey.C) {
  26. var (
  27. action = "test"
  28. hour = 0
  29. slot = 0
  30. kind = "test"
  31. mid = int64(0)
  32. err error
  33. counts []int64
  34. )
  35. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  36. err = d.AddUV(context.Background(), action, hour, slot, mid, kind)
  37. ctx.So(err, convey.ShouldBeNil)
  38. keys := []string{keyUV(action, hour, slot, kind)}
  39. counts, err = d.CountUV(context.Background(), keys)
  40. ctx.So(err, convey.ShouldBeNil)
  41. ctx.So(len(counts), convey.ShouldEqual, 1)
  42. })
  43. })
  44. }
  45. func TestDaokeyRefreshChecker(t *testing.T) {
  46. convey.Convey("keyRefreshChecker", t, func(ctx convey.C) {
  47. var (
  48. oid = int64(0)
  49. tp = int(0)
  50. )
  51. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  52. p1 := keyRefreshChecker(oid, tp)
  53. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  54. ctx.So(p1, convey.ShouldNotBeNil)
  55. })
  56. })
  57. })
  58. }
  59. func TestDaokeyReplyZSet(t *testing.T) {
  60. convey.Convey("keyReplyZSet", t, func(ctx convey.C) {
  61. var (
  62. name = ""
  63. oid = int64(0)
  64. tp = int(0)
  65. )
  66. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  67. p1 := keyReplyZSet(name, oid, tp)
  68. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  69. ctx.So(p1, convey.ShouldNotBeNil)
  70. })
  71. })
  72. })
  73. }
  74. func TestDaokeyReplySet(t *testing.T) {
  75. convey.Convey("keyReplySet", t, func(ctx convey.C) {
  76. var (
  77. oid = int64(0)
  78. tp = int(0)
  79. )
  80. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  81. p1 := keyReplySet(oid, tp)
  82. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  83. ctx.So(p1, convey.ShouldNotBeNil)
  84. })
  85. })
  86. })
  87. }
  88. func TestDaoPingRedis(t *testing.T) {
  89. convey.Convey("PingRedis", t, func(ctx convey.C) {
  90. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  91. err := d.PingRedis(context.Background())
  92. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  93. ctx.So(err, convey.ShouldBeNil)
  94. })
  95. })
  96. })
  97. }
  98. func TestDaoExpireCheckerRds(t *testing.T) {
  99. convey.Convey("ExpireCheckerRds", t, func(ctx convey.C) {
  100. var (
  101. oid = int64(0)
  102. tp = int(0)
  103. )
  104. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  105. ok, err := d.ExpireCheckerRds(context.Background(), oid, tp)
  106. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  107. ctx.So(err, convey.ShouldBeNil)
  108. ctx.So(ok, convey.ShouldNotBeNil)
  109. })
  110. })
  111. })
  112. }
  113. func TestDaoExpireReplyZSetRds(t *testing.T) {
  114. convey.Convey("ExpireReplyZSetRds", t, func(ctx convey.C) {
  115. var (
  116. name = ""
  117. oid = int64(0)
  118. tp = int(0)
  119. )
  120. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  121. ok, err := d.ExpireReplyZSetRds(context.Background(), name, oid, tp)
  122. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  123. ctx.So(err, convey.ShouldBeNil)
  124. ctx.So(ok, convey.ShouldNotBeNil)
  125. })
  126. })
  127. })
  128. }
  129. func TestDaoExpireReplySetRds(t *testing.T) {
  130. convey.Convey("ExpireReplySetRds", t, func(ctx convey.C) {
  131. var (
  132. oid = int64(0)
  133. tp = int(0)
  134. )
  135. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  136. ok, err := d.ExpireReplySetRds(context.Background(), oid, tp)
  137. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  138. ctx.So(err, convey.ShouldBeNil)
  139. ctx.So(ok, convey.ShouldNotBeNil)
  140. })
  141. })
  142. })
  143. }
  144. func TestDaoGetReplySetRds(t *testing.T) {
  145. convey.Convey("ReplySetRds", t, func(ctx convey.C) {
  146. var (
  147. oid = int64(-1)
  148. tp = int(-1)
  149. idMap = make(map[int64]struct{})
  150. )
  151. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  152. for i := 0; i < 10; i++ {
  153. if err := d.AddReplySetRds(context.Background(), oid, tp, int64(i)); err != nil {
  154. return
  155. }
  156. idMap[int64(i)] = struct{}{}
  157. }
  158. d.ExpireReplySetRds(context.Background(), oid, tp)
  159. rpIDs, err := d.ReplySetRds(context.Background(), oid, tp)
  160. ctx.Convey("Then err should be nil.rpIDs should not be nil.", func(ctx convey.C) {
  161. ctx.So(err, convey.ShouldBeNil)
  162. ctx.So(len(rpIDs), convey.ShouldEqual, 10)
  163. for _, rpID := range rpIDs {
  164. if _, ok := idMap[rpID]; !ok {
  165. t.Fatal("id not match")
  166. }
  167. }
  168. })
  169. })
  170. })
  171. }
  172. func TestDaoSetReplySetRds(t *testing.T) {
  173. convey.Convey("SetReplySetRds", t, func(ctx convey.C) {
  174. var (
  175. oid = int64(0)
  176. tp = int(0)
  177. rpIDs = []int64{}
  178. c = context.Background()
  179. )
  180. for i := 0; i < 10000; i++ {
  181. rpIDs = append(rpIDs, int64(i))
  182. }
  183. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  184. err := d.SetReplySetRds(c, oid, tp, rpIDs)
  185. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  186. ctx.So(err, convey.ShouldBeNil)
  187. })
  188. receivedRpIDs, err := d.ReplySetRds(c, oid, tp)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. ctx.So(len(receivedRpIDs), convey.ShouldEqual, len(rpIDs))
  193. })
  194. d.DelReplySetRds(c, oid, tp)
  195. })
  196. }
  197. func TestDaoRemReplySetRds(t *testing.T) {
  198. convey.Convey("RemReplySetRds", t, func(ctx convey.C) {
  199. var (
  200. oid = int64(0)
  201. rpID = int64(0)
  202. tp = int(0)
  203. )
  204. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  205. err := d.RemReplySetRds(context.Background(), oid, rpID, tp)
  206. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  207. ctx.So(err, convey.ShouldBeNil)
  208. })
  209. })
  210. })
  211. }
  212. func TestDaoDelReplySetRds(t *testing.T) {
  213. convey.Convey("DelReplySetRds", t, func(ctx convey.C) {
  214. var (
  215. oid = int64(0)
  216. tp = int(0)
  217. )
  218. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  219. err := d.DelReplySetRds(context.Background(), oid, tp)
  220. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  221. ctx.So(err, convey.ShouldBeNil)
  222. })
  223. })
  224. })
  225. }
  226. func TestDaoAddReplySetRds(t *testing.T) {
  227. convey.Convey("AddReplySetRds", t, func(ctx convey.C) {
  228. var (
  229. oid = int64(0)
  230. tp = int(0)
  231. rpID = int64(0)
  232. )
  233. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  234. err := d.AddReplySetRds(context.Background(), oid, tp, rpID)
  235. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  236. ctx.So(err, convey.ShouldBeNil)
  237. })
  238. })
  239. })
  240. }
  241. func TestDaoGetReplyZSetRds(t *testing.T) {
  242. convey.Convey("ReplyZSetRds", t, func(ctx convey.C) {
  243. var (
  244. name = ""
  245. oid = int64(0)
  246. tp = int(0)
  247. start = int(0)
  248. end = int(0)
  249. )
  250. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  251. rpIDs, err := d.ReplyZSetRds(context.Background(), name, oid, tp, start, end)
  252. ctx.Convey("Then err should be nil.rpIDs should not be nil.", func(ctx convey.C) {
  253. ctx.So(err, convey.ShouldBeNil)
  254. ctx.So(rpIDs, convey.ShouldBeNil)
  255. })
  256. })
  257. })
  258. }
  259. func TestDaoSetReplyZSetRds(t *testing.T) {
  260. convey.Convey("SetReplyZSetRds", t, func(ctx convey.C) {
  261. var (
  262. name = "test"
  263. oid = int64(0)
  264. tp = int(0)
  265. c = context.Background()
  266. rs = []*model.ReplyScore{}
  267. )
  268. for i := 0; i < 10000; i++ {
  269. rs = append(rs, &model.ReplyScore{RpID: int64(i), Score: float64(i)})
  270. }
  271. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  272. err := d.SetReplyZSetRds(c, name, oid, tp, rs)
  273. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  274. ctx.So(err, convey.ShouldBeNil)
  275. })
  276. rpIDs, err := d.ReplyZSetRds(c, name, oid, tp, 0, -1)
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. ctx.So(len(rpIDs), convey.ShouldEqual, len(rs))
  281. })
  282. d.DelReplyZSetRds(c, []string{name}, oid, tp)
  283. })
  284. }
  285. func TestDaoAddReplyZSetRds(t *testing.T) {
  286. convey.Convey("AddReplyZSetRds", t, func(ctx convey.C) {
  287. var (
  288. name = ""
  289. oid = int64(0)
  290. tp = int(0)
  291. rs = &model.ReplyScore{}
  292. )
  293. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  294. err := d.AddReplyZSetRds(context.Background(), name, oid, tp, rs)
  295. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  296. ctx.So(err, convey.ShouldBeNil)
  297. })
  298. })
  299. })
  300. }
  301. func TestDaoRemReplyZSetRds(t *testing.T) {
  302. convey.Convey("RemReplyZSetRds", t, func(ctx convey.C) {
  303. var (
  304. name = ""
  305. oid = int64(0)
  306. tp = int(0)
  307. rpID = int64(0)
  308. )
  309. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  310. err := d.RemReplyZSetRds(context.Background(), name, oid, tp, rpID)
  311. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  312. ctx.So(err, convey.ShouldBeNil)
  313. })
  314. })
  315. })
  316. }
  317. func TestDaoDelReplyZSetRds(t *testing.T) {
  318. convey.Convey("DelReplyZSetRds", t, func(ctx convey.C) {
  319. var (
  320. names = []string{}
  321. oid = int64(0)
  322. tp = int(0)
  323. )
  324. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  325. err := d.DelReplyZSetRds(context.Background(), names, oid, tp)
  326. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  327. ctx.So(err, convey.ShouldBeNil)
  328. })
  329. })
  330. })
  331. }
  332. func TestDaoGetCheckerTsRds(t *testing.T) {
  333. convey.Convey("CheckerTsRds", t, func(ctx convey.C) {
  334. var (
  335. oid = int64(-1)
  336. tp = int(0)
  337. )
  338. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  339. ts, err := d.CheckerTsRds(context.Background(), oid, tp)
  340. ctx.Convey("Then err should be nil.ts should not be nil.", func(ctx convey.C) {
  341. ctx.So(err, convey.ShouldBeNil)
  342. ctx.So(ts, convey.ShouldEqual, 0)
  343. })
  344. })
  345. })
  346. }
  347. func TestDaoSetCheckerTsRds(t *testing.T) {
  348. convey.Convey("SetCheckerTsRds", t, func(ctx convey.C) {
  349. var (
  350. oid = int64(0)
  351. tp = int(0)
  352. )
  353. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  354. err := d.SetCheckerTsRds(context.Background(), oid, tp)
  355. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  356. ctx.So(err, convey.ShouldBeNil)
  357. })
  358. })
  359. })
  360. }
  361. func TestDaoRangeReplyZSetRds(t *testing.T) {
  362. convey.Convey("RangeReplyZSetRds", t, func(ctx convey.C) {
  363. var (
  364. oid = int64(0)
  365. tp = int(0)
  366. name = ""
  367. )
  368. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  369. rpIDs, err := d.RangeReplyZSetRds(context.Background(), name, oid, tp, 0, 0)
  370. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  371. ctx.So(err, convey.ShouldBeNil)
  372. ctx.So(len(rpIDs), convey.ShouldEqual, 0)
  373. })
  374. })
  375. })
  376. }