recommends_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/app/interface/openplatform/article/model"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. var (
  10. r1 = &model.Recommend{ArticleID: 1, RecImageURL: "xx", RecImageStartTime: 0, RecImageEndTime: 1998603966, Rec: true, RecFlag: true}
  11. r2 = &model.Recommend{ArticleID: 2, RecImageURL: "xx", RecImageStartTime: 0, RecImageEndTime: 1398603966, Rec: true}
  12. r3 = &model.Recommend{ArticleID: 3, Rec: true}
  13. r4 = &model.Recommend{ArticleID: 4, Rec: true}
  14. rs = [][]*model.Recommend{
  15. []*model.Recommend{r1},
  16. []*model.Recommend{r2},
  17. []*model.Recommend{r3},
  18. []*model.Recommend{r4},
  19. }
  20. cid = int64(4)
  21. recommendAids = map[int64][]int64{
  22. 0: []int64{r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID},
  23. cid: []int64{r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID},
  24. }
  25. )
  26. func Test_Recommends_1(t *testing.T) {
  27. Convey("get data from page 1", t, WithCleanCache(func() {
  28. s.setting.ShowRecommendNewArticles = true
  29. //s.updateNewArts(context.TODO(), cid)
  30. s.RecommendsMap = map[int64][][]*model.Recommend{cid: rs}
  31. res, err := s.Recommends(context.TODO(), cid, 1, 3, []int64{}, model.FieldDefault)
  32. So(err, ShouldBeNil)
  33. So(len(res), ShouldEqual, 3)
  34. // 不改变原始值
  35. So(r1, ShouldResemble, &model.Recommend{ArticleID: 1, RecImageURL: "xx", RecImageStartTime: 0, RecImageEndTime: 1998603966, Rec: true, RecFlag: true})
  36. So(len(s.RecommendsMap[cid]), ShouldEqual, 4)
  37. So(res[0].Recommend, ShouldResemble, model.Recommend{ArticleID: 0, RecImageURL: "xx", RecImageStartTime: 0, RecImageEndTime: 1998603966, Rec: true, RecFlag: true, RecText: "编辑推荐"})
  38. So(res[0].ID, ShouldEqual, 1)
  39. So(res[1].Recommend, ShouldResemble, model.Recommend{ArticleID: 0, RecImageURL: "", RecImageStartTime: 0, RecImageEndTime: 1398603966, Rec: true, RecText: ""})
  40. So(res[1].ID, ShouldEqual, 2)
  41. So(res[2].ID, ShouldEqual, 3)
  42. }))
  43. Convey("get data from page 1 with aids", t, WithCleanCache(func() {
  44. s.setting.ShowRecommendNewArticles = true
  45. //s.updateNewArts(context.TODO(), cid)
  46. s.RecommendsMap = map[int64][][]*model.Recommend{cid: rs}
  47. res, err := s.Recommends(context.TODO(), cid, 1, 2, []int64{1, 2}, model.FieldDefault)
  48. So(err, ShouldBeNil)
  49. So(len(res), ShouldEqual, 2)
  50. So(res[0].ID, ShouldEqual, 3)
  51. So(res[1].ID, ShouldEqual, 4)
  52. }))
  53. }
  54. func Test_Recommends_2(t *testing.T) {
  55. Convey("get data from page 2", t, WithCleanCache(func() {
  56. //s.updateNewArts(context.TODO(), cid)
  57. s.RecommendsMap = map[int64][][]*model.Recommend{cid: rs}
  58. s.recommendAids = recommendAids
  59. Convey("show new art", func() {
  60. s.setting.ShowRecommendNewArticles = true
  61. res, err := s.Recommends(context.TODO(), cid, 2, 3, []int64{}, model.FieldDefault)
  62. So(err, ShouldBeNil)
  63. So(len(res), ShouldEqual, 3)
  64. So(res[0].ID, ShouldEqual, 4)
  65. So(res[1].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  66. So(res[2].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  67. })
  68. }))
  69. }
  70. func Test_Recommends_recomend_category(t *testing.T) {
  71. Convey("get data from page 1", t, WithCleanCache(func() {
  72. //s.updateNewArts(context.TODO(), 0)
  73. rss := [][]*model.Recommend{[]*model.Recommend{r1, r2, r3, r4}}
  74. s.RecommendsMap = map[int64][][]*model.Recommend{0: rss}
  75. s.recommendAids = recommendAids
  76. Convey("show new art", func() {
  77. s.setting.ShowRecommendNewArticles = true
  78. res, err := s.Recommends(context.TODO(), 0, 2, 3, []int64{}, model.FieldDefault)
  79. So(err, ShouldBeNil)
  80. So(len(res), ShouldEqual, 3)
  81. So(res[0].ID, ShouldBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  82. So(res[1].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  83. So(res[2].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  84. })
  85. Convey("hide new art", func() {
  86. s.setting.ShowRecommendNewArticles = false
  87. res, err := s.Recommends(context.TODO(), 0, 2, 3, []int64{}, model.FieldDefault)
  88. So(err, ShouldBeNil)
  89. So(len(res), ShouldEqual, 1)
  90. So(res[0].ID, ShouldBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  91. })
  92. }))
  93. }
  94. func Test_Recommends_3(t *testing.T) {
  95. Convey("get data from page 3", t, WithCleanCache(func() {
  96. //s.updateNewArts(context.TODO(), cid)
  97. s.RecommendsMap = map[int64][][]*model.Recommend{cid: rs}
  98. s.recommendAids = recommendAids
  99. Convey("show new art", func() {
  100. s.setting.ShowRecommendNewArticles = true
  101. res, err := s.Recommends(context.TODO(), cid, 3, 3, []int64{}, model.FieldDefault)
  102. So(err, ShouldBeNil)
  103. So(len(res), ShouldEqual, 3)
  104. So(res[0].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  105. So(res[1].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  106. So(res[2].ID, ShouldNotBeIn, r1.ArticleID, r2.ArticleID, r3.ArticleID, r4.ArticleID)
  107. })
  108. }))
  109. Convey("other category no data", t, WithCleanCache(func() {
  110. //s.updateNewArts(context.TODO(), cid)
  111. s.RecommendsMap = map[int64][][]*model.Recommend{cid: rs}
  112. res, err := s.Recommends(context.TODO(), 100, 1, 10, []int64{}, model.FieldDefault)
  113. So(err, ShouldNotBeNil)
  114. So(res, ShouldBeNil)
  115. }))
  116. }
  117. func Test_CalculateRecommends(t *testing.T) {
  118. r10 := &model.Recommend{ArticleID: 1, Position: 2, EndTime: 1}
  119. r12 := &model.Recommend{ArticleID: 2, Position: 2, EndTime: 2}
  120. r13 := &model.Recommend{ArticleID: 2, Position: 2, EndTime: 0}
  121. r14 := &model.Recommend{ArticleID: 2, Position: 2, EndTime: 0}
  122. r20 := &model.Recommend{ArticleID: 1, Position: 1, EndTime: 1}
  123. Convey("diffrent position", t, func() {
  124. res := calculateRecommends([]*model.Recommend{r20, r10})
  125. exp := [][]*model.Recommend{[]*model.Recommend{r10}, []*model.Recommend{r20}}
  126. So(res, ShouldResemble, exp)
  127. })
  128. Convey("same position", t, func() {
  129. res := calculateRecommends([]*model.Recommend{r12, r10})
  130. exp1 := [][]*model.Recommend{[]*model.Recommend{r10, r12}}
  131. exp2 := [][]*model.Recommend{[]*model.Recommend{r12, r10}}
  132. So(res, ShouldBeIn, exp1, exp2)
  133. })
  134. Convey("one no endtime", t, func() {
  135. res := calculateRecommends([]*model.Recommend{r13, r10, r20})
  136. exp := [][]*model.Recommend{[]*model.Recommend{r10}, []*model.Recommend{r20}}
  137. So(res, ShouldResemble, exp)
  138. })
  139. Convey("all no endtime", t, func() {
  140. res := calculateRecommends([]*model.Recommend{r13, r14})
  141. exp1 := [][]*model.Recommend{[]*model.Recommend{r13, r14}}
  142. exp2 := [][]*model.Recommend{[]*model.Recommend{r14, r13}}
  143. So(res, ShouldBeIn, exp1, exp2)
  144. })
  145. Convey("no endtime and have endtime ", t, func() {
  146. res := calculateRecommends([]*model.Recommend{r13, r14, r12})
  147. exp := [][]*model.Recommend{[]*model.Recommend{r12}}
  148. So(res, ShouldResemble, exp)
  149. })
  150. }
  151. func Test_DelRecommendArt(t *testing.T) {
  152. Convey("del recommend", t, WithService(func(s *Service) {
  153. s.RecommendsMap = map[int64][][]*model.Recommend{0: rs}
  154. So(s.RecommendsMap, ShouldNotBeNil)
  155. So(len(s.RecommendsMap[0]), ShouldEqual, 4)
  156. s.DelRecommendArt(0, 1)
  157. time.Sleep(50 * time.Millisecond)
  158. So(s.RecommendsMap[0][0][0], ShouldResemble, r2)
  159. }))
  160. }
  161. func Test_genRecommendArtFromPool(t *testing.T) {
  162. Convey("should generate arts", t, WithService(func(s *Service) {
  163. res := s.genRecommendArtFromPool([][]*model.Recommend{[]*model.Recommend{r1, r2, r3, r4}}, s.c.Article.RecommendRegionLen)
  164. So(len(res), ShouldEqual, 4)
  165. }))
  166. }
  167. func Test_sortRecs(t *testing.T) {
  168. Convey("should sort recommends by ptime", t, WithCleanCache(func() {
  169. a1 := &model.RecommendArt{Meta: model.Meta{ID: 1, PublishTime: 1}}
  170. a1.Rec = true
  171. a2 := &model.RecommendArt{Meta: model.Meta{ID: 2, PublishTime: 2}}
  172. a2.Rec = true
  173. a3 := &model.RecommendArt{Meta: model.Meta{ID: 3, PublishTime: 3}}
  174. a3.Rec = true
  175. res := []*model.RecommendArt{a1, a3, a2}
  176. sortRecs(res)
  177. So(res, ShouldResemble, []*model.RecommendArt{a3, a2, a1})
  178. }))
  179. }
  180. func Test_skyHorseGray(t *testing.T) {
  181. Convey("mid", t, func() {
  182. s.c.Article.SkyHorseGray = []int64{}
  183. s.c.Article.SkyHorseGrayUsers = []int64{123}
  184. So(s.skyHorseGray("1", 123), ShouldBeTrue)
  185. So(s.skyHorseGray("", 12), ShouldBeFalse)
  186. So(s.skyHorseGray("", 0), ShouldBeFalse)
  187. So(s.skyHorseGray("1", 0), ShouldBeFalse)
  188. })
  189. Convey("gray", t, func() {
  190. s.c.Article.SkyHorseGray = []int64{3}
  191. s.c.Article.SkyHorseGrayUsers = []int64{}
  192. So(s.skyHorseGray("1", 123), ShouldBeTrue)
  193. So(s.skyHorseGray("", 3), ShouldBeTrue)
  194. So(s.skyHorseGray("", 5), ShouldBeFalse)
  195. So(s.skyHorseGray("", 0), ShouldBeFalse)
  196. So(s.skyHorseGray("1", 0), ShouldBeFalse)
  197. })
  198. }