mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 15:10:46 +00:00
GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues These checks include: - Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`). - Replacing `%s` with %q - Enforcing consistent usage of should/must wording for testify assert/require messages * Add support for checking backticked string format specifiers and fix issues * tests: Fix error comparisons * tests: Replace errors.Is(err, nil) usage with testify and automate check * refactor: Rename ExtractPort to ExtractPortOrDefault * tests: Replace assert with require for error handling in multiple test files * tests: Replace assert with require for error handling and improve assertions in data tests * tests: Fix typo in assertion message for StreamVol test * OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions * OKX: Revert intentional error check * Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
This commit is contained in:
@@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
|
||||
exchange.RestSpot: testnetSpotURL,
|
||||
} {
|
||||
if err := b.API.Endpoints.SetRunning(k.String(), v); err != nil {
|
||||
log.Fatalf("Testnet `%s` URL error with `%s`: %s", k, v, err)
|
||||
log.Fatalf("Testnet %q URL error with %q: %s", k, v, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,27 +85,21 @@ func TestWrapperGetServerTime(t *testing.T) {
|
||||
}
|
||||
|
||||
st, err := b.GetServerTime(t.Context(), asset.Spot)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if st.IsZero() {
|
||||
t.Fatal("expected a time")
|
||||
}
|
||||
|
||||
st, err = b.GetServerTime(t.Context(), asset.USDTMarginedFutures)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if st.IsZero() {
|
||||
t.Fatal("expected a time")
|
||||
}
|
||||
|
||||
st, err = b.GetServerTime(t.Context(), asset.CoinMarginedFutures)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if st.IsZero() {
|
||||
t.Fatal("expected a time")
|
||||
@@ -1083,7 +1077,7 @@ func TestGetExchangeInfo(t *testing.T) {
|
||||
require.NoError(t, err, "GetExchangeInfo must not error")
|
||||
if mockTests {
|
||||
exp := time.Date(2024, 5, 10, 6, 8, 1, int(707*time.Millisecond), time.UTC)
|
||||
assert.True(t, info.ServerTime.Time().Equal(exp), "expected %v received %v", exp.UTC(), info.ServerTime.Time().UTC())
|
||||
assert.Truef(t, info.ServerTime.Time().Equal(exp), "expected %v received %v", exp.UTC(), info.ServerTime.Time().UTC())
|
||||
} else {
|
||||
assert.WithinRange(t, info.ServerTime.Time(), time.Now().Add(-24*time.Hour), time.Now().Add(24*time.Hour), "ServerTime should be within a day of now")
|
||||
}
|
||||
@@ -1984,8 +1978,8 @@ func TestSubscribe(t *testing.T) {
|
||||
mock := func(tb testing.TB, msg []byte, w *gws.Conn) error {
|
||||
tb.Helper()
|
||||
var req WsPayload
|
||||
require.NoError(tb, json.Unmarshal(msg, &req), "Unmarshal should not error")
|
||||
require.ElementsMatch(tb, req.Params, exp, "Params should have correct channels")
|
||||
require.NoError(tb, json.Unmarshal(msg, &req), "Unmarshal must not error")
|
||||
require.ElementsMatch(tb, req.Params, exp, "Params must have correct channels")
|
||||
return w.WriteMessage(gws.TextMessage, fmt.Appendf(nil, `{"result":null,"id":%d}`, req.ID))
|
||||
}
|
||||
b = testexch.MockWsInstance[Binance](t, mockws.CurryWsMockUpgrader(t, mock))
|
||||
@@ -1993,9 +1987,9 @@ func TestSubscribe(t *testing.T) {
|
||||
testexch.SetupWs(t, b)
|
||||
}
|
||||
err = b.Subscribe(channels)
|
||||
require.NoError(t, err, "Subscribe should not error")
|
||||
require.NoError(t, err, "Subscribe must not error")
|
||||
err = b.Unsubscribe(channels)
|
||||
require.NoError(t, err, "Unsubscribe should not error")
|
||||
require.NoError(t, err, "Unsubscribe must not error")
|
||||
}
|
||||
|
||||
func TestSubscribeBadResp(t *testing.T) {
|
||||
@@ -2007,7 +2001,7 @@ func TestSubscribeBadResp(t *testing.T) {
|
||||
tb.Helper()
|
||||
var req WsPayload
|
||||
err := json.Unmarshal(msg, &req)
|
||||
require.NoError(tb, err, "Unmarshal should not error")
|
||||
require.NoError(tb, err, "Unmarshal must not error")
|
||||
return w.WriteMessage(gws.TextMessage, fmt.Appendf(nil, `{"result":{"error":"carrots"},"id":%d}`, req.ID))
|
||||
}
|
||||
b := testexch.MockWsInstance[Binance](t, mockws.CurryWsMockUpgrader(t, mock)) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
|
||||
@@ -2445,7 +2439,7 @@ func TestGenerateSubscriptions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
subs, err := b.generateSubscriptions()
|
||||
require.NoError(t, err, "generateSubscriptions should not error")
|
||||
require.NoError(t, err, "generateSubscriptions must not error")
|
||||
testsubs.EqualLists(t, exp, subs)
|
||||
}
|
||||
|
||||
@@ -2780,38 +2774,38 @@ func TestUpdateOrderExecutionLimits(t *testing.T) {
|
||||
}
|
||||
for _, a := range []asset.Item{asset.CoinMarginedFutures, asset.USDTMarginedFutures} {
|
||||
pairs, err := b.FetchTradablePairs(t.Context(), a)
|
||||
require.NoErrorf(t, err, "FetchTradablePairs should not error for %s", a)
|
||||
require.NotEmptyf(t, pairs, "Should get some pairs for %s", a)
|
||||
require.NoErrorf(t, err, "FetchTradablePairs must not error for %s", a)
|
||||
require.NotEmptyf(t, pairs, "Must get some pairs for %s", a)
|
||||
tests[a] = pairs[0]
|
||||
}
|
||||
|
||||
for _, a := range b.GetAssetTypes(false) {
|
||||
err := b.UpdateOrderExecutionLimits(t.Context(), a)
|
||||
require.NoError(t, err, "UpdateOrderExecutionLimits should not error")
|
||||
require.NoError(t, err, "UpdateOrderExecutionLimits must not error")
|
||||
|
||||
p := tests[a]
|
||||
limits, err := b.GetOrderExecutionLimits(a, p)
|
||||
require.NoErrorf(t, err, "GetOrderExecutionLimits should not error for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MinPrice, "MinPrice must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxPrice, "MaxPrice must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.PriceStepIncrementSize, "PriceStepIncrementSize must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MinimumBaseAmount, "MinimumBaseAmount must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaximumBaseAmount, "MaximumBaseAmount must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.AmountStepIncrementSize, "AmountStepIncrementSize must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MarketMaxQty, "MarketMaxQty must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxTotalOrders, "MaxTotalOrders must be positive for %s pair %s", a, p)
|
||||
require.NoErrorf(t, err, "GetOrderExecutionLimits must not error for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MinPrice, "MinPrice should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxPrice, "MaxPrice should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.PriceStepIncrementSize, "PriceStepIncrementSize should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MinimumBaseAmount, "MinimumBaseAmount should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaximumBaseAmount, "MaximumBaseAmount should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.AmountStepIncrementSize, "AmountStepIncrementSize should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MarketMaxQty, "MarketMaxQty should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxTotalOrders, "MaxTotalOrders should be positive for %s pair %s", a, p)
|
||||
switch a {
|
||||
case asset.Spot, asset.Margin:
|
||||
assert.Positivef(t, limits.MaxIcebergParts, "MaxIcebergParts must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxIcebergParts, "MaxIcebergParts should be positive for %s pair %s", a, p)
|
||||
case asset.USDTMarginedFutures:
|
||||
assert.Positivef(t, limits.MinNotional, "MinNotional must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MinNotional, "MinNotional should be positive for %s pair %s", a, p)
|
||||
fallthrough
|
||||
case asset.CoinMarginedFutures:
|
||||
assert.Positivef(t, limits.MultiplierUp, "MultiplierUp must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MultiplierDown, "MultiplierDown must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MarketMinQty, "MarketMinQty must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MarketStepIncrementSize, "MarketStepIncrementSize must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxAlgoOrders, "MaxAlgoOrders must be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MultiplierUp, "MultiplierUp should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MultiplierDown, "MultiplierDown should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MarketMinQty, "MarketMinQty should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MarketStepIncrementSize, "MarketStepIncrementSize should be positive for %s pair %s", a, p)
|
||||
assert.Positivef(t, limits.MaxAlgoOrders, "MaxAlgoOrders should be positive for %s pair %s", a, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2951,28 +2945,20 @@ func TestSetAssetsMode(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
|
||||
is, err := b.GetAssetsMode(t.Context())
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v', expected '%v'", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = b.SetAssetsMode(t.Context(), !is)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v', expected '%v'", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = b.SetAssetsMode(t.Context(), is)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v', expected '%v'", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetAssetsMode(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
|
||||
_, err := b.GetAssetsMode(t.Context())
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v', expected '%v'", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetCollateralMode(t *testing.T) {
|
||||
@@ -2987,9 +2973,7 @@ func TestGetCollateralMode(t *testing.T) {
|
||||
t.Errorf("received '%v', expected '%v'", err, asset.ErrNotSupported)
|
||||
}
|
||||
_, err = b.GetCollateralMode(t.Context(), asset.USDTMarginedFutures)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v', expected '%v'", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSetCollateralMode(t *testing.T) {
|
||||
@@ -3004,9 +2988,8 @@ func TestSetCollateralMode(t *testing.T) {
|
||||
t.Errorf("received '%v', expected '%v'", err, asset.ErrNotSupported)
|
||||
}
|
||||
err = b.SetCollateralMode(t.Context(), asset.USDTMarginedFutures, collateral.MultiMode)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v', expected '%v'", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = b.SetCollateralMode(t.Context(), asset.USDTMarginedFutures, collateral.PortfolioMode)
|
||||
if !errors.Is(err, order.ErrCollateralInvalid) {
|
||||
t.Errorf("received '%v', expected '%v'", err, order.ErrCollateralInvalid)
|
||||
@@ -3107,18 +3090,14 @@ func TestSetMarginType(t *testing.T) {
|
||||
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
|
||||
|
||||
err := b.SetMarginType(t.Context(), asset.USDTMarginedFutures, currency.NewBTCUSDT(), margin.Isolated)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
p, err := currency.NewPairFromString("BTCUSD_PERP")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = b.SetMarginType(t.Context(), asset.CoinMarginedFutures, p, margin.Isolated)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = b.SetMarginType(t.Context(), asset.Spot, currency.NewBTCUSDT(), margin.Isolated)
|
||||
if !errors.Is(err, asset.ErrNotSupported) {
|
||||
@@ -3418,13 +3397,10 @@ func TestGetFuturesContractDetails(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = b.GetFuturesContractDetails(t.Context(), asset.USDTMarginedFutures)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = b.GetFuturesContractDetails(t.Context(), asset.CoinMarginedFutures)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetFundingRateInfo(t *testing.T) {
|
||||
@@ -3470,8 +3446,8 @@ func TestGetCurrencyTradeURL(t *testing.T) {
|
||||
testexch.UpdatePairsOnce(t, b)
|
||||
for _, a := range b.GetAssetTypes(false) {
|
||||
pairs, err := b.CurrencyPairs.GetPairs(a, false)
|
||||
require.NoError(t, err, "cannot get pairs for %s", a)
|
||||
require.NotEmpty(t, pairs, "no pairs for %s", a)
|
||||
require.NoErrorf(t, err, "cannot get pairs for %s", a)
|
||||
require.NotEmptyf(t, pairs, "no pairs for %s", a)
|
||||
resp, err := b.GetCurrencyTradeURL(t.Context(), a, pairs[0])
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, resp)
|
||||
|
||||
@@ -69,13 +69,13 @@ func (b *Binance) SetDefaults() {
|
||||
|
||||
for a, ps := range defaultAssetPairStores {
|
||||
if err := b.SetAssetPairStore(a, ps); err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", b.Name, a, err)
|
||||
log.Errorf(log.ExchangeSys, "%s error storing %q default asset formats: %s", b.Name, a, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, a := range []asset.Item{asset.Margin, asset.CoinMarginedFutures, asset.USDTMarginedFutures} {
|
||||
if err := b.DisableAssetWebsocketSupport(a); err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s error disabling `%s` asset type websocket support: %s", b.Name, a, err)
|
||||
log.Errorf(log.ExchangeSys, "%s error disabling %q asset type websocket support: %s", b.Name, a, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user