exchange_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "time"
  10. )
  11. func queryExchange(t *testing.T, form *model.ExchangeForm, platform string) *RechargeRes {
  12. params := url.Values{}
  13. params.Set("uid", fmt.Sprintf("%d", form.Uid))
  14. params.Set("src_coin_type", form.SrcCoinType)
  15. params.Set("src_coin_num", fmt.Sprintf("%d", form.SrcCoinNum))
  16. params.Set("dest_coin_type", form.DestCoinType)
  17. params.Set("dest_coin_num", fmt.Sprintf("%d", form.DestCoinNum))
  18. params.Set("extend_tid", form.ExtendTid)
  19. params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
  20. params.Set("transaction_id", form.TransactionId)
  21. req, _ := client.NewRequest("POST", _exchangeURL, "127.0.0.1", params)
  22. req.Header.Set("platform", platform)
  23. var res RechargeRes
  24. err := client.Do(context.TODO(), req, &res)
  25. if err != nil {
  26. t.Errorf("client.Do() error(%v)", err)
  27. t.FailNow()
  28. }
  29. return &res
  30. }
  31. func getTestExchangeForm(t *testing.T, uid int64, srcCoinType string, srcCoinNum int64, destCoinType string, destCoinNum int64, tid interface{}) *model.ExchangeForm {
  32. if tid == nil {
  33. res := queryGetTid(t, int32(model.EXCHANGETYPE), getTestParamsJson())
  34. if res.Code != 0 {
  35. t.Errorf("get tid failed code : %d", res.Code)
  36. t.FailNow()
  37. }
  38. tid = res.Resp.TransactionId
  39. }
  40. return &model.ExchangeForm{
  41. Uid: uid,
  42. SrcCoinType: srcCoinType,
  43. SrcCoinNum: srcCoinNum,
  44. ExtendTid: getTestExtendTid(),
  45. Timestamp: time.Now().Unix(),
  46. TransactionId: tid.(string),
  47. DestCoinNum: destCoinNum,
  48. DestCoinType: destCoinType,
  49. }
  50. }
  51. func TestExchange(t *testing.T) {
  52. once.Do(startHTTP)
  53. Convey("exchange normal 先调用get接口 再调用exchange 再调用get接口 比较用户钱包数据", t, func() {
  54. platforms := []string{"pc", "android", "ios"}
  55. var num int64 = 1000
  56. var exchangeNum int64 = 100
  57. uid := getTestRandUid()
  58. for _, platform := range platforms {
  59. beforeWallet := getTestWallet(t, uid, platform)
  60. res := queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "gold", num, nil), platform)
  61. So(res.Code, ShouldEqual, 0)
  62. So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
  63. res = queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "silver", num, nil), platform)
  64. So(res.Code, ShouldEqual, 0)
  65. So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
  66. afterWallet := getTestWallet(t, uid, platform)
  67. So(getIntCoinForTest(afterWallet.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
  68. So(getIntCoinForTest(afterWallet.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
  69. res = queryExchange(t, getTestExchangeForm(t, uid, "gold", exchangeNum, "silver", exchangeNum, nil), platform)
  70. So(res.Code, ShouldEqual, 0)
  71. afterExchangeWallet := getTestWallet(t, uid, platform)
  72. So(getIntCoinForTest(afterExchangeWallet.Gold)-getIntCoinForTest(afterWallet.Gold), ShouldEqual, exchangeNum*-1)
  73. So(getIntCoinForTest(afterExchangeWallet.Silver)-getIntCoinForTest(afterWallet.Silver), ShouldEqual, exchangeNum)
  74. }
  75. })
  76. }