service_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "testing"
  6. "time"
  7. "go-common/app/job/main/coupon/conf"
  8. "go-common/app/job/main/coupon/model"
  9. "go-common/library/database/sql"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. var (
  13. s *Service
  14. c context.Context
  15. )
  16. func init() {
  17. var (
  18. err error
  19. )
  20. flag.Set("conf", "../cmd/coupon-job.toml")
  21. if err = conf.Init(); err != nil {
  22. panic(err)
  23. }
  24. c = context.Background()
  25. if s == nil {
  26. s = New(conf.Conf)
  27. }
  28. time.Sleep(time.Second)
  29. }
  30. // go test -test.v -test.run TestCheckCouponDeliver
  31. func TestCheckCouponDeliver(t *testing.T) {
  32. Convey("TestCheckCouponDeliver ", t, func() {
  33. var (
  34. err error
  35. )
  36. arg := &model.NotifyParam{
  37. Mid: 1,
  38. CouponToken: "676289266420180402162120",
  39. NotifyURL: "http://bangumi.bilibili.com/pay/inner/notify_ticket",
  40. }
  41. err = s.CheckCouponDeliver(context.TODO(), arg)
  42. So(err, ShouldBeNil)
  43. })
  44. }
  45. func TestCheckInUseCoupon(t *testing.T) {
  46. Convey("TestCheckInUseCoupon ", t, func() {
  47. s.CheckInUseCoupon()
  48. })
  49. }
  50. func TestNotifyproc(t *testing.T) {
  51. Convey("TestNotifyproc ", t, func() {
  52. var err error
  53. time.Sleep(time.Duration(s.c.Properties.NotifyTimeInterval))
  54. for i := 0; i < 10; i++ {
  55. arg := &model.NotifyParam{
  56. Mid: 1,
  57. CouponToken: "729792667120180402161647",
  58. NotifyURL: "http://bangumi.bilibili.com/pay/inner/notify_ticket",
  59. }
  60. if err = s.CheckCouponDeliver(context.TODO(), arg); err != nil {
  61. arg.NotifyCount++
  62. s.notifyChan <- arg
  63. }
  64. So(err, ShouldBeNil)
  65. }
  66. })
  67. }
  68. func TestUpdateCoupon(t *testing.T) {
  69. Convey("TestUpdateCoupon ", t, func() {
  70. cp := &model.CouponInfo{
  71. CouponToken: "729792667120180402161647",
  72. Mid: 1,
  73. CouponType: 1,
  74. Ver: 4,
  75. }
  76. data := &model.CallBackRet{
  77. Ver: 3,
  78. }
  79. err := s.updateCouponState(c, cp, 2, data)
  80. So(err, ShouldBeNil)
  81. })
  82. }
  83. // go test -test.v -test.run TestUpdateBalance
  84. func TestUpdateBalance(t *testing.T) {
  85. Convey("TestUpdateBalance ", t, func() {
  86. var (
  87. tx *sql.Tx
  88. mid int64 = 1
  89. orderNo = "9372774783174654609"
  90. ls []*model.CouponBalanceChangeLog
  91. err error
  92. )
  93. ls, err = s.dao.ConsumeCouponLog(c, mid, orderNo, model.Consume)
  94. So(err, ShouldBeNil)
  95. tx, err = s.dao.BeginTran(c)
  96. So(err, ShouldBeNil)
  97. err = s.UpdateBalance(c, tx, mid, model.Cartoon, ls, orderNo)
  98. So(err, ShouldBeNil)
  99. err = tx.Commit()
  100. So(err, ShouldBeNil)
  101. })
  102. }
  103. // go test -test.v -test.run TestUpdateOrderState
  104. func TestUpdateOrderState(t *testing.T) {
  105. Convey("TestUpdateOrderState ", t, func() {
  106. var (
  107. orderNo = "6462644254161152528"
  108. faildOrderNo = "9176715513161453816"
  109. err error
  110. o *model.CouponOrder
  111. )
  112. data := &model.CallBackRet{
  113. Ver: 123456,
  114. IsPaid: 1,
  115. }
  116. o, err = s.dao.ByOrderNo(c, orderNo)
  117. So(err, ShouldBeNil)
  118. err = s.UpdateOrderState(c, o, model.PaySuccess, data)
  119. So(err, ShouldBeNil)
  120. o, err = s.dao.ByOrderNo(c, faildOrderNo)
  121. So(err, ShouldBeNil)
  122. err = s.UpdateOrderState(c, o, model.PayFaild, data)
  123. So(err, ShouldBeNil)
  124. })
  125. }
  126. // go test -test.v -test.run TestCouponCartoonDeliver
  127. func TestCouponCartoonDeliver(t *testing.T) {
  128. Convey("TestCouponCartoonDeliver ", t, func() {
  129. var (
  130. err error
  131. )
  132. arg := &model.NotifyParam{
  133. CouponToken: "5586615697161708066",
  134. Mid: 1,
  135. Type: 2,
  136. }
  137. err = s.CouponCartoonDeliver(c, arg)
  138. So(err, ShouldBeNil)
  139. })
  140. }