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