github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/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/currency"
    13  
    14  	coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin"
    15  	cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    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  	MarketValueHigh: "0.000000000000000000",
    36  	MarketValueLow:  "0.000000000000000000",
    37  	FeedType:        basetypes.CurrencyFeedType_CoinGecko,
    38  	FeedTypeStr:     basetypes.CurrencyFeedType_CoinGecko.String(),
    39  }
    40  
    41  var req = &npool.CurrencyReq{
    42  	FeedType:        &ret.FeedType,
    43  	MarketValueHigh: &ret.MarketValueHigh,
    44  	MarketValueLow:  &ret.MarketValueLow,
    45  }
    46  
    47  func setupCoin(t *testing.T) func(*testing.T) {
    48  	ret.CoinTypeID = uuid.NewString()
    49  	req.CoinTypeID = &ret.CoinTypeID
    50  	ret.CoinName = uuid.NewString()
    51  
    52  	h1, err := coin1.NewHandler(
    53  		context.Background(),
    54  		coin1.WithEntID(&ret.CoinTypeID, true),
    55  		coin1.WithName(&ret.CoinName, true),
    56  		coin1.WithLogo(&ret.CoinLogo, true),
    57  		coin1.WithUnit(&ret.CoinUnit, true),
    58  		coin1.WithENV(&ret.CoinENV, true),
    59  	)
    60  	assert.Nil(t, err)
    61  
    62  	_, err = h1.CreateCoin(context.Background())
    63  	assert.Nil(t, err)
    64  
    65  	return func(*testing.T) {
    66  		_, _ = h1.DeleteCoin(context.Background())
    67  	}
    68  }
    69  
    70  func create(t *testing.T) {
    71  	handler, err := NewHandler(
    72  		context.Background(),
    73  		WithCoinTypeID(req.CoinTypeID, true),
    74  		WithMarketValueHigh(req.MarketValueHigh, true),
    75  		WithMarketValueLow(req.MarketValueLow, true),
    76  		WithFeedType(req.FeedType, true),
    77  	)
    78  	assert.Nil(t, err)
    79  
    80  	info, err := handler.CreateCurrency(context.Background())
    81  	if assert.Nil(t, err) {
    82  		ret.UpdatedAt = info.UpdatedAt
    83  		ret.CreatedAt = info.CreatedAt
    84  		ret.ID = info.ID
    85  		ret.EntID = info.EntID
    86  		assert.Equal(t, ret, info)
    87  	}
    88  }
    89  
    90  func update(t *testing.T) {
    91  	amount := "123.700000000000000000"
    92  
    93  	ret.MarketValueHigh = amount
    94  	ret.MarketValueLow = amount
    95  
    96  	req.MarketValueHigh = &amount
    97  	req.MarketValueLow = &amount
    98  
    99  	handler, err := NewHandler(
   100  		context.Background(),
   101  		WithCoinTypeID(req.CoinTypeID, true),
   102  		WithMarketValueHigh(req.MarketValueHigh, true),
   103  		WithMarketValueLow(req.MarketValueLow, true),
   104  		WithFeedType(req.FeedType, true),
   105  	)
   106  	assert.Nil(t, err)
   107  
   108  	info, err := handler.CreateCurrency(context.Background())
   109  	if assert.Nil(t, err) {
   110  		ret.UpdatedAt = info.UpdatedAt
   111  		assert.Equal(t, ret, info)
   112  	}
   113  }
   114  
   115  func get(t *testing.T) {
   116  	handler, err := NewHandler(
   117  		context.Background(),
   118  		WithEntID(&ret.EntID, true),
   119  	)
   120  	assert.Nil(t, err)
   121  
   122  	info, err := handler.GetCurrency(context.Background())
   123  	if assert.Nil(t, err) {
   124  		assert.Equal(t, ret, info)
   125  	}
   126  }
   127  
   128  func getMany(t *testing.T) {
   129  	handler, err := NewHandler(
   130  		context.Background(),
   131  		WithConds(&npool.Conds{
   132  			CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID},
   133  		}),
   134  		WithOffset(0),
   135  		WithLimit(100),
   136  	)
   137  	assert.Nil(t, err)
   138  
   139  	infos, total, err := handler.GetCurrencies(context.Background())
   140  	if assert.Nil(t, err) {
   141  		assert.Equal(t, 1, len(infos))
   142  		assert.Equal(t, uint32(1), total)
   143  		assert.Equal(t, ret, infos[0])
   144  	}
   145  }
   146  
   147  func TestCoin(t *testing.T) {
   148  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   149  		return
   150  	}
   151  
   152  	teardown := setupCoin(t)
   153  	defer teardown(t)
   154  
   155  	t.Run("create", create)
   156  	t.Run("update", update)
   157  	t.Run("get", get)
   158  	t.Run("getMany", getMany)
   159  }