github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/client_test.go (about) 1 package coin 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strconv" 8 "testing" 9 10 "github.com/NpoolPlatform/chain-middleware/pkg/testinit" 11 12 "github.com/NpoolPlatform/go-service-framework/pkg/config" 13 14 "bou.ke/monkey" 15 grpc2 "github.com/NpoolPlatform/go-service-framework/pkg/grpc" 16 "google.golang.org/grpc" 17 "google.golang.org/grpc/credentials/insecure" 18 19 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 20 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin" 21 "github.com/stretchr/testify/assert" 22 23 cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 24 25 "github.com/google/uuid" 26 ) 27 28 func init() { 29 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 30 return 31 } 32 if err := testinit.Init(); err != nil { 33 fmt.Printf("cannot init test stub: %v\n", err) 34 } 35 } 36 37 var name = uuid.NewString() 38 var unit = uuid.NewString() 39 var chainType = uuid.NewString() 40 var chainAtomicUnit = uuid.NewString() 41 var chainUnitExp = uint32(1) 42 var gasType = basetypes.GasType_FixedGas 43 var chainID = uuid.NewString() 44 var chainNickname = uuid.NewString() 45 var chainNativeCoinName = uuid.NewString() 46 var ret = &npool.Coin{ 47 Name: name, 48 Presale: false, 49 Unit: unit, 50 ENV: "main", 51 ForPay: false, 52 ReservedAmount: "0.000000000000000000", 53 WithdrawFeeByStableUSD: true, 54 WithdrawFeeAmount: "0.000000000000000000", 55 CollectFeeAmount: "0.000000000000000000", 56 HotWalletFeeAmount: "0.000000000000000000", 57 LowFeeAmount: "0.000000000000000000", 58 HotLowFeeAmount: "0.000000000000000000", 59 HotWalletAccountAmount: "0.000000000000000000", 60 PaymentAccountCollectAmount: "0.000000000000000000", 61 FeeCoinName: name, 62 FeeCoinUnit: unit, 63 FeeCoinENV: "main", 64 LeastTransferAmount: "0.000000000000000000", 65 CheckNewAddressBalance: true, 66 } 67 68 var req = &npool.CoinReq{ 69 Name: &ret.Name, 70 Unit: &ret.Unit, 71 ENV: &ret.ENV, 72 ChainType: &chainType, 73 ChainAtomicUnit: &chainAtomicUnit, 74 ChainUnitExp: &chainUnitExp, 75 GasType: &gasType, 76 ChainID: &chainID, 77 ChainNickname: &chainNickname, 78 ChainNativeCoinName: &chainNativeCoinName, 79 ChainNativeUnit: &ret.Unit, 80 } 81 82 func createCoin(t *testing.T) { 83 info, err := CreateCoin(context.Background(), req) 84 if assert.Nil(t, err) { 85 ret.CreatedAt = info.CreatedAt 86 ret.UpdatedAt = info.UpdatedAt 87 ret.ID = info.ID 88 ret.EntID = info.EntID 89 ret.FeeCoinTypeID = info.FeeCoinTypeID 90 assert.Equal(t, ret, info) 91 } 92 } 93 94 func updateCoin(t *testing.T) { 95 feeByUSD := false 96 amount := "123.700000000000000000" 97 logo := uuid.NewString() 98 99 ret.Logo = logo 100 ret.WithdrawFeeByStableUSD = feeByUSD 101 ret.ReservedAmount = amount 102 ret.WithdrawFeeAmount = amount 103 ret.CollectFeeAmount = amount 104 ret.HotWalletFeeAmount = amount 105 ret.LowFeeAmount = amount 106 ret.HotLowFeeAmount = amount 107 ret.HotWalletAccountAmount = amount 108 ret.PaymentAccountCollectAmount = amount 109 ret.LeastTransferAmount = amount 110 ret.FeeCoinLogo = logo 111 112 req.ID = &ret.ID 113 req.Logo = &logo 114 req.WithdrawFeeByStableUSD = &feeByUSD 115 req.ReservedAmount = &amount 116 req.WithdrawFeeAmount = &amount 117 req.CollectFeeAmount = &amount 118 req.HotWalletFeeAmount = &amount 119 req.LowFeeAmount = &amount 120 req.HotLowFeeAmount = &amount 121 req.HotWalletAccountAmount = &amount 122 req.PaymentAccountCollectAmount = &amount 123 req.LeastTransferAmount = &amount 124 125 info, err := UpdateCoin(context.Background(), req) 126 if assert.Nil(t, err) { 127 ret.UpdatedAt = info.UpdatedAt 128 assert.Equal(t, info, ret) 129 } 130 } 131 132 func getCoin(t *testing.T) { 133 info, err := GetCoin(context.Background(), ret.EntID) 134 if assert.Nil(t, err) { 135 assert.Equal(t, info, ret) 136 } 137 } 138 139 func getCoins(t *testing.T) { 140 infos, total, err := GetCoins(context.Background(), &npool.Conds{ 141 EntID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID}, 142 }, 0, 100) 143 if assert.Nil(t, err) { 144 assert.Equal(t, len(infos), 1) 145 assert.Equal(t, total, uint32(1)) 146 assert.Equal(t, infos[0], ret) 147 } 148 } 149 150 func TestClient(t *testing.T) { 151 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 152 return 153 } 154 // Here won't pass test due to we always test with localhost 155 156 gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort) 157 158 monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) { 159 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 160 }) 161 monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) { 162 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 163 }) 164 165 t.Run("createCoin", createCoin) 166 t.Run("updateCoin", updateCoin) 167 t.Run("getCoin", getCoin) 168 t.Run("getCoins", getCoins) 169 }