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