github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/fiat/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/fiat/currency"
    13  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat/currency/history"
    14  
    15  	fiat1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/fiat"
    16  	currency1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/fiat/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  	FiatName:        "My BTC1",
    34  	FiatLogo:        uuid.NewString(),
    35  	FiatUnit:        "BTC",
    36  	MarketValueHigh: "0.000000000000000000",
    37  	MarketValueLow:  "0.000000000000000000",
    38  	FeedType:        basetypes.CurrencyFeedType_CoinGecko,
    39  	FeedTypeStr:     basetypes.CurrencyFeedType_CoinGecko.String(),
    40  }
    41  
    42  var req = &currencymwpb.CurrencyReq{
    43  	FeedType:        &ret.FeedType,
    44  	MarketValueHigh: &ret.MarketValueHigh,
    45  	MarketValueLow:  &ret.MarketValueLow,
    46  }
    47  
    48  func setupFiat(t *testing.T) func(*testing.T) {
    49  	ret.FiatID = uuid.NewString()
    50  	req.FiatID = &ret.FiatID
    51  	ret.FiatName = uuid.NewString()
    52  
    53  	h1, err := fiat1.NewHandler(
    54  		context.Background(),
    55  		fiat1.WithEntID(&ret.FiatID, true),
    56  		fiat1.WithName(&ret.FiatName, true),
    57  		fiat1.WithLogo(&ret.FiatLogo, true),
    58  		fiat1.WithUnit(&ret.FiatUnit, true),
    59  	)
    60  	assert.Nil(t, err)
    61  
    62  	_, err = h1.CreateFiat(context.Background())
    63  	assert.Nil(t, err)
    64  
    65  	h2, err := currency1.NewHandler(
    66  		context.Background(),
    67  		currency1.WithFiatID(req.FiatID, true),
    68  		currency1.WithMarketValueHigh(req.MarketValueHigh, true),
    69  		currency1.WithMarketValueLow(req.MarketValueLow, true),
    70  		currency1.WithFeedType(req.FeedType, true),
    71  	)
    72  	assert.Nil(t, err)
    73  
    74  	_, err = h2.CreateCurrency(context.Background())
    75  	assert.Nil(t, err)
    76  
    77  	_, err = h2.CreateCurrency(context.Background())
    78  	assert.Nil(t, err)
    79  
    80  	return func(*testing.T) {}
    81  }
    82  
    83  func getMany(t *testing.T) {
    84  	handler, err := NewHandler(
    85  		context.Background(),
    86  		WithConds(&npool.Conds{
    87  			FiatID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.FiatID},
    88  		}),
    89  		WithOffset(0),
    90  		WithLimit(100),
    91  	)
    92  	assert.Nil(t, err)
    93  
    94  	infos, total, err := handler.GetCurrencies(context.Background())
    95  	assert.Nil(t, err)
    96  	assert.Equal(t, uint32(2), total)
    97  	assert.Equal(t, 2, len(infos))
    98  }
    99  
   100  func TestFiat(t *testing.T) {
   101  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   102  		return
   103  	}
   104  
   105  	teardown := setupFiat(t)
   106  	defer teardown(t)
   107  
   108  	t.Run("getMany", getMany)
   109  }