-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package versions | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
|
||
"github.com/buger/jsonparser" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestVersion3Upgrade(t *testing.T) { | ||
t.Parallel() | ||
|
||
in := []byte(`{"name":"Cracken","currencyPairs":{"assetTypes":["spot"],"pairs":{"spot":{"enabled":"BTC-AUD","available":"BTC-AUD"},"futures":{"assetEnabled":true},"options":{}}}}`) | ||
out, err := new(Version3).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 must 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") | ||
|
||
out2, err := new(Version3).UpgradeExchange(context.Background(), out) | ||
require.NoError(t, err, "Must not error on re-upgrading") | ||
assert.Equal(t, out, out2, "Should not effect an already upgraded config") | ||
} | ||
|
||
func TestVersion3Downgrade(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(Version3).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") | ||
} |