github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/usedfor/usedfor_test.go (about) 1 package coinusedfor 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strconv" 8 "testing" 9 10 testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit" 11 types "github.com/NpoolPlatform/message/npool/basetypes/chain/v1" 12 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 13 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/usedfor" 14 15 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 16 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 17 18 "github.com/google/uuid" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func init() { 23 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 24 return 25 } 26 if err := testinit.Init(); err != nil { 27 fmt.Printf("cannot init test stub: %v\n", err) 28 } 29 } 30 31 var ret = &npool.CoinUsedFor{ 32 CoinName: "My BTC1", 33 CoinLogo: uuid.NewString(), 34 CoinUnit: "BTC", 35 CoinENV: "test", 36 UsedFor: types.CoinUsedFor_CoinUsedForGoodFee, 37 UsedForStr: types.CoinUsedFor_CoinUsedForGoodFee.String(), 38 Priority: 1, 39 } 40 41 var req = &npool.CoinUsedForReq{ 42 UsedFor: &ret.UsedFor, 43 } 44 45 func setupCoinUsedFor(t *testing.T) func(*testing.T) { 46 ret.CoinTypeID = uuid.NewString() 47 req.CoinTypeID = &ret.CoinTypeID 48 ret.CoinName = uuid.NewString() 49 50 h1, err := coin1.NewHandler( 51 context.Background(), 52 coin1.WithEntID(&ret.CoinTypeID, true), 53 coin1.WithName(&ret.CoinName, true), 54 coin1.WithLogo(&ret.CoinLogo, true), 55 coin1.WithUnit(&ret.CoinUnit, true), 56 coin1.WithENV(&ret.CoinENV, true), 57 ) 58 assert.Nil(t, err) 59 60 _, err = h1.CreateCoin(context.Background()) 61 assert.Nil(t, err) 62 63 return func(*testing.T) { 64 _, _ = h1.DeleteCoin(context.Background()) 65 } 66 } 67 68 func createCoinUsedFor(t *testing.T) { 69 handler, err := NewHandler( 70 context.Background(), 71 WithCoinTypeID(req.CoinTypeID, true), 72 WithUsedFor(req.UsedFor, true), 73 ) 74 assert.Nil(t, err) 75 76 info, err := handler.CreateCoinUsedFor(context.Background()) 77 if assert.Nil(t, err) { 78 ret.UpdatedAt = info.UpdatedAt 79 ret.CreatedAt = info.CreatedAt 80 ret.ID = info.ID 81 ret.EntID = info.EntID 82 assert.Equal(t, ret, info) 83 } 84 } 85 86 func getCoinUsedFors(t *testing.T) { 87 const singleRowLimit = 2 88 handler, err := NewHandler( 89 context.Background(), 90 WithConds(&npool.Conds{ 91 CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID}, 92 }), 93 WithOffset(0), 94 WithLimit(singleRowLimit), 95 ) 96 assert.Nil(t, err) 97 98 infos, total, err := handler.GetCoinUsedFors(context.Background()) 99 if assert.Nil(t, err) { 100 assert.Equal(t, uint32(1), total) 101 assert.Equal(t, 1, len(infos)) 102 assert.Equal(t, ret, infos[0]) 103 } 104 } 105 106 func deleteCoinUsedFor(t *testing.T) { 107 handler, err := NewHandler( 108 context.Background(), 109 WithID(&ret.ID, true), 110 ) 111 assert.Nil(t, err) 112 113 info, err := handler.DeleteCoinUsedFor(context.Background()) 114 if assert.Nil(t, err) { 115 ret.UpdatedAt = info.UpdatedAt 116 assert.Equal(t, ret, info) 117 } 118 } 119 120 func TestCoin(t *testing.T) { 121 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 122 return 123 } 124 125 teardown := setupCoinUsedFor(t) 126 defer teardown(t) 127 128 t.Run("createCoinUsedFor", createCoinUsedFor) 129 t.Run("getCoinUsedFors", getCoinUsedFors) 130 t.Run("deleteCoinUsedFor", deleteCoinUsedFor) 131 }