dao_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package dao
  2. import (
  3. "context"
  4. // "encoding/binary"
  5. "flag"
  6. "testing"
  7. "time"
  8. "go-common/app/job/main/figure-timer/conf"
  9. "go-common/app/job/main/figure-timer/model"
  10. // "go-common/library/log"
  11. "github.com/pkg/errors"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. var (
  15. dao *Dao
  16. ctx = context.TODO()
  17. )
  18. func init() {
  19. var err error
  20. flag.Set("conf", "../cmd/figure-timer-job-test.toml")
  21. if err = conf.Init(); err != nil {
  22. panic(err)
  23. }
  24. dao = New(conf.Conf)
  25. }
  26. func TestInit(t *testing.T) {
  27. Convey("TEST init", t, func() {
  28. var err error
  29. So(dao.c, ShouldNotBeNil)
  30. So(dao.mysql, ShouldNotBeNil)
  31. So(dao.hbase, ShouldNotBeNil)
  32. So(dao.redis, ShouldNotBeNil)
  33. err = dao.Ping(ctx)
  34. So(err, ShouldBeNil)
  35. })
  36. }
  37. func TestMysql(t *testing.T) {
  38. Convey("TEST figure", t, func() {
  39. var (
  40. figure = &model.Figure{
  41. Mid: 100,
  42. Score: 100,
  43. LawfulScore: 50,
  44. WideScore: 0,
  45. FriendlyScore: 23,
  46. BountyScore: 250,
  47. CreativityScore: 0,
  48. Ver: 0,
  49. }
  50. figure2 *model.Figure
  51. figureRank = &model.Rank{
  52. ScoreFrom: 2000,
  53. ScoreTo: 3000,
  54. Percentage: 50,
  55. Ver: 233,
  56. }
  57. err error
  58. )
  59. figure.ID, err = dao.UpsertFigure(ctx, figure)
  60. So(err, ShouldBeNil)
  61. figure2, err = dao.Figure(ctx, figure.Mid)
  62. So(err, ShouldBeNil)
  63. So(figure2, ShouldNotBeNil)
  64. So(figure2.Ctime, ShouldNotBeEmpty)
  65. So(figure2.Ctime, ShouldHappenBefore, time.Now().Add(time.Second))
  66. So(figure2.Mtime, ShouldNotBeEmpty)
  67. So(figure2.Mtime, ShouldHappenBefore, time.Now().Add(time.Second))
  68. _, err = dao.InsertRankHistory(ctx, figureRank)
  69. So(err, ShouldBeNil)
  70. _, err = dao.UpsertRank(ctx, figureRank)
  71. So(err, ShouldBeNil)
  72. })
  73. Convey("TEST figures", t, func() {
  74. for shard := 0; shard < 100; shard++ {
  75. var (
  76. end bool
  77. fromMid = int64(shard)
  78. figures []*model.Figure
  79. err error
  80. )
  81. for !end {
  82. figures, end, err = dao.Figures(ctx, fromMid, 100)
  83. So(err, ShouldBeNil)
  84. for _, f := range figures {
  85. So(f.Mid, ShouldBeGreaterThan, 0)
  86. if fromMid < f.Mid {
  87. fromMid = f.Mid
  88. }
  89. }
  90. }
  91. }
  92. })
  93. }
  94. func TestRedis(t *testing.T) {
  95. Convey("TEST figure", t, func() {
  96. var (
  97. figure = &model.Figure{
  98. Mid: 23,
  99. Score: 100,
  100. LawfulScore: 50,
  101. WideScore: 0,
  102. FriendlyScore: 23,
  103. BountyScore: 250,
  104. CreativityScore: 0,
  105. Ver: 0,
  106. }
  107. figure2 *model.Figure
  108. err error
  109. )
  110. err = dao.SetFigureCache(ctx, figure)
  111. So(err, ShouldBeNil)
  112. figure2, err = dao.FigureCache(ctx, figure.Mid)
  113. So(err, ShouldBeNil)
  114. So(figure2, ShouldNotBeNil)
  115. So(figure, ShouldResemble, figure2)
  116. })
  117. Convey("TEST pending mids", t, func() {
  118. var (
  119. mid int64 = 23
  120. ver int64
  121. shard = mid % dao.c.Property.PendingMidShard
  122. mids []int64
  123. err error
  124. )
  125. err = dao.setPendingMidCache(ctx, mid, ver)
  126. So(err, ShouldBeNil)
  127. mids, err = dao.PendingMidsCache(ctx, ver, shard)
  128. So(err, ShouldBeNil)
  129. So(mids, ShouldNotBeEmpty)
  130. })
  131. }
  132. func (d *Dao) setPendingMidCache(c context.Context, mid int64, ver int64) (err error) {
  133. var (
  134. key = keyPendingMids(ver, mid%d.c.Property.PendingMidShard)
  135. conn = d.redis.Get(c)
  136. )
  137. defer conn.Close()
  138. if err = conn.Send("SADD", key, mid); err != nil {
  139. err = errors.Wrapf(err, "conn.Send(SADD,%s,%d)", key, mid)
  140. return
  141. }
  142. if err = conn.Send("EXPIRE", key, d); err != nil {
  143. err = errors.Wrapf(err, "conn.Send(EXPIRE,%s,%v)", key, d)
  144. return
  145. }
  146. if err = conn.Flush(); err != nil {
  147. err = errors.WithStack(err)
  148. return
  149. }
  150. if _, err = conn.Receive(); err != nil {
  151. err = errors.WithStack(err)
  152. return
  153. }
  154. return
  155. }
  156. // func testHbase(t *testing.T) {
  157. // Convey("TEST figure_activity", t, func() {
  158. // var (
  159. // mid int64 = 15555180
  160. // err error
  161. // weekVer = time.Date(2017, 10, 2, 0, 0, 0, 0, time.Local).Unix()
  162. // weekVerFrom = time.Date(2016, 10, 3, 0, 0, 0, 0, time.Local).Unix()
  163. // weekVerTo = time.Date(2017, 9, 25, 0, 0, 0, 0, time.Local).Unix()
  164. // userInfo *model.UserInfo
  165. // figureRecord = &model.FigureRecord{
  166. // Mid: mid,
  167. // XPosLawful: 12,
  168. // XNegLawful: 2,
  169. // XPosWide: 12,
  170. // XNegWide: 13,
  171. // XPosFriendly: 233,
  172. // XNegFriendly: 234,
  173. // XPosCreativity: 0,
  174. // XNegCreativity: 1,
  175. // XPosBounty: -250,
  176. // XNegBounty: -251,
  177. // }
  178. // figureRecords []*model.FigureRecord
  179. // )
  180. // err = dao.putSpyScore(ctx, mid, 100)
  181. // So(err, ShouldBeNil)
  182. // userInfo, err = dao.UserInfo(ctx, mid, weekVer)
  183. // So(err, ShouldBeNil)
  184. // So(userInfo, ShouldNotBeNil)
  185. // err = dao.PutCalcRecord(ctx, figureRecord, time.Date(2016, 12, 3, 0, 0, 0, 0, time.Local).Unix())
  186. // So(err, ShouldBeNil)
  187. // figureRecords, err = dao.CalcRecords(ctx, mid, weekVerFrom, weekVerTo+1)
  188. // So(err, ShouldBeNil)
  189. // So(figureRecords, ShouldNotBeEmpty)
  190. // })
  191. // }
  192. // // PutSpyScore add spy score info.
  193. // func (d *Dao) putSpyScore(c context.Context, mid int64, score int8) (err error) {
  194. // var (
  195. // key = rowKeyUserInfo(mid)
  196. // scoreB = make([]byte, 2)
  197. // ctx, cancel = context.WithTimeout(c, time.Duration(conf.Conf.Hbase.WriteTimeout))
  198. // )
  199. // defer cancel()
  200. // binary.BigEndian.PutUint16(scoreB, uint16(score))
  201. // values := map[string]map[string][]byte{_hbaseUserFC: map[string][]byte{_hbaseUserQSpy: scoreB}}
  202. // if _, err = d.hbase.PutStr(ctx, _hbaseUserTable, key, values); err != nil {
  203. // log.Error("hbase.PutStr(%s, %s, %v) error(%v)", _hbaseUserTable, key, values, err)
  204. // return
  205. // }
  206. // return
  207. // }