pay_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package http
  2. import (
  3. "context"
  4. "fmt"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/service/live/wallet/model"
  7. "net/url"
  8. "testing"
  9. )
  10. func queryPay(t *testing.T, form *model.RechargeOrPayForm, platform string) *RechargeRes {
  11. params := url.Values{}
  12. params.Set("uid", fmt.Sprintf("%d", form.Uid))
  13. params.Set("coin_type", form.CoinType)
  14. params.Set("coin_num", fmt.Sprintf("%d", form.CoinNum))
  15. params.Set("extend_tid", form.ExtendTid)
  16. params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
  17. params.Set("transaction_id", form.TransactionId)
  18. req, _ := client.NewRequest("POST", _payURL, "127.0.0.1", params)
  19. req.Header.Set("platform", platform)
  20. var res RechargeRes
  21. err := client.Do(context.TODO(), req, &res)
  22. if err != nil {
  23. t.Errorf("client.Do() error(%v)", err)
  24. t.FailNow()
  25. }
  26. return &res
  27. }
  28. func queryPayWithReason(t *testing.T, form *model.RechargeOrPayForm, platform string, reason string) *RechargeRes {
  29. params := url.Values{}
  30. params.Set("uid", fmt.Sprintf("%d", form.Uid))
  31. params.Set("coin_type", form.CoinType)
  32. params.Set("coin_num", fmt.Sprintf("%d", form.CoinNum))
  33. params.Set("extend_tid", form.ExtendTid)
  34. params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
  35. params.Set("transaction_id", form.TransactionId)
  36. params.Set("reason", reason)
  37. req, _ := client.NewRequest("POST", _payURL, "127.0.0.1", params)
  38. req.Header.Set("platform", platform)
  39. var res RechargeRes
  40. err := client.Do(context.TODO(), req, &res)
  41. if err != nil {
  42. t.Errorf("client.Do() error(%v)", err)
  43. t.FailNow()
  44. }
  45. return &res
  46. }
  47. func TestPay(t *testing.T) {
  48. once.Do(startHTTP)
  49. Convey("pay normal 先调用get接口 再调用pay 再调用get接口 比较用户钱包数据", t, func() {
  50. platforms := []string{"pc", "android", "ios"}
  51. var num int64 = 1000
  52. var payNum int64 = 100
  53. uid := getTestRandUid()
  54. for _, platform := range platforms {
  55. beforeWallet := getTestWallet(t, uid, platform)
  56. res := queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "gold", num, nil), platform)
  57. So(res.Code, ShouldEqual, 0)
  58. So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
  59. res = queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "silver", num, nil), platform)
  60. So(res.Code, ShouldEqual, 0)
  61. So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
  62. afterWallet := getTestWallet(t, uid, platform)
  63. So(getIntCoinForTest(afterWallet.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
  64. So(getIntCoinForTest(afterWallet.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
  65. f1 := getTestRechargeOrPayForm(t, int32(model.PAYTYPE), uid, "gold", payNum, nil)
  66. res = queryPay(t, f1, platform)
  67. So(res.Code, ShouldEqual, 0)
  68. So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(afterWallet.Gold), ShouldEqual, -1*payNum)
  69. sr := queryStatus(t, uid, f1.TransactionId)
  70. So(sr.Code, ShouldEqual, 0)
  71. So(sr.Resp.Status, ShouldEqual, 0)
  72. res = queryPay(t, getTestRechargeOrPayForm(t, int32(model.PAYTYPE), uid, "silver", payNum, nil), platform)
  73. So(res.Code, ShouldEqual, 0)
  74. So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(afterWallet.Silver), ShouldEqual, -1*payNum)
  75. payWallet := getTestWallet(t, uid, platform)
  76. So(getIntCoinForTest(payWallet.Gold)-getIntCoinForTest(afterWallet.Gold), ShouldEqual, -1*payNum)
  77. So(getIntCoinForTest(payWallet.Silver)-getIntCoinForTest(afterWallet.Silver), ShouldEqual, -1*payNum)
  78. }
  79. })
  80. }
  81. func TestPayMetal(t *testing.T) {
  82. once.Do(startHTTP)
  83. Convey("pay metal", t, func() {
  84. var uid int64 = 1
  85. platform := "pc"
  86. f1 := getTestRechargeOrPayForm(t, int32(model.PAYTYPE), uid, "metal", 1, nil)
  87. res := queryPayWithReason(t, f1, platform, "ut")
  88. So(res.Code == 0 || res.Code == 1000000, ShouldBeTrue)
  89. })
  90. }