github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/fiat/currency/feed/client_test.go (about) 1 package feed 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 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 21 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat/currency/feed" 22 "github.com/stretchr/testify/assert" 23 24 cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 25 26 "github.com/google/uuid" 27 ) 28 29 func init() { 30 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 31 return 32 } 33 if err := testinit.Init(); err != nil { 34 fmt.Printf("cannot init test stub: %v\n", err) 35 } 36 } 37 38 var ret = &npool.Feed{ 39 FiatName: uuid.NewString(), 40 FiatUnit: "BTC", 41 FiatLogo: uuid.NewString(), 42 FeedType: basetypes.CurrencyFeedType_CoinGecko, 43 FeedTypeStr: basetypes.CurrencyFeedType_CoinGecko.String(), 44 FeedFiatName: "bitcoin", 45 } 46 47 var req = &npool.FeedReq{ 48 FeedType: &ret.FeedType, 49 FeedFiatName: &ret.FeedFiatName, 50 } 51 52 func setupFeed(t *testing.T) func(*testing.T) { 53 ret.FiatID = uuid.NewString() 54 req.FiatID = &ret.FiatID 55 56 h1, err := fiat1.NewHandler( 57 context.Background(), 58 fiat1.WithEntID(&ret.FiatID, true), 59 fiat1.WithName(&ret.FiatName, true), 60 fiat1.WithUnit(&ret.FiatUnit, true), 61 fiat1.WithLogo(&ret.FiatLogo, true), 62 ) 63 assert.Nil(t, err) 64 65 _, err = h1.CreateFiat(context.Background()) 66 assert.Nil(t, err) 67 68 return func(*testing.T) {} 69 } 70 71 func createFeed(t *testing.T) { 72 info, err := CreateFeed(context.Background(), req) 73 if assert.Nil(t, err) { 74 ret.CreatedAt = info.CreatedAt 75 ret.UpdatedAt = info.UpdatedAt 76 ret.ID = info.ID 77 ret.EntID = info.EntID 78 assert.Equal(t, ret, info) 79 } 80 } 81 82 func updateFeed(t *testing.T) { 83 ret.Disabled = true 84 req.ID = &ret.ID 85 req.Disabled = &ret.Disabled 86 87 info, err := UpdateFeed(context.Background(), req) 88 if assert.Nil(t, err) { 89 ret.UpdatedAt = info.UpdatedAt 90 assert.Equal(t, info, ret) 91 } 92 } 93 94 func getFeeds(t *testing.T) { 95 infos, total, err := GetFeeds(context.Background(), &npool.Conds{ 96 EntID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID}, 97 FiatID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.FiatID}, 98 }, 0, 100) 99 if assert.Nil(t, err) { 100 assert.Equal(t, len(infos), 1) 101 assert.Equal(t, total, uint32(1)) 102 assert.Equal(t, infos[0], ret) 103 } 104 } 105 106 func TestClient(t *testing.T) { 107 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 108 return 109 } 110 // Here won't pass test due to we always test with localhost 111 112 teardown := setupFeed(t) 113 defer teardown(t) 114 115 gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort) 116 117 monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) { 118 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 119 }) 120 monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) { 121 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 122 }) 123 124 t.Run("createFeed", createFeed) 125 t.Run("updateFeed", updateFeed) 126 t.Run("getFeeds", getFeeds) 127 }