github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/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/fiat/currency"
    13  
    14  	fiat1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/fiat"
    15  
    16  	"github.com/google/uuid"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func init() {
    21  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    22  		return
    23  	}
    24  	if err := testinit.Init(); err != nil {
    25  		fmt.Printf("cannot init test stub: %v\n", err)
    26  	}
    27  }
    28  
    29  var ret = &npool.Currency{
    30  	FiatName:        "My BTC1",
    31  	FiatLogo:        uuid.NewString(),
    32  	FiatUnit:        "BTC",
    33  	MarketValueHigh: "0.000000000000000000",
    34  	MarketValueLow:  "0.000000000000000000",
    35  	FeedType:        basetypes.CurrencyFeedType_CoinGecko,
    36  	FeedTypeStr:     basetypes.CurrencyFeedType_CoinGecko.String(),
    37  }
    38  
    39  var req = &npool.CurrencyReq{
    40  	FeedType:        &ret.FeedType,
    41  	MarketValueHigh: &ret.MarketValueHigh,
    42  	MarketValueLow:  &ret.MarketValueLow,
    43  }
    44  
    45  func setupFiat(t *testing.T) func(*testing.T) {
    46  	ret.FiatID = uuid.NewString()
    47  	req.FiatID = &ret.FiatID
    48  	ret.FiatName = uuid.NewString()
    49  
    50  	h1, err := fiat1.NewHandler(
    51  		context.Background(),
    52  		fiat1.WithEntID(&ret.FiatID, true),
    53  		fiat1.WithName(&ret.FiatName, true),
    54  		fiat1.WithLogo(&ret.FiatLogo, true),
    55  		fiat1.WithUnit(&ret.FiatUnit, true),
    56  	)
    57  	assert.Nil(t, err)
    58  
    59  	_, err = h1.CreateFiat(context.Background())
    60  	assert.Nil(t, err)
    61  
    62  	return func(*testing.T) {}
    63  }
    64  
    65  func create(t *testing.T) {
    66  	handler, err := NewHandler(
    67  		context.Background(),
    68  		WithFiatID(req.FiatID, true),
    69  		WithMarketValueHigh(req.MarketValueHigh, true),
    70  		WithMarketValueLow(req.MarketValueLow, true),
    71  		WithFeedType(req.FeedType, true),
    72  	)
    73  	assert.Nil(t, err)
    74  
    75  	info, err := handler.CreateCurrency(context.Background())
    76  	if assert.Nil(t, err) {
    77  		ret.UpdatedAt = info.UpdatedAt
    78  		ret.CreatedAt = info.CreatedAt
    79  		ret.ID = info.ID
    80  		ret.EntID = info.EntID
    81  		assert.Equal(t, info, ret)
    82  	}
    83  }
    84  
    85  func TestFiat(t *testing.T) {
    86  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    87  		return
    88  	}
    89  
    90  	teardown := setupFiat(t)
    91  	defer teardown(t)
    92  
    93  	t.Run("create", create)
    94  }