github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/tx/client_test.go (about) 1 package tx 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strconv" 8 "testing" 9 10 "github.com/NpoolPlatform/chain-middleware/pkg/testinit" 11 "github.com/NpoolPlatform/go-service-framework/pkg/config" 12 13 "bou.ke/monkey" 14 grpc2 "github.com/NpoolPlatform/go-service-framework/pkg/grpc" 15 "github.com/stretchr/testify/assert" 16 "google.golang.org/grpc" 17 "google.golang.org/grpc/credentials/insecure" 18 19 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 20 cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 21 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 22 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/tx" 23 24 "github.com/google/uuid" 25 ) 26 27 func init() { 28 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 29 return 30 } 31 if err := testinit.Init(); err != nil { 32 fmt.Printf("cannot init test stub: %v\n", err) 33 } 34 } 35 36 var ret = &npool.Tx{ 37 EntID: uuid.NewString(), 38 CoinName: uuid.NewString(), 39 CoinUnit: "BTC", 40 CoinENV: "test", 41 FromAccountID: uuid.NewString(), 42 ToAccountID: uuid.NewString(), 43 Amount: "123.345", 44 FeeAmount: "2.001", 45 State: basetypes.TxState_TxStateCreated, 46 StateStr: basetypes.TxState_TxStateCreated.String(), 47 Extra: uuid.NewString(), 48 Type: basetypes.TxType_TxWithdraw, 49 TypeStr: basetypes.TxType_TxWithdraw.String(), 50 } 51 52 var req = &npool.TxReq{ 53 EntID: &ret.EntID, 54 FromAccountID: &ret.FromAccountID, 55 ToAccountID: &ret.ToAccountID, 56 Amount: &ret.Amount, 57 FeeAmount: &ret.FeeAmount, 58 State: &ret.State, 59 Extra: &ret.Extra, 60 Type: &ret.Type, 61 } 62 63 func setupTx(t *testing.T) func(*testing.T) { 64 ret.CoinTypeID = uuid.NewString() 65 req.CoinTypeID = &ret.CoinTypeID 66 67 h1, err := coin1.NewHandler( 68 context.Background(), 69 coin1.WithEntID(&ret.CoinTypeID, true), 70 coin1.WithName(&ret.CoinName, true), 71 coin1.WithUnit(&ret.CoinUnit, true), 72 coin1.WithLogo(&ret.CoinLogo, true), 73 coin1.WithENV(&ret.CoinENV, true), 74 ) 75 assert.Nil(t, err) 76 77 _, err = h1.CreateCoin(context.Background()) 78 assert.Nil(t, err) 79 80 return func(*testing.T) { 81 _, _ = h1.DeleteCoin(context.Background()) 82 } 83 } 84 85 func createTx(t *testing.T) { 86 info, err := CreateTx(context.Background(), req) 87 if assert.Nil(t, err) { 88 ret.CreatedAt = info.CreatedAt 89 ret.UpdatedAt = info.UpdatedAt 90 ret.ID = info.ID 91 ret.EntID = info.EntID 92 assert.Equal(t, ret, info) 93 } 94 } 95 96 func updateTx(t *testing.T) { 97 state := basetypes.TxState_TxStateTransferring 98 99 ret.State = state 100 101 req.ID = &ret.ID 102 req.State = &state 103 104 _, err := UpdateTx(context.Background(), req) 105 assert.NotNil(t, err) 106 107 state = basetypes.TxState_TxStateCreatedCheck 108 109 ret.State = state 110 ret.StateStr = state.String() 111 req.State = &state 112 113 info, err := UpdateTx(context.Background(), req) 114 if assert.Nil(t, err) { 115 ret.UpdatedAt = info.UpdatedAt 116 assert.Equal(t, info, ret) 117 } 118 } 119 120 func getTx(t *testing.T) { 121 info, err := GetTx(context.Background(), ret.EntID) 122 if assert.Nil(t, err) { 123 assert.Equal(t, info, ret) 124 } 125 } 126 127 func getTxs(t *testing.T) { 128 infos, total, err := GetTxs(context.Background(), &npool.Conds{ 129 EntID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID}, 130 }, 0, 1) 131 if assert.Nil(t, err) { 132 assert.Equal(t, len(infos), 1) 133 assert.Equal(t, total, uint32(1)) 134 assert.Equal(t, infos[0], ret) 135 } 136 } 137 138 func TestClient(t *testing.T) { 139 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 140 return 141 } 142 // Here won't pass test due to we always test with localhost 143 144 gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort) 145 146 teardown := setupTx(t) 147 defer teardown(t) 148 149 monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) { 150 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 151 }) 152 monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) { 153 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 154 }) 155 156 t.Run("createTx", createTx) 157 t.Run("updateTx", updateTx) 158 t.Run("getTx", getTx) 159 t.Run("getTxs", getTxs) 160 }