github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/fiat/coinfiat_test.go (about) 1 package coinfiat 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strconv" 8 "testing" 9 10 testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit" 11 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 12 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat" 13 14 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 15 fiat1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/fiat" 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.CoinFiat{ 32 CoinName: "My BTC1", 33 CoinLogo: uuid.NewString(), 34 CoinUnit: "BTC", 35 CoinENV: "test", 36 FiatLogo: uuid.NewString(), 37 FiatUnit: "USD", 38 FeedType: basetypes.CurrencyFeedType_CoinGecko, 39 FeedTypeStr: basetypes.CurrencyFeedType_CoinGecko.String(), 40 } 41 42 var req = &npool.CoinFiatReq{ 43 FeedType: &ret.FeedType, 44 } 45 46 func setupCoinFiat(t *testing.T) func(*testing.T) { 47 ret.CoinTypeID = uuid.NewString() 48 req.CoinTypeID = &ret.CoinTypeID 49 ret.CoinName = uuid.NewString() 50 ret.FiatName = uuid.NewString() 51 ret.FiatID = uuid.NewString() 52 req.FiatID = &ret.FiatID 53 54 h1, err := coin1.NewHandler( 55 context.Background(), 56 coin1.WithEntID(&ret.CoinTypeID, true), 57 coin1.WithName(&ret.CoinName, true), 58 coin1.WithLogo(&ret.CoinLogo, true), 59 coin1.WithUnit(&ret.CoinUnit, true), 60 coin1.WithENV(&ret.CoinENV, true), 61 ) 62 assert.Nil(t, err) 63 64 _, err = h1.CreateCoin(context.Background()) 65 assert.Nil(t, err) 66 67 h2, err := fiat1.NewHandler( 68 context.Background(), 69 fiat1.WithEntID(&ret.FiatID, true), 70 fiat1.WithName(&ret.FiatName, true), 71 fiat1.WithLogo(&ret.FiatLogo, true), 72 fiat1.WithUnit(&ret.FiatUnit, true), 73 ) 74 assert.Nil(t, err) 75 76 _, err = h2.CreateFiat(context.Background()) 77 assert.Nil(t, err) 78 79 return func(*testing.T) { 80 _, _ = h1.DeleteCoin(context.Background()) 81 } 82 } 83 84 func createCoinFiat(t *testing.T) { 85 handler, err := NewHandler( 86 context.Background(), 87 WithCoinTypeID(req.CoinTypeID, true), 88 WithFiatID(req.FiatID, true), 89 WithFeedType(req.FeedType, true), 90 ) 91 assert.Nil(t, err) 92 93 info, err := handler.CreateCoinFiat(context.Background()) 94 if assert.Nil(t, err) { 95 ret.UpdatedAt = info.UpdatedAt 96 ret.CreatedAt = info.CreatedAt 97 ret.ID = info.ID 98 ret.EntID = info.EntID 99 assert.Equal(t, ret, info) 100 } 101 } 102 103 func getCoinFiats(t *testing.T) { 104 const singleRowLimit = 2 105 handler, err := NewHandler( 106 context.Background(), 107 WithConds(&npool.Conds{ 108 CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID}, 109 }), 110 WithOffset(0), 111 WithLimit(singleRowLimit), 112 ) 113 assert.Nil(t, err) 114 115 infos, total, err := handler.GetCoinFiats(context.Background()) 116 if assert.Nil(t, err) { 117 assert.Equal(t, uint32(1), total) 118 assert.Equal(t, 1, len(infos)) 119 assert.Equal(t, ret, infos[0]) 120 } 121 } 122 123 func deleteCoinFiat(t *testing.T) { 124 handler, err := NewHandler( 125 context.Background(), 126 WithID(&ret.ID, true), 127 ) 128 assert.Nil(t, err) 129 130 info, err := handler.DeleteCoinFiat(context.Background()) 131 if assert.Nil(t, err) { 132 ret.UpdatedAt = info.UpdatedAt 133 assert.Equal(t, ret, info) 134 } 135 } 136 137 func TestCoin(t *testing.T) { 138 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 139 return 140 } 141 142 teardown := setupCoinFiat(t) 143 defer teardown(t) 144 145 t.Run("createCoinFiat", createCoinFiat) 146 t.Run("getCoinFiats", getCoinFiats) 147 t.Run("deleteCoinFiat", deleteCoinFiat) 148 }