Skip to content

Commit

Permalink
Adding error declaration checks in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuael committed Sep 29, 2024
1 parent d08a5f4 commit df0ccd6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 16 deletions.
19 changes: 11 additions & 8 deletions exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5879,7 +5879,7 @@ func (b *Binance) CheckLockedValueVIPCollateralAccount(ctx context.Context, orde
// VIPLoanBorrow VIP loan is available for VIP users only.
func (b *Binance) VIPLoanBorrow(ctx context.Context, loanAccountID, loanTerm int64, loanCoin, collateralCoin currency.Code, loanAmount float64, collateralAccountID string, isFlexibleRate bool) ([]VIPLoanBorrow, error) {
if loanAccountID == 0 {
return nil, errors.New("loanAccountId is required")
return nil, fmt.Errorf("%w, loanAccountId is required", errAccountIDRequired)
}
if loanCoin.IsEmpty() {
return nil, fmt.Errorf("%w, loanCoin is required", currency.ErrCurrencyCodeEmpty)
Expand All @@ -5888,7 +5888,7 @@ func (b *Binance) VIPLoanBorrow(ctx context.Context, loanAccountID, loanTerm int
return nil, fmt.Errorf("%w, loanAmount is required", order.ErrAmountBelowMin)
}
if collateralAccountID == "" {
return nil, errors.New("collateralAccountID is required")
return nil, fmt.Errorf("%w, collateralAccountID is required", errAccountIDRequired)
}
if collateralCoin.IsEmpty() {
return nil, fmt.Errorf("%w, collateralCoin is required", currency.ErrCurrencyCodeEmpty)
Expand Down Expand Up @@ -6017,12 +6017,12 @@ func (b *Binance) SendQuoteRequest(ctx context.Context, fromAsset, toAsset curre
if toAsset.IsEmpty() {
return nil, fmt.Errorf("%w, toAsset is required", currency.ErrCurrencyCodeEmpty)
}
params := url.Values{}
params.Set("fromAsset", fromAsset.String())
params.Set("toAsset", toAsset.String())
if fromAmount <= 0 && toAmount <= 0 {
return nil, fmt.Errorf("%w, fromAmount or toAmount is required", order.ErrAmountIsInvalid)
}
params := url.Values{}
params.Set("fromAsset", fromAsset.String())
params.Set("toAsset", toAsset.String())
if fromAmount > 0 {
params.Set("fromAmount", strconv.FormatFloat(fromAmount, 'f', -1, 64))
}
Expand All @@ -6042,7 +6042,7 @@ func (b *Binance) SendQuoteRequest(ctx context.Context, fromAsset, toAsset curre
// AcceptQuote accept the offered quote by quote ID.
func (b *Binance) AcceptQuote(ctx context.Context, quoteID string) (*QuoteOrderStatus, error) {
if quoteID == "" {
return nil, errors.New("quote ID is required")
return nil, errQuoteIDRequired
}
params := url.Values{}
params.Set("quoteId", quoteID)
Expand All @@ -6065,6 +6065,9 @@ func (b *Binance) GetConvertOrderStatus(ctx context.Context, orderID, quoteID st

// PlaceLimitOrder enable users to place a limit order
func (b *Binance) PlaceLimitOrder(ctx context.Context, arg *ConvertPlaceLimitOrderParam) (*OrderStatusResponse, error) {
if *arg == (ConvertPlaceLimitOrderParam{}) {
return nil, common.ErrNilPointer
}
if arg.BaseAsset.IsEmpty() {
return nil, fmt.Errorf("%w, baseAsset is required", currency.ErrCurrencyCodeEmpty)
}
Expand All @@ -6078,7 +6081,7 @@ func (b *Binance) PlaceLimitOrder(ctx context.Context, arg *ConvertPlaceLimitOrd
return nil, order.ErrSideIsInvalid
}
if arg.ExpiredType == "" {
return nil, errors.New("expiredType is required")
return nil, errExpiedTypeRequired
}
var resp *OrderStatusResponse
return resp, b.SendAuthHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, "/sapi/v1/convert/limit/placeOrder", nil, placeLimitOrderRate, arg, &resp)
Expand Down Expand Up @@ -6276,7 +6279,7 @@ func (b *Binance) RedeemBinanaceGiftCard(ctx context.Context, code, externalUID
// VerifyBinanceGiftCardNumber verifying whether the Binance Gift Card is valid or not by entering Gift Card Number.
func (b *Binance) VerifyBinanceGiftCardNumber(ctx context.Context, referenceNumber string) (*GiftCardVerificationResponse, error) {
if referenceNumber == "" {
return nil, errors.New("reference number is required")
return nil, errReferenceNumberRequired
}
params := url.Values{}
params.Set("referenceNo", referenceNumber)
Expand Down
101 changes: 93 additions & 8 deletions exchanges/binance/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ var (

// enabled and active tradable pairs used to test endpoints.
spotTradablePair, usdtmTradablePair, coinmTradablePair, optionsTradablePair currency.Pair

// this pair is used to ensure that endpoints match it correctly
testPairMapping = currency.NewPair(currency.DOGE, currency.USDT)
)

func setFeeBuilder() *exchange.FeeBuilder {
Expand Down Expand Up @@ -6925,6 +6922,9 @@ func TestGetPayTradeHistory(t *testing.T) {

func TestGetAllConvertPairs(t *testing.T) {
t.Parallel()
_, err := b.GetAllConvertPairs(context.Background(), currency.BTC, currency.EMPTYCODE)
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)

result, err := b.GetAllConvertPairs(context.Background(), currency.BTC, currency.EMPTYCODE)
require.NoError(t, err)
assert.NotNil(t, result)
Expand All @@ -6940,6 +6940,13 @@ func TestGetOrderQuantityPrecisionPerAsset(t *testing.T) {

func TestSendQuoteRequest(t *testing.T) {
t.Parallel()
_, err := b.SendQuoteRequest(context.Background(), currency.EMPTYCODE, currency.USDT, 10, 20, "FUNDING", "1m")
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
_, err = b.SendQuoteRequest(context.Background(), currency.BTC, currency.EMPTYCODE, 10, 20, "FUNDING", "1m")
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
_, err = b.SendQuoteRequest(context.Background(), currency.BTC, currency.USDT, 0, 0, "FUNDING", "1m")
require.ErrorIs(t, err, order.ErrAmountIsInvalid)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.SendQuoteRequest(context.Background(), currency.BTC, currency.USDT, 10, 20, "FUNDING", "1m")
require.NoError(t, err)
Expand All @@ -6948,6 +6955,9 @@ func TestSendQuoteRequest(t *testing.T) {

func TestAcceptQuote(t *testing.T) {
t.Parallel()
_, err := b.AcceptQuote(context.Background(), "")
require.ErrorIs(t, err, errQuoteIDRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.AcceptQuote(context.Background(), "933256278426274426")
require.NoError(t, err)
Expand All @@ -6964,6 +6974,28 @@ func TestGetConvertOrderStatus(t *testing.T) {

func TestPlaceLimitOrder(t *testing.T) {
t.Parallel()
arg := &ConvertPlaceLimitOrderParam{}
_, err := b.PlaceLimitOrder(context.Background(), arg)
require.ErrorIs(t, err, common.ErrNilPointer)

arg.ExpiredType = "7_D"
_, err = b.PlaceLimitOrder(context.Background(), arg)
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)

arg.BaseAsset = currency.BTC
arg.QuoteAsset = currency.ETH
_, err = b.PlaceLimitOrder(context.Background(), arg)
require.ErrorIs(t, err, order.ErrPriceBelowMin)

arg.LimitPrice = 0.0122
_, err = b.PlaceLimitOrder(context.Background(), arg)
require.ErrorIs(t, err, order.ErrSideIsInvalid)

arg.Side = "SELL"
arg.ExpiredType = ""
_, err = b.PlaceLimitOrder(context.Background(), arg)
require.ErrorIs(t, err, errExpiedTypeRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.PlaceLimitOrder(context.Background(), &ConvertPlaceLimitOrderParam{
BaseAsset: currency.BTC,
Expand All @@ -6978,6 +7010,9 @@ func TestPlaceLimitOrder(t *testing.T) {

func TestCancelLimitOrder(t *testing.T) {
t.Parallel()
_, err := b.CancelLimitOrder(context.Background(), "")
require.ErrorIs(t, err, order.ErrOrderIDNotSet)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.CancelLimitOrder(context.Background(), "123434")
require.NoError(t, err)
Expand Down Expand Up @@ -7042,6 +7077,11 @@ func TestGetNFTAsset(t *testing.T) {

func TestCreateSingleTokenGiftCard(t *testing.T) {
t.Parallel()
_, err := b.CreateSingleTokenGiftCard(context.Background(), "", 0.1234)
require.ErrorIs(t, err, errTokenRequired)
_, err = b.CreateSingleTokenGiftCard(context.Background(), "BUSD", 0)
require.ErrorIs(t, err, order.ErrAmountBelowMin)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.CreateSingleTokenGiftCard(context.Background(), "BUSD", 0.1234)
require.NoError(t, err)
Expand All @@ -7050,6 +7090,15 @@ func TestCreateSingleTokenGiftCard(t *testing.T) {

func TestCreateDualTokenGiftCard(t *testing.T) {
t.Parallel()
_, err := b.CreateDualTokenGiftCard(context.Background(), "", currency.BNB.String(), 10, 10)
require.ErrorIs(t, err, errTokenRequired)
_, err = b.CreateDualTokenGiftCard(context.Background(), currency.BUSD.String(), "", 10, 10)
require.ErrorIs(t, err, errTokenRequired)
_, err = b.CreateDualTokenGiftCard(context.Background(), currency.BUSD.String(), currency.BNB.String(), 0, 10)
require.ErrorIs(t, err, order.ErrAmountBelowMin)
_, err = b.CreateDualTokenGiftCard(context.Background(), currency.BUSD.String(), currency.BNB.String(), 10, 0)
require.ErrorIs(t, err, order.ErrAmountBelowMin)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.CreateDualTokenGiftCard(context.Background(), currency.BUSD.String(), currency.BNB.String(), 10, 10)
require.NoError(t, err)
Expand All @@ -7066,6 +7115,9 @@ func TestRedeemBinanaceGiftCard(t *testing.T) {

func TestVerifyBinanceGiftCardNumber(t *testing.T) {
t.Parallel()
_, err := b.VerifyBinanceGiftCardNumber(context.Background(), "")
require.ErrorIs(t, err, errReferenceNumberRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
result, err := b.VerifyBinanceGiftCardNumber(context.Background(), "123456")
require.NoError(t, err)
Expand All @@ -7082,6 +7134,9 @@ func TestFetchRSAPublicKey(t *testing.T) {

func TestFetchTokenLimit(t *testing.T) {
t.Parallel()
_, err := b.FetchTokenLimit(context.Background(), "")
require.ErrorIs(t, err, errTokenRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
result, err := b.FetchTokenLimit(context.Background(), currency.BUSD.String())
require.NoError(t, err)
Expand Down Expand Up @@ -7114,6 +7169,14 @@ func TestCheckLockedValueVIPCollateralAccount(t *testing.T) {

func TestVIPLoanBorrow(t *testing.T) {
t.Parallel()
_, err := b.VIPLoanBorrow(context.Background(), 1234, 30, currency.ETH, currency.LTC, 123, "1234", false)

Check failure on line 7172 in exchanges/binance/binance_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

_, err = b.VIPLoanBorrow(context.Background(), 1234, 30, currency.ETH, currency.LTC, 123, "1234", false)

Check failure on line 7174 in exchanges/binance/binance_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

_, err = b.VIPLoanBorrow(context.Background(), 1234, 30, currency.ETH, currency.LTC, 123, "1234", false)

Check failure on line 7176 in exchanges/binance/binance_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

_, err = b.VIPLoanBorrow(context.Background(), 1234, 30, currency.ETH, currency.LTC, 123, "1234", false)

Check failure on line 7178 in exchanges/binance/binance_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
result, err := b.VIPLoanBorrow(context.Background(), 1234, 30, currency.ETH, currency.LTC, 123, "1234", false)
require.NoError(t, err)
Expand Down Expand Up @@ -7146,6 +7209,9 @@ func TestGetVIPApplicationStatus(t *testing.T) {

func TestGetVIPBorrowInterestRate(t *testing.T) {
t.Parallel()
_, err := b.GetVIPBorrowInterestRate(context.Background(), currency.EMPTYCODE)
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
result, err := b.GetVIPBorrowInterestRate(context.Background(), currency.ETH)
require.NoError(t, err)
Expand All @@ -7162,8 +7228,11 @@ func TestCreateSpotListenKey(t *testing.T) {

func TestKeepListenKeyAlive(t *testing.T) {
t.Parallel()
err := b.KeepSpotListenKeyAlive(context.Background(), "")
require.ErrorIs(t, err, errListenKeyIsRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
err := b.KeepSpotListenKeyAlive(context.Background(), "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
err = b.KeepSpotListenKeyAlive(context.Background(), "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
require.NoError(t, err)
}

Expand All @@ -7184,15 +7253,21 @@ func TestCreateMarginListenKey(t *testing.T) {

func TestKeepMarginListenKeyAlive(t *testing.T) {
t.Parallel()
err := b.KeepMarginListenKeyAlive(context.Background(), "")
require.ErrorIs(t, err, errListenKeyIsRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
err := b.KeepMarginListenKeyAlive(context.Background(), "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
err = b.KeepMarginListenKeyAlive(context.Background(), "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
assert.NoError(t, err)
}

func TestCloseMarginListenKey(t *testing.T) {
t.Parallel()
err := b.CloseMarginListenKey(context.Background(), "")
require.ErrorIs(t, err, errListenKeyIsRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
err := b.CloseMarginListenKey(context.Background(), "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
err = b.CloseMarginListenKey(context.Background(), "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
assert.NoError(t, err)
}

Expand All @@ -7209,15 +7284,25 @@ func TestCreateCrossMarginListenKey(t *testing.T) {

func TestKeepCrossMarginListenKeyAlive(t *testing.T) {
t.Parallel()
err := b.KeepCrossMarginListenKeyAlive(context.Background(), "BTCUSDT", "")
require.ErrorIs(t, err, errListenKeyIsRequired)
err = b.KeepCrossMarginListenKeyAlive(context.Background(), "", "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
err := b.KeepCrossMarginListenKeyAlive(context.Background(), "BTCUSDT", "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
err = b.KeepCrossMarginListenKeyAlive(context.Background(), "BTCUSDT", "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
assert.NoError(t, err)
}

func TestCloseCrossMarginListenKey(t *testing.T) {
t.Parallel()
err := b.CloseCrossMarginListenKey(context.Background(), "BTCUSDT", "")
require.ErrorIs(t, err, errListenKeyIsRequired)
err = b.CloseCrossMarginListenKey(context.Background(), "", "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
err := b.CloseCrossMarginListenKey(context.Background(), "BTCUSDT", "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
err = b.CloseCrossMarginListenKey(context.Background(), "BTCUSDT", "T3ee22BIYuWqmvne0HNq2A2WsFlEtLhvWCtItw6ffhhdmjifQ2tRbuKkTHhr")
assert.NoError(t, err)
}

Expand Down
4 changes: 4 additions & 0 deletions exchanges/binance/binance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ var (
errRequestIDRequired = errors.New("request ID is required")
errStartTimeRequired = errors.New("start time is required")
errStrategyTypeRequired = errors.New("strategy type is required")
errReferenceNumberRequired = errors.New("reference number is required")
errExpiedTypeRequired = errors.New("expiredType is required")
errQuoteIDRequired = errors.New("quote ID is required")
errAccountIDRequired = errors.New("account ID is required")
)

// TransferTypes represents asset transfer types
Expand Down

0 comments on commit df0ccd6

Please sign in to comment.