Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"delCache_test.go",
"exchange_test.go",
"getTid_test.go",
"get_test.go",
"pay_test.go",
"recharge_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/live/wallet/conf:go_default_library",
"//app/service/live/wallet/dao:go_default_library",
"//app/service/live/wallet/model:go_default_library",
"//app/service/live/wallet/service:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"http.go",
"wallet.go",
],
importpath = "go-common/app/service/live/wallet/http",
tags = ["automanaged"],
deps = [
"//app/service/live/wallet/conf:go_default_library",
"//app/service/live/wallet/model:go_default_library",
"//app/service/live/wallet/service:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,43 @@
package http
import (
"context"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"go-common/library/ecode"
"net/url"
"testing"
)
type DelCacheRes struct {
Code int `json:"code"`
}
func queryDelCache(t *testing.T, uid int64) *DelCacheRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", uid))
req, _ := client.NewRequest("GET", _delCacheURL, "127.0.0.1", params)
var res DelCacheRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func TestDelCache(t *testing.T) {
once.Do(startHTTP)
Convey("Del Cache", t, func() {
var uid int64 = 1
queryGet(t, uid, "pc")
r := queryDelCache(t, uid)
So(r.Code, ShouldEqual, 0)
r = queryDelCache(t, uid)
So(r.Code, ShouldEqual, ecode.NothingFound)
})
}

View File

@@ -0,0 +1,95 @@
package http
import (
"context"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/service/live/wallet/model"
"net/url"
"testing"
"time"
)
func queryExchange(t *testing.T, form *model.ExchangeForm, platform string) *RechargeRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", form.Uid))
params.Set("src_coin_type", form.SrcCoinType)
params.Set("src_coin_num", fmt.Sprintf("%d", form.SrcCoinNum))
params.Set("dest_coin_type", form.DestCoinType)
params.Set("dest_coin_num", fmt.Sprintf("%d", form.DestCoinNum))
params.Set("extend_tid", form.ExtendTid)
params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
params.Set("transaction_id", form.TransactionId)
req, _ := client.NewRequest("POST", _exchangeURL, "127.0.0.1", params)
req.Header.Set("platform", platform)
var res RechargeRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func getTestExchangeForm(t *testing.T, uid int64, srcCoinType string, srcCoinNum int64, destCoinType string, destCoinNum int64, tid interface{}) *model.ExchangeForm {
if tid == nil {
res := queryGetTid(t, int32(model.EXCHANGETYPE), getTestParamsJson())
if res.Code != 0 {
t.Errorf("get tid failed code : %d", res.Code)
t.FailNow()
}
tid = res.Resp.TransactionId
}
return &model.ExchangeForm{
Uid: uid,
SrcCoinType: srcCoinType,
SrcCoinNum: srcCoinNum,
ExtendTid: getTestExtendTid(),
Timestamp: time.Now().Unix(),
TransactionId: tid.(string),
DestCoinNum: destCoinNum,
DestCoinType: destCoinType,
}
}
func TestExchange(t *testing.T) {
once.Do(startHTTP)
Convey("exchange normal 先调用get接口 再调用exchange 再调用get接口 比较用户钱包数据", t, func() {
platforms := []string{"pc", "android", "ios"}
var num int64 = 1000
var exchangeNum int64 = 100
uid := getTestRandUid()
for _, platform := range platforms {
beforeWallet := getTestWallet(t, uid, platform)
res := queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "gold", num, nil), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
res = queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "silver", num, nil), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
afterWallet := getTestWallet(t, uid, platform)
So(getIntCoinForTest(afterWallet.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
So(getIntCoinForTest(afterWallet.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
res = queryExchange(t, getTestExchangeForm(t, uid, "gold", exchangeNum, "silver", exchangeNum, nil), platform)
So(res.Code, ShouldEqual, 0)
afterExchangeWallet := getTestWallet(t, uid, platform)
So(getIntCoinForTest(afterExchangeWallet.Gold)-getIntCoinForTest(afterWallet.Gold), ShouldEqual, exchangeNum*-1)
So(getIntCoinForTest(afterExchangeWallet.Silver)-getIntCoinForTest(afterWallet.Silver), ShouldEqual, exchangeNum)
}
})
}

View File

@@ -0,0 +1,139 @@
package http
import (
"context"
"encoding/json"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/service/live/wallet/model"
"go-common/library/ecode"
"net/url"
"sync"
"testing"
"time"
)
type TidRes struct {
Code int `json:"code"`
Resp *model.TidResp `json:"data"`
}
type TestTidParams struct {
Biz string
Time int64
}
func getTestTidParams(biz string) *TestTidParams {
return &TestTidParams{
Biz: biz,
Time: time.Now().Unix(),
}
}
func getTestRandServiceType() int32 {
return r.Int31n(4)
}
func testWith(f func()) func() {
once.Do(startHTTP)
return f
}
func getTestParamsJson() string {
params := getTestTidParams("gift")
paramsBytes, _ := json.Marshal(params)
paramsJson := string(paramsBytes[:])
return paramsJson
}
func queryGetTid(t *testing.T, serviceType int32, tidParams string) *TidRes {
params := url.Values{}
params.Set("type", fmt.Sprintf("%d", serviceType))
params.Set("params", tidParams)
req, _ := client.NewRequest("POST", _getTidURL, "127.0.0.1", params)
var res TidRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func TestGetTid(t *testing.T) {
Convey("normal", t, testWith(func() {
serviceType := getTestRandServiceType()
paramsJson := getTestParamsJson()
res := queryGetTid(t, serviceType, paramsJson)
So(res.Code, ShouldEqual, 0)
So(res.Resp.TransactionId, ShouldNotEqual, "")
}))
Convey("Twice Same params 同样的参数调用getTid 得到的tid应该不一样", t, testWith(func() {
serviceType := getTestRandServiceType()
paramsJson := getTestParamsJson()
res := queryGetTid(t, serviceType, paramsJson)
So(res.Code, ShouldEqual, 0)
So(res.Resp.TransactionId, ShouldNotEqual, "")
res1 := queryGetTid(t, serviceType, paramsJson)
So(res1.Code, ShouldEqual, 0)
So(res1.Resp.TransactionId, ShouldNotEqual, res.Resp.TransactionId)
}))
Convey("Test multi Same params", t, testWith(func() {
serviceType := getTestRandServiceType()
paramsJson := getTestParamsJson()
resMap := make(map[int]*TidRes)
wg := sync.WaitGroup{}
var mutex sync.Mutex
times := 10
for i := 0; i < times; i++ {
wg.Add(1)
go func(i int) {
mutex.Lock()
resMap[i] = queryGetTid(t, serviceType, paramsJson)
mutex.Unlock()
wg.Done()
}(i)
}
wg.Wait()
tidMap := map[string]bool{}
for _, res := range resMap {
So(res.Code, ShouldEqual, 0)
So(res.Resp.TransactionId, ShouldNotEqual, "")
_, ok := tidMap[res.Resp.TransactionId]
So(ok, ShouldEqual, false) // 应该找不到 因为同样的参数应该生成不同的tid
tidMap[res.Resp.TransactionId] = true
}
}))
Convey("params error", t, func() {
Convey("serviceType 应该为 0 1 2 3 ", testWith(func() {
wrongServiceTypes := []int32{-1, 5}
for _, v := range wrongServiceTypes {
paramsJson := getTestParamsJson()
res := queryGetTid(t, v, paramsJson)
So(res.Code, ShouldEqual, ecode.RequestErr.Code())
}
}))
Convey("param不为空", testWith(func() {
st := getTestRandServiceType()
params := ""
res := queryGetTid(t, st, params)
So(res.Code, ShouldEqual, ecode.RequestErr.Code())
}))
})
}

View File

@@ -0,0 +1,155 @@
package http
import (
"context"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/service/live/wallet/model"
"go-common/library/ecode"
"net/url"
"strconv"
"testing"
)
type GetRes struct {
Code int `json:"code"`
Resp *model.MelonseedWithMetalResp `json:"data"`
}
type StatusRes struct {
Code int `json:"code"`
Resp *model.QueryResp `json:"data"`
}
type GetAllRes struct {
Code int `json:"code"`
Resp *model.DetailWithMetalResp `json:"data"`
}
func queryGet(t *testing.T, uid int64, platform string) *GetRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", uid))
req, _ := client.NewRequest("GET", _getURL, "127.0.0.1", params)
req.Header.Set("platform", platform)
var res GetRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func queryStatus(t *testing.T, uid int64, tid string) *StatusRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", uid))
params.Set("transaction_id", tid)
req, _ := client.NewRequest("GET", _queryURL, "127.0.0.1", params)
req.Header.Set("platform", "pc")
var res StatusRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func queryGetAll(t *testing.T, uid int64, platform string) *GetAllRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", uid))
req, _ := client.NewRequest("GET", _getAllURL, "127.0.0.1", params)
req.Header.Set("platform", platform)
var res GetAllRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func getTestWallet(t *testing.T, uid int64, platform string) *model.MelonseedWithMetalResp {
res := queryGet(t, uid, platform)
if res.Code != 0 {
t.Errorf("get wallet failed uid : %d, code :%d", uid, res.Code)
t.FailNow()
}
return res.Resp
}
/*
useless now
func getTestWalletDetail(t *testing.T, uid int64, platform string) *model.DetailWithMetalResp {
res := queryGetAll(t, uid, platform)
if res.Code != 0 {
t.Errorf("get wallet failed uid : %d, code :%d", uid, res.Code)
t.FailNow()
}
return res.Resp
}*/
func TestGet(t *testing.T) {
once.Do(startHTTP)
Convey("get normal", t, func() {
res := queryGet(t, 1, "pc")
So(res.Code, ShouldEqual, 0)
melon := res.Resp
coin, err := strconv.Atoi(melon.Gold)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
coin, err = strconv.Atoi(melon.Silver)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
})
Convey("uid params error", t, func() {
res := queryGet(t, -1, "pc")
So(res.Code, ShouldEqual, ecode.RequestErr)
})
Convey("platform params error", t, func() {
res := queryGet(t, 1, "pc1")
So(res.Code, ShouldEqual, ecode.RequestErr)
})
}
func TestGetAll(t *testing.T) {
once.Do(startHTTP)
Convey("normal", t, func() {
res := queryGetAll(t, 1, "pc")
t.Logf("all:%v", res)
So(res.Code, ShouldEqual, 0)
melon := res.Resp
coin, err := strconv.Atoi(melon.Gold)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
coin, err = strconv.Atoi(melon.Silver)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
coin, err = strconv.Atoi(melon.SilverPayCnt)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
coin, err = strconv.Atoi(melon.GoldPayCnt)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
coin, err = strconv.Atoi(melon.GoldRechargeCnt)
So(err, ShouldBeNil)
So(coin, ShouldBeGreaterThan, -1)
So(melon.CostBase, ShouldBeGreaterThanOrEqualTo, 0)
})
}

View File

@@ -0,0 +1,69 @@
package http
import (
"net/http"
"go-common/app/service/live/wallet/conf"
"go-common/app/service/live/wallet/service"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
)
var (
// depend service
idfSvc *verify.Verify
walletSvr *service.Service
)
func Init(c *conf.Config, s *service.Service) {
// init service
walletSvr = s
// init external router
idfSvc = verify.New(nil)
engineOut := bm.DefaultServer(c.BM.Inner)
innerRouter(engineOut)
// init Inner serve
if err := engineOut.Start(); err != nil {
log.Error("bm.DefaultServer error(%v)", err)
panic(err)
}
engineLocal := bm.DefaultServer(c.BM.Local)
localRouter(engineLocal)
// init Local serve
if err := engineLocal.Start(); err != nil {
log.Error("bm.DefaultServer error(%v)", err)
panic(err)
}
}
// outerRouter init outer router api path.
func innerRouter(e *bm.Engine) {
// init api
e.Ping(ping)
group := e.Group("/x/internal/livewallet", idfSvc.Verify)
{
group.GET("/wallet/get", get)
group.GET("/wallet/delCache", delCache)
group.GET("/wallet/getAll", getAll)
group.POST("/wallet/getTid", getTid)
group.POST("/wallet/recharge", recharge)
group.GET("/wallet/query", query)
group.POST("/wallet/pay", pay)
group.POST("/wallet/exchange", exchange)
group.POST("/wallet/modify", modify)
group.POST("/flowwater/recordCoinStream", recordCoinStream)
}
}
// localRouter init local router.
func localRouter(e *bm.Engine) {
}
// ping check server ok.
func ping(c *bm.Context) {
if err := walletSvr.Ping(c); err != nil {
log.Error("live-userwallet http service ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}

View File

@@ -0,0 +1,116 @@
package http
import (
"context"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/service/live/wallet/model"
"net/url"
"testing"
)
func queryPay(t *testing.T, form *model.RechargeOrPayForm, platform string) *RechargeRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", form.Uid))
params.Set("coin_type", form.CoinType)
params.Set("coin_num", fmt.Sprintf("%d", form.CoinNum))
params.Set("extend_tid", form.ExtendTid)
params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
params.Set("transaction_id", form.TransactionId)
req, _ := client.NewRequest("POST", _payURL, "127.0.0.1", params)
req.Header.Set("platform", platform)
var res RechargeRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func queryPayWithReason(t *testing.T, form *model.RechargeOrPayForm, platform string, reason string) *RechargeRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", form.Uid))
params.Set("coin_type", form.CoinType)
params.Set("coin_num", fmt.Sprintf("%d", form.CoinNum))
params.Set("extend_tid", form.ExtendTid)
params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
params.Set("transaction_id", form.TransactionId)
params.Set("reason", reason)
req, _ := client.NewRequest("POST", _payURL, "127.0.0.1", params)
req.Header.Set("platform", platform)
var res RechargeRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func TestPay(t *testing.T) {
once.Do(startHTTP)
Convey("pay normal 先调用get接口 再调用pay 再调用get接口 比较用户钱包数据", t, func() {
platforms := []string{"pc", "android", "ios"}
var num int64 = 1000
var payNum int64 = 100
uid := getTestRandUid()
for _, platform := range platforms {
beforeWallet := getTestWallet(t, uid, platform)
res := queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "gold", num, nil), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
res = queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "silver", num, nil), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
afterWallet := getTestWallet(t, uid, platform)
So(getIntCoinForTest(afterWallet.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
So(getIntCoinForTest(afterWallet.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
f1 := getTestRechargeOrPayForm(t, int32(model.PAYTYPE), uid, "gold", payNum, nil)
res = queryPay(t, f1, platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(afterWallet.Gold), ShouldEqual, -1*payNum)
sr := queryStatus(t, uid, f1.TransactionId)
So(sr.Code, ShouldEqual, 0)
So(sr.Resp.Status, ShouldEqual, 0)
res = queryPay(t, getTestRechargeOrPayForm(t, int32(model.PAYTYPE), uid, "silver", payNum, nil), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(afterWallet.Silver), ShouldEqual, -1*payNum)
payWallet := getTestWallet(t, uid, platform)
So(getIntCoinForTest(payWallet.Gold)-getIntCoinForTest(afterWallet.Gold), ShouldEqual, -1*payNum)
So(getIntCoinForTest(payWallet.Silver)-getIntCoinForTest(afterWallet.Silver), ShouldEqual, -1*payNum)
}
})
}
func TestPayMetal(t *testing.T) {
once.Do(startHTTP)
Convey("pay metal", t, func() {
var uid int64 = 1
platform := "pc"
f1 := getTestRechargeOrPayForm(t, int32(model.PAYTYPE), uid, "metal", 1, nil)
res := queryPayWithReason(t, f1, platform, "ut")
So(res.Code == 0 || res.Code == 1000000, ShouldBeTrue)
})
}

View File

@@ -0,0 +1,151 @@
package http
import (
"fmt"
"sync"
"testing"
"context"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/service/live/wallet/conf"
"go-common/app/service/live/wallet/dao"
"go-common/app/service/live/wallet/model"
"go-common/app/service/live/wallet/service"
httpx "go-common/library/net/http/blademaster"
"math/rand"
"net/url"
"strconv"
"time"
)
const (
_getURL = "http://localhost:9901/x/internal/livewallet/wallet/get"
_delCacheURL = "http://localhost:9901/x/internal/livewallet/wallet/delCache"
_getAllURL = "http://localhost:9901/x/internal/livewallet/wallet/getAll"
_getTidURL = "http://localhost:9901/x/internal/livewallet/wallet/getTid"
_rechargeURL = "http://localhost:9901/x/internal/livewallet/wallet/recharge"
_payURL = "http://localhost:9901/x/internal/livewallet/wallet/pay"
_exchangeURL = "http://localhost:9901/x/internal/livewallet/wallet/exchange"
_queryURL = "http://localhost:9901/x/internal/livewallet/wallet/query"
)
var (
once sync.Once
client *httpx.Client
r *rand.Rand
)
type RechargeRes struct {
Code int `json:"code"`
Resp *model.MelonseedResp `json:"data"`
}
func getTestRandUid() int64 {
return r.Int63n(10000000)
}
func getTestExtendTid() string {
return fmt.Sprintf("test:ex:%d", r.Int31n(1000000))
}
func getTestRechargeOrPayForm(t *testing.T, serviceType int32, uid int64, coinType string, coinNum int64, tid interface{}) *model.RechargeOrPayForm {
if tid == nil {
res := queryGetTid(t, serviceType, getTestParamsJson())
if res.Code != 0 {
t.Errorf("get tid failed code : %d", res.Code)
t.FailNow()
}
tid = res.Resp.TransactionId
}
return &model.RechargeOrPayForm{
Uid: uid,
CoinType: coinType,
CoinNum: coinNum,
ExtendTid: getTestExtendTid(),
Timestamp: time.Now().Unix(),
TransactionId: tid.(string),
}
}
func queryRecharge(t *testing.T, form *model.RechargeOrPayForm, platform string) *RechargeRes {
params := url.Values{}
params.Set("uid", fmt.Sprintf("%d", form.Uid))
params.Set("coin_type", form.CoinType)
params.Set("coin_num", fmt.Sprintf("%d", form.CoinNum))
params.Set("extend_tid", form.ExtendTid)
params.Set("timestamp", fmt.Sprintf("%d", form.Timestamp))
params.Set("transaction_id", form.TransactionId)
params.Set("biz_reason", "2")
params.Set("version", "1")
req, _ := client.NewRequest("POST", _rechargeURL, "127.0.0.1", params)
req.Header.Set("platform", platform)
var res RechargeRes
err := client.Do(context.TODO(), req, &res)
if err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
return &res
}
func startHTTP() {
if err := conf.Init(); err != nil {
panic(fmt.Errorf("conf.Init() error(%v)", err))
}
svr := service.New(conf.Conf)
client = httpx.NewClient(conf.Conf.HTTPClient)
Init(conf.Conf, svr)
r = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func getIntCoinForTest(coinStr string) int64 {
coin, _ := strconv.Atoi(coinStr)
return int64(coin)
}
func TestRecharge(t *testing.T) {
Convey("recharge normal 先调用get接口 再调用recharge 再调用get接口 比较用户钱包数据", t, testWith(func() {
platforms := []string{"pc", "android", "ios"}
var num int64 = 1000
uid := getTestRandUid()
d := dao.New(conf.Conf)
for _, platform := range platforms {
beforeWallet := getTestWallet(t, uid, platform)
resTid := queryGetTid(t, int32(model.RECHARGETYPE), getTestParamsJson())
if resTid.Code != 0 {
t.Errorf("get tid failed code : %d", resTid.Code)
t.FailNow()
}
tid := resTid.Resp.TransactionId
res := queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "gold", num, tid), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
record, err := d.GetCoinStreamByTidAndOffset(context.TODO(), tid, 0)
So(err, ShouldBeNil)
So(record.Reserved1, ShouldEqual, 2)
So(record.Version, ShouldEqual, 1)
res = queryRecharge(t, getTestRechargeOrPayForm(t, int32(model.RECHARGETYPE), uid, "silver", num, nil), platform)
So(res.Code, ShouldEqual, 0)
So(getIntCoinForTest(res.Resp.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
afterWallet := getTestWallet(t, uid, platform)
So(getIntCoinForTest(afterWallet.Gold)-getIntCoinForTest(beforeWallet.Gold), ShouldEqual, num)
So(getIntCoinForTest(afterWallet.Silver)-getIntCoinForTest(beforeWallet.Silver), ShouldEqual, num)
}
}))
}

View File

@@ -0,0 +1,207 @@
package http
import (
"strconv"
"go-common/app/service/live/wallet/model"
"go-common/library/ecode"
bm "go-common/library/net/http/blademaster"
)
func getBasicParam(c *bm.Context) *model.BasicParam {
bp := new(model.BasicParam)
var err error
bp.TransactionId = c.Request.Form.Get("transaction_id")
bp.BizCode = c.Request.Form.Get("biz_code")
bp.Area, err = strconv.ParseInt(c.Request.Form.Get("area_id"), 10, 64)
if err != nil {
bp.Area = 0
}
bp.Source = c.Request.Form.Get("source")
bp.BizSource = c.Request.Form.Get("biz_source")
bp.MetaData = c.Request.Form.Get("metadata")
bp.Reason, err = strconv.ParseInt(c.Request.Form.Get("biz_reason"), 10, 64)
if err != nil {
bp.Reason = 0
}
bp.Version, err = strconv.ParseInt(c.Request.Form.Get("version"), 10, 64)
if err != nil {
bp.Version = 0
}
return bp
}
func getWithMetal(c *bm.Context) (withMetal int, err error) {
withMetalStr := c.Request.Form.Get("with_metal")
if withMetalStr == "" {
withMetal = 0
return
}
// check params
withMetal, err = strconv.Atoi(withMetalStr)
if err != nil || (withMetal != 0 && withMetal != 1) {
err = ecode.RequestErr
return
}
return
}
func get(c *bm.Context) {
uidStr := c.Request.Form.Get("uid")
// check params
uid, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil || uid <= 0 {
c.JSON(nil, ecode.RequestErr)
return
}
withMetal, err := getWithMetal(c)
if err != nil {
c.JSON(nil, ecode.RequestErr)
return
}
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
c.JSON(walletSvr.Get(c, bp, uid, platform, withMetal))
}
func delCache(c *bm.Context) {
uidStr := c.Request.Form.Get("uid")
// check params
uid, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil || uid <= 0 {
c.JSON(nil, ecode.RequestErr)
return
}
bp := getBasicParam(c)
c.JSON(walletSvr.DelCache(c, bp, uid))
}
func getAll(c *bm.Context) {
uidStr := c.Request.Form.Get("uid")
// check params
uid, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil || uid <= 0 {
c.JSON(nil, ecode.RequestErr)
return
}
withMetal, err := getWithMetal(c)
if err != nil {
c.JSON(nil, ecode.RequestErr)
return
}
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
c.JSON(walletSvr.GetAll(c, bp, uid, platform, withMetal))
}
func getTid(c *bm.Context) {
typeStr := c.Request.Form.Get("type")
// check params
serviceType64, err := strconv.ParseInt(typeStr, 10, 64)
serviceType := int32(serviceType64)
if err != nil || serviceType < 0 {
c.JSON(nil, ecode.RequestErr)
return
}
params := c.Request.Form.Get("params")
if params == "" {
c.JSON(nil, ecode.RequestErr)
return
}
bp := getBasicParam(c)
c.JSON(walletSvr.GetTid(c, bp, 0, serviceType, params))
}
func recharge(c *bm.Context) {
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
arg := &model.RechargeOrPayForm{}
if err := c.Bind(arg); err != nil {
return
}
c.JSON(walletSvr.Recharge(c, bp, arg.Uid, platform, arg))
}
func modify(c *bm.Context) {
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
arg := &model.RechargeOrPayForm{}
if err := c.Bind(arg); err != nil {
return
}
c.JSON(walletSvr.Modify(c, bp, arg.Uid, platform, arg))
}
func pay(c *bm.Context) {
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
arg := &model.RechargeOrPayForm{}
if err := c.Bind(arg); err != nil {
return
}
var reason interface{}
reasonFromHttp := c.Request.Form.Get("reason")
if reasonFromHttp == "" {
reason = nil
} else {
reason = reasonFromHttp
}
c.JSON(walletSvr.Pay(c, bp, arg.Uid, platform, arg, reason))
}
func exchange(c *bm.Context) {
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
arg := &model.ExchangeForm{}
if err := c.Bind(arg); err != nil {
return
}
c.JSON(walletSvr.Exchange(c, bp, arg.Uid, platform, arg))
}
func query(c *bm.Context) {
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
if platform == "" {
platform = "pc"
}
tid := c.Request.Form.Get("transaction_id")
if tid == "" {
c.JSON(nil, ecode.RequestErr)
return
}
uidStr := c.Request.Form.Get("uid")
uid, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil || uid <= 0 {
uid = 0
return
}
c.JSON(walletSvr.Query(c, bp, uid, platform, tid))
}
func recordCoinStream(c *bm.Context) {
bp := getBasicParam(c)
platform := c.Request.Header.Get("platform")
arg := &model.RecordCoinStreamForm{}
if err := c.Bind(arg); err != nil {
return
}
c.JSON(walletSvr.RecordCoinStream(c, bp, arg.Uid, platform, arg))
}