Files
gocryptotrader/config/versions/v4_test.go
Gareth Kirwan 16d2d9f35a Config: AssetEnabled upgrade (#1735)
* Config: Move assetEnabled upgrade to Version management

* Assets: Do not error on asset not enabled, or disabled

This became more messy with Disabling something that's defaulted to
disabled.
Taking an idealogical stance against erroring that what you want to have
done is already done.

* CurrencyManager: Set AssetEnabled when StorePairs(enabled)

* RPCServer: Fix tests expecting StoreAssetPairFormat to enable the asset

Also assertifies

* Bitfinex: Fix tests for MarginFunding subs

* GCTWrapper: Improve TestMain clarity

* BTSE: Add futures to testconfig

* Exchanges: Rename StoreAssetPairStore

Previously we were calling it "Format", but accepting everything from
the PairStore.
We were also defaulting to turning the Asset on.

Now callers need to get their AssetEnabled set as they want it, so
there's no magic

This change also moves responsibility for error wrapping outside to the
caller.

* Config: AssetEnabled upgrade should respect assetTypes

Previously we ignored the field and just turned on everything.
I think that was because we couldn't get at the old value.
In either case, we have the option to do better, and respect the
assetEnabled value

* Config: Improve exchange config version upgrade error messages
2025-03-17 21:47:37 +11:00

92 lines
3.9 KiB
Go

package versions
import (
"bytes"
"context"
"testing"
"github.com/buger/jsonparser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVersion4ExchangeType(t *testing.T) {
t.Parallel()
assert.Implements(t, (*ExchangeVersion)(nil), new(Version4))
}
func TestVersion4Exchanges(t *testing.T) {
t.Parallel()
assert.Equal(t, []string{"*"}, new(Version4).Exchanges())
}
func TestVersion4Upgrade(t *testing.T) {
t.Parallel()
_, err := new(Version4).UpgradeExchange(context.Background(), []byte{})
require.ErrorIs(t, err, errUpgradingAssetTypes)
_, err = new(Version4).UpgradeExchange(context.Background(), []byte(`{}`))
require.ErrorIs(t, err, errUpgradingCurrencyPairs)
in := []byte(`{"name":"Cracken","currencyPairs":{"assetTypes":["spot"],"pairs":{"spot":{"enabled":"BTC-AUD","available":"BTC-AUD"},"futures":{"assetEnabled":true},"options":{},"margin":{"assetEnabled":null}}}}`)
out, err := new(Version4).UpgradeExchange(context.Background(), in)
require.NoError(t, err)
require.NotEmpty(t, out)
_, _, _, err = jsonparser.Get(out, "currencyPairs", "assetTypes") //nolint:dogsled // Ignored return values really not needed
assert.ErrorIs(t, err, jsonparser.KeyPathNotFoundError, "assetTypes should be removed")
e, err := jsonparser.GetBoolean(out, "currencyPairs", "pairs", "spot", "assetEnabled")
require.NoError(t, err, "Must find assetEnabled for spot")
assert.True(t, e, "assetEnabled should be set to true")
e, err = jsonparser.GetBoolean(out, "currencyPairs", "pairs", "futures", "assetEnabled")
require.NoError(t, err, "Must find assetEnabled for futures")
assert.True(t, e, "assetEnabled should be set to true")
e, err = jsonparser.GetBoolean(out, "currencyPairs", "pairs", "options", "assetEnabled")
require.NoError(t, err, "Must find assetEnabled for options")
assert.False(t, e, "assetEnabled should be set to false")
e, err = jsonparser.GetBoolean(out, "currencyPairs", "pairs", "margin", "assetEnabled")
require.NoError(t, err, "Must find assetEnabled for margin")
assert.False(t, e, "assetEnabled should be set to false")
out2, err := new(Version4).UpgradeExchange(context.Background(), out)
require.NoError(t, err, "Must not error on re-upgrading")
assert.Equal(t, out, out2, "Should not affect an already upgraded config")
in = []byte(`{"name":"Cracken","currencyPairs":{"assetTypes":["spot"],"pairs":{"spot":{"assetEnabled":{}}}}}`)
_, err = new(Version4).UpgradeExchange(context.Background(), in)
require.NoError(t, err)
in = []byte(`{"name":"Cracken","currencyPairs":{"assetTypes":["spot"],"pairs":{"margin":{"assetEnabled":{}}}}}`)
_, err = new(Version4).UpgradeExchange(context.Background(), in)
require.ErrorIs(t, err, jsonparser.UnknownValueTypeError)
require.ErrorContains(t, err, "`margin`")
require.ErrorContains(t, err, "`object`")
}
func TestVersion4Downgrade(t *testing.T) {
t.Parallel()
in := []byte(`{"name":"Cracken","currencyPairs":{"pairs":{"spot":{"enabled":"BTC-AUD","available":"BTC-AUD","assetEnabled":true},"futures":{"assetEnabled":false},"options":{},"options_combo":{"assetEnabled":true}}}}`)
out, err := new(Version4).DowngradeExchange(context.Background(), in)
require.NoError(t, err)
require.NotEmpty(t, out)
v, vT, _, err := jsonparser.Get(out, "currencyPairs", "assetTypes")
require.NoError(t, err, "assetTypes must be found")
require.Equal(t, jsonparser.Array, vT, "assetTypes must be an array")
require.Equal(t, `["spot","options_combo"]`, string(v), "assetTypes must be correct")
assetEnabledFn := func(k []byte, v []byte, _ jsonparser.ValueType, _ int) error {
_, err = jsonparser.GetBoolean(v, "assetEnabled")
require.ErrorIsf(t, err, jsonparser.KeyPathNotFoundError, "assetEnabled must be removed from %s", k)
return nil
}
err = jsonparser.ObjectEach(bytes.Clone(out), assetEnabledFn, "currencyPairs", "pairs")
require.NoError(t, err, "Must not error visiting currencyPairs")
}