From 5e71fabbf9d381c84c62901aaeb3a4098bc741a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Fri, 15 Nov 2024 10:34:36 +0800 Subject: [PATCH] test --- dctw/v1/api/card/card.go | 2 - dctw/v1/api/card/card_test.go | 19 +++++-- dctw/v1/api/card/code.go | 50 ++++++++++--------- dctw/v1/api/card/trans.go | 18 +++++-- dctw/v1/api/direct/{vo.go => account_type.go} | 0 dctw/v1/api/direct/code.go | 6 ++- dctw/v1/api/direct/direct_test.go | 12 +++-- dctw/v1/api/direct/trans.go | 18 +++++-- go.mod | 4 ++ go.sum | 2 + 10 files changed, 87 insertions(+), 44 deletions(-) rename dctw/v1/api/direct/{vo.go => account_type.go} (100%) diff --git a/dctw/v1/api/card/card.go b/dctw/v1/api/card/card.go index fbf1825..bf7b5d8 100644 --- a/dctw/v1/api/card/card.go +++ b/dctw/v1/api/card/card.go @@ -3,7 +3,6 @@ package card import ( "context" "encoding/json" - "fmt" "gitea.cdlsxd.cn/sdk/plugin/dctw/v1/api" "gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core" "io/ioutil" @@ -34,7 +33,6 @@ func (c *Card) Query(ctx context.Context, request *Query) (*QueryResp, error) { if err != nil { return nil, err } - fmt.Println("result:", string(result)) var response *QueryResp if err = json.Unmarshal(result, &response); err != nil { return nil, err diff --git a/dctw/v1/api/card/card_test.go b/dctw/v1/api/card/card_test.go index e6f53ae..ed8a3da 100644 --- a/dctw/v1/api/card/card_test.go +++ b/dctw/v1/api/card/card_test.go @@ -4,12 +4,13 @@ import ( "bytes" "context" "gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core" + "github.com/stretchr/testify/assert" "io/ioutil" "net/http" "testing" ) -func TestCardOrder(t *testing.T) { +func TestCard_Order(t *testing.T) { server, err := core.NewDctWServer( &core.DctWConfig{ AppId: "1", @@ -24,9 +25,9 @@ func TestCardOrder(t *testing.T) { a := &Card{DctWServer: server} req := &Order{ Number: 1, - MerchantId: "101", + MerchantId: "23329", OutTradeNo: "test_1_zltx_4", - ProductId: "106", + ProductId: "222", Mobile: "18666666666", NotifyUrl: "http://test.openapi.1688sup.cn", Version: "1.0", @@ -36,6 +37,10 @@ func TestCardOrder(t *testing.T) { t.Error(err) return } + if !resp.GetCode().IsSuccess() { + t.Error(resp.Message) + return + } t.Log(resp) } @@ -50,7 +55,7 @@ func TestCard_Query(t *testing.T) { } a := &Card{DctWServer: server} req := &Query{ - MerchantId: "101", + MerchantId: "23329", OutTradeNo: "test_1_zltx_4", Version: "1.0", } @@ -59,7 +64,12 @@ func TestCard_Query(t *testing.T) { t.Error(err) return } + if !resp.GetCode().IsSuccess() { + t.Error(resp.Message) + return + } t.Log(resp) + assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功") } func TestCard_Notify(t *testing.T) { @@ -89,4 +99,5 @@ func TestCard_Notify(t *testing.T) { return } t.Log(resp) + assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功") } diff --git a/dctw/v1/api/card/code.go b/dctw/v1/api/card/code.go index 317f82b..4a3c752 100644 --- a/dctw/v1/api/card/code.go +++ b/dctw/v1/api/card/code.go @@ -1,38 +1,40 @@ package card -type Code string +import "encoding/json" -const CodeSuccess Code = "0000" -const RechargeCodeSuccess Code = "2000" +type Code json.Number + +const ( + CodeSuccess Code = "2000" + CodeSuccess2 Code = "0000" +) var ResMessageMap = map[Code]string{ - "1000": "Ip limit(ip未绑定或绑定失败)", - "1001": "Missing parameters(参数异常)", - "1002": "Invalid merchant(无效商户信息)", - "1003": "Invalid signature(签名校验失败)", - "1004": "Request expiration(请求时间过期)", - "1005": "Order repeat(订单重复)", - "1006": "Invalid item(商品未开通)", - "1007": "Item price invalid(商品价格无效)", - "1008": "Insufficient Balance(余额不足)", - "1009": "Interface adjustment(商品映射无效)", - "1010": "Interface price adjustment(映射价格无效)", - "1011": "Account format matching(充值账号格式不匹配)", - "1012": "no order(无订单信息)", - "1999": "unknown error(异常错误,建议人工处理或查询订单状态)", + CodeSuccess: "成功", + "1000": "Ip limit(ip未绑定或绑定失败)", + "1001": "Missing parameters(参数异常)", + "1002": "Invalid merchant(无效商户信息)", + "1003": "Invalid signature(签名校验失败)", + "1004": "Request expiration(请求时间过期)", + "1005": "Order repeat(订单重复)", + "1006": "Invalid item(商品未开通)", + "1007": "Item price invalid(商品价格无效)", + "1008": "Insufficient Balance(余额不足)", + "1009": "Interface adjustment(商品映射无效)", + "1010": "Interface price adjustment(映射价格无效)", + "1011": "Account format matching(充值账号格式不匹配)", + "1012": "no order(无订单信息)", + "1999": "unknown error(异常错误,建议人工处理或查询订单状态)", } -func (c Code) IsRechargeSuccess() bool { - return c == RechargeCodeSuccess +func (c Code) IsSuccess() bool { + // 根据沟通对接,两种都是请求成功 + return c == CodeSuccess || c == CodeSuccess2 } func (c Code) GetMessage() string { if message, ok := ResMessageMap[c]; ok { return message } - return "" -} - -func (c Code) IsSuccess() bool { - return c == CodeSuccess + return "未知错误,请联系平台" } diff --git a/dctw/v1/api/card/trans.go b/dctw/v1/api/card/trans.go index 5a1743a..9270ae4 100644 --- a/dctw/v1/api/card/trans.go +++ b/dctw/v1/api/card/trans.go @@ -3,7 +3,6 @@ package card import ( "encoding/json" "fmt" - "gitea.cdlsxd.cn/sdk/plugin/alipay/vo" "github.com/go-playground/validator/v10" ) @@ -18,8 +17,9 @@ type Order struct { } type OrderResp struct { - Code vo.Code `json:"code"` - Message string `json:"message"` + Code json.Number `json:"code"` + Message string `json:"message"` + TradeNo string `json:"tradeNo"` } type Query struct { @@ -29,7 +29,7 @@ type Query struct { } type QueryResp struct { - Code vo.Code `json:"code"` + Code json.Number `json:"code"` Status OrderStatus `json:"status"` Message string `json:"message"` OutTradeNo string `json:"outTradeNo"` @@ -97,3 +97,13 @@ func (o *Notify) Validate() error { } return nil } + +func (o *OrderResp) GetCode() Code { + // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal + return Code(o.Code) +} + +func (o *QueryResp) GetCode() Code { + // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal + return Code(o.Code) +} diff --git a/dctw/v1/api/direct/vo.go b/dctw/v1/api/direct/account_type.go similarity index 100% rename from dctw/v1/api/direct/vo.go rename to dctw/v1/api/direct/account_type.go diff --git a/dctw/v1/api/direct/code.go b/dctw/v1/api/direct/code.go index c6e4cd8..801c1a4 100644 --- a/dctw/v1/api/direct/code.go +++ b/dctw/v1/api/direct/code.go @@ -5,7 +5,8 @@ import "encoding/json" type Code json.Number const ( - CodeSuccess Code = "2000" + CodeSuccess Code = "2000" + CodeSuccess2 Code = "0000" ) var ResMessageMap = map[Code]string{ @@ -27,7 +28,8 @@ var ResMessageMap = map[Code]string{ } func (c Code) IsSuccess() bool { - return c == CodeSuccess + // 根据沟通对接,两种都是请求成功 + return c == CodeSuccess || c == CodeSuccess2 } func (c Code) GetMessage() string { diff --git a/dctw/v1/api/direct/direct_test.go b/dctw/v1/api/direct/direct_test.go index a715554..210aca5 100644 --- a/dctw/v1/api/direct/direct_test.go +++ b/dctw/v1/api/direct/direct_test.go @@ -26,7 +26,7 @@ func TestDirect_Order(t *testing.T) { req := &Order{ Number: 1, MerchantId: "23329", - OutTradeNo: "test_1_zltx_6", + OutTradeNo: "test_1_zltx_7", ProductId: "106", AccountType: 1, RechargeAccount: "18666666666", @@ -38,7 +38,7 @@ func TestDirect_Order(t *testing.T) { t.Error(err) return } - if !resp.Code.IsSuccess() { + if !resp.GetCode().IsSuccess() { t.Error(resp.Message) return } @@ -56,8 +56,8 @@ func TestDirect_Query(t *testing.T) { } a := &Direct{DctWServer: server} req := &Query{ - MerchantId: "106", - OutTradeNo: "test_1_zltx_4", + MerchantId: "23329", + OutTradeNo: "test_1_zltx_7", Version: "1.0", } resp, err := a.Query(context.Background(), req) @@ -65,6 +65,10 @@ func TestDirect_Query(t *testing.T) { t.Error(err) return } + if !resp.GetCode().IsSuccess() { + t.Error(resp.Message) + return + } t.Log(resp) assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功") } diff --git a/dctw/v1/api/direct/trans.go b/dctw/v1/api/direct/trans.go index 43b7cd4..421e286 100644 --- a/dctw/v1/api/direct/trans.go +++ b/dctw/v1/api/direct/trans.go @@ -18,9 +18,9 @@ type Order struct { } type OrderResp struct { - Code Code `json:"code"` - Message string `json:"message"` - TradeNo string `json:"tradeNo"` + Code json.Number `json:"code"` + Message string `json:"message"` + TradeNo string `json:"tradeNo"` } type Query struct { @@ -30,7 +30,7 @@ type Query struct { } type QueryResp struct { - Code Code `json:"code"` + Code json.Number `json:"code"` Status OrderStatus `json:"status"` Message string `json:"message"` OutTradeNo string `json:"outTradeNo"` @@ -97,3 +97,13 @@ func (o *Notify) Validate() error { } return nil } + +func (o *OrderResp) GetCode() Code { + // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal + return Code(o.Code) +} + +func (o *QueryResp) GetCode() Code { + // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal + return Code(o.Code) +} diff --git a/go.mod b/go.mod index 5892bc0..497e0e9 100644 --- a/go.mod +++ b/go.mod @@ -7,11 +7,13 @@ require ( github.com/go-playground/validator/v10 v10.22.0 github.com/hashicorp/go-plugin v1.6.1 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -24,9 +26,11 @@ require ( github.com/mattn/go-isatty v0.0.10 // indirect github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect github.com/oklog/run v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/crypto v0.25.0 // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 55fd121..feba17b 100644 --- a/go.sum +++ b/go.sum @@ -62,5 +62,7 @@ google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=