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

     1  package currency
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"testing"
     9  
    10  	testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit"
    11  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    12  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat/currency"
    13  
    14  	coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin"
    15  	fiat1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/fiat"
    16  
    17  	"github.com/google/uuid"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func init() {
    22  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    23  		return
    24  	}
    25  	if err := testinit.Init(); err != nil {
    26  		fmt.Printf("cannot init test stub: %v\n", err)
    27  	}
    28  }
    29  
    30  var ret = &npool.Currency{
    31  	CoinName:        "My BTC1",
    32  	CoinLogo:        uuid.NewString(),
    33  	CoinUnit:        "BTC",
    34  	CoinENV:         "test",
    35  	FiatLogo:        uuid.NewString(),
    36  	FiatUnit:        "USD",
    37  	MarketValueHigh: "0.000000000000000000",
    38  	MarketValueLow:  "0.000000000000000000",
    39  	FeedType:        basetypes.CurrencyFeedType_CoinGecko,
    40  	FeedTypeStr:     basetypes.CurrencyFeedType_CoinGecko.String(),
    41  }
    42  
    43  var req = &npool.CurrencyReq{
    44  	FeedType:        &ret.FeedType,
    45  	MarketValueHigh: &ret.MarketValueHigh,
    46  	MarketValueLow:  &ret.MarketValueLow,
    47  }
    48  
    49  func setupCoin(t *testing.T) func(*testing.T) {
    50  	ret.CoinTypeID = uuid.NewString()
    51  	req.CoinTypeID = &ret.CoinTypeID
    52  	ret.CoinName = uuid.NewString()
    53  	ret.FiatName = uuid.NewString()
    54  	ret.FiatID = uuid.NewString()
    55  	req.FiatID = &ret.FiatID
    56  
    57  	h1, err := coin1.NewHandler(
    58  		context.Background(),
    59  		coin1.WithEntID(&ret.CoinTypeID, true),
    60  		coin1.WithName(&ret.CoinName, true),
    61  		coin1.WithLogo(&ret.CoinLogo, true),
    62  		coin1.WithUnit(&ret.CoinUnit, 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  	h2, err := fiat1.NewHandler(
    71  		context.Background(),
    72  		fiat1.WithEntID(&ret.FiatID, true),
    73  		fiat1.WithName(&ret.FiatName, true),
    74  		fiat1.WithLogo(&ret.FiatLogo, true),
    75  		fiat1.WithUnit(&ret.FiatUnit, true),
    76  	)
    77  	assert.Nil(t, err)
    78  
    79  	_, err = h2.CreateFiat(context.Background())
    80  	assert.Nil(t, err)
    81  
    82  	return func(*testing.T) {
    83  		_, _ = h1.DeleteCoin(context.Background())
    84  	}
    85  }
    86  
    87  func create(t *testing.T) {
    88  	handler, err := NewHandler(
    89  		context.Background(),
    90  		WithCoinTypeID(req.CoinTypeID, true),
    91  		WithFiatID(req.FiatID, true),
    92  		WithMarketValueHigh(req.MarketValueHigh, true),
    93  		WithMarketValueLow(req.MarketValueLow, true),
    94  		WithFeedType(req.FeedType, true),
    95  	)
    96  	assert.Nil(t, err)
    97  
    98  	info, err := handler.CreateCurrency(context.Background())
    99  	if assert.Nil(t, err) {
   100  		ret.UpdatedAt = info.UpdatedAt
   101  		ret.CreatedAt = info.CreatedAt
   102  		ret.ID = info.ID
   103  		ret.EntID = info.EntID
   104  		assert.Equal(t, info, ret)
   105  	}
   106  }
   107  
   108  func TestCoin(t *testing.T) {
   109  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   110  		return
   111  	}
   112  
   113  	teardown := setupCoin(t)
   114  	defer teardown(t)
   115  
   116  	t.Run("create", create)
   117  }