topic_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/bbq/topic/api"
  6. "go-common/app/service/bbq/topic/internal/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "math/rand"
  10. "testing"
  11. "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestDaoRawTopicInfo(t *testing.T) {
  14. convey.Convey("RawTopicInfo", t, func(convCtx convey.C) {
  15. var (
  16. ctx = context.Background()
  17. topicIDs = []int64{1}
  18. )
  19. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  20. res, err := d.RawTopicInfo(ctx, topicIDs)
  21. log.Infow(ctx, "topics", res)
  22. convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
  23. convCtx.So(err, convey.ShouldBeNil)
  24. convCtx.So(res, convey.ShouldNotBeNil)
  25. })
  26. })
  27. })
  28. }
  29. func TestDaoCacheTopicInfo(t *testing.T) {
  30. convey.Convey("CacheTopicInfo", t, func(convCtx convey.C) {
  31. var (
  32. ctx = context.Background()
  33. topicIDs = []int64{1}
  34. )
  35. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  36. res, err := d.CacheTopicInfo(ctx, topicIDs)
  37. log.Infow(ctx, "res", res)
  38. convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
  39. convCtx.So(err, convey.ShouldBeNil)
  40. convCtx.So(res, convey.ShouldNotBeNil)
  41. })
  42. })
  43. })
  44. }
  45. func TestDaoAddCacheTopicInfo(t *testing.T) {
  46. convey.Convey("AddCacheTopicInfo", t, func(convCtx convey.C) {
  47. var (
  48. ctx = context.Background()
  49. topicInfos map[int64]*api.TopicInfo
  50. )
  51. topicInfos = make(map[int64]*api.TopicInfo)
  52. topicInfos[1] = &api.TopicInfo{TopicId: 1, Name: "Test", State: 0, Desc: "test for tester"}
  53. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  54. err := d.AddCacheTopicInfo(ctx, topicInfos)
  55. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  56. convCtx.So(err, convey.ShouldBeNil)
  57. })
  58. })
  59. })
  60. }
  61. func TestDaoDelCacheTopicInfo(t *testing.T) {
  62. convey.Convey("DelCacheTopicInfo", t, func(convCtx convey.C) {
  63. var (
  64. ctx = context.Background()
  65. topicID = int64(1)
  66. )
  67. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  68. d.DelCacheTopicInfo(ctx, topicID)
  69. res, _ := d.CacheTopicInfo(ctx, []int64{topicID})
  70. topicInfo := res[topicID]
  71. convCtx.Convey("No return values", func(convCtx convey.C) {
  72. convCtx.So(topicInfo, convey.ShouldBeNil)
  73. })
  74. })
  75. })
  76. }
  77. func TestDaoInsertTopics(t *testing.T) {
  78. convey.Convey("InsertTopics", t, func(convCtx convey.C) {
  79. var (
  80. ctx = context.Background()
  81. topics map[string]*api.TopicInfo
  82. )
  83. topics = make(map[string]*api.TopicInfo)
  84. //topicName := fmt.Sprintf("test_%d", rand.Int()%10000000)
  85. topicName := "Test"
  86. topics[topicName] = &api.TopicInfo{Name: topicName, Score: float64(rand.Int()%10000) / float64(10000)}
  87. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  88. _, err := d.InsertTopics(ctx, topics)
  89. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  90. convCtx.So(err, convey.ShouldBeNil)
  91. })
  92. })
  93. log.Infow(ctx, "log", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:"+topicName)
  94. topicName = fmt.Sprintf("test_%d", rand.Int()%10000000)
  95. topics[topicName] = &api.TopicInfo{Name: topicName, Score: float64(rand.Int()%10000) / float64(10000)}
  96. topicName = fmt.Sprintf("test_%d", rand.Int()%10000000)
  97. topics[topicName] = &api.TopicInfo{Name: topicName, Score: float64(rand.Int()%10000) / float64(10000)}
  98. convCtx.Convey("multi insert", func(convCtx convey.C) {
  99. _, err := d.InsertTopics(ctx, topics)
  100. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  101. convCtx.So(err, convey.ShouldBeNil)
  102. })
  103. })
  104. longTopics := make(map[string]*api.TopicInfo)
  105. longName := "test_toolonggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
  106. longTopics[longName] = &api.TopicInfo{Name: longName}
  107. convCtx.Convey("error case", func(convCtx convey.C) {
  108. _, duplicateErr := d.InsertTopics(ctx, longTopics)
  109. convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
  110. convCtx.So(duplicateErr, convey.ShouldAlmostEqual, ecode.TopicNameLenErr)
  111. })
  112. })
  113. })
  114. }
  115. func TestDaoTopicID(t *testing.T) {
  116. convey.Convey("TopicID", t, func(convCtx convey.C) {
  117. var (
  118. ctx = context.Background()
  119. names = []string{"Test"}
  120. )
  121. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  122. topics, err := d.TopicID(ctx, names)
  123. log.Infow(ctx, "names", names, "topics", topics)
  124. convCtx.Convey("Then err should be nil.topics should not be nil.", func(convCtx convey.C) {
  125. convCtx.So(err, convey.ShouldBeNil)
  126. convCtx.So(topics, convey.ShouldNotBeNil)
  127. })
  128. })
  129. })
  130. }
  131. func TestDaoUpdateTopic(t *testing.T) {
  132. convey.Convey("TopicID", t, func(convCtx convey.C) {
  133. convCtx.Convey("update desc", func(convCtx convey.C) {
  134. var (
  135. ctx = context.Background()
  136. topicID = int64(1)
  137. field = "desc"
  138. value = "update_desc"
  139. )
  140. origin, _ := d.TopicInfo(ctx, []int64{topicID})
  141. originTopic := origin[topicID]
  142. err := d.UpdateTopic(ctx, topicID, field, value)
  143. curr, _ := d.TopicInfo(ctx, []int64{topicID})
  144. currTopic := curr[topicID]
  145. d.UpdateTopic(ctx, topicID, field, originTopic.Desc)
  146. convCtx.Convey("Then err should be nil.topics should not be nil.", func(convCtx convey.C) {
  147. convCtx.So(err, convey.ShouldBeNil)
  148. convCtx.So(currTopic.Desc, convey.ShouldEqual, value)
  149. })
  150. })
  151. convCtx.Convey("update state", func(convCtx convey.C) {
  152. var (
  153. ctx = context.Background()
  154. topicID = int64(1)
  155. field = "state"
  156. value = model.TopicStateUnavailable
  157. )
  158. err := d.UpdateTopic(ctx, topicID, field, value)
  159. curr, _ := d.TopicInfo(ctx, []int64{topicID})
  160. currTopic := curr[topicID]
  161. d.UpdateTopic(ctx, topicID, field, model.TopicStateAvailable)
  162. convCtx.Convey("Then err should be nil.topics should not be nil.", func(convCtx convey.C) {
  163. convCtx.So(err, convey.ShouldBeNil)
  164. convCtx.So(currTopic.State, convey.ShouldEqual, value)
  165. })
  166. })
  167. })
  168. }
  169. func TestDaoListUnAvailableTopics(t *testing.T) {
  170. convey.Convey("ListUnAvailableTopics", t, func(convCtx convey.C) {
  171. var (
  172. ctx = context.Background()
  173. page = int32(1)
  174. )
  175. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  176. list, hasMore, err := d.ListUnAvailableTopics(ctx, page, model.CmsTopicSize)
  177. log.Infow(ctx, "list", list, "has_more", hasMore)
  178. convCtx.Convey("Then err should be nil.list,hasMore should not be nil.", func(convCtx convey.C) {
  179. convCtx.So(err, convey.ShouldBeNil)
  180. convCtx.So(list, convey.ShouldNotBeNil)
  181. })
  182. })
  183. })
  184. }
  185. func TestDaoListRankTopics(t *testing.T) {
  186. convey.Convey("ListRankTopics", t, func(convCtx convey.C) {
  187. var (
  188. ctx = context.Background()
  189. page = int32(1)
  190. )
  191. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  192. list, hasMore, err := d.ListRankTopics(ctx, page, model.DiscoveryTopicSize)
  193. log.Infow(ctx, "list", list, "topics", hasMore)
  194. convCtx.Convey("Then err should be nil.list,hasMore should not be nil.", func(convCtx convey.C) {
  195. convCtx.So(err, convey.ShouldBeNil)
  196. convCtx.So(hasMore, convey.ShouldBeTrue)
  197. convCtx.So(list, convey.ShouldNotBeNil)
  198. })
  199. })
  200. convCtx.Convey("stick test", func(convCtx convey.C) {
  201. originStickList, _ := d.GetStickTopic(ctx)
  202. d.setStickTopic(ctx, []int64{111111110, 111111111, 111111112})
  203. list, hasMore, err := d.ListRankTopics(ctx, 1, model.DiscoveryTopicSize)
  204. log.Infow(ctx, "list", list, "has_more", hasMore)
  205. convCtx.So(err, convey.ShouldBeNil)
  206. convCtx.So(list[0], convey.ShouldEqual, 111111110)
  207. convCtx.So(list[1], convey.ShouldEqual, 111111111)
  208. convCtx.So(list[2], convey.ShouldEqual, 111111112)
  209. convCtx.So(len(list), convey.ShouldEqual, 3+model.DiscoveryTopicSize)
  210. // 恢复原来的置顶话题
  211. if len(originStickList) > 0 {
  212. d.setStickTopic(ctx, originStickList)
  213. }
  214. })
  215. })
  216. }
  217. func TestDaogetStickTopic(t *testing.T) {
  218. convey.Convey("GetStickTopic", t, func(convCtx convey.C) {
  219. var (
  220. ctx = context.Background()
  221. )
  222. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  223. list, err := d.GetStickTopic(ctx)
  224. convCtx.Convey("Then err should be nil.list should not be nil.", func(convCtx convey.C) {
  225. convCtx.So(err, convey.ShouldBeNil)
  226. convCtx.So(list, convey.ShouldNotBeNil)
  227. })
  228. })
  229. })
  230. }
  231. func TestDaoStickTopic(t *testing.T) {
  232. convey.Convey("StickTopic", t, func(convCtx convey.C) {
  233. var (
  234. ctx = context.Background()
  235. opTopicID = int64(1)
  236. op = int64(1)
  237. )
  238. originStickList, _ := d.GetStickTopic(ctx)
  239. convCtx.Convey("common stick operate", func(convCtx convey.C) {
  240. err := d.StickTopic(ctx, opTopicID, op)
  241. newStickList, _ := d.GetStickTopic(ctx)
  242. log.V(1).Infow(ctx, "new_stick_list", newStickList)
  243. convCtx.So(err, convey.ShouldBeNil)
  244. convCtx.So(newStickList[0], convey.ShouldEqual, 1)
  245. })
  246. convCtx.Convey("common cancel stick operate", func(convCtx convey.C) {
  247. err := d.StickTopic(ctx, opTopicID, 0)
  248. newCancelStickList, _ := d.GetStickTopic(ctx)
  249. log.V(1).Infow(ctx, "new_cancel_stick_list", newCancelStickList)
  250. convCtx.So(err, convey.ShouldBeNil)
  251. convCtx.So(newCancelStickList[0], convey.ShouldNotEqual, 1)
  252. })
  253. convCtx.Convey("stick num test", func(convCtx convey.C) {
  254. for i := 1; i < model.MaxStickTopicNum+3; i++ {
  255. d.StickTopic(ctx, int64(i), 1)
  256. }
  257. list, _ := d.GetStickTopic(ctx)
  258. log.V(1).Infow(ctx, "list", list)
  259. convCtx.So(len(list), convey.ShouldEqual, model.MaxStickTopicNum)
  260. })
  261. // 恢复原来的置顶话题
  262. if len(originStickList) > 0 {
  263. d.setStickTopic(ctx, originStickList)
  264. }
  265. })
  266. }