get_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package http
  2. import (
  3. "context"
  4. "fmt"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/service/live/wallet/model"
  7. "go-common/library/ecode"
  8. "net/url"
  9. "strconv"
  10. "testing"
  11. )
  12. type GetRes struct {
  13. Code int `json:"code"`
  14. Resp *model.MelonseedWithMetalResp `json:"data"`
  15. }
  16. type StatusRes struct {
  17. Code int `json:"code"`
  18. Resp *model.QueryResp `json:"data"`
  19. }
  20. type GetAllRes struct {
  21. Code int `json:"code"`
  22. Resp *model.DetailWithMetalResp `json:"data"`
  23. }
  24. func queryGet(t *testing.T, uid int64, platform string) *GetRes {
  25. params := url.Values{}
  26. params.Set("uid", fmt.Sprintf("%d", uid))
  27. req, _ := client.NewRequest("GET", _getURL, "127.0.0.1", params)
  28. req.Header.Set("platform", platform)
  29. var res GetRes
  30. err := client.Do(context.TODO(), req, &res)
  31. if err != nil {
  32. t.Errorf("client.Do() error(%v)", err)
  33. t.FailNow()
  34. }
  35. return &res
  36. }
  37. func queryStatus(t *testing.T, uid int64, tid string) *StatusRes {
  38. params := url.Values{}
  39. params.Set("uid", fmt.Sprintf("%d", uid))
  40. params.Set("transaction_id", tid)
  41. req, _ := client.NewRequest("GET", _queryURL, "127.0.0.1", params)
  42. req.Header.Set("platform", "pc")
  43. var res StatusRes
  44. err := client.Do(context.TODO(), req, &res)
  45. if err != nil {
  46. t.Errorf("client.Do() error(%v)", err)
  47. t.FailNow()
  48. }
  49. return &res
  50. }
  51. func queryGetAll(t *testing.T, uid int64, platform string) *GetAllRes {
  52. params := url.Values{}
  53. params.Set("uid", fmt.Sprintf("%d", uid))
  54. req, _ := client.NewRequest("GET", _getAllURL, "127.0.0.1", params)
  55. req.Header.Set("platform", platform)
  56. var res GetAllRes
  57. err := client.Do(context.TODO(), req, &res)
  58. if err != nil {
  59. t.Errorf("client.Do() error(%v)", err)
  60. t.FailNow()
  61. }
  62. return &res
  63. }
  64. func getTestWallet(t *testing.T, uid int64, platform string) *model.MelonseedWithMetalResp {
  65. res := queryGet(t, uid, platform)
  66. if res.Code != 0 {
  67. t.Errorf("get wallet failed uid : %d, code :%d", uid, res.Code)
  68. t.FailNow()
  69. }
  70. return res.Resp
  71. }
  72. /*
  73. useless now
  74. func getTestWalletDetail(t *testing.T, uid int64, platform string) *model.DetailWithMetalResp {
  75. res := queryGetAll(t, uid, platform)
  76. if res.Code != 0 {
  77. t.Errorf("get wallet failed uid : %d, code :%d", uid, res.Code)
  78. t.FailNow()
  79. }
  80. return res.Resp
  81. }*/
  82. func TestGet(t *testing.T) {
  83. once.Do(startHTTP)
  84. Convey("get normal", t, func() {
  85. res := queryGet(t, 1, "pc")
  86. So(res.Code, ShouldEqual, 0)
  87. melon := res.Resp
  88. coin, err := strconv.Atoi(melon.Gold)
  89. So(err, ShouldBeNil)
  90. So(coin, ShouldBeGreaterThan, -1)
  91. coin, err = strconv.Atoi(melon.Silver)
  92. So(err, ShouldBeNil)
  93. So(coin, ShouldBeGreaterThan, -1)
  94. })
  95. Convey("uid params error", t, func() {
  96. res := queryGet(t, -1, "pc")
  97. So(res.Code, ShouldEqual, ecode.RequestErr)
  98. })
  99. Convey("platform params error", t, func() {
  100. res := queryGet(t, 1, "pc1")
  101. So(res.Code, ShouldEqual, ecode.RequestErr)
  102. })
  103. }
  104. func TestGetAll(t *testing.T) {
  105. once.Do(startHTTP)
  106. Convey("normal", t, func() {
  107. res := queryGetAll(t, 1, "pc")
  108. t.Logf("all:%v", res)
  109. So(res.Code, ShouldEqual, 0)
  110. melon := res.Resp
  111. coin, err := strconv.Atoi(melon.Gold)
  112. So(err, ShouldBeNil)
  113. So(coin, ShouldBeGreaterThan, -1)
  114. coin, err = strconv.Atoi(melon.Silver)
  115. So(err, ShouldBeNil)
  116. So(coin, ShouldBeGreaterThan, -1)
  117. coin, err = strconv.Atoi(melon.SilverPayCnt)
  118. So(err, ShouldBeNil)
  119. So(coin, ShouldBeGreaterThan, -1)
  120. coin, err = strconv.Atoi(melon.GoldPayCnt)
  121. So(err, ShouldBeNil)
  122. So(coin, ShouldBeGreaterThan, -1)
  123. coin, err = strconv.Atoi(melon.GoldRechargeCnt)
  124. So(err, ShouldBeNil)
  125. So(coin, ShouldBeGreaterThan, -1)
  126. So(melon.CostBase, ShouldBeGreaterThanOrEqualTo, 0)
  127. })
  128. }