github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/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  	coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin"
    20  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    21  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/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  	CoinName:     uuid.NewString(),
    40  	CoinUnit:     "BTC",
    41  	CoinLogo:     uuid.NewString(),
    42  	CoinENV:      "main",
    43  	FeedType:     basetypes.CurrencyFeedType_CoinGecko,
    44  	FeedTypeStr:  basetypes.CurrencyFeedType_CoinGecko.String(),
    45  	FeedCoinName: "bitcoin",
    46  }
    47  
    48  var req = &npool.FeedReq{
    49  	FeedType:     &ret.FeedType,
    50  	FeedCoinName: &ret.FeedCoinName,
    51  }
    52  
    53  func setupFeed(t *testing.T) func(*testing.T) {
    54  	ret.CoinTypeID = uuid.NewString()
    55  	req.CoinTypeID = &ret.CoinTypeID
    56  
    57  	h1, err := coin1.NewHandler(
    58  		context.Background(),
    59  		coin1.WithEntID(&ret.CoinTypeID, true),
    60  		coin1.WithName(&ret.CoinName, true),
    61  		coin1.WithUnit(&ret.CoinUnit, true),
    62  		coin1.WithLogo(&ret.CoinLogo, true),
    63  		coin1.WithENV(&ret.CoinENV, true),
    64  	)
    65  	assert.Nil(t, err)
    66  
    67  	_, err = h1.CreateCoin(context.Background())
    68  	assert.Nil(t, err)
    69  
    70  	return func(*testing.T) {
    71  		_, _ = h1.DeleteCoin(context.Background())
    72  	}
    73  }
    74  
    75  func createFeed(t *testing.T) {
    76  	info, err := CreateFeed(context.Background(), req)
    77  	if assert.Nil(t, err) {
    78  		ret.CreatedAt = info.CreatedAt
    79  		ret.UpdatedAt = info.UpdatedAt
    80  		ret.ID = info.ID
    81  		ret.EntID = info.EntID
    82  		assert.Equal(t, ret, info)
    83  	}
    84  }
    85  
    86  func updateFeed(t *testing.T) {
    87  	ret.Disabled = true
    88  	req.ID = &ret.ID
    89  	req.Disabled = &ret.Disabled
    90  
    91  	info, err := UpdateFeed(context.Background(), req)
    92  	if assert.Nil(t, err) {
    93  		ret.UpdatedAt = info.UpdatedAt
    94  		assert.Equal(t, info, ret)
    95  	}
    96  }
    97  
    98  func getFeeds(t *testing.T) {
    99  	infos, total, err := GetFeeds(context.Background(), &npool.Conds{
   100  		EntID:      &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID},
   101  		CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID},
   102  	}, 0, 100)
   103  	if assert.Nil(t, err) {
   104  		assert.Equal(t, len(infos), 1)
   105  		assert.Equal(t, total, uint32(1))
   106  		assert.Equal(t, infos[0], ret)
   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 := setupFeed(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("createFeed", createFeed)
   129  	t.Run("updateFeed", updateFeed)
   130  	t.Run("getFeeds", getFeeds)
   131  }