topic_video_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/service/bbq/topic/internal/model"
  6. "go-common/library/log"
  7. "math/rand"
  8. "testing"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestDaoInsertTopicVideo(t *testing.T) {
  12. convey.Convey("InsertTopicVideo", t, func(convCtx convey.C) {
  13. var (
  14. ctx = context.Background()
  15. svid = rand.Int63() % 1000000
  16. topicIDs = []int64{1}
  17. )
  18. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  19. rowsAffected, err := d.InsertTopicVideo(ctx, svid, topicIDs)
  20. convCtx.Convey("Then err should be nil.rowsAffected should not be nil.", func(convCtx convey.C) {
  21. convCtx.So(err, convey.ShouldBeNil)
  22. convCtx.So(rowsAffected, convey.ShouldEqual, 1)
  23. })
  24. })
  25. })
  26. }
  27. func TestDaoUpdateVideoScore(t *testing.T) {
  28. convey.Convey("UpdateVideoScore", t, func(convCtx convey.C) {
  29. var (
  30. ctx = context.Background()
  31. svid = int64(1)
  32. score = float64(1.0)
  33. )
  34. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  35. err := d.UpdateVideoScore(ctx, svid, score)
  36. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  37. convCtx.So(err, convey.ShouldBeNil)
  38. })
  39. })
  40. })
  41. }
  42. func TestDaoUpdateVideoState(t *testing.T) {
  43. convey.Convey("UpdateVideoState", t, func(convCtx convey.C) {
  44. var (
  45. ctx = context.Background()
  46. svid = int64(1)
  47. state = int32(1)
  48. )
  49. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  50. err := d.UpdateVideoState(ctx, svid, state)
  51. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  52. convCtx.So(err, convey.ShouldBeNil)
  53. })
  54. })
  55. })
  56. }
  57. func TestDaoListTopicVideos(t *testing.T) {
  58. convey.Convey("ListTopicVideos", t, func(convCtx convey.C) {
  59. var (
  60. ctx = context.Background()
  61. topicID = int64(1)
  62. )
  63. res, hasMore, err := d.ListTopicVideos(ctx, topicID, "", "", model.TopicVideoSize)
  64. log.V(1).Infow(ctx, "res", res)
  65. convCtx.Convey("Then err should be nil.res,hasMore should not be nil.", func(convCtx convey.C) {
  66. convCtx.So(err, convey.ShouldBeNil)
  67. convCtx.So(hasMore, convey.ShouldNotBeNil)
  68. convCtx.So(res, convey.ShouldNotBeNil)
  69. })
  70. originStickList, _ := d.GetStickTopicVideo(ctx, topicID)
  71. newStickList := []int64{1, 2, 3, 4, 5, 6}
  72. d.SetStickTopicVideo(ctx, topicID, newStickList)
  73. var data []byte
  74. convCtx.Convey("cursor_in_rank && direction_next", func(convCtx convey.C) {
  75. data, _ = json.Marshal(model.CursorValue{Offset: 0, StickRank: 2})
  76. res, hasMore, err = d.ListTopicVideos(ctx, topicID, "", string(data), model.TopicVideoSize)
  77. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  78. convCtx.So(res[0].Svid, convey.ShouldEqual, 3)
  79. convCtx.So(res[1].Svid, convey.ShouldEqual, 4)
  80. convCtx.So(res[2].Svid, convey.ShouldEqual, 5)
  81. convCtx.So(res[3].Svid, convey.ShouldEqual, 6)
  82. // 检验cursor值是否符合要求
  83. var unmarshalCursor model.CursorValue
  84. json.Unmarshal([]byte(res[3].CursorValue), &unmarshalCursor)
  85. convCtx.So(err, convey.ShouldBeNil)
  86. convCtx.So(unmarshalCursor.StickRank, convey.ShouldEqual, 6)
  87. convCtx.So(unmarshalCursor.Offset, convey.ShouldEqual, 0)
  88. json.Unmarshal([]byte(res[4].CursorValue), &unmarshalCursor)
  89. convCtx.So(unmarshalCursor.StickRank, convey.ShouldEqual, 0)
  90. convCtx.So(unmarshalCursor.Offset, convey.ShouldEqual, 1)
  91. convCtx.So(len(res), convey.ShouldEqual, model.TopicVideoSize+4)
  92. convCtx.So(hasMore, convey.ShouldBeTrue)
  93. convCtx.So(res, convey.ShouldNotBeNil)
  94. })
  95. convCtx.Convey("cursor_in_rank && direction_prev", func(convCtx convey.C) {
  96. data, _ = json.Marshal(model.CursorValue{Offset: 0, StickRank: 4})
  97. res, hasMore, err = d.ListTopicVideos(ctx, topicID, string(data), "", model.TopicVideoSize)
  98. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  99. convCtx.So(res[0].Svid, convey.ShouldEqual, 3)
  100. convCtx.So(res[1].Svid, convey.ShouldEqual, 2)
  101. convCtx.So(res[2].Svid, convey.ShouldEqual, 1)
  102. convCtx.So(len(res), convey.ShouldEqual, 3)
  103. convCtx.So(hasMore, convey.ShouldBeFalse)
  104. // 边缘情况,选择了rank=1的视频
  105. data, _ = json.Marshal(model.CursorValue{Offset: 0, StickRank: 1})
  106. res, hasMore, err = d.ListTopicVideos(ctx, topicID, string(data), "", model.TopicVideoSize)
  107. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  108. convCtx.So(len(res), convey.ShouldEqual, 0)
  109. convCtx.So(hasMore, convey.ShouldBeFalse)
  110. })
  111. convCtx.Convey("direction_prev", func(convCtx convey.C) {
  112. data, _ = json.Marshal(model.CursorValue{Offset: 1, StickRank: 0})
  113. res, hasMore, err = d.ListTopicVideos(ctx, topicID, string(data), "", model.TopicVideoSize)
  114. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  115. convCtx.So(len(res), convey.ShouldEqual, len(newStickList))
  116. convCtx.So(hasMore, convey.ShouldBeFalse)
  117. data, _ = json.Marshal(model.CursorValue{Offset: 5, StickRank: 0})
  118. res, hasMore, err = d.ListTopicVideos(ctx, topicID, string(data), "", model.TopicVideoSize)
  119. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  120. // 检验cursor值是否符合要求
  121. var unmarshalCursor model.CursorValue
  122. json.Unmarshal([]byte(res[0].CursorValue), &unmarshalCursor)
  123. convCtx.So(err, convey.ShouldBeNil)
  124. convCtx.So(unmarshalCursor.StickRank, convey.ShouldEqual, 0)
  125. convCtx.So(unmarshalCursor.Offset, convey.ShouldEqual, 4)
  126. json.Unmarshal([]byte(res[1].CursorValue), &unmarshalCursor)
  127. convCtx.So(unmarshalCursor.StickRank, convey.ShouldEqual, 0)
  128. convCtx.So(unmarshalCursor.Offset, convey.ShouldEqual, 3)
  129. })
  130. convCtx.Convey("direction_next", func(convCtx convey.C) {
  131. data, _ = json.Marshal(model.CursorValue{Offset: 1, StickRank: 0})
  132. res, hasMore, err = d.ListTopicVideos(ctx, topicID, "", string(data), model.TopicVideoSize)
  133. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  134. convCtx.So(len(res), convey.ShouldEqual, int(model.TopicVideoSize))
  135. convCtx.So(hasMore, convey.ShouldBeTrue)
  136. data, _ = json.Marshal(model.CursorValue{Offset: 5, StickRank: 0})
  137. res, hasMore, err = d.ListTopicVideos(ctx, topicID, "", string(data), model.TopicVideoSize)
  138. log.V(1).Infow(ctx, "res", res, "has_more", hasMore)
  139. // 检验cursor值是否符合要求
  140. var unmarshalCursor model.CursorValue
  141. json.Unmarshal([]byte(res[0].CursorValue), &unmarshalCursor)
  142. convCtx.So(err, convey.ShouldBeNil)
  143. convCtx.So(unmarshalCursor.StickRank, convey.ShouldEqual, 0)
  144. convCtx.So(unmarshalCursor.Offset, convey.ShouldEqual, 6)
  145. json.Unmarshal([]byte(res[1].CursorValue), &unmarshalCursor)
  146. convCtx.So(unmarshalCursor.StickRank, convey.ShouldEqual, 0)
  147. convCtx.So(unmarshalCursor.Offset, convey.ShouldEqual, 7)
  148. })
  149. // 恢复原来的置顶话题
  150. if len(originStickList) > 0 {
  151. d.SetStickTopicVideo(ctx, topicID, originStickList)
  152. }
  153. })
  154. }
  155. func TestDaogetStickTopicVideo(t *testing.T) {
  156. convey.Convey("GetStickTopicVideo", t, func(convCtx convey.C) {
  157. var (
  158. ctx = context.Background()
  159. topicID = int64(1)
  160. )
  161. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  162. list, err := d.GetStickTopicVideo(ctx, topicID)
  163. log.V(1).Infow(ctx, "list", list)
  164. convCtx.Convey("Then err should be nil.list should not be nil.", func(convCtx convey.C) {
  165. convCtx.So(err, convey.ShouldBeNil)
  166. convCtx.So(list, convey.ShouldNotBeNil)
  167. })
  168. })
  169. })
  170. }
  171. func TestDaoStickTopicVideo(t *testing.T) {
  172. convey.Convey("StickTopicVideo", t, func(convCtx convey.C) {
  173. var (
  174. ctx = context.Background()
  175. opTopicID = int64(1)
  176. opSvid = int64(1)
  177. op = int64(1)
  178. )
  179. originStickList, _ := d.GetStickTopicVideo(ctx, opTopicID)
  180. convCtx.Convey("common stick operate", func(convCtx convey.C) {
  181. err := d.StickTopicVideo(ctx, opTopicID, opSvid, op)
  182. newStickList, _ := d.GetStickTopicVideo(ctx, opTopicID)
  183. log.V(1).Infow(ctx, "new_stick_list", newStickList)
  184. convCtx.So(err, convey.ShouldBeNil)
  185. convCtx.So(newStickList[0], convey.ShouldEqual, 1)
  186. })
  187. convCtx.Convey("common cancel stick operate", func(convCtx convey.C) {
  188. err := d.StickTopicVideo(ctx, opTopicID, opSvid, 0)
  189. newCancelStickList, _ := d.GetStickTopicVideo(ctx, opTopicID)
  190. log.V(1).Infow(ctx, "new_cancel_stick_list", newCancelStickList)
  191. convCtx.So(err, convey.ShouldBeNil)
  192. convCtx.So(newCancelStickList[0], convey.ShouldNotEqual, 1)
  193. })
  194. convCtx.Convey("stick num test", func(convCtx convey.C) {
  195. for i := 1; i < model.MaxStickTopicVideoNum+3; i++ {
  196. d.StickTopicVideo(ctx, opTopicID, int64(i), 1)
  197. }
  198. list, _ := d.GetStickTopicVideo(ctx, opTopicID)
  199. log.V(1).Infow(ctx, "list", list)
  200. convCtx.So(len(list), convey.ShouldEqual, model.MaxStickTopicVideoNum)
  201. })
  202. // 恢复原来的置顶话题
  203. if len(originStickList) > 0 {
  204. d.SetStickTopicVideo(ctx, opTopicID, originStickList)
  205. }
  206. })
  207. }
  208. func TestDaoGetVideoTopic(t *testing.T) {
  209. convey.Convey("GetVideoTopic", t, func(convCtx convey.C) {
  210. var (
  211. ctx = context.Background()
  212. svid = int64(1547635456050324977)
  213. )
  214. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  215. res, err := d.GetVideoTopic(ctx, svid)
  216. data, _ := json.Marshal(res)
  217. log.V(1).Infow(ctx, "res", string(data))
  218. convCtx.Convey("Then err should be nil.list should not be nil.", func(convCtx convey.C) {
  219. convCtx.So(err, convey.ShouldBeNil)
  220. convCtx.So(res, convey.ShouldNotBeNil)
  221. })
  222. })
  223. })
  224. }