article_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. xtime "go-common/library/time"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func Test_Article(t *testing.T) {
  10. Convey("get data", t, WithService(func(s *Service) {
  11. res, err := s.Article(context.TODO(), dataID)
  12. So(err, ShouldBeNil)
  13. So(res, ShouldNotBeNil)
  14. So(res, ShouldNotBeEmpty)
  15. }))
  16. Convey("no data", t, WithService(func(s *Service) {
  17. res, err := s.Article(context.TODO(), noDataID)
  18. So(err, ShouldBeNil)
  19. So(res, ShouldBeNil)
  20. }))
  21. Convey("ArticleRemainCount", t, WithService(func(s *Service) {
  22. _, err := s.ArticleRemainCount(context.TODO(), art.Author.Mid)
  23. So(err, ShouldBeNil)
  24. }))
  25. }
  26. func Test_ArticleMetas(t *testing.T) {
  27. Convey("get data", t, WithService(func(s *Service) {
  28. res, err := s.ArticleMetas(context.TODO(), []int64{dataID})
  29. So(err, ShouldBeNil)
  30. So(res, ShouldNotBeNil)
  31. So(res, ShouldNotBeEmpty)
  32. }))
  33. Convey("no data", t, WithService(func(s *Service) {
  34. res, err := s.ArticleMetas(context.TODO(), []int64{noDataID})
  35. So(err, ShouldBeNil)
  36. So(res, ShouldBeEmpty)
  37. }))
  38. }
  39. func Test_AddArticleCache(t *testing.T) {
  40. Convey("add data", t, WithService(func(s *Service) {
  41. var c = context.TODO()
  42. err := s.AddArticleCache(context.TODO(), dataID)
  43. So(err, ShouldBeNil)
  44. Convey("del cache", func() {
  45. err := s.DelArticleCache(c, 175, dataID)
  46. So(err, ShouldBeNil)
  47. Convey("delete twice return null", func() {
  48. err := s.DelArticleCache(c, 175, dataID)
  49. So(err, ShouldBeNil)
  50. })
  51. })
  52. }))
  53. }
  54. func Test_FilterNoDistributeArts(t *testing.T) {
  55. a1 := artmdl.Meta{ID: 1}
  56. a2 := artmdl.Meta{ID: 2}
  57. a3 := artmdl.Meta{ID: 3}
  58. a2.AttrSet(int32(1), artmdl.AttrBitNoDistribute)
  59. Convey("array work", t, WithService(func(s *Service) {
  60. res := filterNoDistributeArts([]*artmdl.Meta{&a1, &a2, &a3})
  61. So(res, ShouldResemble, []*artmdl.Meta{&a1, &a3})
  62. }))
  63. Convey("map work", t, WithService(func(s *Service) {
  64. arg := map[int64]*artmdl.Meta{1: &a1, 2: &a2, 3: &a3}
  65. res := map[int64]*artmdl.Meta{1: &a1, 3: &a3}
  66. filterNoDistributeArtsMap(arg)
  67. So(res, ShouldResemble, res)
  68. }))
  69. }
  70. func Test_fmtMoreArts(t *testing.T) {
  71. a1 := &artmdl.Meta{ID: 1, PublishTime: xtime.Time(1)}
  72. a2 := &artmdl.Meta{ID: 2, PublishTime: xtime.Time(2)}
  73. a3 := &artmdl.Meta{ID: 3, PublishTime: xtime.Time(3)}
  74. a4 := &artmdl.Meta{ID: 4, PublishTime: xtime.Time(4)}
  75. a5 := &artmdl.Meta{ID: 5, PublishTime: xtime.Time(5)}
  76. m := map[int64]*artmdl.Meta{1: a1, 2: a2, 3: a3, 4: a4, 5: a5}
  77. Convey("position: x5432", t, func() {
  78. res := fmtMoreArts([]int64{2, 3, 4, 5}, []int64{}, m)
  79. So(res, ShouldResemble, []*artmdl.Meta{a5, a4, a3, a2})
  80. })
  81. Convey("position: x32", t, func() {
  82. res := fmtMoreArts([]int64{2, 3}, []int64{}, m)
  83. So(res, ShouldResemble, []*artmdl.Meta{a3, a2})
  84. })
  85. Convey("position: 54x321", t, func() {
  86. res := fmtMoreArts([]int64{3, 2, 1}, []int64{5, 4}, m)
  87. So(res, ShouldResemble, []*artmdl.Meta{a4, a3, a2, a1})
  88. })
  89. Convey("position: 5432x1", t, func() {
  90. res := fmtMoreArts([]int64{1}, []int64{5, 4, 3, 2}, m)
  91. So(res, ShouldResemble, []*artmdl.Meta{a4, a3, a2, a1})
  92. })
  93. Convey("position: 4321x", t, func() {
  94. res := fmtMoreArts([]int64{}, []int64{4, 3, 2, 1}, m)
  95. So(res, ShouldResemble, []*artmdl.Meta{a4, a3, a2, a1})
  96. })
  97. Convey("position: 2x1", t, func() {
  98. res := fmtMoreArts([]int64{1}, []int64{2}, m)
  99. So(res, ShouldResemble, []*artmdl.Meta{a2, a1})
  100. })
  101. }
  102. func Test_splitAids(t *testing.T) {
  103. aids := []int64{4, 3, 2, 1}
  104. Convey("position: 4", t, func() {
  105. before, after := splitAids(aids, 4)
  106. So(after, ShouldResemble, []int64{})
  107. So(before, ShouldResemble, []int64{3, 2, 1})
  108. })
  109. Convey("position: 3", t, func() {
  110. before, after := splitAids(aids, 3)
  111. So(after, ShouldResemble, []int64{4})
  112. So(before, ShouldResemble, []int64{2, 1})
  113. })
  114. Convey("position: 2", t, func() {
  115. before, after := splitAids(aids, 2)
  116. So(after, ShouldResemble, []int64{4, 3})
  117. So(before, ShouldResemble, []int64{1})
  118. })
  119. Convey("position: 1", t, func() {
  120. before, after := splitAids(aids, 1)
  121. So(after, ShouldResemble, []int64{4, 3, 2})
  122. So(before, ShouldResemble, []int64{})
  123. })
  124. }