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

     1  package currencyhistory
     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  	currencymwpb "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/currency"
    13  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/currency/history"
    14  
    15  	coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin"
    16  	currency1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin/currency"
    17  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    18  
    19  	"github.com/google/uuid"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func init() {
    24  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    25  		return
    26  	}
    27  	if err := testinit.Init(); err != nil {
    28  		fmt.Printf("cannot init test stub: %v\n", err)
    29  	}
    30  }
    31  
    32  var ret = &currencymwpb.Currency{
    33  	CoinName:        "My BTC1",
    34  	CoinLogo:        uuid.NewString(),
    35  	CoinUnit:        "BTC",
    36  	CoinENV:         "test",
    37  	MarketValueHigh: "0.000000000000000000",
    38  	MarketValueLow:  "0.000000000000000000",
    39  	FeedType:        basetypes.CurrencyFeedType_CoinGecko,
    40  	FeedTypeStr:     basetypes.CurrencyFeedType_CoinGecko.String(),
    41  }
    42  
    43  var req = &currencymwpb.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  
    54  	h1, err := coin1.NewHandler(
    55  		context.Background(),
    56  		coin1.WithEntID(&ret.CoinTypeID, true),
    57  		coin1.WithName(&ret.CoinName, true),
    58  		coin1.WithLogo(&ret.CoinLogo, true),
    59  		coin1.WithUnit(&ret.CoinUnit, true),
    60  		coin1.WithENV(&ret.CoinENV, true),
    61  	)
    62  	assert.Nil(t, err)
    63  
    64  	_, err = h1.CreateCoin(context.Background())
    65  	assert.Nil(t, err)
    66  
    67  	h2, err := currency1.NewHandler(
    68  		context.Background(),
    69  		currency1.WithCoinTypeID(req.CoinTypeID, true),
    70  		currency1.WithMarketValueHigh(req.MarketValueHigh, true),
    71  		currency1.WithMarketValueLow(req.MarketValueLow, true),
    72  		currency1.WithFeedType(req.FeedType, true),
    73  	)
    74  	assert.Nil(t, err)
    75  
    76  	_, err = h2.CreateCurrency(context.Background())
    77  	assert.Nil(t, err)
    78  
    79  	_, err = h2.CreateCurrency(context.Background())
    80  	assert.Nil(t, err)
    81  
    82  	return func(*testing.T) {
    83  		_, _ = h1.DeleteCoin(context.Background())
    84  	}
    85  }
    86  
    87  func getMany(t *testing.T) {
    88  	handler, err := NewHandler(
    89  		context.Background(),
    90  		WithConds(&npool.Conds{
    91  			CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID},
    92  		}),
    93  		WithOffset(0),
    94  		WithLimit(100),
    95  	)
    96  	assert.Nil(t, err)
    97  
    98  	infos, total, err := handler.GetCurrencies(context.Background())
    99  	assert.Nil(t, err)
   100  	assert.Equal(t, uint32(2), total)
   101  	assert.Equal(t, 2, len(infos))
   102  }
   103  
   104  func TestCoin(t *testing.T) {
   105  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   106  		return
   107  	}
   108  
   109  	teardown := setupCoin(t)
   110  	defer teardown(t)
   111  
   112  	t.Run("getMany", getMany)
   113  }