github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/app/coin/coin_test.go (about) 1 package appcoin 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/app/coin" 12 13 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 14 15 "github.com/google/uuid" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func init() { 20 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 21 return 22 } 23 if err := testinit.Init(); err != nil { 24 fmt.Printf("cannot init test stub: %v\n", err) 25 } 26 } 27 28 var ( 29 coinUnit = "BTC" 30 coinENV = "test" 31 ) 32 33 var ret = &npool.Coin{ 34 AppID: uuid.NewString(), 35 Name: "My BTC1", 36 DisplayNames: []string{"123123", "2323"}, 37 Logo: uuid.NewString(), 38 Unit: coinUnit, 39 ENV: coinENV, 40 ReservedAmount: "0.000000000000000000", 41 WithdrawFeeByStableUSD: true, 42 WithdrawFeeAmount: "0.000000000000000000", 43 CollectFeeAmount: "0.000000000000000000", 44 HotWalletFeeAmount: "0.000000000000000000", 45 LowFeeAmount: "0.000000000000000000", 46 HotLowFeeAmount: "0.000000000000000000", 47 HotWalletAccountAmount: "0.000000000000000000", 48 PaymentAccountCollectAmount: "0.000000000000000000", 49 FeeCoinUnit: coinUnit, 50 FeeCoinENV: coinENV, 51 WithdrawAutoReviewAmount: "0.000000000000000000", 52 MarketValue: "0.000000000000000000", 53 SettleValue: "0.000000000000000000", 54 SettlePercent: 90, 55 SettleTips: []string{"1213", "234234"}, 56 Setter: uuid.NewString(), 57 Display: true, 58 DailyRewardAmount: "0.000000000000000000", 59 MaxAmountPerWithdraw: "0.000000000000000000", 60 LeastTransferAmount: "0.000000000000000000", 61 } 62 63 var req = &npool.CoinReq{ 64 AppID: &ret.AppID, 65 Name: &ret.Name, 66 DisplayNames: ret.DisplayNames, 67 Logo: &ret.Logo, 68 SettlePercent: &ret.SettlePercent, 69 SettleTips: ret.SettleTips, 70 Setter: &ret.Setter, 71 } 72 73 func setupCoin(t *testing.T) func(*testing.T) { 74 ret.CoinTypeID = uuid.NewString() 75 req.CoinTypeID = &ret.CoinTypeID 76 ret.CoinName = uuid.NewString() 77 ret.FeeCoinName = ret.CoinName 78 79 h1, err := coin1.NewHandler( 80 context.Background(), 81 coin1.WithEntID(&ret.CoinTypeID, true), 82 coin1.WithName(&ret.CoinName, true), 83 coin1.WithUnit(&coinUnit, true), 84 coin1.WithENV(&coinENV, true), 85 coin1.WithChainNativeUnit(&coinUnit, true), 86 ) 87 assert.Nil(t, err) 88 89 _, err = h1.CreateCoin(context.Background()) 90 assert.Nil(t, err) 91 92 return func(*testing.T) { 93 _, _ = h1.DeleteCoin(context.Background()) 94 } 95 } 96 97 func create(t *testing.T) { 98 handler, err := NewHandler( 99 context.Background(), 100 WithEntID(req.EntID, false), 101 WithAppID(req.AppID, true), 102 WithCoinTypeID(req.CoinTypeID, true), 103 WithName(req.Name, true), 104 WithDisplayNames(req.DisplayNames, false), 105 WithLogo(req.Logo, true), 106 WithForPay(req.ForPay, false), 107 WithProductPage(req.ProductPage, false), 108 WithWithdrawAutoReviewAmount(req.WithdrawAutoReviewAmount, false), 109 WithDailyRewardAmount(req.DailyRewardAmount, false), 110 WithDisplay(req.Display, false), 111 WithDisplayIndex(req.DisplayIndex, false), 112 WithMaxAmountPerWithdraw(req.MaxAmountPerWithdraw, false), 113 WithMarketValue(req.MarketValue, false), 114 WithSettlePercent(req.SettlePercent, false), 115 WithSettleTips(req.SettleTips, false), 116 WithSetter(req.Setter, false), 117 ) 118 assert.Nil(t, err) 119 120 info, err := handler.CreateCoin(context.Background()) 121 if assert.Nil(t, err) { 122 ret.UpdatedAt = info.UpdatedAt 123 ret.CreatedAt = info.CreatedAt 124 ret.ID = info.ID 125 ret.EntID = info.EntID 126 ret.FeeCoinTypeID = ret.CoinTypeID 127 ret.DisplayNamesStr = info.DisplayNamesStr 128 ret.SettleTipsStr = info.SettleTipsStr 129 ret.CheckNewAddressBalance = info.CheckNewAddressBalance 130 assert.Equal(t, info, ret) 131 } 132 } 133 134 func update(t *testing.T) { 135 amount := "123.700000000000000000" 136 logo := uuid.NewString() 137 138 ret.Logo = logo 139 ret.WithdrawAutoReviewAmount = amount 140 ret.MarketValue = amount 141 ret.SettleValue = "111.330000000000000000" 142 143 req.ID = &ret.ID 144 req.Logo = &logo 145 req.WithdrawAutoReviewAmount = &amount 146 req.MarketValue = &amount 147 148 handler, err := NewHandler( 149 context.Background(), 150 WithID(req.ID, true), 151 WithName(req.Name, false), 152 WithDisplayNames(req.DisplayNames, false), 153 WithLogo(req.Logo, false), 154 WithForPay(req.ForPay, false), 155 WithProductPage(req.ProductPage, false), 156 WithWithdrawAutoReviewAmount(req.WithdrawAutoReviewAmount, false), 157 WithDailyRewardAmount(req.DailyRewardAmount, false), 158 WithDisplay(req.Display, false), 159 WithDisplayIndex(req.DisplayIndex, false), 160 WithMaxAmountPerWithdraw(req.MaxAmountPerWithdraw, false), 161 WithMarketValue(req.MarketValue, false), 162 WithSettlePercent(req.SettlePercent, false), 163 WithSettleTips(req.SettleTips, false), 164 WithSetter(req.Setter, false), 165 ) 166 assert.Nil(t, err) 167 168 info, err := handler.UpdateCoin(context.Background()) 169 if assert.Nil(t, err) { 170 ret.UpdatedAt = info.UpdatedAt 171 assert.Equal(t, info, ret) 172 } 173 } 174 175 func _delete(t *testing.T) { 176 handler, err := NewHandler( 177 context.Background(), 178 WithID(&ret.ID, true), 179 ) 180 assert.Nil(t, err) 181 182 _, err = handler.DeleteCoin(context.Background()) 183 assert.Nil(t, err) 184 } 185 186 func TestCoin(t *testing.T) { 187 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 188 return 189 } 190 191 teardown := setupCoin(t) 192 defer teardown(t) 193 194 t.Run("create", create) 195 t.Run("update", update) 196 t.Run("delete", _delete) 197 }