github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/currency/client_test.go (about) 1 package currency 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 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 20 currency1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin/currency" 21 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 22 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/currency" 23 "github.com/stretchr/testify/assert" 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 ret = &npool.Currency{ 40 EntID: uuid.NewString(), 41 CoinName: uuid.NewString(), 42 CoinUnit: uuid.NewString(), 43 CoinENV: "test", 44 FeedType: basetypes.CurrencyFeedType_CoinBase, 45 FeedTypeStr: basetypes.CurrencyFeedType_CoinBase.String(), 46 MarketValueHigh: "12.001000000000000000", 47 MarketValueLow: "11.001000000000000000", 48 } 49 50 var req = &npool.CurrencyReq{ 51 ID: &ret.ID, 52 FeedType: &ret.FeedType, 53 MarketValueHigh: &ret.MarketValueHigh, 54 MarketValueLow: &ret.MarketValueLow, 55 } 56 57 func setupCurrency(t *testing.T) func(*testing.T) { 58 ret.CoinTypeID = uuid.NewString() 59 req.CoinTypeID = &ret.CoinTypeID 60 61 h1, err := coin1.NewHandler( 62 context.Background(), 63 coin1.WithEntID(&ret.CoinTypeID, true), 64 coin1.WithName(&ret.CoinName, true), 65 coin1.WithUnit(&ret.CoinUnit, true), 66 coin1.WithLogo(&ret.CoinLogo, true), 67 coin1.WithENV(&ret.CoinENV, true), 68 ) 69 assert.Nil(t, err) 70 71 _, err = h1.CreateCoin(context.Background()) 72 assert.Nil(t, err) 73 74 h2, err := currency1.NewHandler( 75 context.Background(), 76 currency1.WithCoinTypeID(req.CoinTypeID, true), 77 currency1.WithMarketValueHigh(req.MarketValueHigh, true), 78 currency1.WithMarketValueLow(req.MarketValueLow, true), 79 currency1.WithFeedType(req.FeedType, true), 80 ) 81 assert.Nil(t, err) 82 83 info, err := h2.CreateCurrency(context.Background()) 84 assert.Nil(t, err) 85 86 ret.ID = info.ID 87 ret.EntID = info.EntID 88 ret.CreatedAt = info.CreatedAt 89 ret.UpdatedAt = info.UpdatedAt 90 91 return func(*testing.T) { 92 _, _ = h1.DeleteCoin(context.Background()) 93 } 94 } 95 96 func getCurrency(t *testing.T) { 97 info, err := GetCurrency(context.Background(), ret.EntID) 98 if assert.Nil(t, err) { 99 assert.Equal(t, ret, info) 100 } 101 } 102 103 func getCurrencies(t *testing.T) { 104 infos, total, err := GetCurrencies(context.Background(), &npool.Conds{ 105 CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID}, 106 }, 0, 100) 107 if assert.Nil(t, err) { 108 assert.Equal(t, 1, len(infos)) 109 assert.Equal(t, uint32(1), total) 110 assert.Equal(t, ret, infos[0]) 111 } 112 } 113 114 func TestClient(t *testing.T) { 115 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 116 return 117 } 118 // Here won't pass test due to we always test with localhost 119 120 teardown := setupCurrency(t) 121 defer teardown(t) 122 123 gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort) 124 125 monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) { 126 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 127 }) 128 monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) { 129 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 130 }) 131 132 t.Run("getCurrency", getCurrency) 133 t.Run("getCurrencies", getCurrencies) 134 }