service_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "testing"
  6. "time"
  7. "go-common/app/job/main/point/conf"
  8. "go-common/app/job/main/point/model"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. var (
  14. s *Service
  15. c = context.TODO()
  16. )
  17. func init() {
  18. var (
  19. err error
  20. )
  21. flag.Set("conf", "../cmd/point-job.toml")
  22. if err = conf.Init(); err != nil {
  23. panic(err)
  24. }
  25. c = context.Background()
  26. if s == nil {
  27. s = New(conf.Conf)
  28. }
  29. time.Sleep(time.Second)
  30. }
  31. // go test -test.v -test.run TestAddPoint
  32. func TestAddPoint(t *testing.T) {
  33. Convey("TestAddPoint", t, func() {
  34. p := &model.VipPoint{
  35. Mid: 111,
  36. PointBalance: 1,
  37. Ver: 1,
  38. }
  39. err := s.AddPoint(c, p)
  40. So(err, ShouldBeNil)
  41. })
  42. }
  43. // go test -test.v -test.run TestUpdatePoint
  44. func TestUpdatePoint(t *testing.T) {
  45. Convey("TestUpdatePoint", t, func() {
  46. p := &model.VipPoint{
  47. Mid: 27515415,
  48. PointBalance: 2,
  49. Ver: 260,
  50. }
  51. oldp := &model.VipPoint{
  52. Ver: 259,
  53. }
  54. err := s.UpdatePoint(c, p, oldp)
  55. So(err, ShouldBeNil)
  56. })
  57. }
  58. // go test -test.v -test.run TestChangeHistory
  59. func TestChangeHistory(t *testing.T) {
  60. Convey("TestChangeHistory", t, func() {
  61. h := &model.VipPointChangeHistoryMsg{
  62. Mid: 111,
  63. Point: 2,
  64. OrderID: "wqwqe22112",
  65. ChangeType: 10,
  66. ChangeTime: "2018-03-14 18:30:57",
  67. RelationID: "11",
  68. PointBalance: 111,
  69. }
  70. err := s.AddPointHistory(c, h)
  71. So(err, ShouldBeNil)
  72. })
  73. }
  74. // go test -test.v -test.run TestUpdatePointByHistory
  75. func TestUpdatePointByHistory(t *testing.T) {
  76. Convey("TestUpdatePointByHistory", t, func() {
  77. var (
  78. count int64
  79. err error
  80. hs []*model.VipPointChangeHistoryMsg
  81. history *model.VipPointChangeHistoryMsg
  82. )
  83. history = &model.VipPointChangeHistoryMsg{
  84. Mid: int64(26645632),
  85. Point: int64(150),
  86. OrderID: "201804121752494042821812",
  87. ChangeType: 3,
  88. ChangeTime: "2018-04-12 17:52:49",
  89. RelationID: "seasonId:6339",
  90. PointBalance: int64(150),
  91. Remark: "欢迎来到实力至上主义的教室",
  92. Operator: "",
  93. }
  94. hs = append(hs, history)
  95. history = &model.VipPointChangeHistoryMsg{
  96. Mid: int64(26645632),
  97. Point: int64(100),
  98. OrderID: "201804121755088867934612",
  99. ChangeType: 3,
  100. ChangeTime: "2018-04-12 17:55:09",
  101. RelationID: "seasonId:1699",
  102. PointBalance: int64(250),
  103. Remark: "四月是你的谎言",
  104. Operator: "",
  105. }
  106. hs = append(hs, history)
  107. for _, history := range hs {
  108. if count, err = s.dao.HistoryCount(c, history.Mid, history.OrderID); err != nil {
  109. log.Error("update point(%v) history(%v)", err, history)
  110. continue
  111. }
  112. So(err, ShouldBeNil)
  113. So(count == 0, ShouldBeTrue)
  114. changeTime, err := time.ParseInLocation("2006-01-02 15:04:05", history.ChangeTime, time.Local)
  115. So(err, ShouldBeNil)
  116. ph := &model.PointHistory{
  117. Mid: history.Mid,
  118. Point: history.Point,
  119. OrderID: history.OrderID,
  120. ChangeType: int(history.ChangeType),
  121. ChangeTime: xtime.Time(changeTime.Unix()),
  122. RelationID: history.RelationID,
  123. PointBalance: history.PointBalance,
  124. Remark: history.Remark,
  125. Operator: history.Operator,
  126. }
  127. s.updatePointWithHistory(c, ph)
  128. So(err, ShouldBeNil)
  129. }
  130. })
  131. }
  132. // go test -test.v -test.run TestFixData
  133. func TestFixData(t *testing.T) {
  134. Convey("TestFixData", t, func() {
  135. err := s.fixdata("2018-04-12 18:20:00")
  136. So(err, ShouldBeNil)
  137. })
  138. }
  139. // go test -test.v -test.run TestFixData
  140. func TestNotify(t *testing.T) {
  141. Convey("TestNotify", t, func() {
  142. msg := &model.VipPointChangeHistoryMsg{
  143. Mid: 1,
  144. }
  145. err := s.Notify(context.TODO(), msg)
  146. So(err, ShouldBeNil)
  147. })
  148. }