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