github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/fiat/client_test.go (about)

     1  package coinfiat
     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  	fiat1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/fiat"
    21  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    22  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat"
    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.CoinFiat{
    40  	CoinName:    uuid.NewString(),
    41  	CoinUnit:    uuid.NewString(),
    42  	CoinENV:     "test",
    43  	FiatName:    uuid.NewString(),
    44  	FiatLogo:    uuid.NewString(),
    45  	FiatUnit:    "USD",
    46  	FeedType:    basetypes.CurrencyFeedType_CoinBase,
    47  	FeedTypeStr: basetypes.CurrencyFeedType_CoinBase.String(),
    48  }
    49  
    50  var req = &npool.CoinFiatReq{
    51  	FeedType: &ret.FeedType,
    52  }
    53  
    54  func setupCoinFiat(t *testing.T) func(*testing.T) {
    55  	ret.CoinTypeID = uuid.NewString()
    56  	req.CoinTypeID = &ret.CoinTypeID
    57  	ret.FiatID = uuid.NewString()
    58  	req.FiatID = &ret.FiatID
    59  
    60  	h1, err := coin1.NewHandler(
    61  		context.Background(),
    62  		coin1.WithEntID(&ret.CoinTypeID, true),
    63  		coin1.WithName(&ret.CoinName, true),
    64  		coin1.WithUnit(&ret.CoinUnit, true),
    65  		coin1.WithLogo(&ret.CoinLogo, true),
    66  		coin1.WithENV(&ret.CoinENV, true),
    67  	)
    68  	assert.Nil(t, err)
    69  
    70  	_, err = h1.CreateCoin(context.Background())
    71  	assert.Nil(t, err)
    72  
    73  	h2, err := fiat1.NewHandler(
    74  		context.Background(),
    75  		fiat1.WithEntID(&ret.FiatID, true),
    76  		fiat1.WithName(&ret.FiatName, true),
    77  		fiat1.WithLogo(&ret.FiatLogo, true),
    78  		fiat1.WithUnit(&ret.FiatUnit, true),
    79  	)
    80  	assert.Nil(t, err)
    81  
    82  	_, err = h2.CreateFiat(context.Background())
    83  	assert.Nil(t, err)
    84  
    85  	return func(*testing.T) {
    86  		_, _ = h1.DeleteCoin(context.Background())
    87  	}
    88  }
    89  
    90  func createCoinFiat(t *testing.T) {
    91  	info, err := CreateCoinFiat(context.Background(), req)
    92  	if assert.Nil(t, err) {
    93  		ret.CreatedAt = info.CreatedAt
    94  		ret.UpdatedAt = info.UpdatedAt
    95  		ret.ID = info.ID
    96  		ret.EntID = info.EntID
    97  		assert.Equal(t, ret, info)
    98  	}
    99  }
   100  
   101  func getCoinFiats(t *testing.T) {
   102  	infos, total, err := GetCoinFiats(context.Background(), &npool.Conds{
   103  		CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID},
   104  	}, 0, 100)
   105  	if assert.Nil(t, err) {
   106  		assert.Equal(t, 1, len(infos))
   107  		assert.Equal(t, uint32(1), total)
   108  		assert.Equal(t, ret, infos[0])
   109  	}
   110  }
   111  
   112  func deleteCoinFiat(t *testing.T) {
   113  	info, err := DeleteCoinFiat(context.Background(), ret.ID)
   114  	if assert.Nil(t, err) {
   115  		assert.Equal(t, ret, info)
   116  	}
   117  }
   118  
   119  func TestClient(t *testing.T) {
   120  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   121  		return
   122  	}
   123  	// Here won't pass test due to we always test with localhost
   124  
   125  	teardown := setupCoinFiat(t)
   126  	defer teardown(t)
   127  
   128  	gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort)
   129  
   130  	monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) {
   131  		return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials()))
   132  	})
   133  	monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) {
   134  		return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials()))
   135  	})
   136  
   137  	t.Run("createCoinFiat", createCoinFiat)
   138  	t.Run("getCoinFiats", getCoinFiats)
   139  	t.Run("deleteCoinFiat", deleteCoinFiat)
   140  }