Files
gocryptotrader/exchanges/subscription/list_test.go
Gareth Kirwan b41fe27684 Kucoin: Add subscription templating and various fixes (#1579)
* Currency: Variadic Pairs.Add

This version of Pairs.Add is simpler and [more
performant](https://gist.github.com/gbjk/06a1fc1832d04ee41213ca518938cf74)

Behavioural difference: If there's nothing to add, the same slice is
returned unaltered. This seems like good sauce

* Currency: Variadic Remove

* Common: Add Batch function

* Common: Add common.SortStrings for stringers

* Subscriptions: Add batching to templates

* Subscriptions: Sort list of pairs

* Kucoin: Switch to sub templating

* Kucoin: Simplify channel prefix usage

* Kucoin: Fix race on fetchedFuturesOrderbook

* Subscriptions: Filter AssetPairs

Now only the assetPairs relevant to the subscription are in the context

* Subscriptions: Respect subscription Pairs

* Subscriptions: Trim AssetSeparator early

We want to trim before checking for "AssetSeparator vs All" because a
template should be allowed to reuse a range template and generate just one trailing AssetSeparator
whilst using a specific Asset

* Kucoin: Fix empty margin asset added

* Kucoin: Add Subscription batching

Turns out that contary to the documentation, kucoin supports batching of
all symbols and currencies

* Kucoin: Fix checkSubscriptions and coverage

* Subscriptions: Simplify error checking

This reduces the complexity of error checking to just be "do we get the
correct numbers".

Fixes Asset.All with only one asset erroring on xpandPairs, because we
trimmed the only asset separator, and then errored that we're not
xpanding Assets and the asset on the sub is asset.All

This use-case conflicted with commit 6bbd546d74, which required:
```
Subscriptions: Trim AssetSeparator early

We want to trim before checking for "AssetSeparator vs All" because a
template should be allowed to reuse a range template and generate just one trailing AssetSeparator
whilst using a specific Asset
```

Now we set up the assets earlier, and we remove the check for xpandAssets, since the number of asset lines matching is all that matters.

I've removed the asset tests for this, but they were correctly erroring
on the number of asset lines instead.

Everything hits coverage, as well.

* Kucoin: Remove deprecated fundingBook endpoint

* BTCMarkets: Use common.Batch
2024-08-09 12:33:15 +10:00

109 lines
3.6 KiB
Go

package subscription
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
)
// TestListStrings exercises List.Strings()
func TestListStrings(t *testing.T) {
t.Parallel()
l := List{
&Subscription{
Channel: TickerChannel,
Asset: asset.Spot,
Pairs: currency.Pairs{ethusdcPair, btcusdtPair},
},
&Subscription{
Channel: OrderbookChannel,
Pairs: currency.Pairs{ethusdcPair},
},
}
exp := []string{"orderbook ETH/USDC", "ticker spot ETH/USDC,BTC/USDT"}
assert.ElementsMatch(t, exp, l.Strings(), "String must return correct sorted list")
}
// TestQualifiedChannels exercises List.QualifiedChannels()
func TestQualifiedChannels(t *testing.T) {
t.Parallel()
l := List{
&Subscription{
QualifiedChannel: "ticker-btc",
},
&Subscription{
QualifiedChannel: "candles-btc",
},
}
exp := []string{"ticker-btc", "candles-btc"}
assert.ElementsMatch(t, exp, l.QualifiedChannels(), "QualifiedChannels should return correct sorted list")
}
// TestListGroupPairs exercises List.GroupPairs()
func TestListGroupPairs(t *testing.T) {
t.Parallel()
l := List{
{Asset: asset.Spot, Channel: TickerChannel, Pairs: currency.Pairs{ethusdcPair, btcusdtPair}},
}
for _, c := range []string{TickerChannel, OrderbookChannel} {
for _, p := range []currency.Pair{ethusdcPair, btcusdtPair} {
l = append(l, &Subscription{
Channel: c,
Asset: asset.Spot,
Pairs: currency.Pairs{p},
})
}
}
n := l.GroupPairs()
assert.Len(t, l, 5, "Orig list should not be changed")
assert.Len(t, n, 2, "New list should be grouped")
exp := []string{"ticker spot ETH/USDC,BTC/USDT", "orderbook spot ETH/USDC,BTC/USDT"}
assert.ElementsMatch(t, exp, n.Strings(), "String must return correct sorted list")
}
// TestListSetStates exercises List.SetState()
func TestListSetStates(t *testing.T) {
t.Parallel()
l := List{{Channel: TickerChannel}, {Channel: OrderbookChannel}}
assert.NoError(t, l.SetStates(SubscribingState), "SetStates should not error")
assert.Equal(t, SubscribingState, l[1].State(), "SetStates should set State correctly")
require.NoError(t, l[0].SetState(SubscribedState), "Individual SetState must not error")
err := l.SetStates(SubscribedState)
assert.ErrorIs(t, ErrInStateAlready, err, "SetStates should error when duplicate state")
assert.Equal(t, SubscribedState, l[1].State(), "SetStates should set State correctly after the error")
}
// TestAssetPairs exercises AssetPairs error handling
// All other code is covered under TestExpandTemplates
func TestAssetPairs(t *testing.T) {
t.Parallel()
for _, a := range []asset.Item{asset.Spot, asset.All} {
e := newMockEx()
l := &List{{Channel: CandlesChannel, Asset: a}}
e.errFormat = errors.New("Krypton is back")
_, err := l.assetPairs(e)
assert.ErrorIs(t, err, e.errFormat, "Should error correctly on GetPairFormat")
e.errPairs = errors.New("Krypton is gone")
_, err = l.assetPairs(e)
assert.ErrorIs(t, err, e.errPairs, "Should error correctly on GetEnabledPairs")
}
}
func TestListClone(t *testing.T) {
t.Parallel()
l := List{{Channel: TickerChannel}, {Channel: OrderbookChannel}}
n := l.Clone()
assert.NotSame(t, n, l, "Slices must not be the same")
require.NotEmpty(t, n, "List must not be empty")
assert.NotSame(t, n[0], l[0], "Subscriptions must be cloned")
assert.Equal(t, n[0], l[0], "Subscriptions should be equal")
l[0].Interval = kline.OneHour
assert.NotEqual(t, n[0], l[0], "Subscriptions should be cloned")
}