recharge_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package http
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "context"
  7. . "github.com/smartystreets/goconvey/convey"
  8. "go-common/app/service/live/wallet/conf"
  9. "go-common/app/service/live/wallet/dao"
  10. "go-common/app/service/live/wallet/model"
  11. "go-common/app/service/live/wallet/service"
  12. httpx "go-common/library/net/http/blademaster"
  13. "math/rand"
  14. "net/url"
  15. "strconv"
  16. "time"
  17. )
  18. const (
  19. _getURL = "http://localhost:9901/x/internal/livewallet/wallet/get"
  20. _delCacheURL = "http://localhost:9901/x/internal/livewallet/wallet/delCache"
  21. _getAllURL = "http://localhost:9901/x/internal/livewallet/wallet/getAll"
  22. _getTidURL = "http://localhost:9901/x/internal/livewallet/wallet/getTid"
  23. _rechargeURL = "http://localhost:9901/x/internal/livewallet/wallet/recharge"
  24. _payURL = "http://localhost:9901/x/internal/livewallet/wallet/pay"
  25. _exchangeURL = "http://localhost:9901/x/internal/livewallet/wallet/exchange"
  26. _queryURL = "http://localhost:9901/x/internal/livewallet/wallet/query"
  27. )
  28. var (
  29. once sync.Once
  30. client *httpx.Client
  31. r *rand.Rand
  32. )
  33. type RechargeRes struct {
  34. Code int `json:"code"`
  35. Resp *model.MelonseedResp `json:"data"`
  36. }
  37. func getTestRandUid() int64 {
  38. return r.Int63n(10000000)
  39. }
  40. func getTestExtendTid() string {
  41. return fmt.Sprintf("test:ex:%d", r.Int31n(1000000))
  42. }
  43. func getTestRechargeOrPayForm(t *testing.T, serviceType int32, uid int64, coinType string, coinNum int64, tid interface{}) *model.RechargeOrPayForm {
  44. if tid == nil {
  45. res := queryGetTid(t, serviceType, getTestParamsJson())
  46. if res.Code != 0 {
  47. t.Errorf("get tid failed code : %d", res.Code)
  48. t.FailNow()
  49. }
  50. tid = res.Resp.TransactionId
  51. }
  52. return &model.RechargeOrPayForm{
  53. Uid: uid,
  54. CoinType: coinType,
  55. CoinNum: coinNum,
  56. ExtendTid: getTestExtendTid(),
  57. Timestamp: time.Now().Unix(),
  58. TransactionId: tid.(string),
  59. }
  60. }
  61. func queryRecharge(t *testing.T, form *model.RechargeOrPayForm, platform string) *RechargeRes {
  62. params := url.Values{}
  63. params.Set("uid", fmt.Sprintf("%d", form.Uid))
  64. params.Set("coin_type", form.CoinType)
  65. params.Set("coin_num", fmt.Sprintf("%d", form.CoinNum))
  66. params.Set("extend_tid", form.ExtendTid)
  67. params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
  68. params.Set("transaction_id", form.TransactionId)
  69. params.Set("biz_reason", "2")
  70. params.Set("version", "1")
  71. req, _ := client.NewRequest("POST", _rechargeURL, "127.0.0.1", params)
  72. req.Header.Set("platform", platform)
  73. var res RechargeRes
  74. err := client.Do(context.TODO(), req, &res)
  75. if err != nil {
  76. t.Errorf("client.Do() error(%v)", err)
  77. t.FailNow()
  78. }
  79. return &res
  80. }
  81. func startHTTP() {
  82. if err := conf.Init(); err != nil {
  83. panic(fmt.Errorf("conf.Init() error(%v)", err))
  84. }
  85. svr := service.New(conf.Conf)
  86. client = httpx.NewClient(conf.Conf.HTTPClient)
  87. Init(conf.Conf, svr)
  88. r = rand.New(rand.NewSource(time.Now().UnixNano()))
  89. }
  90. func getIntCoinForTest(coinStr string) int64 {
  91. coin, _ := strconv.Atoi(coinStr)
  92. return int64(coin)
  93. }
  94. func TestRecharge(t *testing.T) {
  95. Convey("recharge normal 先调用get接口 再调用recharge 再调用get接口 比较用户钱包数据", t, testWith(func() {
  96. platforms := []string{"pc", "android", "ios"}
  97. var num int64 = 1000
  98. uid := getTestRandUid()
  99. d := dao.New(conf.Conf)
  100. for _, platform := range platforms {
  101. beforeWallet := getTestWallet(t, uid, platform)
  102. resTid := queryGetTid(t, int32(model.RECHARGETYPE), getTestParamsJson())
  103. if resTid.Code != 0 {
  104. t.Errorf("get tid failed code : %d", resTid.Code)
  105. t.FailNow()
  106. }
  107. tid := resTid.Resp.TransactionId
  108. res := queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "gold", num, tid), platform)
  109. So(res.Code, ShouldEqual, 0)
  110. So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
  111. record, err := d.GetCoinStreamByTidAndOffset(context.TODO(), tid, 0)
  112. So(err, ShouldBeNil)
  113. So(record.Reserved1, ShouldEqual, 2)
  114. So(record.Version, ShouldEqual, 1)
  115. res = queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "silver", num, nil), platform)
  116. So(res.Code, ShouldEqual, 0)
  117. So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
  118. afterWallet := getTestWallet(t, uid, platform)
  119. So(getIntCoinForTest(afterWallet.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
  120. So(getIntCoinForTest(afterWallet.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
  121. }
  122. }))
  123. }