github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/currency/feed/feed_test.go (about) 1 package currencyfeed 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/currency/feed" 13 14 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 15 16 "github.com/google/uuid" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func init() { 21 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 22 return 23 } 24 if err := testinit.Init(); err != nil { 25 fmt.Printf("cannot init test stub: %v\n", err) 26 } 27 } 28 29 var ret = &npool.Feed{ 30 CoinName: "My BTC1", 31 CoinLogo: uuid.NewString(), 32 CoinUnit: "BTC", 33 CoinENV: "test", 34 FeedType: basetypes.CurrencyFeedType_CoinGecko, 35 FeedTypeStr: basetypes.CurrencyFeedType_CoinGecko.String(), 36 FeedCoinName: "BTC123", 37 } 38 39 var req = &npool.FeedReq{ 40 FeedType: &ret.FeedType, 41 FeedCoinName: &ret.FeedCoinName, 42 } 43 44 func setupCoin(t *testing.T) func(*testing.T) { 45 ret.CoinTypeID = uuid.NewString() 46 req.CoinTypeID = &ret.CoinTypeID 47 ret.CoinName = uuid.NewString() 48 49 h1, err := coin1.NewHandler( 50 context.Background(), 51 coin1.WithEntID(&ret.CoinTypeID, true), 52 coin1.WithName(&ret.CoinName, true), 53 coin1.WithLogo(&ret.CoinLogo, true), 54 coin1.WithUnit(&ret.CoinUnit, true), 55 coin1.WithENV(&ret.CoinENV, true), 56 ) 57 assert.Nil(t, err) 58 59 _, err = h1.CreateCoin(context.Background()) 60 assert.Nil(t, err) 61 62 return func(*testing.T) { 63 _, _ = h1.DeleteCoin(context.Background()) 64 } 65 } 66 67 func create(t *testing.T) { 68 handler, err := NewHandler( 69 context.Background(), 70 WithCoinTypeID(req.CoinTypeID, true), 71 WithFeedType(req.FeedType, true), 72 WithFeedCoinName(req.FeedCoinName, true), 73 ) 74 assert.Nil(t, err) 75 76 info, err := handler.CreateFeed(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, info, ret) 83 } 84 } 85 86 func update(t *testing.T) { 87 ret.FeedCoinName = "BTCWWWW" 88 ret.Disabled = true 89 90 req.FeedCoinName = &ret.FeedCoinName 91 req.Disabled = &ret.Disabled 92 93 handler, err := NewHandler( 94 context.Background(), 95 WithID(&ret.ID, true), 96 WithFeedCoinName(req.FeedCoinName, true), 97 WithDisabled(req.Disabled, true), 98 ) 99 assert.Nil(t, err) 100 101 info, err := handler.UpdateFeed(context.Background()) 102 if assert.Nil(t, err) { 103 ret.UpdatedAt = info.UpdatedAt 104 assert.Equal(t, info, ret) 105 } 106 } 107 108 func TestCoin(t *testing.T) { 109 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 110 return 111 } 112 113 teardown := setupCoin(t) 114 defer teardown(t) 115 116 t.Run("create", create) 117 t.Run("update", update) 118 }