service_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "math/rand"
  6. "testing"
  7. "time"
  8. "go-common/app/job/main/figure-timer/conf"
  9. "go-common/app/job/main/figure-timer/dao/mock_dao"
  10. "go-common/app/job/main/figure-timer/model"
  11. "go-common/library/log"
  12. "github.com/golang/mock/gomock"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. var (
  16. s *Service
  17. c = context.TODO()
  18. )
  19. func init() {
  20. var err error
  21. flag.Set("conf", "../cmd/figure-timer-job-test.toml")
  22. if err = conf.Init(); err != nil {
  23. panic(err)
  24. }
  25. log.Init(conf.Conf.Log)
  26. s = New(conf.Conf)
  27. }
  28. func TestSplitMids(t *testing.T) {
  29. Convey("TEST split mids", t, func() {
  30. var (
  31. mids []int64
  32. concurrency int64 = 100
  33. midsSize int64 = 233
  34. )
  35. for i := int64(0); i < midsSize; i++ {
  36. mids = append(mids, i)
  37. }
  38. smids := splitMids(mids, concurrency)
  39. actualC := concurrency
  40. if actualC == 0 {
  41. actualC = 1
  42. }
  43. if actualC > midsSize {
  44. actualC = midsSize
  45. }
  46. So(len(smids), ShouldEqual, 233/3+1)
  47. total := 0
  48. for s := range smids {
  49. total += len(smids[s])
  50. }
  51. So(total, ShouldEqual, len(mids))
  52. })
  53. }
  54. func TestVersion(t *testing.T) {
  55. Convey("TEST week version", t, func() {
  56. var (
  57. ts = []time.Time{
  58. time.Date(2017, 9, 25, 0, 0, 0, 0, time.Local),
  59. time.Date(2017, 10, 1, 23, 59, 59, 99, time.Local),
  60. time.Date(2017, 9, 29, 12, 12, 12, 12, time.Local),
  61. }
  62. actual = time.Date(2017, 9, 25, 0, 0, 0, 0, time.Local)
  63. )
  64. for _, t := range ts {
  65. ver := weekVersion(t)
  66. So(ver, ShouldResemble, actual.Unix())
  67. }
  68. })
  69. }
  70. func TestCalcFigure(t *testing.T) {
  71. Convey("TEST calc offset", t, func() {
  72. var (
  73. max = 100.0
  74. k = 0.1
  75. x1 = 10.0
  76. x2 = 0.0
  77. )
  78. r := calcOffset(max, k, x1)
  79. So(r, ShouldEqual, 63)
  80. r = calcOffset(max, k, x2)
  81. So(r, ShouldEqual, 0)
  82. })
  83. Convey("TEST calc figure", t, func() {
  84. var (
  85. userInfo = &model.UserInfo{
  86. Mid: 233,
  87. Exp: 673,
  88. SpyScore: 100,
  89. ArchiveViews: 0,
  90. VIPStatus: 0,
  91. }
  92. actionCounters = []*model.ActionCounter{
  93. {
  94. Mid: 233,
  95. CoinCount: 0,
  96. ReplyCount: 0,
  97. CoinLowRisk: 0,
  98. CoinHighRisk: 0,
  99. ReplyLowRisk: 0,
  100. ReplyHighRisk: 0,
  101. ReplyLiked: 0,
  102. ReplyUnliked: 0,
  103. Version: time.Date(2017, 10, 2, 0, 0, 0, 0, time.Local),
  104. },
  105. }
  106. records = []*model.FigureRecord{
  107. {
  108. XPosCreativity: 0,
  109. XNegFriendly: 0,
  110. XPosFriendly: 0,
  111. XNegLawful: 80,
  112. Version: time.Date(2017, 9, 25, 0, 0, 0, 0, time.Local),
  113. },
  114. }
  115. weekVer = time.Date(2018, 1, 2, 0, 0, 0, 0, time.Local).Unix()
  116. )
  117. figure, newRecord := s.CalcFigure(c, userInfo, actionCounters, records, weekVer)
  118. So(figure, ShouldNotBeNil)
  119. So(newRecord, ShouldNotBeNil)
  120. So(figure.LawfulScore, ShouldEqual, 2859)
  121. So(newRecord.XNegLawful, ShouldEqual, 0)
  122. So(newRecord.XPosLawful, ShouldEqual, 0)
  123. })
  124. }
  125. func TestRank(t *testing.T) {
  126. rand.Seed(time.Now().Unix())
  127. rank.Init()
  128. for i := 0; i < 1; i++ {
  129. rank.AddScore(rand.Int31n(5000))
  130. }
  131. s.calcRank(c, time.Now().Unix())
  132. }
  133. func TestFix(t *testing.T) {
  134. ctrl := gomock.NewController(t)
  135. defer ctrl.Finish()
  136. mockDao := mock_dao.NewMockDaoInt(ctrl)
  137. mockDao.EXPECT().Figures(gomock.Any(), gomock.Any(), gomock.Any()).Return(
  138. []*model.Figure{
  139. {Mid: 1000},
  140. {Mid: 2000},
  141. },
  142. true,
  143. nil,
  144. ).AnyTimes()
  145. mockDao.EXPECT().CalcRecords(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(
  146. []*model.FigureRecord{
  147. {Version: time.Date(2018, 1, 2, 0, 0, 0, 0, time.Local)},
  148. },
  149. nil,
  150. ).AnyTimes()
  151. mockDao.EXPECT().ActionCounter(gomock.Any(), gomock.Any(), gomock.Any()).Return(
  152. &model.ActionCounter{
  153. PayLiveMoney: 2000,
  154. Version: time.Date(2018, 1, 2, 0, 0, 0, 0, time.Local),
  155. },
  156. nil,
  157. ).AnyTimes()
  158. mockDao.EXPECT().PutCalcRecord(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
  159. s.dao = mockDao
  160. Convey("TEST fix record", t, func() {
  161. s.fixproc()
  162. })
  163. }