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