Files
gocryptotrader/exchanges/subscription/template_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

138 lines
6.4 KiB
Go

package subscription
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
)
// TestExpandTemplates exercises ExpandTemplates
func TestExpandTemplates(t *testing.T) {
t.Parallel()
e := newMockEx()
e.tpl = "subscriptions.tmpl"
// Functionality tests
l := List{
{Channel: "single-channel"},
{Channel: "expand-assets", Asset: asset.All, Interval: kline.FifteenMin},
{Channel: "expand-pairs", Asset: asset.All, Levels: 1},
{Channel: "expand-pairs", Asset: asset.Spot, Levels: 2},
{Channel: "single-channel", QualifiedChannel: "just one sub already processed"},
{Channel: "update-asset-pairs", Asset: asset.All},
{Channel: "expand-pairs", Asset: asset.Spot, Pairs: e.pairs[0:2], Levels: 3},
{Channel: "batching", Asset: asset.Spot},
{Channel: "single-channel", Authenticated: true},
}
got, err := l.ExpandTemplates(e)
require.NoError(t, err, "ExpandTemplates must not error")
exp := List{
{Channel: "single-channel", QualifiedChannel: "single-channel"},
{Channel: "expand-assets", QualifiedChannel: "spot-expand-assets@15m", Asset: asset.Spot, Pairs: e.pairs, Interval: kline.FifteenMin},
{Channel: "expand-assets", QualifiedChannel: "future-expand-assets@15m", Asset: asset.Futures, Pairs: e.pairs, Interval: kline.FifteenMin},
{Channel: "single-channel", QualifiedChannel: "just one sub already processed"},
{Channel: "update-asset-pairs", QualifiedChannel: "spot-btcusdt-update-asset-pairs", Asset: asset.Spot, Pairs: currency.Pairs{btcusdtPair}},
{Channel: "expand-pairs", QualifiedChannel: "spot-USDTBTC-expand-pairs@3", Asset: asset.Spot, Pairs: e.pairs[:1], Levels: 3},
{Channel: "expand-pairs", QualifiedChannel: "spot-USDCETH-expand-pairs@3", Asset: asset.Spot, Pairs: e.pairs[1:2], Levels: 3},
}
for _, p := range e.pairs {
exp = append(exp, List{
{Channel: "expand-pairs", QualifiedChannel: "spot-" + p.Swap().String() + "-expand-pairs@1", Asset: asset.Spot, Pairs: currency.Pairs{p}, Levels: 1},
{Channel: "expand-pairs", QualifiedChannel: "future-" + p.Swap().String() + "-expand-pairs@1", Asset: asset.Futures, Pairs: currency.Pairs{p}, Levels: 1},
{Channel: "expand-pairs", QualifiedChannel: "spot-" + p.Swap().String() + "-expand-pairs@2", Asset: asset.Spot, Pairs: currency.Pairs{p}, Levels: 2},
}...)
}
for _, b := range common.Batch(common.SortStrings(e.pairs), 3) {
exp = append(exp, &Subscription{Channel: "batching", QualifiedChannel: "spot-" + b.Join() + "-batching", Asset: asset.Spot, Pairs: b})
}
if !equalLists(t, exp, got) {
t.FailNow() // If the first list isn't equal testing it again will duplicate test failures
}
e.auth = true
got, err = l.ExpandTemplates(e)
require.NoError(t, err, "ExpandTemplates must not error")
exp = append(exp,
&Subscription{Channel: "single-channel", QualifiedChannel: "single-channel-authed"},
)
equalLists(t, exp, got)
// Test with just one asset to ensure asset.All works, and disabled assets don't error
e.assets = e.assets[:1]
l = List{
{Channel: "expand-assets", Asset: asset.All, Interval: kline.OneHour},
{Channel: "expand-pairs", Asset: asset.All, Levels: 4},
{Channel: "single-channel", Asset: asset.Futures},
}
got, err = l.ExpandTemplates(e)
require.NoError(t, err, "ExpandTemplates must not error")
exp = List{
{Channel: "expand-assets", QualifiedChannel: "spot-expand-assets@1h", Asset: asset.Spot, Pairs: e.pairs, Interval: kline.OneHour},
}
for _, p := range e.pairs {
exp = append(exp, List{
{Channel: "expand-pairs", QualifiedChannel: "spot-" + p.Swap().String() + "-expand-pairs@4", Asset: asset.Spot, Pairs: currency.Pairs{p}, Levels: 4},
}...)
}
equalLists(t, exp, got)
// Error cases
_, err = List{{Channel: "nil"}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")
_, err = List{{Channel: "single-channel", Asset: asset.Spot, Pairs: currency.Pairs{currency.NewPairWithDelimiter("NOPE", "POPE", "🐰")}}}.ExpandTemplates(e)
assert.ErrorIs(t, err, currency.ErrPairNotContainedInAvailablePairs, "Should error correctly when pair not available")
e.tpl = "errors.tmpl"
_, err = List{{Channel: "error1"}}.ExpandTemplates(e)
assert.ErrorContains(t, err, "wrong number of args for String", "Should error correctly with execution error")
_, err = List{{Channel: "empty-content", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errNoTemplateContent, "Should error correctly when no content generated")
assert.ErrorContains(t, err, "empty-content", "Should error correctly when no content generated")
_, err = List{{Channel: "error2", Asset: asset.All}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errAssetRecords, "Should error correctly when invalid number of asset entries")
_, err = List{{Channel: "error3", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errPairRecords, "Should error correctly when invalid number of pair entries")
_, err = List{{Channel: "error4", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errTooManyBatchSizePerAsset, "Should error correctly when too many BatchSize directives")
_, err = List{{Channel: "error5", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, common.ErrTypeAssertFailure, "Should error correctly when batch size isn't an int")
e.tpl = "parse-error.tmpl"
e.tpl = "parse-error.tmpl"
_, err = l.ExpandTemplates(e)
assert.ErrorContains(t, err, "function \"explode\" not defined", "Should error correctly on unparsable template")
e.errFormat = errors.New("the planet Krypton is gone")
_, err = l.ExpandTemplates(e)
assert.ErrorIs(t, err, e.errFormat, "Should error correctly on GetPairFormat")
e.errPairs = errors.New("bad parenting from Jor-El")
_, err = l.ExpandTemplates(e)
assert.ErrorIs(t, err, e.errPairs, "Should error correctly on GetEnabledPairs")
l = List{{Channel: "single-channel", QualifiedChannel: "already happy"}}
got, err = l.ExpandTemplates(e)
require.NoError(t, err)
require.Len(t, got, 1, "Must get back the one sub")
assert.Equal(t, "already happy", l[0].QualifiedChannel, "Should get back the one sub")
assert.NotSame(t, got, l, "Should get back a different actual list")
_, err = List{{Channel: "nil"}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")
}