github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/app/coin/client_test.go (about) 1 package appcoin 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/app/coin" 21 "github.com/stretchr/testify/assert" 22 23 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 24 25 cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 26 27 "github.com/google/uuid" 28 ) 29 30 func init() { 31 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 32 return 33 } 34 if err := testinit.Init(); err != nil { 35 fmt.Printf("cannot init test stub: %v\n", err) 36 } 37 } 38 39 var name = uuid.NewString() 40 var unit = uuid.NewString() 41 var logo = uuid.NewString() 42 43 var ret = &npool.Coin{ 44 EntID: uuid.NewString(), 45 AppID: uuid.NewString(), 46 CoinName: name, 47 Name: name, 48 DisplayNamesStr: "[]", 49 DisplayNames: []string{"tttttttt", "tttt1"}, 50 Logo: logo, 51 Unit: unit, 52 Presale: false, 53 ENV: "main", 54 ForPay: false, 55 ReservedAmount: "0.000000000000000000", 56 WithdrawFeeByStableUSD: true, 57 WithdrawFeeAmount: "0.000000000000000000", 58 CollectFeeAmount: "0.000000000000000000", 59 HotWalletFeeAmount: "0.000000000000000000", 60 LowFeeAmount: "0.000000000000000000", 61 HotLowFeeAmount: "0.000000000000000000", 62 HotWalletAccountAmount: "0.000000000000000000", 63 PaymentAccountCollectAmount: "0.000000000000000000", 64 FeeCoinName: name, 65 FeeCoinUnit: unit, 66 FeeCoinENV: "main", 67 WithdrawAutoReviewAmount: "0.000000000000000000", 68 MarketValue: "0.000000000000000000", 69 SettleValue: "0.000000000000000000", 70 SettlePercent: 80, 71 SettleTipsStr: "[]", 72 SettleTips: []string{"tttttttttttt", "ttt"}, 73 Setter: uuid.NewString(), 74 Display: true, 75 DailyRewardAmount: "0.000000000000000000", 76 MaxAmountPerWithdraw: "0.000000000000000000", 77 LeastTransferAmount: "0.000000000000000000", 78 } 79 80 var chainType = uuid.NewString() 81 var chainAtomicUnit = uuid.NewString() 82 var chainUnitExp = uint32(1) 83 var gasType = basetypes.GasType_FixedGas 84 var chainID = uuid.NewString() 85 var chainNickname = uuid.NewString() 86 var chainNativeCoinName = uuid.NewString() 87 88 var req = &npool.CoinReq{ 89 AppID: &ret.AppID, 90 Name: &ret.Name, 91 DisplayNames: ret.DisplayNames, 92 Logo: &ret.Logo, 93 ForPay: &ret.ForPay, 94 WithdrawAutoReviewAmount: &ret.WithdrawAutoReviewAmount, 95 MarketValue: &ret.MarketValue, 96 SettlePercent: &ret.SettlePercent, 97 SettleTips: ret.SettleTips, 98 Setter: &ret.Setter, 99 DailyRewardAmount: &ret.DailyRewardAmount, 100 MaxAmountPerWithdraw: &ret.MaxAmountPerWithdraw, 101 } 102 103 func setupAppCoin(t *testing.T) func(*testing.T) { 104 ret.CoinTypeID = uuid.NewString() 105 ret.FeeCoinLogo = ret.Logo 106 req.CoinTypeID = &ret.CoinTypeID 107 108 h1, err := coin1.NewHandler( 109 context.Background(), 110 coin1.WithEntID(&ret.CoinTypeID, true), 111 coin1.WithName(&ret.CoinName, true), 112 coin1.WithUnit(&ret.Unit, true), 113 coin1.WithLogo(&ret.Logo, true), 114 coin1.WithENV(&ret.ENV, true), 115 coin1.WithChainType(&chainType, true), 116 coin1.WithChainNativeUnit(&ret.Unit, true), 117 coin1.WithChainAtomicUnit(&chainAtomicUnit, true), 118 coin1.WithChainUnitExp(&chainUnitExp, true), 119 coin1.WithGasType(&gasType, true), 120 coin1.WithChainID(&chainID, true), 121 coin1.WithChainNickname(&chainNickname, true), 122 coin1.WithChainNativeCoinName(&chainNativeCoinName, true), 123 ) 124 assert.Nil(t, err) 125 126 _, err = h1.CreateCoin(context.Background()) 127 assert.Nil(t, err) 128 129 return func(*testing.T) { 130 _, _ = h1.DeleteCoin(context.Background()) 131 } 132 } 133 134 func createCoin(t *testing.T) { 135 info, err := CreateCoin(context.Background(), req) 136 if assert.Nil(t, err) { 137 ret.CreatedAt = info.CreatedAt 138 ret.UpdatedAt = info.UpdatedAt 139 ret.ID = info.ID 140 ret.EntID = info.EntID 141 ret.FeeCoinTypeID = info.FeeCoinTypeID 142 ret.DisplayNamesStr = info.DisplayNamesStr 143 ret.SettleTipsStr = info.SettleTipsStr 144 ret.CheckNewAddressBalance = info.CheckNewAddressBalance 145 assert.Equal(t, ret, info) 146 } 147 } 148 149 func updateCoin(t *testing.T) { 150 amount := "123.700000000000000000" 151 index := uint32(1) 152 153 ret.WithdrawAutoReviewAmount = amount 154 ret.MarketValue = amount 155 ret.SettleValue = "98.960000000000000000" 156 ret.DisplayIndex = index 157 158 req.ID = &ret.ID 159 req.WithdrawAutoReviewAmount = &amount 160 req.MarketValue = &amount 161 req.DisplayIndex = &index 162 163 info, err := UpdateCoin(context.Background(), req) 164 if assert.Nil(t, err) { 165 ret.UpdatedAt = info.UpdatedAt 166 assert.Equal(t, info, ret) 167 } 168 } 169 170 func getCoin(t *testing.T) { 171 info, err := GetCoin(context.Background(), ret.EntID) 172 if assert.Nil(t, err) { 173 assert.Equal(t, info, ret) 174 } 175 } 176 177 func getCoins(t *testing.T) { 178 infos, total, err := GetCoins(context.Background(), &npool.Conds{ 179 EntID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID}, 180 }, 0, 100) 181 if assert.Nil(t, err) { 182 assert.Equal(t, len(infos), 1) 183 assert.Equal(t, total, uint32(1)) 184 if assert.Equal(t, len(infos), 1) { 185 assert.Equal(t, infos[0], ret) 186 } 187 } 188 } 189 190 func deleteCoin(t *testing.T) { 191 info, err := DeleteCoin(context.Background(), ret.ID) 192 if assert.Nil(t, err) { 193 assert.Equal(t, info, ret) 194 } 195 196 info, err = GetCoin(context.Background(), ret.EntID) 197 assert.Nil(t, err) 198 assert.Nil(t, info) 199 } 200 201 func TestClient(t *testing.T) { 202 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 203 return 204 } 205 // Here won't pass test due to we always test with localhost 206 207 teardown := setupAppCoin(t) 208 defer teardown(t) 209 210 gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort) 211 212 monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) { 213 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 214 }) 215 monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) { 216 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 217 }) 218 219 t.Run("createCoin", createCoin) 220 t.Run("updateCoin", updateCoin) 221 t.Run("getCoin", getCoin) 222 t.Run("getCoins", getCoins) 223 t.Run("deleteCoin", deleteCoin) 224 }