github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/tx/tx_test.go (about) 1 package tx 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strconv" 8 "testing" 9 10 testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit" 11 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/tx" 12 13 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 14 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 15 16 "github.com/google/uuid" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func init() { 21 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 22 return 23 } 24 if err := testinit.Init(); err != nil { 25 fmt.Printf("cannot init test stub: %v\n", err) 26 } 27 } 28 29 var ret = &npool.Tx{ 30 CoinUnit: "BTC", 31 CoinLogo: uuid.NewString(), 32 CoinENV: "test", 33 FromAccountID: uuid.NewString(), 34 ToAccountID: uuid.NewString(), 35 Amount: "123.1", 36 FeeAmount: "2.01", 37 State: basetypes.TxState_TxStateCreated, 38 StateStr: basetypes.TxState_TxStateCreated.String(), 39 Type: basetypes.TxType_TxWithdraw, 40 TypeStr: basetypes.TxType_TxWithdraw.String(), 41 Extra: uuid.NewString(), 42 } 43 44 var req = &npool.TxReq{ 45 FromAccountID: &ret.FromAccountID, 46 ToAccountID: &ret.ToAccountID, 47 Amount: &ret.Amount, 48 FeeAmount: &ret.FeeAmount, 49 State: &ret.State, 50 Type: &ret.Type, 51 Extra: &ret.Extra, 52 } 53 54 func setupCoin(t *testing.T) func(*testing.T) { 55 ret.CoinTypeID = uuid.NewString() 56 req.CoinTypeID = &ret.CoinTypeID 57 ret.CoinName = uuid.NewString() 58 59 h1, err := coin1.NewHandler( 60 context.Background(), 61 coin1.WithEntID(&ret.CoinTypeID, true), 62 coin1.WithName(&ret.CoinName, true), 63 coin1.WithUnit(&ret.CoinUnit, true), 64 coin1.WithLogo(&ret.CoinLogo, true), 65 coin1.WithENV(&ret.CoinENV, true), 66 ) 67 assert.Nil(t, err) 68 69 _, err = h1.CreateCoin(context.Background()) 70 assert.Nil(t, err) 71 72 return func(*testing.T) { 73 _, _ = h1.DeleteCoin(context.Background()) 74 } 75 } 76 77 func create(t *testing.T) { 78 handler, err := NewHandler( 79 context.Background(), 80 WithCoinTypeID(req.CoinTypeID, true), 81 WithFromAccountID(req.FromAccountID, true), 82 WithToAccountID(req.ToAccountID, true), 83 WithAmount(req.Amount, true), 84 WithFeeAmount(req.FeeAmount, true), 85 WithChainTxID(req.ChainTxID, false), 86 WithState(req.State, true), 87 WithExtra(req.Extra, true), 88 WithType(req.Type, true), 89 ) 90 assert.Nil(t, err) 91 92 info, err := handler.CreateTx(context.Background()) 93 if assert.Nil(t, err) { 94 ret.UpdatedAt = info.UpdatedAt 95 ret.CreatedAt = info.CreatedAt 96 ret.ID = info.ID 97 ret.EntID = info.EntID 98 assert.Equal(t, info, ret) 99 } 100 } 101 102 func update(t *testing.T) { 103 ret.State = basetypes.TxState_TxStateCreatedCheck 104 ret.StateStr = ret.State.String() 105 req.State = &ret.State 106 107 handler, err := NewHandler( 108 context.Background(), 109 WithID(&ret.ID, true), 110 WithChainTxID(req.ChainTxID, false), 111 WithState(req.State, false), 112 WithExtra(req.Extra, false), 113 WithType(req.Type, false), 114 ) 115 assert.Nil(t, err) 116 117 info, err := handler.UpdateTx(context.Background()) 118 if assert.Nil(t, err) { 119 ret.UpdatedAt = info.UpdatedAt 120 assert.Equal(t, info, ret) 121 } 122 123 ret.State = basetypes.TxState_TxStateWait 124 ret.StateStr = ret.State.String() 125 req.State = &ret.State 126 127 handler, err = NewHandler( 128 context.Background(), 129 WithID(&ret.ID, true), 130 WithChainTxID(req.ChainTxID, false), 131 WithState(req.State, false), 132 WithExtra(req.Extra, false), 133 WithType(req.Type, false), 134 ) 135 assert.Nil(t, err) 136 137 _, err = handler.UpdateTx(context.Background()) 138 assert.Nil(t, err) 139 } 140 141 func TestTx(t *testing.T) { 142 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 143 return 144 } 145 146 teardown := setupCoin(t) 147 defer teardown(t) 148 149 t.Run("create", create) 150 t.Run("update", update) 151 }